You are on page 1of 129

18.

What will be the output of the following program :


int main()
{
int val=97;
"Printing..."+printf("%c",val);
return(0);
}

19.

What will be the output of the following program :


int main()
{
int val=5;
val=printf("C") + printf("Skills");
printf("%d",val);
return(0);
}

20.

What will be the output of the following program :


int main()
{
int val=5;
printf("%*d",val,val);
return(0);
}

21.

What will be the output of the following program :


int main()
{
printf("%f",123.);
return(0);
}

22.

What will be the output of the following program :


int main()
{
char str[]="look at me";
printf("%d",sizeof str);
return(0);
}

23.

What will be the output of the following program :


int main()
{
char str[]="look at me";
printf("%d",-sizeof(str));
return(0);
1

}
24.

What will be the output of the following program :


int main()
{
printf("%d",!(100==100)+1);
return(0);
}

25.

What will be the output of the following program :


int main()
{
int x=5,y=6,z=2;
z/=y/z==3?y/z:x*y;
printf("%d",z);
return(0);
}

26.

What will be the output of the following program :


int main()
{
printf("%d %d %d",5,!5,25-!25);
return(0);
}

27.

What will be the output of the following program :


int main()
{
int a=500,b=100,c=30,d=40,e=19;
a+=b-=c*=d/=e%=5;
printf("%d %d %d %d %d",a,b,c,d,e);
return(0);
}

28.

What will be the output of the following program :


int main()
{
int a=500,b=100,c=30,d=40,e=19;
if ((((a > b) ? c : d) >= e) && !((e <= d) ? ((a / 5) == b) : (c == d)))
printf("Success");
else
printf("Failure");
return(0);
}

29.

What will be the output of the following program :


2

int main()
{
int a=1,b=2,c=3,d=4;
printf("%d",!a?b?!c:!d:a);
return(0);
}
30.

What will be the output of the following program [NOTE : 3 values


entered by the user are:100 200 300] :
int main()
{
int a=1,b=2,c=3;
scanf("%d %*d %d",&a,&b,&c);
printf("a=%d b=%d c=%d",a,b,c);
return(0);
}

31.

What will be the output of the following program [NOTE : THE USER
INPUT IS:Dear Friends, What is the output?] :
int main()
{
char line[80]; // Max. length=80 Chars
scanf("%[^,]s",line);
printf("\n%s",line);
return(0);
}

32.

What will be the output of the following program [NOTE : THE USER
INPUT IS :A B C] :
int main()
{
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
return(0);
}

33.

What will be the output of the following program [NOTE : THE USER
INPUT IS:5 5.75] :
void main()
{
int i=1;
float f=2.25;
scanf("%d a %f",&i,&f);
printf("%d %.2f",i,f);
}
3

34.

What will be the output of the following program [NOTE : THE USER
INPUT IS :ABC DEF GHI] :
int main()
{
char a,b,c;
scanf("%c %c %c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
return(0);
}

35.

What will be the output of the following program [NOTE : THE USER
INPUT IS:CMeansSea Ocean Vast] :
int main()
{
char a[80],b[80],c[80];
scanf("%1s %5s %3s",a,b,c);
printf("%s %s %s",a,b,c);
return(0);
}

36.

What will be the output of the following program [NOTE : THE USER
INPUT IS :123456 44 544] :
int main()
{
int a,b,c;
scanf("%1d %2d %3d",&a,&b,&c);
printf("Sum=%d",a+b+c);
return(0);
}

37.

What will be the output of the following program :


int main()
{
char line[80];
scanf("%[^1234567890\n]",line);
return(0);
}

38.

What will be the output of the following program :


int main()
{
char line[80];
scanf("%[^*]",line);
return(0);
}
4

C TOKENS PREDICTION (BASED ON RULES)


1.

Determine which of the following are VALID identifiers. If invalid, state


the reason.
(a) sample1 (b) 5sample
(c) data_7
(d) return
(e) #fine
(f) variable
(g) 91-080-100
(h) name & age
(i) _val
(j) name_and_age

2.

Determine which of the following are VALID character constants. If


invalid, state the reason.
(a) 'y'
(b) '\r' (c) 'Y'
(d) '@'
(e) '/r'
(f) 'word'
(g) '\0' (h) '\?' (i) '\065'
(j) '\'' (k) ' '

3.

Determine which of the following are VALID string constants. If


invalid, state the reason.
(a) 'Hi Friends'
(b) "abc,def,ghi"
(c) "Qualification
(d) "4325.76e-8"
(e) "Don\'t sleep" (f) "He said, "You\'re great"
(g) ""
(h) "
" (i) "Rs.100/-"

4.

Determine which of the following numerical values are valid constants.


If a constant is valid, specify whether it is integer or real. Also, specify
the base for each valid integer constant.
(a) 10,500 (b) 080
(c) 0.007
(d) 5.6e7
(e) 5.6e-7
(f) 0.2e-0.3 (g) 0.2e 0.3 (h) 0xaf9s82 (i) 0XABCDEFL (j) 0369CF
(k) 87654321l
(l) 87654321

5.

Determine which of the following floating-point constants are VALID


for the quantity (5 * 100000).
(a) 500000 (b) 0.5e6
(c) 5E5
(d) 5e5
(e) 5e+5
(f) 500E3
(g) .5E6
(h) 50e4
(i) 50.E+4 (j) 5.0E+5
(k) All of the above
(l) None of these

CHAPTER -4.3
Arithmetic
Operators,
Relational
Operators,
Assignment
Operators,Logical Operators, Increment And Decrement Operators,
Conditional Operator, Bitwise Operators
ONE WORD ANSWERS
Q1) How many arithmetic operators exist in C ?
Ans: Five.
Q2) What are the different arithmetic operators?
Ans: The different arithmetic operators are:
+ (addition),
- (subtraction),
* (multiplication),
/ (division),
% (modulo division).
Q3) What is precedence of an operator?
Ans: Precedence of an operators decides the order in which different
operators are applied.
Q4) What is associativity ?
Ans: Associativity decides the order in which operands are associated with
operator.
Q5) How many types of associativity exist in C ?
Ans : Two types.
Q6) What are two types of associativity ?
Ans : Left to Right & Right to Left.
Q7) Which arithmetic operator/s has highest precedence?
Ans: * , / and %
Q8) Which arithmetic operator/s has lowest precedence?
Ans: + and Q9)

What is the associativity of arithmetic operators?


6

Ans: Left to Right.


Q10) Which operator has highest precedence level in c?
Ans: ( ) [] has highest precedence level in c .
Q11) What will be the value of x,y,z for a=9,b=12,c=3 (all are declared as
float data type)
1. x = a-b/3+c*2-1;
2. y = a-b/(3+c)*(2-1);
3. z = a-(b/(3+c)*2)-1;
Ans: 1.x = 10.0000
2.y = 7.0000
3.z = 4.000.
Q12) How many relational operators exist in C?
Ans: Six
Q13) What are the different relational operators?
Ans: The different relational operators are:
< (is less than), <= (less than or equal to), > (greater than),
>= (greater than or equal to), == (equal to), !=(not equal to).
Q14) Which relational operators have highest precedence?
Ans: >, <, >=, <=
Q15) Which relational operator has lowest precedence?
Ans: = =, !=
Q16) What is the associativity of relational operators?
Ans: Left to Right.
Q17) What are the different assignment operators?
Ans: =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=
Q18) What is the associativity of assignment operators?
Ans: Right to Left
Q19) What will be the values of a, b, c, d (all are integer data types) and
initially a=10,b=3,c=2,d=8
1. a+=1;
2. b-=1;
3. c*=2;
4. d/=4;
Ans: a=11, b=2, c=8, d=2.
Q20) How many Logical operators exist in C?
7

Ans: Three
Q21) What are the different logical operators?
Ans: && (LOGICAL AND),
|| (LOGICAL OR),
! (LOGICAL NOT)
Q22) What is the associativity of logical and (&&), logical or (||) ?
Ans: Left to Right
Q23) What is the associativity of logical not ?
Ans: Right to Left
Q24) What is the associativity of increment and decrement operators?
Ans: Right to Left
Q25) What is an increment operator?
Ans: The operator which automatically increments the the value by one.
Q26) How many types of increment operators exist in C ?
Ans: Two types.
Q27) What are the two types of increment operators exist in C ?
Ans : Post increment and pre increment.
Q28) Which is a pre / post increment operator?
Ans: ++
Q29) Give an example for usage of pre increment operator
Ans: ++a
Q30) Give an example for usage of post increment operator
Ans: a++
Q31) What is post increment operator?
Ans : An operator which increments the value of a variable after the operation
Q32) What is pre increment operator?
Ans : An operator which increments the value of a variable before the
operation.
Q33) What is a decrement operator?
Ans: The operator which automatically reduce the the value by one.
Q34) How many types of decrement operators exist in C ?
Ans: Two types.
8

Q35) What are the two types of decrement operators exist in C ?


Ans : Post decrement and pre decrement.
Q36)

Give an example for usage of pre decrement and post decrement


operator?
Ans: --a & a--;
Q37) What is the value of m and y in the below expression?
m = 5;
y = ++m;
Ans: m=6,y=6.
Q38) What is the value of m and y in the below expression?
m = 5;
y = m++;
Ans: y=5, m=6.
Q39) Which is a conditional operator?
Ans: A ternary operator (? :) is a condiational operator. Which is an alternate
for if else statement.
Q40) What is the associativity of a conditional operator?
Ans: Right to Left
Q41) What will be the value of x after evaluating the following expression?
a=10; b=15; x= (a>b)?a:b;
Ans: 15
Q41) How many bitwise operators exist in C ?
Ans: Six.
Q42) What are the different Bit wise operators?
ans: Bit wise AND(&), bit wise OR(|), bit wise exclusive OR(^),
shift left(<<), shift right (>>), and Ones compelent ( ~) are called
Bit wise operators.
Q43) What is the associativity of bitwise operators?
Ans: Left to Right.

MULTIPLE CHOICE QUESTIONS:


1. What will be output of the following c program?
#include<stdio.h>
9

int main()
{
int goto=5;
printf("%d",goto);
return 0;
}
a. goto
b.5
c.go to 5

d.compilation error

2. What will be output of the following c program?


#include<stdio.h>
int main()
{
long int 1a=5l;
printf("%ld",1a);
return 0;
}
a. 51
b. 510
c. 58
d.60
3. What will be output of the following c program?
#include<stdio.h>
int main()
{
int _=5;
int __=10;
int ___;
___=_+__;
printf("%i",___);
return 0;
}
a. 51
b. 61
c. 15

d.151

4.What will be output of the following c program?


#include<stdio.h>
int main()
{
int max-val=100;
int min-val=10;
int avg-val;
avg-val = max-val + min-val / 2;
printf("%d",avg-val);
return 0;
}
a.10
b.15
c. 105

d.150

5. What will be output of the following c program?


#include<stdio.h>
10

int main()
{
int class=150, public=25, private=30;
class = class >> private - public;
printf("%d",class);
return 0;
}
a.2
b. 1
c.45

d.257

OUTPUT PREDICTION
1. What will be output of the following program?
#include<stdio.h>
int main()
{
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
}
2. What will be output of the following program?
#include<stdio.h>
int main()
{
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}
3. What will be output of the following program?
#include<stdio.h>
void main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
4. What will be output of the following program?
#include<stdio.h>
int main()
{
11

int a;
a=015 + 0x71 +5;
printf("%d",a);
return 0;
}
5. What will be output of the following program?
#include<stdio.h>
int main()
{
printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
return 0;
}
6. What will be output of the following program?
#include<stdio.h>
int main()
{
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}
7. What will be output of the following program?
#include<stdio.h>
int main()
{
int a=2;
a=a++ + ~++a;
printf("%d",a);
return 0;
}
8. What will be output of the following program?
#include<stdio.h>
int main()
{
int a;
a=sizeof(!5.6);
printf("%d",a);
return 0;
}
9. What will be output of the following program?
#include<stdio.h>
12

int main()
{
float a;
(int)a= 45;
printf("%d,a);
return 0;
}
10. What will be output of the following program?
#include<stdio.h>
int main()
{
int i=5;
int a=++i + ++i + ++i;
printf("%d",a);
return 0;
}

ASSIGNMENT
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.

Write a C program to illustrate the use of arithmatic operators


Write a C program to illustrate the use of unary operators
Write a C program to illustrate the usage type cast operators
Write a C program to evaluate the polynomial
3x^3-5x^2+6=0 for x=2.55
Write a C program to evaluate the following expression
(3.31x10^-8x2.01x10^-7)/(7.16x10^-6=2.01x10^-8)
a=1.82,b=3.18,c=3.37,d=4.18
Write a C program to evaluate the expression
[8.8(a+b)2/c-0.5+29(c+d)]/[(a-b)/d]
Write a C program to evaluate the expression
[7.7b(bc+a)/d-0.8+2b]/[d+a/c]
If a five digit number is input through keyboard, write a c program to
calculate sum of digits

13

CHAPTER 4.4
Decision making : The If Statement , The If Else Construct,
Nested If Statements, Switch Statement, The Conditional Operator
The IF Statement
The if statement is a decision making statement and used to control the
flow of execution of statements. It's general syntax is
if(test expression)
{
executable part;
}
Q1. When if block is executed?
Ans: Only when the test expression is true.
Q2. What is the other name of if statements?
Ans: Decission control statement.
The If Else Construct:
The if else statement is an extension of the simple if statement .It's
general syntax is
if(test expression)
{
true block statements;
}
else
{
false block statements;
}
if the test expression is true,then the true block statements are executed
otherwise the flase block statements are executed.
Q3. When will be the else part be executed?
Ans: Only when ifs text expression is false.
Nested If Statements:
When a series of decisions are involved we may have to use more than
one if statements. Then we use nested if statements. Its general syntax is
if(test condition 1)
{
if(test condition 2)
14

{
statements..
}
}
Q4. What is nested if statement ?
Ans. If statement within an if statement is known as nested if statement.
Q5. When will be the nested if be executed?
Ans: Nested ifs are executed only when all the testconditions are satisfied.
Switch Statement:
The switch statement test the value of a given expression against a list of
case values. When a match is found a block of statements associated with the
case is executed, its general syntax is
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
....
default:
default -block;
break;
}
Q6. When the default statements are executed?
Ans:
When the entered value is not among the switch case values then
default statements are executed.
Q7. Which type of data is allowed in switch case?
Ans:
int or char constant.
The Conditional Operator
The ternary operator( ?: ) is called as conditional operator and it works
like if else statement.
expression1? expression2 : expression3 ;
Q8. How many operands required for conditional operator?
Ans: Three operands .

15

MULTIPLE CHOICE QUESTIONS:


1)

What will be the output of the following program :


void main()
{
int i=1;
for (; i<4; i++);
printf("%d\n",i);
}
a.No Output
b. 1
c. 4
d. None of these
2
3

2)

What will be the output of the following program :


void main()
{
int a,b;
for (a=0; a<10; a++);
for (b=25; b>9; b-=3);
printf("%d %d",a,b);
}
a. Compile-Time error
b. 10 9
c.10 7
d. None of these
3)What will be the output of the following program :
void main
{
float i;
for (i=0.1; i<0.4; i+=0.1)
printf("%.1f",i);
}
a. 0.10.20.3 b. Compile-Time Error
c. Run-Time Error d. No Output
4)

What will be the output of the following program :


void main()
{
int i;
for (i=-10; !i; i++);
printf("%d",-i);
}
16

a. 0
5)

a. 5

b. Compile-Time Error

c.10

d. No Output

What will be the output of the following program :


void main()
{
int i=5;
do;
printf("%d",i--);
while (i>0);
}
b. 54321
c. Compile-Time Error
d. None of these

6) What will be the output of the following program :


void main()
{
int i;
for (i=2,i+=2; i<=9; i+=2)
printf("%d",i);
}
a. Compile-Time error b. 2468 c. 468
d. None of these
7) What will be the output of the following program :
void main()
{
int i=3;
for (i--; i<7; i=7)
printf("%d",i++);
}
a. No Output
b. 3456
c. 23456
d. None of these.
8) What will be the output of the following program :
void main()
{
int i;
for (i=5; --i;)
printf("%d",i);
}
a. No Output
b. 54321
c. 4321
9) What will be the output of the following program :
void main()
{
for (;printf(""););
}
a. Compile-Time error
b. Executes ONLY once
c. Executes INFINITELY
d. None of these
17

d. None of these

10)What will be the output of the following program :


void main()
{
int i;
for (;(i=4)?(i-4):i++;)
printf("%d",i);
}
a. Compile-Time error

b. 4

c . Infinite Loop

18

d.No Output

CHAPTER -4.5
PROGRAM LOOPING:The For Statement, Nested For Loops, The While
Statement, The Do While Statement, The Break And Continue Statements
The For Statement
The for loop is a entry controlled loop that provides a more concise loop
control structure.

its general syntax is


for(initialization;test-condition;operation)
{
body of the loop;
}
Nested For Loops
Nested for loops means one for loop within other for loop its general
syntax is
for(initialization1;testcondition;operation)
{
for(inittialization2;testcondition2;operation)
{
...............
}
}
The While Statement
it is the simplest of all looping structures in C.it is also an entry controlled
statement.
its general syntax is
while(test condition)
{
body of loop;
}
The do while statement
It is same as the while statement but the test condition is placed at the
bottom of the loop.
its general syntax is

19

do
{
bodyof the loop;
}
while(test-condition);
Q1) what is the minimum execution time of do while loop.?
Ans: 1 time.
The break and continue statements
When a break statement is encountered in a loop, the loop is immediately
exited from the loop and the program continues with the statement immediately
following the loop.
while (test-condition)
{
if(-----------)
break;
----------------}
when it is necessary to skip a part of the loop we use continue statements.
its general syntax is
while(test-condition)
{
if(-----------)
continue;
----------------}
MULTIPLE CHOICE QUESTIONS
1)

What will be the output of the following program :


void main()
{
printf("Hi!");
if (-1)
printf("Bye");
}
a. No Output
b. Hi!
C. Bye
2)

What will be the output of the following program :


void main()
20

d.Hi!Bye

{
printf("Hi!");
if (0 || -1)
printf("Bye");
}
a. No Output
3)

b. Hi!

C. Bye

d. Hi!Bye

What will be the output of the following program :


void main()
{
printf("Hi!");
if (!1)
printf("Bye");
}

a. Compile-Time error

b. Hi!

c. Bye

What will be the output of the following program :


void main()
{
printf("Hi!");
if !(0)
printf("Bye");
}
a. Compile-Time error
b. Hi!
c .Bye

d. Hi!Bye

4)

What will be the output of the following program :


void main()
{
printf("Hi!");
if (-1+1+1+1-1-1-1+(-1)-(-1))
printf("Bye");
}
a. No Output
b. Hi!
c. Bye

d. Hi!Bye

5)

6)

d. Hi!Bye

What will be the output of the following program :


void main()
{
if (sizeof(int) &&sizeof(float) &&sizeof(float)/2-sizeof(int))
printf("Testing");
printf("OK");
}
21

a. No Output

7)

b. OK

c. Testing

d. TestingOK

What will be the output of the following program :


void main()
{
int a=1,b=2,c=3,d=4,e;
if (e=(a & b | c ^ d))
printf("%d",e);
}
b. 7
c. 3

d. No Output

What will be the output of the following program :


void main()
{
unsigned val=0xffff;
if (~val)
printf("%d",val);
printf("%d",~val);
}
a. Compile-Time error
b. -1
c. 0

d. -1 0

a. 0
8)

9)

What will be the output of the following program :


void main()
{
unsigned a=0xe75f,b=0x0EF4,c;
c=(a|b);
if ((c > a) && (c > b))
printf("%x",c);
}
a. No Output
b. 0xe75f
c. 0xefff
10)

What will be the output of the following program :


void main()
{
unsignedval=0xabcd;
if (val>>16 | val<<16)
{
printf("Success");
return;
}
22

d. None of these

printf("Failure");
}
a. No Output

b. Success

c. Failure

d. SuccessFailure

11)

What will be the output of the following program :


void main()
{
unsigned x=0xf880,y=5,z;
z=x<<y;
printf("%#x %#x",z,x>>y-1);
}
a. 1000 f87
b. 8800 0xf88
c. 1000 f88
What will be the output of the following program :
void main()
{
register int a=5;
int *b=&a;
printf("%d %d",a,*b);
}
a. Compile-Time error
b. Run-Time error c. 5 5

d. 0x1000 0xf88

12)

What will be the output of the following program :


autoint a=5;
void main()
{
printf("%d",a);
}
a. Compile-Time error
b. Run-Time error c. 5

d. Unpredictable

13)

What will be the output of the following program :


void main()
{
autoint a=5;
printf("%d",a);
}
a. Compile-Time error
b. Run-Time error c. 5

d. Unpredictable

14)

15)

What will be the output of the following program :


void main()
{
int a=1,b=2,c=3,d=4;
23

d. Unpredictable

if (d > c)
if (c > b)
printf("%d %d",d,c);
else if (c > a)
printf("%d %d",c,d);
if (c > a)
if (b < a)
printf("%d %d",c,a);
else if (b < c)
printf("%d %d",b,c);
}
a. 4 3 3 4
16)

b. 4 3 3 2

c. 4 32 3

d. 4 33 1

What will be the output of the following program :


void main()
{
int a=1,b=2,c=3,d=4;
if (d > c)
if (c > b)
printf("%d %d",d,c);
if (c > a)
printf("%d %d",c,d);
if (c > a)
if (b < a)
printf("%d %d",c,a);
if (b < c)
printf("%d %d",b,c);

}
a. 4 32 3

b. 4 33 42 3

c. 4 3 3 4 2 3

17)

d. None of these

What will be the output of the following program :


void main()
{
int a=1;
if (a == 2);
printf("C Program");
}
a. No Output
b . C Program
c. Compile-Time Error
d. none of these
18)

What will be the output of the following program :


void main()
24

{
int a=1;
if (a)
printf("Test");
else;
printf("Again");
}
a. Again
19)

b. Test

c. Compile-Time Error

d. TestAgain

What will be the output of the following program


void main()
{
int choice=3;
switch(choice)
{
default:
printf("Default");
case 1:
printf("Choice1");
break;
case 2:
printf("Choice2");
break;
}

}
a. No Output
20)

b. Default

c. DefaultChoice1

What will be the output of the following program :


void main()
{
staticint choice;
switch(--choice,choice-1,choice-1,choice+=2)
{
case 1:
printf("Choice1");
break;
case 2:
printf("Choice2");
break;
default:
printf("Default");
25

d. None of these

}
}
a. Choice1

b. Choice2

c. Default

OUTPUT PREDICTION
(1)

What will be output of following c code?


#include<stdio.h>
extern int x;
int main()
{
do
{
do
{
printf("%o",x);
}while(!-2);
}while(0);
return 0;
}
int x=8;

(2)

What will be output of following c code


#include<stdio.h>
int main()
{
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}

(3)

What will be output of following c code?


#include<stdio.h>
int main()
{
int x=011,i;
for(i=0;i<x;i+=3)
{
printf("Start ");
continue;
printf("End");
26

d. None of these

}
return 0;
}
4)

What will be output of following c code?


#include<stdio.h>
int main()
{
int i,j;
i=j=2,3;
while(--i&&j++)
printf("%d %d",i,j);
return 0;
}

(5)

What will be output of following c code?


#include<stdio.h>
int main()
{
staticint i;
for(++i;++i;++i)
{
printf("%d ",i);
if(i==4)
break;
}
return 0;
}

(6)

What will be output of following c code?


#include<stdio.h>
int main()
{
int i=1;
for(i=0;i=-1;i=1)
{
printf("%d ",i);
if(i!=1)
break;
}
return 0;
}

27

(7)

What will be output of following c code?


#include<stdio.h>
int main()
{
for(;;)
{
printf("%d ",10);
}
return 0;
}

(8)

What will be output of following c code?


#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main()
{
do
{
int i=15,j=3;
printf("%d",p(i-+,+j));
}while(*(call(625)+3));
return 0;
}

(9)

#include<stdio.h>
int main()
{
int i;
for(i=0;i<=5;i++);
printf("%d",i)
return 0;
}
What will be output of following c code?
#include<stdio.h>
int i=40;
extern int i;
int main()
{
do
{
printf("%d",i++);
}while(5,4,3,2,1,0);
return 0;

(10)

28

(11)

}
What will be output of following c code?
#include<stdio.h>
int main()
{
int i;
for(i=10;i<=15;i++)
{
while(i)
{
do
{
printf("%d ",1);
if(i>>1)
continue;
}while(0);
break;
}
}
return 0;
}

(12)

How many times this loop will execute?


#include<stdio.h>
int main()
{
char c=125;
do
printf("%d ",c);
while(c++);
return 0;
}

(13)

What will be output of following c code?


#include<stdio.h>
int main()
{
int x=123;
int i;
{
printf("c" "++")
};
for(x=0;x<=i;x++)
{
printf("%x ",x);
29

}
return 0;
}

ASSIGNMENT
Q.Write a C program to calculate the 200th triangular number.
Q.Write a C program to reverse the digits of given number.
Q.Write a C program to determine if a number is even or odd
Q.Write a C program to implement the sign functions
Q.Write a C program to print prime numbers below 100
Q.Write a C program to print multiplication table of the number entered by the
user,the table should be displayed in the
following form
29x1=29
29x2=58
: :
: :
Q.Write a C program to produce the following output
1
2 3
4 5 6
7 8 9 10
Q.Write a C program to recieve an integer and find its octal equivalent.
Q.Write a C program to illustrate nested loops.
Q.Write a C program to illustrate the nested if elses.

30

CHAPTER 4.6
WORKKING WITH ARRAYS:Defining An Array,Declaration And
Initializing The Array,Character Arrays,The Const Qualifier ,
Multidimensional Array,Variable Length Arrays
ONE WORD ANSWERS
Q1) What is an array?
Ans: Array is collection of homogeneous data.It stores data in contigeous
memory location.
Q2) How many types of arrays exist and what are they?
Ans: There are 3 types of arrays they are
a) one dimensional array
b) two dimensional array
c) multi dimensional array
Q3) What is an one dimensional array?
Ans: A list of items given in one variable name using only one subscript is
known as one dimensional array.
Example: int a[3]
Q4) What is two dimensional array?
Ans: Collection of homogeneous elements in rows and columns is known as
two-dimensional array.
Example: int matrix[3][3]
Q5) What is the general format of declaring an array?
Ans: datatype Arrayname[size]
note: the size must be an integer.
Q6) What is the value of starting index of an array?
Ans: zero
Q7) What is the use of const qualifier?
Ans: The compiler allows to associate the const qualifier with variables
whose values will not be changed by the program.
Q8)

What value is automatically assigned to those array elements that are not
explicitly initialized?

31

Ans: All of array elements automatically set to zero except those that have
been explicitly initialized with in array definitions.
Q9)

State the rule that determines the order in which initial values
are assigned to multi dimensional array elements?
Ans: the rule is that the last (right most) subscript increases most
rapidly and the first (left most) increases least rapidly.

MULTIPLE CHOICE QUESTIONS


1.

A array an example of ---?


a. derived types
b. fundamental types
c. user-defined types
d. none of the above

2.

Which of the following is not a data structure?


a. linked list
b. stack
c. tree
d. pointer

3.

int a[n] will reserve how many locations in the memory?


a. n
b. n-1
c. n+1
d. none of the above

4.

Which of the following is the correct syntax for the initialisation of


one dimensioal array?
a. num[3]={0 0 0};
b. num[3]={0,0,0};
c. num[3]={0;0;0};
d. num[3]=0

5.

Which of the following is the correct syntax for initialisation of


Two dimensional array?
a.table[2][3]={0,0,0,1,1,1};
b.table[2][3]={
{0,0,0}
{1,1,1}
};
c.table[2][3]={0,1},{0,1},{0,1};
d.None of the above

6.

Which of the following multi-dimensional array declaration is correct


32

for realizing a 2x3 matrix?


a. int m[2][3]
b. int m[3][2]
c. int m[3],m[2]
d. None of the above
7.

Which of the following is not the name of sorting technique?


a.Bubble
b.Selection
c.Binary
d.Insertion

8.

Which of the following is not the name of a searching technique?


a.Selection
b.Sequential
c.Binary
d.All of the above are searching techniques

9.

Which is the last element of character string?


a. Last element of the string
b. Blank space
c. Null character
d. New line character

OUTPUT PREDICTION
1.

What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
char arr[11]="The African Queen";
printf("%s",arr);
}

2.

What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
char arr[20]="MysticRiver";
printf("%d",sizeof(arr));
}

33

3.

What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
int const SIZE=5;
int expr;
double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
expr=1|2|3|4;
printf("%f",value[expr]);
}

4.

What will be output if you will execute following c code?


#include<stdio.h>
#define var 3
void main()
{
char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
printf("%o",data[0][2][1]);
}

5.

What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
int arr[][3]={{1,2},{3,4,5},{5}};
printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}

6.

What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
int xxx[10]={5};
printf("%d %d",xxx[1],xxx[9]);
}

7.

What will be output if you will execute following c code?


#include<stdio.h>
#define WWW -1
enum {cat,rat};
void main()
{
int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW};
34

int i;
for(i=0;i<8;i++)
printf(" %d",Dhoni[i]);
}
8.

What will be output if you execute the following c code?


#include<stdio.h>
void main()
{
long double a;
signed char b;
int arr[sizeof(!a+b)];
printf("%d",sizeof(arr))
}
ASSIGNMENT

Q.

Write a C program to print first 15 numbers of fibonacci series

Q.

Write a C program to illustrate usage of character arrays

Q.

Write a C program to perform arithmetic operation on matrices

Q.

Write a C program to find if a square matrix is symmetric

Q.

Write a C program to multiply any 3x3 matrices

Q.

Write a C program for finding the largest number in an one dimensional


array

Q.

Write a C program for sorting the elements of an array in descending


order

Q.

Write a C program to perform linear search on a one dimensional array

Q.

Write a C program to generate transpose of a 3x3 matrix

Q.
Write a C program to read 10 integer numbers and print their average,
minimum and max. numbers

35

CHAPTER -4.7
WORKING WITH FUNCTIONS:
Defining A Function, Arguments And Local Variables, Returning
Function Results, Function Calling, Declaring Return Types And
Argument Types,Top Down Programming, Functions And Arrays,
Global Variables, Automtic And Static Variables, Recursive Function .
ONE WORD ANSWERS
Q1) What is a function?
Ans: A function is a self-contained block of code that performs a particular
task.
Q2) How many types of functions exist in C,what are they?
Ans: There are two types of functions they are
a.User-defined functions
b.standard library functions.
Q3) What are the advantages of using functions?
Ans: a. The length of the source program can be reduced by using function.
b.It is easy to locate and isolate a faulty functions for further
investigations.
Q4) What is Function definition?
Ans: It is an independent program module that is especially written to
implement the requirements of the function.
Q5) What are the elements involved in function definition?
Ans: The six elements that are involved in a function definition are:
a.Function return type
b.Function name
c.Parameter list
d.Local variables
e.Function statements and
f.Return statement
Q6) What is function header?
Ans: The function type,function name and the parameter list together is called
a function header.
36

Q7) What is function body?


Ans: The local variables the function statements and the return statement on a
whole is known as function body.
Q8) What are formal parameters?
Ans: The parameter list declares the variables that will receive the data sent
by the calling programme. They serve as the input data to function to
carry out the specified task since they represent actual input values, they
are called as the "formal parameters".
Q9) What are actual parameters?
Ans: The parameters which are used in the function call are called as actual
parameters. These may be simple constants, variables or expressions.
Q10) what is an Argument?
Ans: This is one type of parameter which collects the value from the calling
function.
Q11) what is Local variable ?
Ans: The variables which are declared inside the function is known as local
variable
Q12) what is global variable ?
Ans: The variables which are declared outside the function is known as global
variable
Q13) What is function declaration?
Ans: The calling function should declare any function that is to be used later
in the program this is known as function declaration or function
prototype.
Q14) what are the main elements required in the function prototype?
Ans: The main elements required in the function prototype are:
a.function return type
b.function name
c.parameters list
d.terminating semicolon
NOTE: Terminating semicolon(;) is important.
Q15) What are the types of functions depending upon categories of arguments
and return statements?
Ans: Depending upon categories of arguments and return statements there are
four types of functions
a. function with no arguments and no return values
b. function with arguments and no return value
37

c. function with arguments and one return value


d. function with no arguments but return a value
Q16) How do you return multiple values to the calling function?
Ans: Multiple values to the calling function can be returned using
"POINTERS".
Q17) How do you pass multiple variables using pointers?
Ans: Multiple values can be passed using pointers by using addresses of
variables in the actual parameters.
Q18) What are the rules to pass an array to a function?
Ans: The rules are:
a.the function must be called by passing only the name of an array
b.the function definition, the formal parameter must be an array type; the
size of an array does not need to be specified.
c.the function prototype must show that the argument is an array.
Q19) Is it possible to pass an entire array to a function as an argument?
A:
yes, it is possible to pass an entire array to a function as an argument.
Q20) How to pass an array to a function?
Ans: To pass an array to a function ,the name of the array must appear itself,
without brackets or subscripts, as an actual argument with in the
function call.
Q21) What are automatic variables?
Ans: These are the variables declared inside a function in which they are
utilised. They are cerated when the function is 'called' and destroyed
automatically when the function is exited.
Q22) What are static variables? what are its types?
Ans: These are the variables which persist until the end of the program. They
are of two types
a.Internal static variables: are those declared inside a function.
b.External static variables: are those declared outside a function.
Q23) What is a recursive function?
Ans: a function which calls itself is known as recusive function.
Q24) Give an example where we use recursion?
Ans: Recusion can be used for doing progrms like mathematical induction,
fibbonaci series etc.....

38

Multiple Choice Questions.


1.

The default return type of a function is ___________


a. Void
b. Int
c. Float
d. Char

2.

Which of the following are the incorrect fuction declaration?


a. int funct(int a,b;);
b. int funct(int a,int b);
c. int funct(int ,int );
d. int funct(int, );

3.

Which of the following is not feasible?


a. fuctions with no arguments and no return values
b. function with arguments and no return values
c. function with no arguments but return value
d. all the above are feasible

4.

Recursion is function where:


a. A function calls a main function
b. A function calls any of the system function
c. A function calls itself
d. None of the above

5.

Which of the following is not a variable storage class


a. automatic
b. extern
c. static
d. dynamic

6.

Which of the fillowig keywords is used for declaring an external


variable?
a. external
b. extern
c. auto extern
d. ext

7.

Which of the following types of variables remain alive for the entire life
time of the program?
a. extern
39

b. auto
c. register
d. static
8.

9.

10.

Which of the following refers to the region of a programme where a


variable is available for use?
a. scope
b. visibility
c. life time
d. None of the above
The formal parameters in the function header must be prefixed by which
of the following indirection operator?
a. *
b. +
c. d. /
What will be the output of the following program :
static int funct(int val)
{
static int sum;
sum+=val;
return sum;
}
void main()
{
int i,n=9;
for (i=1; i<n--; i++)
funct(i*2);
printf("%d",funct(0));
}
a.209
b.20
c.30
d.90

11.

What will be the output of the following program :


void print(int a[],...)
{
while (*a != -1)
printf("%d",*a++);
}
void main()
{
int a[]={1,2,3,4,5,-1};
print(a,5,6,7,8,9,-1);
}
a.1
b.2
c.12345
d.34

12.

What will be the output of the following program :


40

void print(int *);


void print(int *);
void main()
{
int x=100;
print(&x);
}
void print(int *a)
{
printf("%d",*a);
}
a.12
b.321

c.101

d.100

13.

What will be the output of the following program :


void main()
{
void funct1(void);
void funct2(void);
clrscr();
funct1();
}
void funct1(void)
{
printf("Ocean of ");
funct2();
}
void funct2(void)
{
printf("Knowledge");
}
a.ocean
b.know
c. Knowledge
d.compile timeerror

14.

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
}
void main()
{
int var=7,sum;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a.11

b.11 7

c.7

d.17
41

15.

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
return *ptr;
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a.1
b.7
c.11 7
d.17

16.

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
*ptr=25;
return *ptr;
return (a+b);
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a.25
b.25 25
c.30
d.30 25
What will be the output of the following program :
#include <stdio.h>
#define ToStr(s) #s
#define Swap(x,y) y##x
void Swap(in,ma)()
{
if (printf(ToStr("Friends"))){}
}
a. Friends
b.friend
c.error
d.none of these
What will be the output of the following program :
int add(int a,int b)
{
return a+b;
}
main()
{

17.

18.

42

int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
}
a.23

b.12

c.52

d.31

OUTPUT PREDICTION
1)

The following code is not well-written. What does the program do ?


void main()
{
int a=1,b=2;
printf("%d",add(a,b));
}
int add(int a,b)
{
return (a+b);
}

2)

What will be the output of the following program :


int add(int a,int b)
{
int c=a+b;
}
void main()
{
int a=10,b=20;
printf("%d %d %d",a,b,add(a,b));
}

3)

What will be the output of the following program :


int add(int a,int b)
{
int c=a+b;
return;
}
void main()
{
int a=10,b=20;
printf("%d %d %d",a,b,add(a,b));
}

4)

What will be the output of the following program :


void main()
{
int add(int,int);
int a=7,b=13;
43

printf("%d",add(add(a,b),add(a,b)));
}
int add(a,b)
int a,b;
{
return (a+b);
}
5)

What will be the output of the following program :


int add(a,b)
{
int c=a+b;
return c;
}
void main()
{
int a=10,b=20;
printf("%d",add(a,b));
}

6)

What will be the output of the following program :


int funct2(int b)
{
if (b == 0)
return b;
else
funct1(b--);
}
int funct1(int a)
{
if (a == 0)
return a;
else
funct2(a--);
}
void main()
{
int a=7;
printf("%d",funct1(a));
}

7)

What will be the output of the following program :


int funct2(int b)
{
if (b == 0)
return b;
44

else
funct1(--b);
}
int funct1(int a)
{
if (a == 0)
return a;
else
funct2(--a);
}
void main()
{
int a=7;
printf("%d",funct1(a));
}
8)

What will be the output of the following program :


int funct1(int a)
{{;}{{;}return a;}}
void main()
{
int a=17;
printf("%d",funct1(a));
}

9)

What will be the output of the following program :


int funct1(int a)
{
if (a)
return funct1(--a)+a;
else
return 0;
}
void main()
{
int a=7;
printf("%d",funct1(a));
}

10)

What will be the output of the following program :


int compute(int a,int b)
int c;
{
c=a+b;
45

return c;
}
void main()
{
int a=7,b=9;
printf("%d",compute(ab));
}
11)

What will be the output of the following program :


int a=10;
void compute(int a)
{
a=a;
}
void main()
{
int a=100;
printf("%d ",a);
compute(a);
printf("%d",a);
}

12)

What will be the output of the following program :


int funct(char ch)
{
ch=ch+1;
return ch;
}
void main()
{
int a=127;
printf("%d %d",a,funct(a));
}

13)

What will be the output of the following program :


auto int a;
void change(int x)
{
a=x;
}
void main()
{
46

a=15;
printf("%d",a);
changeval(75);
printf("%d",a);
}
14)

What will be the output of the following program :


int val;
static int funct()
{
return val*val;
}
void main()
{
val=5;
funct();
val++;
printf("%d",funct());
}

15)

What will be the output of the following program :


static int count=1;
void funct3(void)
{
printf("%d",++count);
}
void funct2(void)
{
printf("%d",count);
funct3();
}
void funct1(void)
{
printf("Counting...%d",count++);
funct2();
}
void Main()
{
funct1();
}

ASSIGNMENT

47

Q.

Write a C program to compute area of a rectangle

Q.

Write a C program to implement a simple arithmetic calculator using


functions

Q.

Write a C program to generate fibonacci series using recursion

Q.

Write a C program to print numbers from 0 to given n using recursion

Q.

Write a C program to print from given n to 0 using recursion

Q.

Write a C program to regenrate the one's compliment of given binary


number

Q.

Write a C program to generate binary equivalent of entered number


using recursion

Q.

A 5 digit positive number is entered through the keyboard, write a


function to calculate sum of digits of 5 digit number using recursion

Q.

Write a C program to to do the same above functions without using


recursion

48

CHAPTER 5.1
Working With Structures: Defining A Structure,Functions And Structures,
Initializing Structures, Array Of Structures,Structures Containing
Structures,Structures Containing Arrays,Structure Variants
Structure:
A structure is a user defined datatype and it is used for handling a group
of logically related datatypes.Structures can be intialised either at
compiletime or at runtime.
ONE WORD ANSWERS
Q1) What is a structure?
Ans: A structure is a collection of heterogeneous data.
Q2) What is the general format of the structure?
Ans: The general format\ of a structure is
struct tag_name
{
datatype name1;
datatype name2;
:
:
:
};
where name1,name2......... are called as structure members.
Q3). What are the major differences between arrays and structures?
Ans: a. an array is a collection of similar datatypes where as the stuctrure is a
collection of different data types.
b. an array is derived datatype where as a structure is user defined
datatype.
Q4). How the members of a structure can be accessed?
Ans: The members of a structure can be accessed using the dot operator or
arrow operator.
Q5) How is the structure initialized at compiletime?
Ans: Structure at compiletime can be initialised in this way
struct Test
{
49

int a;
char c[10];
};
struct tag_name s1={10,"ravi"};
Q6). How is the structure initialised at runtime?
Ans: Structure at runtime can be initialised in this way
struct tag_name
{
int a;
char c[10];
};
struct tag_name s1;
scanf("%d,%c",&s1.a,s1.c);
Q7). What is nested structure?
Ans: A structure within a structure is called as nested structure.
Q8) Give an example of array of structures?
Ans: struct student s[100];
Q9.

What is the general format of sending a copy of a structure to the called


function?
Ans: The general fomrat of sending a copy of a structure to the called
function is
function_name(structure_variable name);
MULTIPLE CHOICE QUESTIONS
1.

A structure should store ---?


a. multiple values of the same type
b. multiple values of different types
c. multiple values of the same user-defined type
d. None of the above

2.

Which of the following is the correct way of assigning a value to the


"rollno" of a simple structure variable student
a. student.rollno
b. student->rollno
c. student(rollno)
d. None of the above

50

3.

The uninitialized integer data type of a structure contains which of the


following default values?
a. garbage
b. zero
c. one
d. None of the above

4.

Which of the following expressions arre correct for accessing the 'num'
variable value of the ith element of a structure array 'student'
a. student[i].num
b. student.num[i]
c. student[i]_>num
d. None of the above

5.

which is necessary at the end of definition of a structure?


a. *
b. &
c. :
d. ;

6.

What comes after the closing brace when we use typedef definition
a. type_name;
b. type name;
c. type_type;
d. None of the above

7.

what must be used to declare a variable in an independent statement?


a. name
b. type
c. type_name
d. None of the above

8.

A structure member can be accessed using___________


a. arrow operator
b. dot operator
c. both a&b
d. None of the above

9.

A structure can contain


a. only int data types
b. only char data types
c. any data type
d. None of the above

51

OUTPUT PREDICTION
1)

What will be the output of the following program :


struct
{
int i;
float f;
}var;
void main()
{
var.i=5;
var.f=9.76723;
printf("%d %.2f",var.i,var.f);
}

2)

What will be the output of the following program :


struct
{
int i;
float f;
};
void main()
{
int i=5;
float f=9.76723;
printf("%d %.2f",i,f);
}

3)

What will be the output of the following program :


struct values
{
int i;
float f;
};
void main()
{
struct values var={555,67.05501};
printf("%2d %.2f",var.i,var.f);
52

4)

What will be the output of the following program :


typedef struct
{
int i;
float f;
}values;
void main()
{
static values var={555,67.05501};
printf("%2d %.2f",var.i,var.f);
}

5)

What will be the output of the following program :


struct my_struct
{
int i=7;
float f=999.99;
}var;
void main()
{
var.i=5;
printf("%d %.2f",var.i,var.f);
}

6)

What will be the output of the following program :


struct first
{
int a;
float b;
}s1={32760,12345.12345};
typedef struct
{
char a;
int b;
}second;
struct my_struct
{
float a;
unsigned int b;
};
53

typedef struct my_struct third;


void main()
{
static second s2={'A',- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf("%d %.2f\n%c %d\n%.2f %u",(s1.a)-,s1.b+0.005,s2.a+32,s2.b,++(s3.a),--s3.b);
}
7)

What will be the output of the following program :


struct
{
int i,val[25];
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
void main()
{
printf("%d %d %d\n",var.i,vptr->i,(*vptr).i);
printf("%d %d %d %d %d %d",var.val[4],*(var.val+4),
vptr- >val[4],*(vptr->val+4),(*vptr).val[4],*((*vptr).val+4));
}

8)

What will be the output of the following program :


typedef struct
{
int i;
float f;
}temp;
void alter(temp *ptr,int x,float y)
{
ptr->i=x;
ptr->f=y;
}
void main()
{
temp a={111,777.007};
printf("%d %.2f\n",a.i,a.f);
alter(&a,222,666.006);
printf("%d %.2f",a.i,a.f);
}

9)

What will be the output of the following program :


typedef struct
{
54

int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={111,777.007};
printf("%d %.3f\n",a.i,a.f);
a=alter(a,222,666.006);
printf("%d %.3f",a.i,a.f);
}
10)

What will be the output of the following program :


typedef struct
{
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp=*ptr;
printf("%d %.2f\n",tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={65535,777.777};
a=alter(&a,-1,666.666);
printf("%d %.2f",a.i,a.f);
}

11)

What will be the output of the following program :


struct my_struct1
{
int arr[2][2];
};
typedef struct my_struct1 record;
struct my_struct2
55

{
record temp;
}list[2]={1,2,3,4,5,6,7,8};
void main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j<2; j++)
for (k=1; k>=0; k--)
printf("%d",list[i].temp.arr[j][k]);
}
12)

What will be the output of the following program :


struct my_struct
{
int i;
unsigned int j;
};
void main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf("%d %u",temp2.i,temp2.j);
}

13)

What will be the output of the following program :


struct names
{
char str[25];
struct names *next;
};
typedef struct names slist;
void main()
{
slist *list,*temp;
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,"Hai");
list->next=NULL;
temp=(slist *)malloc(sizeof(slist));
strcpy(temp->str,"Friends");
temp->next=list;
list=temp;
while (temp != NULL)
{
56

printf("%s",temp->str);
temp=temp->next;
}
}
14)

Which of the following declarations is NOT Valid :


(i) struct A{
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B{
int b;
struct B *next;
};
struct A{
int a;
struct B tempB;
struct A *next;
};
(iii)struct B{
int b;
}tempB;
struct {
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;

15)

What will be the output of the following program :


union A
57

{
char ch;
int i;
float f;
}tempA;
void main()
{
tempA.ch='A';
tempA.i=777;
tempA.f=12345.12345;
printf("%d",tempA.i);
}
16)

What will be the output of the following program :


struct A
{
int i;
float f;
union B
{
char ch;
int j;
}temp;
}temp1;
void main()
{
struct A temp2[5];
printf("%d %d",sizeof temp1,sizeof(temp2));
}

17)

What will be the output of the following program :


void main()
{
static struct my_struct
{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf("%d %d %d %d",v.a,v.b,v.c,v.d);
printf("\nSize=%d bytes",sizeof v);
}

18)

What are the largest values that can be assigned to each of the bit fields
defined in [Q017] .
58

(a)a=0 b=2 c=3 d=4


(c)a=1 b=3 c=7 d=15

(b)a=1 b=2 c=7 d=15


(d)None of thes

19)

What will be the output of the following program :


void main()
{
struct sample
{
unsigned a:1;
unsigned b:4;
}v={0,15};
unsigned *vptr=&v.b;
printf("%d %d",v.b,*vptr);
}

20)

What will be the output of the following program :


void main()
{
static struct my_struct
{
unsigned a:1;
int i;
unsigned b:4;
unsigned c:10;
}v={1,10000,15,555};
printf("%d %d %d %d",v.i,v.a,v.b,v.c);
printf("\nSize=%d bytes",sizeof v);
}

21)

What will be output of following c code?


void main()
{
struct employee
{
unsigned id: 8;
unsigned sex:1;
unsigned age:7;
};
struct employee emp1={203,1,23};
printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
}

22)

What will be output of following c code?


59

void main()
{
struct bitfield
{
unsigned a:5;
unsigned c:5;
unsigned b:6;
}bit;
char *p;
struct bitfield *ptr,bit1={1,3,3};
p=&bit1;
p++;
printf("%d",*p);
}
23)

What will be output of following c code?


void main()
{
struct bitfield
{
signed int a:3;
unsigned int b:13;
unsigned int c:1;
};
struct bitfield bit1={2,14,1};
printf("%d",sizeof(bit1));
}

24)

What will be output of following c code?


void main()
{
struct bitfield
{
unsigned a:3;
char b;
unsigned c:5;
int d;
}bit;
clrscr();
printf("%d",sizeof(bit));
}

25)

What will be output of following c code?


void main()
{
60

struct field
{
int a;
char b;
}bit;
struct field bit1={5,'A'};
char *p=&bit1;
*p=45;
printf("\n%d",bit1.a);
}
26)

What will be output of following c code?


void main()
{
struct india
{
char c;
float d;
};
struct world
{
int a[3];
char b;
struct india orissa;
};
struct world st ={{1,2,3},'P','q',1.4};
printf("%d\t%c\t%c\t%f",st.a[1],st.b,st.orissa.c,st.orissa.d);
}
ASSIGNMENT

Q.

Write a c program that compares two given dates to store the data use
structure that contains three members namely date, month and year. If
the dates are equal then display the message as equal otherwise unequal

Q.

There is a structure called employee that holds information like


employee code, name, date of joining

Q.

Write a c program to create an array of structures and enter some date


into it then ask to user to enter the current date .Display the names of
those employees whose tenure is 3 or more than 3 years according to the
given current date

Q.

Write a simple program to demonstrate the process of difining a


structure variable and assigning values to its members
61

Q.

Write a c program to display the size of structure variable

CHAPTER -5.2
CHARACTER STRINGS: Array Of Characters, Variable Length
Character Strings,Escape Characters, Character Strings, Structures And
Arrays,Character Operations
ONE WORD ANSWERS
Q1. What is a string?
Ans: Sstring is a collection of characters.
Q2) Which is the last character of a string ?
Ans: Null character is the last char of a string
Q3). Write the general form of the declaration of a simple string?
Ans: char string_name[size];
Q4)

Other than the scanf function,what is the other function that can be used
to read the string?
Ans: gets() is the other function that can be used to read the string.
Q5) What is the funtcion we use to write the string on the screen?
Ans: printf() function by using format code %s.
Q6) What is getchar()?
Ans: it reads a charactor from the keyboard.
Q7) What is meant by concatenation?
Ans: The process of combining one string with another is known as string
concatneation.
Q8) What is meant by strcmp?
Ans: The process of comparing the one string into another is known as
strcmp.
Q9) Which header file is used for string operations?
Ans: string.h .
Q10) What is meant by strncmp?
62

Ans: This is a three parametre function that compares only 'n' characters of
both the given strings.
the general format of the function is
strncmp(s1,s2,n); .

MULTIPLE CHOICE QUESTIONS


1.

Which of the following is used to represent the end of a string?


a. blank space
b. null character
c. newline character
d. last element of the string

2.

Which of the following is used to display a string on I/O console?


a. %s
b. %d
c. %c
d. %f

3.

Which of the following is true for getchar?


a. read a string of characters
b. read a character
c. read the characters until \n is encountered
d. none of the above

4.

What is the value of x in the following character arithmetic expression?


X='A'-2;
a. 63
b. 64
c. 65
d. 66

5.

Which function is used to determine the length of a string?


a. strcmp
b. strcpy
c. strlen
d. strcat

6.

What value will strlen functions return for the string {'r','a','m','\0'}
a. 3
b. 4
c. 5
d. None of the above

7.

Which of the following is the correct syntax for copying a string S1 into
S2?
63

a. strcmp(S2,S1);
b. strcpy(S1,S2);
c. strcmp(S1,S2);
d. strcpy(S2,S1);

8.

Which of the following should be used for printing using


statement"?
a.
"""
b.
"\
c
\"
d.
/"

OUTPUT PREDICTION
1.

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
char arr[7]="Network";
printf("%s",arr);
}

2.

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
char arr[11]="The African Queen";
printf("%s",arr);
}

3.

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
int const SIZE=5;
int expr;
double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
expr=1|2|3|4;
printf("%f",value[expr]);
}

4.What will be output when you will execute following c code?


64

printf

#include<stdio.h>
enum power
{ Dalai, Vladimir=3, Barack, Hillary};
void main()
{
float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
enum power p=Barack;
printf("%0.f",leader[p>>1+1]);
}
5)

What will be output when you will execute following c code?


#include<stdio.h>
#define var 3
void main()
{
char *cricket[var+~0]={"clarke","kallis"};
char *ptr=cricket[1+~0];
printf("%c",*++ptr);
}

6)

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
printf("%o",data[0][2][1]);
}

7)

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
short num[3][2]={3,6,9,12,15,18};
printf("%d %d",*(num+1)[1],**(num+2));
}

8)

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
char *ptr="cquestionbank";
printf("%d",-3[ptr]);
}
What will be output when you will execute following c code?

9)

65

#include<stdio.h>
void main()
{
long myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
printf("%ld\t",myarr[1][2]);
printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
printf("%ld%ld%ld\t",*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
}
10)

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
int array[2][3]={5,10,15,20,25,30};
int (*ptr)[2][3]=&array;
printf("%d\t",***ptr);
printf("%d\t",***(ptr+1));
printf("%d\t",**(*ptr+1));
printf("%d\t",*(*(*ptr+1)+2));
}

11.

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
static int a=2,b=4,c=8;
static int *arr1[2]={&a,&b};
static int *arr2[2]={&b,&c};
int* (*arr[2])[2]={&arr1,&arr2};
printf("%d %d\t",*(*arr[0])[1], *(*(**(arr+1)+1)));
}

12.What will be output when you will execute following c code?


#include<stdio.h>
#include<math.h>
double myfun(double);
void main()
{
double(*array[3])(double);
array[0]=exp;
array[1]=sqrt;
array[2]=myfun;
printf("%.1f\t",(*array)((*array[2])((**(array+1))(4))));
}
66

double myfun(double d)
{
d-=1;
return d;
}
13)

What will be output when you will execute following c code?


#include<stdio.h>
typedef struct
{
char *name;
double salary;
}job;
void main()
{
static job a={"TCS",15000.0};
static job b={"IBM",25000.0};
static job c={"Google",35000.0};
int x=5;
job * arr[3]={&a,&b,&c};
printf("%s %f\t",(3,x>>5-4)[*arr]);
}
double myfun(double d)
{
d-=1;
return d;
}

14)What will be output when you will execute following c code?


#include<stdio.h>
union group
{
char xarr[2][2];
char yarr[4];
};
void main()
{
union group x={'A','B','C','D'};
printf("%c",x.xarr[x.yarr[2]-67][x.yarr[3]-67]);
}
15.What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
int a=5,b=10,c=15;
67

int *arr[3]={&a,&b,&c};
printf("%d",*arr[*arr[1]-8]);
}
16)

What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
int arr[][3]={{1,2},{3,4,5},{5}};
printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}

17)What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
int xxx[10]={5};
printf("%d %d",xxx[1],xxx[9]);
}
18)

What will be output when you will execute following c code?


#include<stdio.h>
#define WWW -1
enum {cat,rat};
void main()
{
int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW};
int i;
for(i=0;i<8;i++)
printf(" %d",Dhoni[i]);
}

19)What will be output when you will execute following c code?


#include<stdio.h>
void main()
{
long double a;
signed char b;
int arr[sizeof(!a+b)];
printf("%d",sizeof(arr));
}
20)What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
68

char array[]="Ashfaq \0 Kayani";


char *str="Ashfaq \0 Kayani";
printf("%s %c\n",array,array[2]);
printf("%s %c\n",str,str[2]);
printf("%d %d\n",sizeof(array),sizeof(str));
}
ASSIGNMENT
Q.

Write a c program to sort a set of names stored iin an array in


alphabetical order

Q.

Write a c program to that will read a line and delete from it all the
occurences of the word "the"

Q.

Write a c program to illustrate basic string operations (string handling


functions)

Q.

Write a c program to demonstrate the conversion of string to its


equivalent integer and floation point value

Q.

Write a c program that can check whether a string is a palidrome or not

69

CHAPTER- 5.3
POINTERS:Defining A Pointer Variable,Using Pointers In
Expressions,Pointers And Structures,Pointers And Functions,Pointers And
Arrays,Operation On Pointers,Pointer And Memory Address.

ONE WORD ANSWERS


Q1). What is a pointer?
Ans: A pointer is a variable which stores the address of other variable.
Q2) What are pointer constants?
Ans: Memory address within a computer memory are reffered to as pointer
constants.we cannot change them.we can use them only to store values.
Q3) How the address of the varible can be accessed?
Ans: by using '&' operator.
Q4) How the pointer varible can be declared?
Ans: By using the syntax given below
data type *pt_name
where data type is - what type of data the varible is going to assigned
pt - means pointer
Q5. What is meant by initialization of pointers?
Ans: The process of assigning the address of variable to a pointer is known as
initialization of pointer.
Chain of pointers:
The process of linking the pointer varible to another pointer varible is
known as chain of pointers.this can be shown as **ptr.
Q6) What are pointer expressions?
Ans: The experssion is a squence of operators and operands.if the operands
used here are pointers then the expression becomes pointer expressions.
Q7)

Can the pointer increments are possible?


70

Ans: Yes,it is possible.


Q8) What is scale factor?
Ans: When the pointer is incremented,its value is increased by the 'size of the
data type'.this is known as scale factor.
Q9)

What the compiler does when the array name has been assigned to
pointer variable?
Ans: The compiler allocate the address of the first member of an array to the
pointer.
Q10) What is meant by call by value?
Ans: The process of passing th actual address of varibles to the function
is known as call by value.
Q11) Which operator is used to access the structure member when we use
pointer in structures?
Ans: Arrow operator(->) or member selection operator.
Q12) What are the disadvantages of the pointer?
Ans: a.Debugging becomes difficult task.
b.Direct access of memory loose data security
Q13) Which arithmetic operations are allowed in pointers?
Ans: Addition and Subtraction.
Q14) What is a near pointer?
Ans: A pointer which can store an address value between 0 to 65,535.
near pointer requires 2 bytes of memory.
Q15) What is a far pointer?
Ans: A pointer which can store an address value between 0 to 232-1.
far pointer requires four bytes of memory.
Q16) What is a generic pointer?
Ans: A pointer which can store any type of address, that pointer is known as
generic pointer.
Q17) How to represent a generic pointer ?
Ans: void * is used to represent generic pointer
Q18) what is wild pointer?
Ans: A pointer which is not initialized with any addresss is known as wild
pointer
Q19) What is single pointer?
71

Ans: A single pointer stores address of a variable.


example: int *ptr,val;
val=54;
ptr=&val;

Q20) What is double pointer?


Ans: A pointer which stores address of single pointer
Example: int **p2,*p1,x;
p1=&x;
p2=&p1;
Multiple Choice Questions
1.

Pointer is an example of which of the following type?


a. derived type
b. fundamental type
c. User-defined type
d. none of the above

2.

An integer pointer:
a. Points to the address of another integer value
b. Points to any data type
c. Points to itself
d. None of the above

3.

In the expression *ptr=&a,what does & signify


a. address of a
b. address of ptr
c. value of a
d. none of the above

4.

Which of the following expressions in c is used for accessing the


address Of a variable var?
a. &(*var)
b. *var
c. &var
d. *(&var)

5.

Which of the following is a syntactically correct pointer declaration?


a. float* X
b. float *x
c. float * X
d. all are correct
72

6.

Which of the following expressions will give the value stored in variable
X?
a. X
b. *x
C. *&X
d. &X

7.

If(int a1,*a2) a1=2 and a2=&a1 then what does a2++ depict(consider the
address value of a1 to be 3802,a1&a2 are integer pointers)
a. 3
b. 3803
c. 3804
d. 3802

8.

If a1=2 and a2=&a1 then what does a2++ depict(consider the address
value of a1 to be 3802,a1&a2 are char pointers)
a. 3
b. 3803
c. 3804
d. 3802

9.

If a1=2 and a2=&a1 then what does a2++ depict(consider the address
value of a1 to be 3802,a1&a2 are float pointers)
a. 3
b. 3803
c. 3804
d. 3806

10.

If a1=&x and a2=&a1,what will be the output generated by the


expression **a2?
a. address of a2
b. address of a1
c. value of x
d. address of x

11.

How to get address of ith element of an array A?


a. A[i]
b. &A[i]
c. *A[i]
d. A[&i]

12.

What is the size of an integer & float pointer?


a. 2 & 4
b. 4 & 4
c. 2 & 2
73

d. 1 & 1
13)

What is meaning of following declaration?


int(*ptr[5])();
a. ptr is pointer to function.
b. ptr is array of pointer to function.
c. ptr is pointer to such function which return type is array.
d. ptr is pointer to array of function.
e. None of these

14)

What is meaning of following pointer declaration?


int(*(*ptr1)())[2];
a. ptr is pointer to function.
b. ptr is array of pointer to function.
c. ptr is pointer to such function which return type is pointer to an array
d. ptr is pointer array of function.
e. None of these

15)

What will be the output of the following program :


void main()
{
int val=50;
const int *ptr1=&val;
int const *ptr2=&ptr1;
printf("%d %d %d",++val,*ptr1,*ptr2);
*(int *)ptr1=98;
printf("\n%d %d %d",++val,*ptr1,*ptr2);
}
a. Compile-Time Error
b.
51 50 50
99 98 98
c. Run-Time Error
d.
None of these

16)

What will be the output of the following program :


void main()
{
int (*a)[5];
printf("%d %d",sizeof(*a),sizeof(a));
}
a. Compile-Time Error
b. 2 5
c. 5 2

17)

What will be output of following program?


#include<stdio.h>
void main()
74

d. None of these

{
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
getch();
a. 25
18)

19)

}
b. Address c. Garbage d. Compilation error e. None of above
What will be the output of the following program :
void main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",++val,*ptr);
}
a.1235 1235
b.1235 1234 c. 1235 1236 d. 1235 1237
What will be the output of the following program :
void main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",val,*ptr++);
}
a.1235 1235
b.1235 1234 c. 1234 1234 d. 1235 1237

20)

What will be the output of the following program :


void main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",val,++*ptr);
}
a.1239 1239
b.1235 1234 c. 1235 1236 d. 1235 1235

21)

What will be the output of the following program :


void main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",val,(*ptr)++);
}
a.1235 1235
b.1235 1236 c. 1235 1234 d. 1235 1237
75

22)

What will be the output of the following program :


void main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",++val,(*(int *)ptr)--);
}
a.1234 1234
b.1235 1234 c. 1235 1236 d. 1235 1237

23)

What will be the output of the following program :


void main()
{
int a=555,*ptr=&a,b=*ptr;
printf("%d %d %d",++a,--b,*ptr++);
}
a.556 555 554
b. 556 555 554
c. 556 554 555
d. 556 555 554
What will be the output of the following program :
void main()
{
int a=555,b=*ptr,*ptr=&a;
printf("%d %d %d",++a,--b,*ptr++);
}
a.compile time error
b.456 564
c. 555 554
d. 555 555

24)

25)

What will be the output of the following program :


void main()
{
int a=555,*ptr=&a,b=*ptr;
printf("%d %d %d",a,--*&b,*ptr++);
}
a.56 564
b.56 68 666
c.555 554 555
d.56 57 578

26)

What will be the output of the following program :


void main()
{
int a=555,*ptr=&a,b=*ptr=777;
printf("%d %d",--*&b,*(int *)&b);
}
a.46 6478
b.776 777
c.8 878

27)

What will be the output of the following program :


void main()
76

d.777 777

{
int a=5u,*b,**c,***d,****e;
b=&a;
c=&b;
d=&c;
e=&d;
printf("%u %u %u %u",*b-5,**c-11,***d-6,65535+****e);
}
a. 0 65530 65535 5
c.0 65530 65535 4
28)

29)

30)

31)

b. 0 65530 65534 4
d. 0 65530 65502 5

What will be the output of the following program :


void main()
{
float val=5.75;
int *ptr=&val;
printf("%.2f %.2f",*(float *)ptr,val);
}
a.508 509
b.5.6 5.6
c.5.8 5.9
What will be the output of the following program :
void main()
{
int val=77;
const int *ptr1=&val;
int const *ptr2=ptr1;
printf("%d %d %d",--val,(*ptr1)++,*ptr2);
}
a.58
b.96
c.compile time error
What will be the output of the following program :
int main()
{
int a=50,b=60;
int* const ptr1=&a;
printf("%d %d",--a,(*ptr1)++);
ptr1=&b;
printf("\n%d %d",++b,(*ptr1)++);
}
a.58
b. compile time error
c.586
What will be the output of the following program :
void main()
{
int a=50;
const int* const ptr=&a;
77

d.5.75 5.75

d.69

d.69

printf("%d %d",*ptr++,(*ptr)++);
}
a.50
32)

33)

34)

b.96

c.compile time error

d.69

What will be the output of the following program :


void main()
{
int val=77;
const int const *ptr=&val;
printf("%d",*ptr);
}
a.78
b.56
c.77
d.79
What will be the output of the following program :
void main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;
printf("%d %d",--*ptr+1,1+*--ptr);
}
a.25
b.56
c.89
d.23
What will be the output of the following program :
void main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;
printf("%d %d",*++a,--*ptr);
}
a.12
b.32
c. compile time error

d.56

35)

What will be the output of the following program :


void main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;
printf("%d %d",*(1+a),--*ptr);
}
a.36
b.6 6
c.3 3
d.2 2

36)

What will be the output of the following program :


void main()
{
int matrix[2][3]={{1,2,3},{4,5,6}};
printf("%d %d %d\n",*(*(matrix)),*(*(matrix+1)+2),*(*matrix+1));
printf("%d %d %d",*(matrix[0]+2),*(matrix[1]+1),*(*(matrix+1)));
78

}
a.162
354

b.163
325

c.158
356

d.569
589

OUTPUT PREDICTION
1)

What will be output of following program?


#include<stdio.h>
void main()
{
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
getch();
}

2)

What will be output of following program?


#include<stdio.h>
#include<conio.h>
void main()
{
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)("gayathri degree college");
(*q)();
}

3)

What will be output of following program?


#include<stdio.h>
void main()
{
int i = 3;
int *j;
int **k;
j=&i;
79

k=&j;
printf(%u %u %d ,k,*k,**k);
}
4)

What will be output of following program?


#include<stdio.h>
#include<string.h>
void main()
{
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
getch();
}

5)

What will be output of following program?


#include<stdio.h>
#include<string.h>
void main()
{
register int a = 25;
int far *p;
p=&a;
printf("%d ",*p);
getch();
}

6)

What will be output of following program?


#include<stdio.h>
#include<string.h>
void main()
{
int register a;
scanf("%d",&a);
printf("%d",a);
getch();
}
//if a=25
7)

What will be output of following program?


#include<stdio.h>
80

void main()
{
char arr[10];
arr = "world";
printf("%s",arr);
getch();
}
8)

What will be output if you will compile and execute the following c
code?
#include<stdio.h>
int main()
{
int a=5,b=10,c=15;
int *arr[]={&a,&b,&c};
printf("%d",*arr[1]);
return 0;
}

9)

What will be output of following program?


#include<stdio.h>
#include<string.h>
void main()
{
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
getch();
}

10)

What will be output of following program?


#include<stdio.h>
unsigned long int (* avg())[3]
{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
}

81

11)

What will be output of following program?


#include<stdio.h>
void main()
{
int * p , b;
b = sizeof(p);
printf(%d , b);
}

12)

What will be output of following program?


#include<stdio.h>
void main()
{
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("value of i : %d value of j : %d",*p,*q);
}

13)

What will be output of following program?


#include<stdio.h>
void main()
{
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
}

14)

What will be output of following program?


#include<stdio.h>
void main()
{
int i = 100;/* note address of i 3000*/
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
}

15)

What will be output of following program?


#include<stdio.h>
void main()
{
82

int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf(%u %u %u,i,j,k);
}
16)

What will be output if you will compile and execute the following c
code?
#include<stdio.h>
int main()
{
static char *s[3]={"math","phy","che"};
typedef char *( *ppp)[3];
static ppp p1=&s,p2=&s,p3=&s;
char * (*(*array[3]))[3]={&p1,&p2,&p3};
char * (*(*(*ptr)[3]))[3]=&array;
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[2]);
return 0;
}

ASSIGNMENT
Q.

Write a c program to illustrate the use of indirectional operator "*" to


access the value pointed to by a pointer

Q.

Write a c program to illustrate the use of pointers in arithmatic


operations

Q.

Write a c program using pointers to compare sum of all the elements


stored in an array

Q.

Write a c program using pointers to determine the length of character


string

Q.

Write a c program to using pointers to exchange the values stored in two


locations in the memory

Q.

Write a c program that uses function pointers as function argument

Q.

Write a c program to illustrate the use of structure pointers


83

Q.

Write a c program that a function (using pointer parameters) that


reverses the elements of given array

CHAPTER 5.4
OPERATIONS ON BITS: BIT OPERATORS,BIT FIELD
Q1) What are bit-wise operators ex?
Ans: Bit wise AND(&),bit wise OR(|),bit wise exclusive OR(^),shift
left(<<),shift right(>>) and ones complement ( ~) are called bit
operators.
Q2)

What is bit field ?

Ans : A bit field is a common idiom used in computer programming to compactly


store a value as a short series of bits. A bit field is most commonly used to
represent integral types of known, fixed bit-width. Perhaps the most well
known usage of bit-fields is to represent single bit flags, with each flag stored
in a separate bit.
A bit field is distinguished from a bit array in that the latter is used to store a
large set of bits indexed by integers and is often wider than any integral type
supported by the language. Bit fields, on the other hand, typically fit within a
machine word, and the denotation of bits is independent of their numerical
index.

84

CHAPTER -5.5
THE PREPROCESSOR: The #Define Statement, The ##Operator & #
Operator, The #Include Statement, Condition Compilation
Q1) What is #define directive?
Ans: A #define is a preprocessor compiler directive and not a statement.
Q2) What is the use of #define statement?
Ans: The use of #define statement is to assign symbolic names to program
constants.
Q3) Give some examples as how to use #define statement.
Ans: #define YES 1
#define NO 0
Q4) Would #define lines end with a semicolon(;)?
Ans: NO,#define lines donot end with a semicolon(;)
Q5)

What is a ## operator ?

Ans:

It is a token pasting operator, (Token merge, creates a single token from two
adjacent ones)

Q6) Where do we use ## operator?


Ans: This operator is used in macro definitions to join two tokens together
Q7) What is # operator
Ans: It is one type Stringization operatorand replaces a macro parameter with
a string constant.
Q8)

what is the output of following code.?


#include <stdio.h>
#define message_for(a, b)
printf(#a " and " #b ": We love you.\n")
int main(void)
{
message_for(Carole, Debra);
return 0;
}

85

Output : Carole and Debra : We love you.


Q9)

What is the output of following code.?


#define tokenpaster(n)
printf ("token" #n " = %d", token##n)
void main()
{
int token2=45;
tokenpaster(2);
}
Output : token2=45
Q10) What is header file ?
Ans: The file which stores Library functions is known as 'header file'
Q11) How to acess functions stored in the library?
Ans: Functions in the library can be accessed using the #include directive.
Q12) What is the function of #ifdef?
Ans: It is to test the macro definition.
Q13) What is the function of #endif?
Ans: It specifies the end of #if
Q14) What is the function of #if?
Ans: IT specifies alternatives when #if test fails.
Q15) What is the function of #ifndef?
Ans: It tests whether a macro is defined or not

86

CHAPTER-5.6.
MORE ON DATA TYPES: Enumerated Data Types, The Typedef
Statement, Data Type Conversions
Q1) What is enum?
Ans: It is a special keyword which allows to define symbolic constants.
Q2) How is enum defined?
Ans: It
is
defined
as
follows
identifier{value1,value2,..................valuen};

enum

Q3) Give examples for enumerated datatypes.


Ans: enum day {monday,tuesday..............sunday};
enum day week_st,week_end;
Q4) What is the starting value of enumerated set?
Ans: zero
Q5) What is typedef?
Ans: It is a keyword , used to redfine an existing data type.
Q6) Give some examples how to use typedef?
Ans: typedef int units;
typedef float marks;
they can later be used to declare the variables as follows
units batch1,batch2;
marks name1[50],name2[50];
Q7) Illustrate the use of data type conversion
Ans: average=(float) total/n;
the value of the variable total; is converted to type float before the
operation is
performed,thereby guarantee that the division will be carried out as a
floating point operation

87

CHAPTER 5.7
INPUT AND OUTPUT OPERATIONS in c: Character I/O, Formatted
I/O, Input And Output Operations With Files, Special Functions For
Working With Files
Q1) What are character I/O functions?
Ans: putchar and getchar are character I/O functions
Q2) When do we use getchar?
Ans: getchar function is used when we want to read a single character at a
time.
Q3) What is the use of putchar?
Ans: It is used to display single character data
Q4) What are formatted I/O functions?
Ans: scanf and printf are formatted I/O functions
Q5) What is a file ?
Ans: File is collection of records ? It is stored in secondary storage area.
Q6) How many types files are exist in C ?what are they?
Ans: Two types ,they are: i) text file ii) binary file
Q7) What does a text file stores?
Ans: A text file stores different characters such as:
upper case,lower case english alphabets,numeric characters,
punctuation characters,special characters.
Q8) How to create a text file?
Ans: Text files can be created by using text editor ( like tc and notepad.)
Q9) How to declare and open a file for writting?
Ans: FILE *fp;
fp=fopen("file name","w");

88

Q10) How to declare and open a file for reading?


Ans: FILE *fp;
fp=fopen("file name","r");
Q11) How to declare and open a file for appending?
Ans: FILE *fp;
fp=fopen("file name","a");
Q12) How to close a file?
Ans: fclose(file_pointer);
Q13) What are the simplest file i/o functions?
Ans: getc,putc,fprintf,fscanf
Q14) What is the general form of fgetc?
Ans: fgetc(fp);
reads a character from the file
whose file pointer is fp.
Q15) What is the general form of putc?
Ans: putc(c,fp);writes the character contained in the character variable c
to the file associated with FILE pointer fp.
Q16) What are the simplest file i/o integer functions?
Ans: getw,putw.
Q17) What is the general form of fprintf?
Ans: fpintf(fp,"control string",list);
example:fprintf(fp,"%s %d %f ",name,age,7.5);
Q18) What is the general form of fscanf?
Ans: fscanf(fp ,"control string",list);
example: fscanf(fp,"%s %d",item,&quantity);
Q19) How many types of status enquiry library functions are there?what are
they?
Ans: Two.They are
1.feof
2.ferror
Q20) What is the use of status enquiry library functions?
ans: they help to detect i/o errors with in the files.
Q21) What is the use of ftell? write the general form of ftell?
Ans: ftell function is useful in saving the current position of file .
it takes the general form:
n=ftell(fp)
89

it returns a number of type long.


Q22) what is the rewind function?
Ans: rewind takes a file pointer and resets the position to the start of the file.
general form:rewind(fp);
Q23) Where is fseek function used?
Ans: fseek function is used to move the file position to a desired
location with in the file.
Q24) What is the general form of fseek function?
Ans: fseek(file_ptr,offset,position);
offset specifies number of positions to be moved,position is 0(meaning
begining of file),1(current position),2(end of file).
Q25) What is the meaning of the following statement?
fseek(fp,m,1)
Ans: go forward by m bytes from current file pointer position.
Q26) How many standard modes exist to open a file ?
Ans : Three .
Q27) What are the standard modes exist to open a file ?
R reading W-writing A-appending

90

CHAPTER 5.8
MISCELLANIOUS AND ADVANCED FUNCTIONS:The goto statement,
working with unions, the comma operator:
Q1) What is the use of goto statement?
Ans: goto is one type of unconditional jumping statements.
goto statement is used to branch unconditionally from one point to
another point in the specified block of a program
Q2) What is union?
Ans: A union, is a collection of variables of different types, just like a structure.
However, with unions, you can only store information in one field at any one
time

Q3) What is the size of the memory allocated by union?


Ans: The compiler allocates a piece of storage that is large enough to hold the
largest variable type in the union.
Q4) What is the use of comma operator?
Ans: The comma operator is used to separate the items.

91

CHAPTER 5.4 TO 5.8


MULTIPLE CHOICE QUESTIONS
1)

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
}
void main()
{
int var=7,sum;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a.5 6
b.6 11
c.11 6
d. 11 7

2)

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
return *ptr;
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a. 11 7
b. 7 11
c. 5 6
d. 6 5

3)

What will be the output of the following program :


int func(int *ptr,int a,int b)
{

92

*ptr=25;
return *ptr;
return (a+b);
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a. 25 40
b.25 25
c.40 25

d.35 35

4)

What will be the output of the following program :


void main()
{
int a=(5,a=50)/a++;
printf("%d",a);
}
a. 2
b. 4
c. 1
d. 0

5)

What will be the output of the following program :


void main()
{
int a=(5,a=50)/++a;
printf("%d",a);
}
a.0
b.3
c.6
d.8

6)

What will be the output of the following program :


void main()
{
int a=7;
if ((++a < 7) && (++a < 9) && (++a < 25)
;
printf("%d",a);
}
a.10
b.9
c.8
d.11

7)

What will be the output of the following program :


void main()
{
int a=7;
if ((++a < 7) && (a++ < 9) && (++a < 10);
printf("%d",a);
}
93

a.20

b.10

c.30

d.39

8)

What will be the output of the following program :


main()
{
for ( ; ; )
main();
}
a.compile time error
b.syntax error
c.infinite loop
d.none of these

9)

What will be the output of the following program :


void main()
{
int a=0,b=1,c;
c = a=0 ? (a=1) : (b=2);
printf("%d %d %d",a,b,c);
}
a.0 0 0
b. 2 2 2
c.1 1 1
d.4 4 4

10)

What will be the output of the following program :


void main()
{
int a=5,b=1,c;
c = a ? a=1 : b=2;
printf("%d %d %d",a,b,c);
}
a.infinite loop
b.syntax error
c. Compile-Time Error
d. none of these
What will be the output of the following program :
main()
{
unsigned _=5;
_=_--- ++_;
printf("%d",_);
}
a.-1
b.65535
c.35689
d.56989

11)

12)

What will be the output of the following program :


main()
{
unsigned _=5;
_=_--- ++_;
printf("%u",_);
}
94

a.-1

b.65535

c.35689

d.56989

OUTPUT PREDICTION:
1)

What will be the output of the following program :


int fun(int a,int b)
{
#define NUM 5
return a+b;
}
main()
{
printf("Sum=%d",fun(NUM,NUM));
}

2)

What will be the output of the following program :


void main()
{
int a=5,b=6,c=7;
printf("%d %d",a,b,c);
}

3)

What will be the output of the following program :


int add(int a,int b)
{
return a+b;
}
main()
{
int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
}

4)

What will be the output of the following program :


void main()
{
int a=5,b=6,c=7,d=8,e;
95

e = a ? b , c : c , d;
printf("%d",e);
}

5)

What will be the output of the following program :


void main()
{
int a=5,b=6,c;
c++ = b % (b - a)
printf("%d",c);
}

6)

What will be the output of the following program :


void main()
{
int x=5,y=6,z;
z = x++ ;+++y;
printf("%d %d %d",x,y,z);
}

ASSIGNMENT
Q.

Write a c program to illustrate the bit iperations

Q.

Write a c program to illustrate conditional compilation

Q.

Write a c program to illustrate enum,typedef statement and data type


conversions

Q.

Write a c program to illustrate different formats

Q.

Write a c program to illustrate goto,null statements

Q.

Write a c program to illustrate type qualifiers

Q.

Write a c program to illustrate basic operations of file

96

PROGRAMMING PROBLEMS- 1
1)

The following code is not well-written. What does the program do ?


void
main( )
{
int a=1,b=2,c=3,d=4;
printf("%d %d",a,b);printf( " %d %d",c,d);
}

a. Run-Time Error b.Compile-Time Error

c. 1 2 3 4

d. None of these

2)

What will be the output of the following program :


void main()
{
int a=1,b=2,c=3;
c=(--a,b++)-c;
printf("%d %d %d",a,b,c);
}
a. 0 3 -3
b.Compile-Time Error
c. 0 3 -1
What will be the output of the following program :
void main()
{
int a=1,b=2,c=3,d=4,e;
e=(a,a)+(b,c)+(c,d)-(d,b);
printf("%d",e);
}
a. Compile-Time Error
b. 10
c. 6

d. 0 3 0

3)

What will be the output of the following program :


void main()
{
float val=2.;
printf("%.2",val);
}
a. Compile-Time error
b. 2.00
c. %.2

d. 2

4)

5)

What will be the output of the following program :


void main()
97

d. 2.000000

{
int a=5;
int b=6;;
int c=a+b;;;
printf("%d",c);;;;
}
a. Compile-Time Error
6)

b. Run-Time Error c. 11

d. None of these

What will be the output of the following program :


void main()
{
int i,j;
for (i=1; i<=3; i++)
for (j=1; j<3; j++)
{
if (i == j)
continue;
if ((j % 3) > 1)
break;
printf("%d",i);
}
}

7)

What will be the output of the following program :


#define swap(a,b) temp=a; a=b; b=temp;
void main()
{
static int a=5,b=6,temp;
if (a > b)
swap(a,b);
printf("a=%d b=%d",a,b);
}
a. a=5 b=6
b. a=6 b=5
c. a=6 b=0
What will be the output of the following program :
void main()
{
unsigned int val=5;
printf("%u %u",val,val-11);
}
a. Compile-Time error
b. 5 -6
c. 5 65530

d. None of these

8)

9)

What will be the output of the following program :


void main()
{
int x=4,y=3,z=2;
98

d. None of these

*&z*=*&x**&y;
printf("%d",z);
}
a. Compile-Time error

b. Run-Time Error

c. 24 d. Unpredictable

10)

What will be the output of the following program :


void main()
{
int i=5,j=5;
i=i++*i++*i++*i++;
printf("i=%d ",i);
j=++j*++j*++j*++j;
printf("j=%d",j);
}
a. Compile-Time Error
b. i=1680 j=1680
c. i=629 j=6561
d.i=1681 j=3024

11)

What will be the output of the following program :


void main()
{
int i=5;
printf("%d %d %d %d %d",++i,i++,i++,i++,++i);
}
a.Compile-Time Error
b.10 9 8 7 6
c. 9 8 7 6 6
d. 10 8 7 6 6
12)

What will be the output of the following program :


void main()
{
unsigned ch='Y';
printf("%d",sizeof ch);
}
a. Compile-Time Error
b. 2
c. 4
What will be the output of the following program :
void main()
{
int a=5;
printf("%d");
}
a. Compile-Time Error
b. 5
c. Unpredictable

d. 1

13)

14)

What will be the output of the following program :


void main()
{
99

d. No Output

int a=5,b=6;
printf("%d");
}
a. Compile-Time Error

b. 5

c. 6

What will be the output of the following program :


void main()
{
int a=5,b=6,c=7;
printf("%d %d %d");
}
a. Compile-Time Error
b. 5 6 7
c. 7 6 5

d. Unpredictable

15)

d. Unpredictable

16)

What will be the output of the following program :


void main()
{
int val=50;
void *ptr;
ptr=&val;
printf("%d %d",val,*(int *)ptr);
}
a. Compile-Time Error
b. 50 50
c. 50 0
d. None of these
17)

What will be the output of the following program :


void main()
{
int val=5;
void *ptr;
*(int *)ptr=5;
val=ptr;
printf("%d %d",*(int *)val,*(int *)ptr);
}
a. Compile-Time Error
b. Unpredictable
c. 5 5
d. None of these
18)

What will be the output of the following program :


void main()
{
int val=2;
val = - --val- val--- --val;
printf("%d",val);
}
a. Compile-Time Error
b. 3
c. -1
19) What will be the output of the following program :
void main()
100

d. 0

{
int i,n=10;
for (i=1; i<n--; i+=2)
printf("%d\n",n-i);
}
a. 84

b. 840
c. 852
PROGRAMMING PROBLEMS-2

d. 864

MULTIPLE CHOICE QUESTIONS:


1)

What will be the output of the following program :


#define func(a,b) b-##--a
void main()
{
int a=5,b=3,c;
c=func(a,b);
printf("%d %d %d",a,b,c);
}

a. Compile-Time Error
2)

b. 5 3 2 (c)4 3 -1

d. 5 2 -2

What will be the output of the following program :


void main()
{
#define b 100
int a=5,c;
c=a+b;
printf("%d %d %d",a,b,c);
}

a. Compile-Time Error
c. 5 100 105

b. Run-Time Error
d. None of these

3)

What will be the output of the following program :


void main()
{
int a=25.0;
#include<math.h>
printf("%.2lf %.2lf",sqrt(a),pow(sqrt(a)*5.0,2.0));
}
a. Compile-Time Error
b. 5 625
c. 5.00 125.00
d. 5.00 625.00
4)

What will be the output of the following program :


void main()
int c;
{
101

int a=5,b=10;
c=a+=b-=a;
printf("%d %d %d",a,b,c);
}
a. Compile-Time error

b. 5 10 5

c. 10 5 10

What will be the output of the following program :


#define func(x,y) { func(x,y) }
void main()
{
int a=5,b=6;
c=func(x,y);
printf("%d %d %d",c);
}
a. Compile-Time Error
b. Linker Error
c. 5 6 11

d. 10 10 5

5)

What will be the output of the following program :


void main()
{
const val=57;
printf("%d ",val);
*(int *)&val=75;
printf("%d",val);
}
a. Compile-Time Error
b. 75 57
c. 57 75

d. Infinite Loop

6)

7)

What will be the output of the following program :


#define print(msg) printf("Message:" #msg)
void main()
{
print("Hi Friends");
}
a. Compile-Time Error
b. Message:Hi Friends
c. Message:"Hi Friends"
d. "Message:Hi Friends"
8)

What will be the output of the following program :


#define print(val) printf("x" #val "=%.2f ",x##val)
void main()
{
float x1=5.74,x2=23.78;
print(1);
102

d. None of these

print(2);
}
a. Compile-Time error
c. Linker Error

b. x1=5.74 x2=23.78
d. None of these

9)

What will be the output of the following program :


#define print(msg) printf(#msg)
void main()
{
print(gayathri college);
}
a. Compile-Time error
b. gayathri college
c. syntax error
d. No Output
10)

What will be the output of the following program :


#define int float
void main()
{
int a=5.75,b=9.63,c;
c=a+b;
printf("%.2f %.2f %.2f",a,b,c);
}

a. Compile-Time Error
c. Linker Error

b. 5.75 9.63 15.38


d. None of these

11)

What will be the output of the following program :


void main()
{
int i=5;
float j=56.78;
char str[20];
scanf("%s %*d %f",str,&i,&j);
printf("%s %d %.2f",str,i,j);
}
[Assume the INPUT values entered by the user are :testing 12345 2.25]
a. Compile-Time Error
b. testing 12345 2.25
c. testing 5 2.25
d. testing 12345 56.78
12)

What will be the output of the following program :


main()
{
static int val=7;
int data;
if (--val)
{
103

data=main()+val;
printf("%d ",data);
}
return 0;
}
a. Compile-Time Error

b. INFINITE LOOP c. 1 2 3 4 5 6

d. 0 0 0 0 0 0

13)

What will be the output of the following program :


#define val 100
void main()
{
printf("%d ",val);
#define val 1000
printf("%d",val*10);
}
a. Compile-Time Error
b. 100 1000 c. 100 10000 d. None of these
14)

What will be the output of the following program :


void main()
{
int a[5];
printf("%d",9-*a-3+*a);
}
a. Compile-Time Error
b. 6 c. Garbage Value d. None of these
15)

What will be the output of the following program :


void main()
{
static int i;
while (i <= 10)
(i > 2) ? i++ : i--;
printf("%d",i);
}
a. Compile-Time Error
b. 11
c. 32767
d. None of these
16)

What will be the output of the following program :


void main()
{
int a=5,b=6,c;
c=(b++ == 6) || (--a < 2);
printf("%d %d %d",a,b,c);
}
a. Compile-Time Error
b. 4 7 1
c. 4 7 6

104

d. 5 7 1

17)

What will be the output of the following program :


void main()
{
int *ptr;
ptr=0x1fa;
*ptr++=5000;
*ptr=6000;
printf("%d ",++*ptr--);
printf("%d",(*ptr)++);
}
a. Compile-Time Error
b. 6000 6000 c. 6001 5000
What will be the output of the following program :
void main()
{
char *str="ABCDEFGHI";
printf("%c %c %c ",str[7]+1,3[++str],++*(str+5));
printf("%c %c %c",++*str,*++str,++*str++);
}
a. Compile-Time Error
b. J D G E D B c. J E G E D C

d. 6001 5001

18)

105

d. J E G D C B

PROGRAMMING PROBLEMS 3

MULTIPLE CHOICE QUESTIONS:


1)

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
}
void main()
{
int var=7,sum;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}

a. Compile-Time Error
d. None of these
2)

c. 11 7

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
return *ptr;
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}

a. Compile-Time Error
3)

b. 11 25

b. 11 25

c. 11 7

What will be the output of the following program :


int func(int *ptr,int a,int b)
{
*ptr=25;
106

d. 25 7

return *ptr;
return (a+b);
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a. Compile-Time Error
4)

a. 51

c. 11 7

d. 25 25

What will be the output of the following program :


#include <stdio.h>
#define ToStr(s) #s
#define Swap(x,y) y##x
void Swap(in,ma)()
{
if (printf(ToStr("Friends"))){}
}

a. Compile-Time Error
d. None of these
5)

b. 11 25

b. "Friends"

c. Friends

What will be the output of the following program :


void main()
{
int a=(5,a=50)/a++;
printf("%d",a);
}
b. 0

c. 6

What will be the output of the following program :


void main()
{
int a=(5,a=50)/++a;
printf("%d",a);
}
a. 51
b. 0
c. 6

d. 1

6)

7)

What will be the output of the following program :


void main()
{
int a=7;
if ((++a < 7) && (++a < 9) && (++a < 25))
107

d. 1

printf("%d",a);
}
a. 7
8)

a. 7

b. 8

c. 9

d. 10

What will be the output of the following program :


void main()
{
int a=7;
if ((++a < 7) && (a++ < 9) && (++a < 10);
printf("%d",a);
}
b. 8

c. 9

d. 10

9)

What will be the output of the following program :


main()
{
for ( ; ; )
main();
}
a. Compile-Time error
b. Run-Time Error
d. None of these
10)

What will be the output of the following program :


void main()
{
int a=0,b=1,c;
c = a=0 ? (a=1) : (b=2);
printf("%d %d %d",a,b,c);
}

a. Compile-Time Error
d. None of these
11)

b. 2 2 2

c. 1 1 1

What will be the output of the following program :


void main()
{
int a=5,b=1,c;
c = a ? a=1 : b=2;
printf("%d %d %d",a,b,c);
}

a. Compile-Time Error
12)

c. Infinite Loop

b. 2 2 2

c. 1 1 1

d. None of these

What will be the output of the following program :


main()
108

{
unsigned _=5;
_=_--- ++_;
printf("%d",_);
}
a. Compile-Time Error

b. 5

c. 65535

What will be the output of the following program :


main()
{
unsigned _=5;
_=_--- ++_;
printf("%u",_);
}
a. Compile-Time Error
b. 5
c. 65535

d. -1

13)

14)

d. -1

What will be the output of the following program :


int fun(int a,int b)
{
#define NUM 5
return a+b;
}
main()
{
printf("Sum=%d",fun(NUM,NUM));
}

a. Compile-Time Error

b. Run-Time Error c. Sum=10

15)

d. None of these

What will be the output of the following program :


void main()
{
int a=5,b=6,c=7;
printf("%d %d",a,b,c);
}
a. Compile-Time Error
b. 5 6
c. Run-Time Error d. 5 6 7
16)

What will be the output of the following program :


int add(int a,int b)
{
return a+b;
}
main()
{
109

int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
}
a. Garbage value

17)

b. Run-Time Error c. Compile-Time Error

What will be the output of the following program :


void main()
{
int a=5,b=6,c=7,d=8,e;
e = a ? b , c : c , d;
printf("%d",e);
}

a.Compile-Time Error
18)

d. 12

b. 6

c. 7

d. 8

What will be the output of the following program :


void main()
{
int a=5,b=6,c;
c++ = b % (b - a);
printf("%d",c);
}

a. Compile-Time Error

b. 6

c. 7

What will be the output of the following program :


void main()
{
int x=5,y=6,z;
z = x++ +++y;
printf("%d %d %d",x,y,z);
}
a. Compile-Time Error
b. 6 7 12 c. 5 6 11

d. None of these

19)

110

d. None of these

PROGRAMMING PROBLEM 4
MULTIPLE CHOICE QUESTIONS:
1)

What will be the output of the following program :


void main()
{
int i;
float a[5];
for (i=0; i<5; i++)
a[i] = (printf, ("%d",i/10.0));
for (i=0; i<5; i++)
printf("%.1f ",a[i]);
}

a.
c.

Compile-Time Error
0.0 0.1 0.2 0.3 0.4

b. 0.0 0.0 0.0 0.0 0.0


d. 1.0 1.0 1.0 1.0 1.0

2)

What will be the output of the following program :


void func()
{
printf("Testing...Done\n");
}
void main()
{
func;
func();
}
a. Compile-Time Error
b. Testing...Done
c. Testing...Done
d. None of these
Testing...Done
3)

A signed int bitfield 1-bit wide can only hold the values

a. 0 and 1
d. None of these
4)

b. 0 and -1

c. 0, 1 and -1

What will be the output of the following program :


void main()
111

{
int a=19,b=4;
float c;
c=a/b;
printf("%f",c);
}
a. 4.75

b. 4

c. 4.750000

d. 4.000000

5)

What will be the output of the following program :


void main()
{
int _;
_=70;
printf("%d",_);
}
a. Compile-Time Error
b. Run-Time Error
c. 70
d. None of these
6)

In DOS environment, what is the maximum combined length of the


command-line arguments passed to main (including the space between
adjacent arguments and the name of the program itself).

a. 80 Characters
c. Until RETURN KEY
(i.e '\n') is encountered

b. 128 Characters
d. None of these

7)

What will be the output of the following program :


void main()
{
int (*foo)(char *, ...) = printf;
(*foo)("hello, %s", "world!");
}
a. Compile-Time error
b. hello, world!
c. Run-TimeError
d. None of these
8)

What will be the output of the following program :


void main()
{
int i=5,(*foo)(char *, ...);
foo=printf;
printf("%d",i=(*foo)("hello, %s\n", "world!"));
}
a. Compile-Time error b. hello, world! C. hello, world!
D. hello, world!
5
13
14

112

9)

What will be the output of the following program :


void main()
{
int choice=2;
switch(choice)
{
default:
printf("Default1");
case 1:
printf("Case1");
break;
default:
printf("Default2");
}

}
a.Compile-Time Error
10)

b. Default1Case1

c. Default2

d. Default1

What is the MAXIMUM LIMIT for cases in a switch statement ?

a. 32767 cases

b. 257 cases

c. 127

What will be the output of the following program :


#define big(a,b) a > b ? a : b
#define swap(a,b) temp=a; a=b; b=temp;
void main()
{
int a=3,b=5,temp;
if ((3+big(a,b)) > b)
swap(a,b);
printf("%d %d",a,b);
}
a. 3 0
b. 5 3
c. 3 5

d. None of these

11)

12)

What will be the output of the following program :


#define main main()
void main
{
#define END }
printf("First"
"Second"
"Third");
END

a. Compile-Time Error
113

d. 5 0

b. First
Second
Third
c. FirstSecondThird
d. None of these

13)

What will be the output of the following program :


void main()
{
long double val;
printf("%d bytes",sizeof(val));
}
a. Compile-Time Error
b. 4 bytes
c. 8 bytes
What will be the output of the following program :
int * func()
{
int temp=50;
return &temp;
}
void main()
{
int *val;
val = func();
printf("%d",*val);
}
a. Compile-Time Error
b. 50
c. Garbage Value

d. 10 bytes

14)

114

d. None of these

ASSIGNMENT PROBLEMS PART I


1)

Write a program (W.A.P.) in C to SWAP the contents of 3 variables


without using the temporary (or extra) variables.

Ans.
/* Swapping 3 numbers without using extra variable */
#include<stdio.h>
#include<conio.h>
void Swap(int *a,int *b,int *c)
{
*a = *a + *b + *c;
*b = *a - (*b + *c);
*c = *a - (*b + *c);
*a = *a - (*b + *c);
}
void main()
{
int x=1,y=2,z=3;
clrscr();
printf("BEFORE SWAPPING : %d %d %d\n",x,y,z);
Swap(&x,&y,&z);
printf("AFTER SWAPPING : %d %d %d",x,y,z);
} /* End of Main */
2)

W.A.P. in C to find the Fifth root of the sum of the squares of the first
100 ODD numbers only.

Ans.
/* To find the Fifth root of the sum of the squares of the first 100 ODD
numbers ONLY */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
115

long i,oddnum=1,sqrnum,sum=0;
clrscr();
for (i=1; i<=100; i++)
{
sqrnum=oddnum * oddnum; // Square the ODD number
sum+=sqrnum;
// Add Square value to the sum
oddnum+=2;
// Get the next ODD number
}
printf("\nThe result is : %ld,%.2f",sum,pow((double)sum,(1.0/5.0)));
} /* End of Main */
3)

W.A.P. in C to multiply any two numbers without using * (asterisk) and


other arithmetic operators like +, -, / and %. [Hint : Use BITWISE
OPERATORS]

Ans:/* Multiplication of two numbers using BITWISE OPERATORS ONLY*/


#include<stdio.h>
void main()
{
long int i,n,mul,mul2,count,temp,a,b,sum,carry,res,tot;
clrscr();
printf("\nEnter any 2 numbers : ");
scanf("%ld %ld",&mul,&n);
mul2=temp=mul;
for (i=2; i<=n; i++)
{
temp=mul;
count=32;
res=1;
tot=sum=carry=0;
while (count--)
{
a=temp & 0x1;
b=mul2 & 0x1;
if ((a^b==1) && (carry==1))
{
sum=(a^b)^carry;
carry=(a^b)&carry;
}
else
{
sum=a^b|carry;
carry=a&b;
}
temp=temp>>1;
mul2=mul2>>1;
116

tot+=res*sum;
res=res*2;
}
mul2=tot;
}
printf("\n%3ld * %3ld = %3ld",mul,i-1,tot);
getch();
} /* End of Main */
4)

W.A.P. in C to check whether given number x is equal to the value 2


POWER i or something, where i>=0 using BITWISE operators ONLY.
[Hint : Check whether the given number x is equal to the value 2
POWER i or something using BITWISE operators ONLY]

Ans.
/* Check whether the given number x is equal to the value 2 power i or
not using BITWISE
operators ONLY */
#include<stdio.h>
#include<conio.h>
void main(void)
{
long x;
clrscr();
printf("Enter a number : ");
scanf("%ld",&x);
if ((x & 0x1) == 0x0)
printf("The given number %ld is EQUAL to the value 2
POWER something",x);
else
printf("The given number %ld is NOT EQUAL to the
value 2 POWER something",x);
getch();
} /* End of Main */
5)

W.A.P. in C to maintain 2 STACKS within a SINGLE ARRAY and the


values of one stack should not overwrite the values of another stack.

Ans:
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int stack[MAX],top1,top2;
void init()
{
117

top1=-1;
top2=10;
}
void Push1(int item)
{
stack[++top1]=item;
}
void Push2(int item)
{
stack[--top2]=item;
}
int Pop1()
{
return stack[top1--];
}
int Pop2()
{
return stack[top2++];
}
void Display()
{
int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{
printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{
printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
void main()
{
int item,ch;
clrscr();
init();
while(1)
{
118

printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.
Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 2 : if ((top2 - 1) > top1)
{
printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is : %d",Pop1());
break;
case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is : %d",Pop2());
break;
case 5 : Display();
break;
case 6 : exit(0);
default: printf("\nInvalid Choice");
}
119

}
} /* End of Main */
6)

W.A.P. in C that act as a guessing game in which the user has eight tries
to guess a randomly generated number. The program will tell the user
each time whether he guessed high or low. The user WINS the game
when the number guessed is same as randomly generated number.

Ans:
/* Guessing Game Solution */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i=8,rval,val,flag=1;
randomize();
// Initialize the random number generator
rval=random (100); // Generates a number in the range 0 to 90
printf("Welcome to Guessing Game.\n");
printf("RULES:\n1. Only 8 chances to guess the randomly
generated number.");
printf("\n2. You can WIN the game when the number guessed is
same as the randomly generated number.");
printf("\n3. Hints will be provided during the PLAY.");
printf("\n\n$$$ Good Luck. Start Guessing $$$");
for (i=1; i<=8; i++)
{
printf("\n\nGUESS %d ? ",i);
scanf("%d",&val);
if (val > rval)
printf("Your value is GREATER THAN the randomly
generated number");
else if (val < rval)
printf("Your value is LESSER THAN the randomly
generated number");
else
{
flag=1;
break;
}
}
if (flag)
printf("\n\n*** You are the WINNER. No. of tries = %d ***",i);
else
printf("\n\n*** You are the LOSER. ***");
120

} /* End of Main */
7)

W.A.P. to determine how much money is in a piggy bank that contains


several 50 paisecoins, 25 paise coins, 20 paise coins, 10 paise coins and
5 paise coins. Use the following values to test your program : Five 50
paise coins, Three 25 paise coins, Two 20 paise coins,One 10 paise coin
and Fifteen 5 paise coins. (Answer : Rs. 4.50)

Ans:
/* To determine how much money in a piggy bank */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(void)
{
float coin1=0.50,coin2=0.25,coin3=0.20,coin4=0.10,coin5=0.05,total=0.0;
int ncoins;
clrscr();
printf("How many 50 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin1);
printf("** %.2f **",total);
printf("\nHow many 25 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin2);
printf("** %.2f **",total);
printf("\nHow many 20 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin3);
printf("** %.2f **",total);
printf("\nHow many 10 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin4);
printf("** %.2f **",total);
printf("\nHow many 5 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin5);
printf("\n\nThe total amount is Rs.%.2f",total);
getch();
} /* End of Main */
121

8)

Modify the program given in [Q007] to accept total amount (in rupees)
and convert them into paise.(Vice-versa of [Q007])

Ans:
/* Denominations */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(void)
{
int nc1,nc2,nc3,nc4,nc5,temp;
float total;
clrscr();
printf("Enter the amount : ");
scanf("%f",&total);
temp = total * 100;
nc1 = temp / 50;
temp = temp % 50;
nc2 = temp / 25;
temp = temp % 25;
nc3 = temp / 20;
temp = temp % 20;
nc4 = temp / 10;
temp = temp % 10;
nc5=temp;
printf("\n\nNo. of 50 paise coins = %d",nc1);
printf("\nNo. of 25 paise coins = %d",nc2);
printf("\nNo. of 20 paise coins = %d",nc3);
printf("\nNo. of 10 paise coins = %d",nc4);
printf("\nNo. of 5 paise coins = %d",nc5);
getch();
} /* End of Main *
9)

W.A.P. in C to perform 4-letter WORD UNSCRAMBLING i.e. List all


possible combinations of 4-letters in a word. Ex: The word 'TEST' can
be unscrambled as TEST,TETS,TSET,TSTE,TTSE,TTES,etc.

Ans:
/* 4-letter word unscrambling */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
122

int i,j,k,l,sum=6;
char *str;
clrscr();
printf("Enter a 4-letter word or string : ");
scanf("%s",str);
if (strlen(str) == 4)
{
printf("The possible combinations of the given 4-letter word is
shown.");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j)
{
for (k = 0; k < 4; k++)
if ((k != i) && (k != j))
{
l = sum - (i + j + k);
printf("\n%c%c%c
%c",str[i],str[j],str[k],str[l]);
}
}
printf("\nTotal combinations = %d",4*3*2*1);
}
else
printf("\nInvalid string. Length of the string must be 4-letters only ");
getch();
} /* End of Main */

123

IMPORTANT DIFFERENCES
1. Distinguish between getchar() and gets() functions.

getchar()
Used to receive a single character.

gets()
Used to receive a single string with
white spaces.
It requires a single argument.

Does not require any argument.

2. Distinguish between scanf() and gets() functions.

scanf()
Strings with spaces cannot be
accessed.
All data types can be accessed.
Spaces and tabs are not acceptable as
a part of the input string.
Any number of characters, integers,
float etc. can be read.

gets()
Strings with any number of spaces
can be accessed.
Only character array data can be
accessed.
Spaces and tabs are perfectly
acceptable of the input string as a
part.
Only one string can be read at a time.

3. Distinguish between printf() and puts() functions.

puts()

printf()

It can display only one string at a


time.

It can display any number of


characters, integers or strings at a
time.
Each data type is considered
separately depending upon the
conversion specifications.

No such conversion specifications.


Every thing is treated as string.

124

4. What is the difference between a pre increment and a post increment operation?
A pre-increment operation such as ++a, increments the value of a by 1, before a is
used for computation, while a post increment operation such as a++, uses the
current value of a in the calculation and then increments the value of a by 1.

5. Distinguish between break and continue statement.

Break

Continue

Used to terminate the loops or to exit


from loop or switch.

Used to transfer the control to the


start of loop.
The continue statement when
The break statement when executed
executed cause immediate termination
causes immediate termination of loop.
of the current iteration of the loop.
6. Distinguish between while and do-while loops.
While loop

Do-while loop

The while loop tests the condition


before each iteration.
If the condition fails initially the loop
is skipped entirely even in the first
iteration.

The do-while loop tests the condition


after the first iteration.
Even if the condition fails initially the
loop is executed once.

7. Distinguish between local and global variables


Local variables

Global variables

These are declared within the body of


the function.
These variables can be referred only
within the function in which it is
declared.
The value of the variables disappear
once the function finishes its
execution.

These are declared outside the


function.
These variables can be referred from
any part of the program.
The value of the variables disappear
only after the entire execution of the
program.

8. State the differences between the function prototype the function definition
Function prototype

Function Definition

125

It declares the function.

It defines the function.

It ends with a semicolon.

It doesnt ends with a semicolon.

The declaration need not include


parameters.

It should include names for the


parameters.

9. Compare and contrast recursion and iteration


Both involve repetition.
Both involve a termination test.
Both can occur infinitely.
Iteration
Iteration explicitly user a repetition
structure.
Iteration terminates when the loop
continuation condition fails.
Iteration keeps modifying the
counter until the loop continuation
condition fails.
Iteration normally occurs within a
loop so, the extra memory assigned
is omitted.
It reduces the processors operating
time.

Recursion
Recursion achieves repetition
through repeated function calls.
Recursion terminates when a base
case is recognized.
Recursion keeps producing simple
versions of the original problem
until the base case is reached.
Recursion causes another copy of
the function and hence a
considerable memory space is
occupied.
It increases the processors
operating time.

10. State the uses of pointers.

Pointers provided an easy way to represent multidimensional arrays.

Pointers increase the execution speed.

Pointers reduce the length and complexity of program.

11. What is the difference between the address stored in a pointer and a value at the
address?
The address stored in the pointer is the address of another variable. The value
stored at that address is a stored in a different variable. The indirection operator
(*) returns the value stored at the address.
12. What is the difference between the indirection operator and the address of
operator?

126

The indirection operator (*) returns the value of the address stored in a pointer.
The address of operator (&) returns the memory address of the variable.

13. State the difference between call by value and call by reference.
Call by value

Call by reference
Formal parameter is a reference
variable.

Formal parameter is a local variable.

It cannot change the actual parameter. It can change the actual parameter.
Actual parameter must be a
variable.

Actual parameter may be a constant,


a variable, or an expression.

14. State the difference between arrays and structures.


Arrays

Structures

An array is an single entity


representing a collection of data
items of same data types.

A structure is a single entity


representing a collection of data
items of different data types.

Individual entries in an array are


called elements.

Individual entries in a structure are


called members.

An array declaration reserves enough


memory space for its elements.
There is no keyword to represent
arrays but the square braces []
preceding the variable name tells us
that we are dealing with arrays.

The structure definition reserves


enough memory space for its
members.
The keyword struct tells us that we
are dealing with structures.

Initialization of elements can be done Initialization of members can be done


during array declaration.
only during structure definition.
The elements of an array are stored
in sequence of memory locations.

The members of a structure are stored


in sequence of memory locations.

The array elements are accessed by


the square braces [] within which the
index is placed.

The members of a structure are


accessed by the dot operator.

127

Its general format:


Data type array name [size];

Example:
int sum [100];

Its general format is:


struct structure name
{
data_type structure member 1;
data_type structure member 2;
.
.
.
data_type structure member N;
} structure variable;
Example:
struct student
{
char studname [25];
int rollno;
} stud1;

15. State the difference between structures and unions.


Structures
Each member in a structure
occupies and uses its own memory
space.
The keyword struct tells us that we
are dealing with structures.
All the members of a structure can
be initialized.
More memory space is required
since each member is stored in a
separate memory locations.
Its general format is:
struct structure name
{
data_type structure Member1;
data_type structure Member2;
.
.
data_type structure Member N;
} structure variable;
Example:
struct student
{
char studname[25];
int rollno;
} stud1;

Unions
All the members of a union use the
same memory space.
The keyword union tells us that we
are dealing with unions.
Only one member of a union can be
initialized.
Less memory space is required
since all members are stored in the
same memory locations.
Its general formula is:
union union name
{
data_type structure Member1;
data_type structure Member2;
.
.
data_type structure Member N;
} union variable;
Example:
union student
{
char studname[25];
int rollno;
} stud1;

128

129