Professional Documents
Culture Documents
Training On "C-Programming" Day-1
Training On "C-Programming" Day-1
“C- Programming”
Day-1
Topics:
DATA TYPES, VARIABLES ,Type
conversion AND I/O
introduction:
C is a:
• general-purpose
• block structured,
• Procedural
• case sensitive
• free flow
• Portable
• high level programming language developed by Dennis Ritchie at the Bell
Telephone Laboratories.
The selection of ‘C’ as a name of a programming language seems to be an odd
choice but it was named C because it was evolved from earlier languages BCPL
(Basic Combined Programming Language) and B.
IDENTIFIERS
An identifier refers to the name of the object.
The syntactic rules to write an identifier name in C are:
• Can have letters , digits or underscores.
• First character must be a letter or an underscore but
can’t be a digit.
• No special character except underscore can be used.
• Keywords or reserved words cannot form a valid
identifier name.
• Maximum number of characters that form an
identifier name is compiler dependent.
KEYWORDS
1. Keyword refers to a reserved word that has a
particular meaning in programming language.
2. It cannot be used as an identifier name in C.
3. There are 32 keywords available in C
language
declaration statement
• Every identifier (except label name) needs to be declared before it is used.
• An identifier can be declared by making use of declaration statement.
ROLE: To introduce the name of an identifier along with its data type (or just
type) to the compiler before its use.
DATA
DATA TYPES
TYPES DERIVED
DERIVED DATA
DATA 1. Array eg char[],int[]..
TYPES
TYPES 2. Pointer e.g. char*, int *..
3. Function eg. Int(int,int),
float(int), ..
1. Structure
USER
USER DEFINED
DEFINED
DATA
2. Union
DATA TYPES
TYPES
3.
3. Enumeration
Enumeration
TYPE QUALIFIERS
A type qualifier neither affects the range of
value nor the arithmetic properties of the
declared object. They are used to indicate the
special properties of data within an object.
volatile QUALIFIER
const QUALIFIER: volatile qualifier announces
Value of the object declared as that the object has some
const will not be changed during special properties relevant to
the execution of a program. optimization.
TYPE MODIFIERS
A type modifier modifies the base type to yield a
new type. It modifies the range† and the arithmetic
properties of the base type.
Signed (signed)
Unsigned (unsigned)
Short (short)
Long (long)
DATA OBJECT, L-VALUE AND
R-VALUE:
An identifier is allocated some space in memory
depending upon its data type and the working
environment. This memory allocation gives rise to two
important concepts known as l-value concept and r-value
concept.
---> l in l-value stands for “left”, this means that l-value could legally stand on the
left side of assignment operator.
R-VALUE
• r in r-value stands for “right” or “read”, this means that if an identifier name appears
on the right side of assignment operator it refers to r-value.
Consider the expression:
variable=variable+20
• variable on the left side of assignment operator refers to l-value. variable on the right
side of assignment operator (in bold) refers to r-value.
• variable appearing on the right side refers to 20. 20 is added to 20 and the value of
expression comes out to be 40 (r-value). This outcome (40) is assigned to variable on
the left side of assignment operator, which signifies l-value.
• l-value variable locates the memory location where this value is to be placed i.e. at
2000 say.
Remember it as:
• l-value refers to location value i.e. location of the object and r-value refers to read
value i.e. value of the object.
Variables
• A name associated with memory cells
(boxes) that store data.
• Type of variable determines the size of the
box.
int m = 64; 64
char c = ‘X’; X
float f = 3.1416; 3.1416
• Variables can change their value during
program
f = 2.7183; 3.1416
2.7183
Programming 15
Variable Declaration
• To communicate to compiler the names and
types of the variables used by the program
– Type tells size of the box to store value
– Variable must be declared before used
– Optionally, declaration can be combined with
definition (initialization)
Programming 17
VARIABLES AND
CONSTANTS
VARIABLES CONSTANTS
1. A variable is an entity whose value 1. A constant is an entity whose
can vary (i.e. change) during the value remains same throughout
execution of a program. the execution of a program.
2. The value of a variable can be 2. It cannot be placed on the left-
changed because it has a side of assignment operator
modifiable l-value. Since, it has
because it does not have a
modifiable l-value, it can be placed
modifiable l-value.
on the left side of assignment
operator.
3. Variable can also be placed on the 3. It can only be placed on the
right side of assignment operator. right side of assignment
Hence, it has r-value too. Thus, a operator. Thus, a constant has
variable has both l-value and r- an r-value only
value.
Format Specifier
Format Specifier Description Supported Data type
%c Character Char, unsigned char
%d Signed integer int
%f Floating point Float
%i Signed integer Int
%l or %ld Signed integer Long
%lf Floating Point Double
%Lf Floating Point Long Double
%o Octal representation of Int
integer
NON-
PRINTABLE
PRINTABLE
CONSTANTS
CONSTANTS
CONSTANTS
Literal constant or just literal denotes a fixed value, which may be an integer,
floating point number, character or a string. The type of literal constant is
determined by its value.
Symbolic constants are created with the help of define preprocessor directive .
For example: #define PI 3.14124 defines PI as a symbolic constant with value
3.14124. Each symbolic constant is replaced by its actual value during the
preprocessing stage .
Qualified constants are created by using const qualifier. The following statement creates a
qualified character constant named a:
const char a=’A’;
Since, qualified constants are placed in the memory, they have l-value. But, as it is not
possible to modify them, this means that they do not have modifiable l-value i.e. they
have non-modifiable l-value. E.g.
int a=10; // It is possible to modify the value of a.
const int a=10;. //it is possible read the value placed within the memory location, but it is
not possible to modify the value.
INTEGER LITERAL CONSTANTS
Integer literal constants are integer values like -1, 2, 8 etc.
The following are the rules for writing integer literal constants:
• An integer literal constant must have at least one digit.
• It should not have any decimal point.
• It can be either positive or negative. If no sign precedes an integer literal constant, then it is
assumed to be positive.
• No special characters (even underscore) and blank spaces are allowed within an integer literal
constant.
• If an integer literal constant starts with 0, then it assumed to be in octal number system e.g. 023 is
a valid integer literal constant, which means 23 in octal number system and is equivalent to 19 in
decimal number system.
• If an integer literal constant starts with 0x or 0X, then it is assumed to be in hexadecimal number
system e.g. 0x23 or 0X23 is a valid integer literal constant, which means 23 in hexadecimal
number system and is equivalent to 35 in decimal number system.
• The size of integer literal constant can be modified by using a length modifier. The length modifier
can be a suffix character l, L, u, U, f or F. If the integer literal constant is terminated with l or L then
it is assumed to be long. If it is terminated with u or U then it is assumed to be an unsigned
integer e.g. 23l is a long integer and 23u is an unsigned integer. The length modifier f or F can only
be used with floating point literal constant and not with integer literal constant.
FLOATING POINT LITERAL
CONSTANTS
• Floating point literal constants are values like -23.1, 12.8, -1.8e12 etc.
• Floating point literal constants can be written in: fractional form or in exponential form.
FRACTIONAL FORM:
The following are the rules for writing floating point literal constants in fractional form:
• A fractional floating point literal constant must have at least one digit.
• It should have a decimal point.
• It can be either positive or negative. If no sign precedes a floating point literal constant then it
is assumed to be positive.
• No special characters (even underscore) and blank spaces are allowed within a floating point
literal constant.
• A floating point literal constant by default is assumed to be of type double, e.g. the type of
23.45 is double.
• The size of floating point literal constant can be modified by using the length modifier f or F
i.e. if 23.45 is written as 23.45f or 23.45F, then it is considered to be of type float instead of
double.
Following are valid floating point literal constants in fractional form:
-2.5, 12.523, 2.5f, 12,5F
Cont…
EXPONENTIAL FORM:
• The following are the rules for writing floating point literal constants in exponential form:
• A floating point literal constant in exponential form has two parts: the mantissa part and
the exponent part. Both parts are separated by e or E.
• The mantissa can be either positive or negative. The default sign is positive.
• The mantissa part should have at least one digit.
• The mantissa part can have a decimal point but it is not mandatory.
• The exponent part must have at least one digit. It can be either positive or negative. The
default sign is positive.
• The exponent part cannot have a decimal point.
• No special characters (even underscore) and blank spaces are allowed within the mantissa
part and the exponent part.
• Following are valid floating point literal constants in exponential form:
• -2.5E12, -2.5e-12, 2e10 (i.e. equivalent to 2×1010)
CHARACTER LITERAL CONSTANT
A character literal constant can have one or at most two characters enclosed within single
quotes e.g. ‘A’, ‘a’, ‘\n’ etc. Character literal constants are classified as:
• Printable character literal constants
• Non-Printable character literal constants
PRINTABLE CHARACTER LITERAL CONSTANT:
• All characters of source character set except quotation mark, backslash and new line
character when enclosed within single quotes form printable character literal constant.
Following are the examples of printable character literal constants: ‘A’, ‘#’, ‘6’.
• Explanation:
All the outputs are desired in the decimal due to
%d specifier.
72-----decimal
072------Octal No. ----Eql decimal=58
0x72 or 0X72--hexadecimal--Eql decimal=114
2.
main()
{
printf("%d%o%x%d",72,72,72,);
}
Output: 72 110 48
3.
main()
{
printf("%i %i %i %i",72,072,0x72,0X72);
}
Output: 72 58 114 114
4.
main()
{
printf("%05d,%5d,%-5d",32,32,32);
}
Output: 00032, 32,32
sizeof(32),sizeof(32U),sizeof(32L));
printf("%d%d%d",sizeof(32.4),
sizeof(32.4f),sizeof(32.4F));
}
Output: 22224
844
Explanation:
• 032, 0x32, 32--- All are integers in different
No. system --------size 2 bytes
• 32U ---- unsigned int-------size 2 bytes
• 32L-----Long int--------------size 4 bytes
• 32.4---- treated as double---size 8 bytes
• 32.4f, 32.4F----------float--------size 4 bytes
12.
main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
Output: hai
printf("\nab");
ab
printf("\bsi");
asi
printf("\rha");
hai
13.
main()
{
printf("c:\tc\bin");
}
Output:
C in
14.
main()
{
printf("c:\\tc\\bin");
}
Output:
c:\tc\bin
15.
main()
{
printf("hello.world
\");
}
Output: hello.world
Explanation:
Each instance of the backspace character (\)
immediately followed by a new line is deleted. This
process is known as “line splicing”.
16.
main()
{
printf("hello.world
");
}
Output: Compilation Error
Explanation:
String can not multiple lines in this way.
17.
main()
{
char*p="Welcome!..""to C
programming";
printf(p);
}
Output:
Welcome!..to C programming
Explanation:
Adjacent string literals get concatenated. Hence
“Welcome!..” and ”to C programming” gets
concatenated and becomes Welcome!..to C
programming.
18. main()
{
unsigned a=-1;
printf("%u“,a);
printf("%u",++a);
}
Output: 65535 0
-1 in 2’s complement stored as:
1111111111111111
Since a is unsigned hence sign bit also include in
magnitude value i.e 65535
Now increased by one gives 65536 i.e 0
19. main()
{
float u=3.5;
int v,w,x,y;
v=(int)(u+0.5);
w=(int)u+0.5;
x=(int)((int)u+0.5);
y=(u+(int)0.5);
printf("%d%d%d%d",v,w,x,y);
}
Output:4 3 3 3
20. main()
{
int i=5;
printf("%d%d%d%d%d“,i++,i--,++i,--i,i);
}
Output:4 5 5 4 5
printf() evaluated from right in turbo C