Basics of ‘C’
General Aspect of ‘C’
• C was originally developed in the 1970s, by Dennis Ritchie
at Bell Telephone Laboratories, Inc.
• C is a High level , general –purpose structured
programming language
• C contains certain additional features that allows it to be
used at a lower level , acting as bridge between machine
language and the high level languages
• This allows C to be used for system programming as well as
for applications programming
The Character set of ‘C’
• C language consist of some characters set, numbers and
some special symbols
• The character set of C consist of all the alphabets of English
language:
• Alphabets a to z, A to Z
• Numeric 0,1 to 9
• Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
• The words formed from the character set are building blocks
of C known as tokens
• Different types of token are used in C:
• Identifiers
• Keywords
• Constants
• Operators
• Punctuation Symbols
Identifiers
• A 'C' program consist of two types of elements , user defined
and system defined. Idetifiers is nothing but a name given to
these elements
• An identifier is a word used by a programmer to name a
variable , function, or label
• Identifiers consist of letters and digits in any order, except that
the first character must be letter
• Both Upper and lowercase letters can be used
Keywords
• Keywords are nothing but auto double int struct
system defined identifiers
break else long switch
• Keywords are reserved words
of the language
case enum register typedef
• They have specific meaning in
the language and cannot be used char extern return union
by the programmer as variable
or constant names const float short unsigned
• C is case sensitive, it means
these must be used as it is continue for signed void
• 32 Keywords
default goto sizeof volatile
do if static while
Variables:
• Variable names are names given to locations in memory
• These locations can contain integer, real or character constants
• For example, an integer variable can hold only an integer
constant, a real variable can hold only a real constant and a
character variable can hold only a character constant
• Each variable in C has a specific type, which determines the
size and layout of the variable's memory; the range of values
that can be stored within that memory; and the set of operations
that can be applied to the variable
Rules for Constructing Variable Names:
• A variable name is any combination of 1 to 31 alphabets, digits or
underscores
• Some compilers allow variable names whose length could be up to
247 characters
• The first character in the variable name must be an alphabet or
• underscore
• No commas or blanks are allowed within a variable name
• No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.
• Ex.: si_int
• m_hra
• pop_e_89
• Examples of type declaration statements:
• int si, m_hra ;
• float bassal ;
• char code ;
Constants:
• A constant is a value or an identifier whose value cannot be altered in
a program. For example: 1, 2.5
• An identifier also can be defined as a constant
• eg. const double PI = 3.14
• Types of C Constants:
• Primary Constants
• Secondary Constants
Integer constants
• An integer constant must have at least one digit
• It must not have a decimal point
• It can be either positive or negative
• If no sign precedes an integer constant it is assumed to
be positive
• No commas or blanks are allowed within an integer
constant
• The allowable range for integer constants is -32768 to
32767
• Ex.:
• 426
• +782
• -8000
• -7605
Real constants
• A real constant must have at least one digit
• It must have a decimal point
• It could be either positive or negative
• Default sign is positive
• No commas or blanks are allowed within a real
constant
• Ex.:
• +325.34
• 426.0
• -32.76
• -48.5792
• The exponential form of representation of real constants is usually used if the
value of the constant is either too small or too large
• In exponential form of representation, the real constant is represented in two
parts:
• The part appearing before ‘e’ is called mantissa
• the part following ‘e’ is called exponent
• Following rules must be observed while constructing real constants expressed in
exponential form:
• The mantissa part and the exponential part should be separated by a letter e
• The mantissa part may have a positive or negative sign
• Default sign of mantissa part is positive
• The exponent must have at least one digit, which must be a positive or negative
integer. Default sign is positive
• Range of real constants expressed in exponential form is -3.4e38 to 3.4e38
• Ex.: +3.2e-5
• 4.1e8
• -0.2e+3
• -3.2e-5
Character constants
• A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted
commas Both the inverted commas should point to the left.
• Example, ’A’ is a valid character constant whereas ‘A’ is not
• The maximum length of a character constant can be 1
character
• Ex.:
• 'A'
• 'I'
• '5'
• '='
Escape Sequences
• For example: \n is used for newline. The backslash ( \ ) causes "escape" from the
normal way the characters are interpreted by the compiler
Escape Sequences Character
• \b Backspace
• \f Form feed
• \n Newline
• \r Return
• \t Horizontal tab
• \v Vertical tab
• \\ Backslash
• \’ Single quotation mark
• \" Double quotation mark
• \? Question mark
• \0 Null character
The First C Program
• Rules applicable to all C programs:
• Each instruction in a C program is written as a
separate statement.
• The statements in a program must appear in the
same order in which we wish them to be executed;
unless of course the logic of the problem demands a
deliberate ‘jump’ or transfer of control to a
statement, which is out of sequence.
• Blank spaces may be inserted between two words to
improve the readability of the statement. However,
no blank spaces are allowed within a variable,
constant or keyword
• All statements are entered in small case letters
• It is a free-form language
• Every C statement must end with a ;. Thus ; acts as
a statement terminator.
First ‘C’ Program
/* sum of two numbers*/
main( )
{
float a,b,sum;
printf(“Enter the value of a and b”);
scanf(“%f%f”, a,b);
sum=a+b;
printf ( ”The sum of %f and %f is %f" ,a,b, sum ) ;
}
First ‘C’ Program
/* Square of a number
Author: Jency
Date: 25th June 2024*/
main( )
{
int n;
printf(“Enter the value of n”);
scanf(“%d”,n);
sq=n*n;
printf ( ”The square of %d is %d" ,n,sq) ;
}
printf() and scanf() in c
• The printf() and scanf() functions are used for input and
output in C language
• Both functions are inbuilt library functions, defined in
stdio.h (header file)
• printf() function:
• The printf() function is used for output. It prints the given
statement to the console.
• The syntax of printf() :
printf ( "<format string>", <list of variables> ) ;
• <format string> can contain,
• %f for printing real values
• %d for printing integer values
• %c for printing character values
• scanf() function
• The scanf() function is used for input. It reads the input data from
the console.
• Syntax: scanf("format string",argument_list);
//Cube of a number
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
//Average of three subjects
#include<stdio.h>
int main()
{
int maths, biology,english;
float avg;
printf("enter the marks of math:");
scanf("%d",&maths);
printf("enter the marks of biology:");
scanf("%d",&biology);
printf("enter the marks of English");
scanf("%d",&english);
avg=(maths+biology+english)/3 or 300;
printf(”The average of 3 subjects is: %f", avg);
return 0;
}
C Instructions
• Three types of instructions in C:
• Type Declaration Instruction- To declare the type of variables used in a C program
• Ex.: int bas ;
• float rs, grosssal ;
• char name, code ;
• Arithmetic Instruction- To perform arithmetic operations between constants and variables
• int ad ;
• float kot, deta, alpha, beta, gamma ;
• ad = 3200 ;
• kot = 0.0056 ;
• deta = alpha * beta / gamma + 3.2 * 2 / 5 ;
• Control Instruction - To control the sequence of execution of various statements in a C
program
• Sequence Control Instruction
• Selection or Decision Control Instruction
• Repetition or Loop Control Instruction
• Case Control Instruction
Type Declaration Instruction
• This instruction is used to declare the type of
variables being used in the program
• Any variable used in the program must be declared
before using it in any statement
• The type declaration statement is written at the
beginning of main( ) function
• Ex.:
• int bas ;
• float rs, grosssal ;
• char name, code ;
• While declaring the type of variable we can also initialize it
as
shown below:
• int i = 10, j = 25 ;
• float a = 1.5, b = 1.99 + 2.4 * 1.44 ;
• The order in which we define the variables is sometimes
important sometimes not. For example,
• int i = 10, j = 25 ;
is same as
• int j = 25, j = 10 ;
• However,
• float a = 1.5, b = a + 3.1 ;
• is alright, but
Thank you