C Language
C Language
HISTORY
• INVENTORS - Dennis Ritchie and Kerningham.
• PLACE - Bell Laboratories.
• YEAR OF INVENTION – 1972
• C was evolved from the ALGOL, BCPL and B.
• C is strongly associated with UNIX operating
system, which is one of the most popular
network operating systems.
Sample Program: Printing a message
#include<stdio.h> /* Library function */
#include<conio.h> /* Library function */
main() /* main program begins */
{
printf(“Welcome to Training!”); /* Printing Statement */
}
----------------------------------------------------------------
Output : Welcome to Training!
C TOKENS
In a C program, the smallest individual units are known as C tokens.
Key Words
Every C word is classified as either a keyword or an identifier. All
Keywords have fixed meanings and these are basic building blocks
program statements.
Identifiers
Identifiers refers to the names of variables,
functions etc. These are user-defined names
consists of a sequence of letters and digits.
Rules of Identifiers:
1. First character must be an alphabet or underscore(_).
2. Must consists of only letters, digits or underscore.
3. Only first 31 characters are significant.
4. Cannot use a keyword.
5. Must not contain white space.
Constants
Constants in C refer to fixed values that do not change during
the execution of a program. C supports several types of
constants.
Examples : Integer constants like 12,-23,65147 etc.
Real constants like 0.058,56.12,-4.2, 0.2e14 etc.
Single character constants like ‘5’, ‘x’, ‘Z’, ‘;’ etc.
String constants like “Hello!”, “ 10+5” etc.
Back slash character constants like ‘\n’ which
indicates new line, ‘\t’ indicates horizontal tab space etc.
#MCQ
In a 'C' program, constant is defined?
A Before main
B After main
C Anywhere, but starting on a new line
D Any where in the program
#MCQ
In a 'C' program, constant is defined?
A. Before main
B. After main
C. Anywhere, but starting on a new line
D. Any where in the program
#MCQ
Which of the following is not a numbering
system supported for integer constants in C?
a) Binary system b) Octal system
c) Decimal system d) Hexadecimal system
#MCQ
Which of the following is not a numbering
system supported for integer constants in C?
a) Binary system b) Octal system
c) Decimal system d) Hexadecimal system
Variables
A variable is a data name that may be used to
store a data value.
Unlike constants that remain unchanged during the execution
of a program, a variable may take different values at different
times.
A variable name can be chosen by the programmer in a
meaningful way so as to reflect its function or nature in the
program.
Examples : Average, height, class_strength etc.
Data Types
1.Primary Data Types
#MCQ
Which is correct with respect to the size
of the data types?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
#MCQ
Which is correct with respect to the size
of the data types?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
#MCQ
#include <stdio.h> a) a
int main() b) run time error
{ c) a.0000000
float x = 'a'; d) 97.000000
printf("%f", x);
return 0;
}
#MCQ
#include <stdio.h> a) a
int main() b) run time error
{ c) a.0000000
float x = 'a'; d) 97.000000
printf("%f", x);
return 0;
}
#MCQ
#include <stdio.h> a) 2
int main() b) 8
{ c) 9
int var = 010; d) 10
printf("%d", var);
}
#MCQ
#include <stdio.h> a) 2
int main() b) 8
{ c) 9
int var = 010; d) 10
printf("%d", var);
} Explanation: Octal
Representation
Implicit Type Conversion
• A type cast is basically a conversion from one type to
another.
There are two types of type conversions:
1. Implicit Type Conversion
The syntax in C:
(type) expression
#MCQ
#include <stdio.h> 1. 120
int main() 2. Compilation Error
{ 3. Unpredictable
char a = 30, b = 40, c = 4. Run Time Error
10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
#MCQ
if (a == b)
printf("\nSame");
else
printf("\nNot Same");
return 0;
}
#MCQ
#include <stdio.h> Answer
int main() a=-5
{
b=251
char a = 0xfb;
unsigned char b = 0xfb; Not Same
printf("a = %c", a);
printf("\nb = %c", b);
if (a == b)
printf("\nSame");
else
printf("\nNot Same");
return 0;
}
#MCQ
Which of the following is not a valid
declaration in C?
1. short int x;
2. signed short x;
3. short x;
4. unsigned short x;
#MCQ
Which of the following is not a valid
declaration in C?
1. short int x;
2. signed short x;
3. short x;
4. unsigned short x;
#MCQ
#include <stdio.h>
int main()
{ 1. Yes
if (sizeof(int) > -1) 2. No
printf("Yes");
3. Compiler Error
else
printf("No"); 4. Runtime Error
return 0;
}
#MCQ
#include <stdio.h>
int main()
{ 1. Yes
if (sizeof(int) > -1) 2. No
printf("Yes");
3. Compiler Error
else
printf("No"); 4. Runtime Error
return 0;
}
#MCQ
#include<stdio.h> 1. ELSE IF
int main()
{ 2. IF
float x = 0.1; 3. ELSE
if ( x == 0.1 )
printf("IF");
else if (x == 0.1f)
printf("ELSE IF");
else
printf("ELSE");
}
#MCQ
#include<stdio.h> 1. ELSE IF
int main()
{ 2. IF
float x = 0.1; 3. ELSE
if ( x == 0.1 )
printf("IF");
else if (x == 0.1f)
printf("ELSE IF");
else
printf("ELSE");
}
#MCQ
#include <stdio.h> 1. Not Equal
int main() 2. Equal
{ 3. ERROR : Incompatibility
float f1 = 0.1; 4. Run Time Error
if (f1 == 0.1f)
printf("equal\n");
else
printf("not equal\n");
}
#MCQ
#include <stdio.h> 1. Not Equal
int main() 2. Equal
{ 3. ERROR : Incompatibility
float f1 = 0.1; 4. Run Time Error
if (f1 == 0.1f)
printf("equal\n");
else
printf("not equal\n");
}
Operators
Some more operators
sizeof -
• It is a compile time unary operator which can be used to compute the size
of its operand.
• The result of sizeof is of unsigned integral type which is usually denoted by
size_t. Basically, sizeof operator is used to compute the size of the
variable.
comma(,) -
•The comma operator (represented by the token ,) is a binary operator that
evaluates its first operand and discards the result, it then evaluates the
second operand and returns this value (and type).
• The comma operator has the lowest precedence of any C operator. Comma
acts as both operator and separator.
BITWISE OPERATORS
#MCQ
Choose the option that contains unary
operators in C.
a) sizeof(), increment(++) b) &,++
c)>=,! d) ==,!=
#MCQ
Choose the option that contains unary
operators in C.
a) sizeof(), increment(++) b) &,++
c)>=,! d) ==,!=
#MCQ
#include <stdio.h>
int main() 1. 10
{ 2. 1
int a = 10, b = 5, c = 5; 3. Compilation
int d; Error
d = a == (b + c); 4. 5
printf("%d", d);
}
#MCQ
#include <stdio.h>
int main() 1. 10
{ 2. 1
int a = 10, b = 5, c = 5; 3. Compilation
int d; Error
d = a == (b + c); 4. 5
printf("%d", d);
}
#MCQ
void main()
{
printf(“%d”,sizeof('a'));
}
1. Error 2. 2 or 4
3. 1 4. 97
#MCQ
void main()
{
printf(“%d”,sizeof('a'));
}
1. Error 2. 2 or 4
3. 1 4. 97
#MCQ
main()
{
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}
What will be the output of above program?
a) 8 24 b) 8 8 c) 24 8 d)
24 24
#MCQ
main()
{
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}
What will be the output of above program?
a) 8 24 b) 8 8 c) 24 8 d)
24 24
#MCQ
void main()
{
int x=1;
if(x--)
printf("AITAM %d",--x);
else
printf("Training",--x);
}
1. AITAM 0 2. Training -1 3. Training 0
4. AITAM -1
#MCQ
void main()
{
int x=1;
if(x--)
printf("AITAM %d",--x);
else
printf("Training",--x);
}
1. AITAM 0 2. Training -1 3. Training 0
4. AITAM -1
#MCQ
void main(){
int z,x=5,y=-10,a=4,b=2;
z = x++ - --y*(b/a);
printf(“%d”,z);
}
a) 5 b) 6 c) 10 d) 11
#MCQ
void main(){
int z,x=5,y=-10,a=4,b=2;
z = x++ - --y*(b/a);
printf(“%d”,z);
}
a) 5 b) 6 c) 10 d) 11
#MCQ
#include <stdio.h>
void main()
{
printf("value is = %d",(10++));
}
void main() 1. 1
{ 2. 3
int a=1,2,3; 3. 6
printf(“%d”,a); 4. Compilation
} Error
#MCQ
void main() 1. 1
{ 2. 3
int a=1,2,3; 3. 6
printf(“%d”,a); 4. Compilation
} Error
#MCQ
void main() 1. 1
{ 2. 3
int a; 3. 6
a=1,2,3; 4. Compilation
printf(“%d”,a); Error
}
#MCQ
void main() 1. 1
{ 2. 3
int a; 3. 6
a=1,2,3; 4. Compilation
printf(“%d”,a); Error
}
#MCQ
int main() 1. 1
{ 2. 2
int i=2; 3. 7
int j = i + (1, 2, 3, 4, 5); 4. Compilation
printf("%d\n", j); Error
}
A4 B7 C6D5
#MCQ
int main() 1. 1
{ 2. 2
int i=2; 3. 7
int j = i + (1, 2, 3, 4, 5); 4. Compilation
printf("%d\n", j); Error
}
A4 B7 C6D5
Thank you!