Sie sind auf Seite 1von 99

Programming in C

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

2. Explicit Type Conversion


Implicit Type Conversion
•Also known as ‘automatic type
conversion’.
•Done by the compiler on its own,
without any external trigger from the
user.
•Generally takes place when in an
expression more than one data type is
present. In such condition type
conversion (type promotion) takes place
to avoid lose of data.
Explicit Type Conversion
This process is also called
type casting and it is user
defined. Here the user
can type cast the result
to make it of a particular
data type.

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

#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
#include <stdio.h> 1. 180
int main() 2. Compilation Error
{ 3. -76
char a = 30, b = 60, c = 4. Run Time Error
10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
#MCQ
#include <stdio.h> 1. 180
int main() 2. Compilation Error
{ 3. -76
char a = 30, b = 60, c = 4. Run Time Error
10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
#MCQ
#include <stdio.h> Answer
int main() a=?
{
b=?
char a = 0xfb;
unsigned char b = 0xfb; Same or Not Same?
printf("a = %c", a);
printf("\nb = %c", b);

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++));
}

1.10 2.11 3.0 4.ERROR


#MCQ
#include <stdio.h>
void main()
{
printf("value is = %d",(10++));
}

1.10 2.11 3.0 4.ERROR


#MCQ
#include <stdio.h>
void main()
{
const char var='A';
++var;
printf("%c",var);
}
1.B 2.A 3.ERROR 4.66
#MCQ
#include <stdio.h>
void main()
{
const char var='A';
++var;
printf("%c",var);
}
1.B 2.A 3.ERROR 4.66
#MCQ
#include <stdio.h>
void main()
{
char var=10;
printf("var is = %d",++var++);
}
1. ERROR : Can not modify var.
2. ERROR : L-Value required.
3. ERROR : Expression syntax.
4. 12
#MCQ
#include <stdio.h>
void main()
{
char var=10;
printf("var is = %d",++var++);
}
1. ERROR : Can not modify var.
2. ERROR : L-Value required.
3. ERROR : Expression syntax.
4. 12
#MCQ
main()
{
int i=10,j=2,k=0,m;
m=++i&&++j&&++k;
printf(“%d,%d,%d,%d”i,j,k,m);
}
1. 11,3,1,1 2. 11,2,0,1
3. 11,3,1,0 4. 10,2,0,1
#MCQ
main()
{
int i=10,j=2,k=0,m;
m=++i&&++j&&++k;
printf(“%d,%d,%d,%d”i,j,k,m);
}
1. 11,3,1,1 2. 11,2,0,1
3. 11,3,1,0 4. 10,2,0,1
#MCQ
#include <stdio.h>
int main()
{ 1. -1 -1
int i = 3; 2. 0 0
int l = i / -2; 3. -1 1
int k = i % -2; 4. 0 1
printf("%d %d\n", l, k);
return 0;
}
#MCQ
#include <stdio.h>
int main()
{ 1. -1 -1
int i = 3; 2. 0 0
int l = i / -2; 3. -1 1
int k = i % -2; 4. 0 1
printf("%d %d\n", l, k);
return 0;
}
#MCQ
#include <stdio.h>
void main() 1. 10 0
{ 2. 0 5
int x = 1, y = 0, z = 5; 3. 1 5
int a = x && y && z++; 4. 0 10
printf("%d %d",a, z);
}
#MCQ
#include <stdio.h>
void main() 1. 1 0 1 5
{ 2. 1 1 1 5
int x = 1, y = 0, z = 5; 3. 0 1 0 5
int a = x--|| ++y || z++; 4. 1 0 0 5
printf("%d %d",a,x,y,z);
}
#MCQ
#include <stdio.h>
void main() 1. 1 0 1 5
{ 2. 1 1 1 5
int x = 1, y = 0, z = 5; 3. 0 1 0 5
int a = x--|| ++y || z++; 4. 1 0 0 5
printf("%d %d",a,x,y,z);
}
#MCQ
#include <stdio.h>
void main() 1. -1
{ 2. 8
int x = 1, z = 3; 3. 11
int y = x << 3; 4. Compilation
printf(" %d\n", y); Error
}
#MCQ
#include <stdio.h>
void main() 1. -1
{ 2. 8
int x = 1, z = 3; 3. 11
int y = x << 3; 4. Compilation
printf(" %d\n", y); Error
}
#MCQ
#include <stdio.h>
void main() 1. 3
{ 2. 0
int x = 0, y = 2, z = 3; 3. 1
int a = x & y | z; 4. 7
printf("%d", a);
}
#MCQ
#include <stdio.h>
void main() 1. 3
{ 2. 0
int x = 0, y = 2, z = 3; 3. 1
int a = x & y | z; 4. 7
printf("%d", a);
}
#MCQ
#include <stdio.h>
int main() 1. Yes
{ 2. No
int i = 1; 3. Depends
if (i++ && (i == 1)) on Compiler
printf("Yes\n"); 4. Yes No
else
printf("No\n");
}
#MCQ
#include <stdio.h>
int main() 1. Yes
{ 2. No
int i = 1; 3. Depends
if (i++ && (i == 1)) on Compiler
printf("Yes\n"); 4. Yes No
else
printf("No\n");
}
#MCQ
#include <stdio.h> 1. 1
int main() 2. 8
{ 3. 9
int c = 2 ^ 3; 4. 0
printf("%d\n", c);
}
#MCQ
#include <stdio.h> 1. 1
int main() 2. 8
{ 3. 9
int c = 2 ^ 3; 4. 0
printf("%d\n", c);
}
#MCQ
#include <stdio.h> a) -9
int main() b) -10
{ c) -11
unsigned int a = 10; d) 10
a = ~a;
printf("%d\n", a);
}
#MCQ
#include <stdio.h> a) -9
int main() b) -10
{ c) -11
unsigned int a = 10; d) 10
a = ~a;
printf("%d\n", a);
}
#MCQ
#include <stdio.h> 1. Honesty
int main() 2. is the best policy
{
3. Honesty is the
if (7 & 8) best policy
printf("Honesty");
4. Error
if ((~7 & 0x000f) == 8)
printf("is the best
policy");
}
#MCQ
#include <stdio.h> 1. Honesty
int main() 2. is the best policy
{
3. Honesty is the
if (7 & 8) best policy
printf("Honesty");
4. Error
if ((~7 & 0x000f) == 8)
printf("is the best
policy");
}
#MCQ
#include <stdio.h> 1. 7
void main() 2. 0
{ 3. Error
char a = 'a'; 4. 8
int x = (a % 10)++;
printf("%d\n", x);
}
#MCQ
#include <stdio.h> 1. 7
void main() 2. 0
{ 3. Error
char a = 'a'; 4. 8
int x = (a % 10)++;
printf("%d\n", x);
}
#MCQ
#include <stdio.h> a) 2
int main() b) 1
{ c) 0.5
int x = 2, y = 2; d) Undefined behaviour
x /= x / y;
printf("%d\n", x);
return 0;
}
#MCQ
#include <stdio.h> a) 2
int main() b) 1
{ c) 0.5
int x = 2, y = 2; d) Undefined behaviour
x /= x / y;
printf("%d\n", x);
return 0;
}
#MCQ
#include <stdio.h>
void main() 1. AITAM
{ 2. Training
int x = 0; 3. AITAM Training
if (x = 0)
4. Compilation Error
printf("AITAM\n");
else
printf("Training\n");
}
#MCQ
#include <stdio.h>
void main() 1. AITAM
{ 2. Training
int x = 0; 3. AITAM Training
if (x = 0)
4. Compilation Error
printf("AITAM\n");
else
printf("Training\n");
}
#MCQ
#include <stdio.h>
int main() a) float
{ b) short int
int x = 1;
c) Undefined
short int i = 2;
float f = 3;
behaviour
if (sizeof((x == 2) ? f : i) == sizeof(float)) d) Compile
printf("float\n"); time error
else if (sizeof((x == 2) ? f : i) == sizeof(short
int))
printf("short int\n");
}
#MCQ
#include <stdio.h>
int main() a) float
{ b) short int
int x = 1;
c) Undefined
short int i = 2;
float f = 3;
behaviour
if (sizeof((x == 2) ? f : i) == sizeof(float)) d) Compile
printf("float\n"); time error
else if (sizeof((x == 2) ? f : i) == sizeof(short
int))
printf("short int\n");
}
#MCQ
#include <stdio.h> a) returns 1
void main() b) returns 2
{ c) Varies
1 < 2 ? return 1 : return 2; d) Compile time
} error
#MCQ
#include <stdio.h> a) returns 1
void main()
{ b) returns 2
1 < 2 ? return 1 : return 2; c) Varies
}
d) Compile time
Works only like this:
#include <stdio.h> error
void main()
{
1 < 2 ? printf("1") : printf("2");
}
#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=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!

Das könnte Ihnen auch gefallen