0% found this document useful (0 votes)
3 views

c Practice Questions and Answers

The document contains a series of C programming examples along with their outputs and explanations. Each example demonstrates various concepts such as static variables, extern variables, logical operations, switch statements, macros, and error handling in C. The document is prepared by S. Ananthakrishnan for B.Tech students in 2018.

Uploaded by

shahsham005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

c Practice Questions and Answers

The document contains a series of C programming examples along with their outputs and explanations. Each example demonstrates various concepts such as static variables, extern variables, logical operations, switch statements, macros, and error handling in C. The document is prepared by S. Ananthakrishnan for B.Tech students in 2018.

Uploaded by

shahsham005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SASTRA Deemed UNIVERSITY

C Programming examples with Output for Practice

-----------------------------------------------------------------------------------------------------
1.
main()
{
static int var = 5;
printf(“%d ”,var--);
if(var)
main();
}
Answer:
54321
Explanation:
When static storage class is given, it is initialized once. The change in the value of a
static variable is retained even between the function calls. Main is also treated like any
other ordinary function, which can be called recursively.
--------------------------------------------------------------------------------------------------------
2.
main()
{
extern int i;
i=20;
printf(“%d”,i);
}

Answer:
Linker Error :
Undefined symbol ‘_i’
Explanation:
extern storage class in the following declaration,extern int i; specifies to the compiler that
the memory for I is allocated in some other program and that address will be given to the
current program at the time of linking. But linker finds that no other variable of name I is
available in any other program with memory space allocated for it. Hence a linker error
has occurred.
-----------------------------------------------------------------------------------------------------

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------
3.
main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf(“%d %d %d %d %d”,i,j,k,l,m);
}

Answer:
00131

Explanation:
Logical operations always give a result of 1 or 0.And also the logical AND (&&)
operator has higher priority over the logical OR (||) operator. So the expression ‘i++ &&
j++ &&k++’is executed first. The result of this expression is 0(-1 && -1 && 0 = 0). Now
the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except
for ‘0|| 0’ combination- for which it gives 0). So the value of m is 1. The values of other
variables are also incremented by 1.
-----------------------------------------------------------------------------------------------------
4.
main()
{
int i=3;
switch(i)
{
default:printf(“zero”);
case 1: printf(“one”);
break;
case 2:printf(“two”);
break;
case 3: printf("three");
break;
}
}
Answer:
three
Explanation:
The default case can be placed anywhere inside the loop. It is executed only when all
other cases doesn’t match.

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------
5.
#define int char
main()
{
int i=65;
printf(“sizeof(i)=%d”,sizeof(i));
}
Answer:
sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro char

-----------------------------------------------------------------------------------------------------
6.
#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf(“%d”,i);
}
Answer:
64
Explanation:
The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4.
Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e., 16*4 =
64
-----------------------------------------------------------------------------------------------------
7.
#include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
{
case 1: printf(“GOOD”);
break;
case j: printf(“BAD”);
break;
}
}

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


Answer:
Compiler Error: Constant expression required in function main.
Explanation:
The case statement can have only constant expressions (this implies that we cannot
use variable names directly so an error).

-----------------------------------------------------------------------------------------------------
8.
main()
{
printf(“%d”, out);
}
int out=100;
Answer:
Compiler error: undefined symbol out in function main.
Explanation:
The rule is that a variable is available for use from the point of declaration. Even
though a is a global variable, it is not available for main. Hence an error

-----------------------------------------------------------------------------------------------------
9.
main()
{
extern out;
printf(“%d”, out);
}
int out=100;
Answer:
100
Explanation:
This is the correct way of writing the previous program.

-----------------------------------------------------------------------------------------------------

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


10.
int A_abc(int i)
{
return i++;
}

main()
{
int i=A_abc(10);
printf(“%d\n”,--i);
}
Answer:
9
Explanation:
return(i++) it will first return i and then increments.
i.e.,10 will be returned

-----------------------------------------------------------------------------------------------------
11.
#include<stdio.h>
main()
{
int a= 0;int b = 1;int c =0;int d =1;
if(a,b,c,d)
printf("hello");
if(d,c,b,a)
printf("hello");
}

Answer:
hello
Explanation:
The comma operator has associativity from left to right. Only the rightmost value is
returned and the other values are evaluated and ignored. Thus the value of last variable
y is returned to check in if. Since it is a non zero value if becomes true so, “hello” will
be printed.

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------
12.

#include<stdio.h>

int z=5;
void print();
main()
{
static int var = 3;
printf("%d\n",--var);
if(var)
main();
print();
z++;
}

void print()
{
auto int w=0;
printf("%d\n",w++);
printf("%d\n",z);
}
Answer:
210050607
Explanation:
Function, Recursion, Stack, Auto, Static and Global concepts are applied.

-----------------------------------------------------------------------------------------------------
13.
main()
{
unsigned int i=10;
while(i-->=0)
printf(“%u ”,i);
}
Answer:
10 9 8 7 6 5 4 3 2 1 0 65535 65534.....
Explanation:
Since i is an unsigned integer it can never become negative. So the expression i-- >=0
will always be true, leading to an infinite loop.

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------
14.
Are parameter names mandatory in function prototype? Give example.
Answer: No, it is not mandatory.
Example:
void main()
{
int f1(int, int);
printf(“%d”,f1(10,20));
}
int f1(int a,int b)
{
return a+b;
}
-----------------------------------------------------------------------------------------------------
15.
Can we use a function as a parameter of another function? Justify your answer
Answer: No, Directly can’t take as a parameter to other function.
-----------------------------------------------------------------------------------------------------

16.
If any error in the following code, correct those errors otherwise write its output.
#include<stdio.h>
void main() {
int a[3]=30,i;
for(i=0;i<3;i++)
printf(“%d”,a[i]); }

Answer: Error: int a[3]=30(This statement not permitted), rewrite this as int a[3]={30};
-----------------------------------------------------------------------------------------------------
17.
If any error in the following code, correct those errors otherwise write its output.
#include<stdio.h>
int i=0;
void main()
{ void f1(); f1(); f1(); f1();}
void f1()
{
static int i; int j=0;
printf(“%d %d”,i,j); i++; j++;
}
Answer: Output: 0 0 1 0 2 0 (ie. i values are 0 1 2 (bcz of static); j values are 0 0 0)

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------
18.
What is the output of the program given below?
main()
{
signed char i=0;
for(;i>=0;i++) ;
printf(“%d\n”,i);
}
Answer:
-128
Explanation:
Notice the semicolon at the end of the for loop. The initial value of the i is set to 0. The
inner loop executes to increment the value from 0 to 127 (the positive range of char)
and then it rotates to the negative value of -128. The condition in the for loop fails and
so comes out of the for loop. It prints the current value of i that is -128
-----------------------------------------------------------------------------------------------------

19.
main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf(“%d %d”,i,j);
}

Answer:
10 10
Explanation:
The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question
can be written as:
if(i,j)
{
if(i,j)
j = i;
else
j = j;
}
else
j = j;

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------

20.
main()
{
float i=1.5;
switch(i)
{
case 1: printf(“1”);
case 2: printf(“2”);
default : printf(“0”);
}
}
Answer:
Compiler Error: switch expression not integral
Explanation:
Switch statements can be applied only to integral types.
-----------------------------------------------------------------------------------------------------
21.
#include <stdio.h>
void main()
{
int i = 0;
for (i = 0;i < 5; i++)
if (i < 4)
{
printf("Hello");
break;
}
}

Answer: Hello
Explanation: For i=0 itself, if condition satisfied. Once the Hello is printed break will
terminate the loop.

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


-----------------------------------------------------------------------------------------------------
22.
#include <stdio.h>
void main()
{
int i = 1;
if (i == 1)
{
printf("%d",++i);
continue;
}
}
Answer: Compile time error
Explanation: Continue statement not within a loop.
-----------------------------------------------------------------------------------------------------
23.
#include<stdio.h>
main()
{
while(!!7)
printf("JAMES BOND");
}

Answer: Infinite loop.


Explanation: NOT NOT 7 will be always true.
-----------------------------------------------------------------------------------------------------
24.
#include<stdio.h>
main()
{
int i=3,j=2;
while(--i&&j++)
printf("%d %d ",i,j);
}
Answer: 2 3 1 4
Explanation:
Initial value of variable
i=3
j=2

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.


Consider the while condition : --i && j++
In first iteration:
--i && j++
= 2 && 2 //In c any non-zero number represents true.
In print i and j values are 2 and 3.
Next i and j values are 1 and 4 will be printed.
-----------------------------------------------------------------------------------------------------
25.
#include<stdio.h>
int main(){
static int i;
for(++i;++i;++i) {
printf("%d ",i);
if(i==4) break;
}
return 0;
}
Answer: 24
Explanation:
Default value of static int variable in c is zero. So, initial value of variable i = 0
First iteration:
For loop starts value: ++i i.e. i = 0 + 1 = 1
For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement
will print 2
Loop incrimination: ++I i.e. i = 2 + 1 =3
Second iteration:
For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement
will print 4.
Since is equal to for so if condition is also true. But due to break keyword program
control will come out of the for loop.
26.
#include<stdio.h>
main()
{
int arr[2] = {1, 2, 3, 4, 5};
printf("%d", arr[3]);
}
Answer : Compile time error (or) Some garbage value.
Explanation: Here the size of an array is 2, but the value inside array is exceed 2. Thus it
prints garbage value for index more than 1.

******

Prepared by S.Ananthakrishnan, CSE/SoC and T-section, B.Tech students, 2018.

You might also like