Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 68

Training on

“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.

• The general form of declaration statement is:


 [storage_class_specifier ][type_qualifier|type_modifier ]type identifier
[=value, identifier [, ...]];
•  The terms enclosed within [] (i.e. square brackets) are optional and might not
be present in a declaration statement. The type name and identifier name are
mandatory parts of a declaration statement.
Examples of valid declaration in C:
• int variable; (type int and identifier name variable present)
• static int variable; (Storage class specifier static, type int and identifier
name variable present)
• int a=20, b=10; (type int, identifier name a and its initial value 20 present,
another identifier name b and its initial value 10 present )
DECLARATION STATEMENTS:
2 TYPES
SHORTHAND DECLARATION LONG HAND DECLARATION
STATEMENT STATEMENT

1. Declaration statements in which more 1. The corresponding longhand


than one identifier is declared is declaration statements are:
known as shorthand declaration e.g. int a=20; int b=10;
statement. E.g. int a=20, b=10;

2. Can only be used to declare identifiers 2. It can be used to declare identifiers of


of the same type. different types
e.g. int a=10, float b=2.3; is an invalid e.g. int a=10;float b=2.3; are valid
statement. statements.
1. Character(char)
2. Integer(int)
3. Single precision floating
BASIC
BASIC DATA
DATA point(float)
TYPES(Primitive
TYPES(Primitive
data 4. Double precision floating
data types)
types)
point(double)
5. No value available(void)

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.

• DATA OBJECT is a term that is used to specify the region


of data storage that is used to hold values. Once an
identifier is allocated memory space, it will be known as
a data object.
L-VALUE:

L-value is a data object locator. It is an expression that locates an


object. Variable is a sort of name given to the memory location
(say2000). Variable here refers to l-value , an object locator. The
term l-value can be further categorized as:

 MODIFIABLE L-VALUE: A modifiable l-value is an expression that refers


to an object that can be accessed and legally changed in the
memory.
 NON-MODIFIABLE L-VALUE: A non-modifiable l-value refers to an object
that can be accessed but cannot be changed in the memory.

--->  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)

int count; Declaration without initialization

int min = 5; Declaration with 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

%p Address of pointer Void *


%s String Char *
%x or %X Hexadecimal representation Int
of integer
CONSTANTS

LITERAL QUALIFIED SYMBOLIC


CONSTANTS CONSTANTS CONSTANTS

INTEGER CHARACTE FLOATING STRING


LITERAL R LITERAL POINT LITERAL LITERAL
CONSTANTS CONSTANTS CONSTANTS CONSTANTS

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’.

NON-PRINTABLE CHARACTER LITERAL CONSTANT:


• Non-printable character literal constants are represented with the help of escape
sequences. An escape sequence consists of a backward slash (i.e. \) followed by a
character and both enclosed within single quotes. An escape sequence is treated as a
single character. It can be used in a string like any other printable character.
LIST OF ESCAPE SEQUENCES
ESCAPE SEQUENCES CHARACTER VALUE ACTION ON OUTPUT DEVICE
‘\'’ Single quotation mark Prints ‘
‘\"’ Double quotation mark Prints “
(")
‘\?’ Question mark (?) Prints ?
‘\\’ Backslash character (\) Prints \
‘\a’ Alert Alerts by generating beep
‘\b’ Backspace Moves the cursor one position
to the left of its current
position.
‘\f’ Form feed Moves the cursor to the
beginning of next page.
‘\n’ New line Moves the cursor to the
beginning of the next line.
‘\r’ Carriage return Moves the cursor to the
beginning of the current line.
‘\t’ Horizontal tab Moves the cursor to the next
horizontal tab stop.
‘\v’ Vertical tab Moves the cursor to the next
vertical tab stop.
‘\0’ Null character Prints nothing
STRING LITERAL CONSTANTS
• A string literal constant consists of a sequence
of characters (possibly an escape sequence)
enclosed within double quotes.
• Every string literal constant is implicitly
terminated by null character (i.e. ‘\0’). Hence,
the number of bytes occupied by a string
literal constant is one more than the number
of characters present in the string.
• The additional byte is occupied by the
terminating null character. Even the empty
Code Snippets
1.
main()
{
printf("%d%d%d%d",72,072,0x72,0X72);
}
Output: 72 58 114 114

• 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

Here width specifiers is used along with the format


specifier.
 %5d means output minimum 5 columns wide
and will be right justified.
 %-5d means output minimum 5 columns wide
and will be left justified.
 %05d means output minimum 5 columns wide
and will be right justified and the blank columns
will be padded by zeros.
5.
main()
{
printf("%6.3f,%06.3f,%09.3f,%-09.3f,%6.0f,
%6.0f",45.6,45.6,45.6,45.6,45.4,45.6);
}
Output
45.600,45.600,00045.600,45.600 , 45, 46
%6.3 means: output is 6 columns wide. 3 is the
no. of digits after decimal.
4 5 . 6 0 0

%09.3 means: output is 9 columns wide. 3 is the


no. of digits after decimal and blank spaces
will be padded by zeros.
0 0 0 4 5 . 6 0 0
%-09.3 means: output is 9 columns wide. 3 is
the no. of digits after decimal. – is used for left
justified. Padding by by zeros has not been
done because only 3 digits can be printed
after decimal.
4 5 . 6 0 0

%6.0 means: output is 6 columns wide. 0 is the


no. of digits after decimal. Rounding off will
takes place. E.g 45.6
4 6
6.
main()
{
int a=32768;
unsigned int b=65536;
printf("%d%d",a,b);
}
Output: -32768 0
7.
main()
{
char a=128;
unsigned char b=256;
printf("%d%d\n",a,b);
}
Output: -128 0
8.
main()
{
float a=3.5e38;
double b=3.5e309;
printf("%f%lf",a,b);
}
Output
+INF +INF
Explanation: Range wrap around only in case of
integral data type. Wrap around does not
occur in case of float and double data types.
9.
main()
{
printf("bytes occupied by '7'=%d\n",sizeof('7'));
printf("bytes occupied by 7=%d\n",sizeof(7));
printf("bytes occupied by 7.o=%d",sizeof(7.0));
}
Output
bytes occupied by ‘7’=1 (char)
bytes occupied by 7=4 (long int)
bytes occupied by 7.0=8 (double)
10.
#include<stdio.h>
main()
{
printf ("%d\n", sizeof (4) / sizeof (2.0));
printf ("%d", sizeof (2.0) / sizeof ( 4 ) ) ;
}
Output
0
2
11.
main()
{
printf("%d%d%d%d%d\n",sizeof(032),sizeof(0x32),

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

You might also like