Big collection of c programming language questions with solution



(1)What will be output of following c program?

#include<stdio.h>
void main(){
printf("%d",sizeof(3.8));
}

Output: 8

Explanation:

3.8f is float constant, 3.8 is double constant and 3.8L is long double constant .Here are finding size of double constantan which is 8.

(2) What will be output of following c program?

#include<stdio.h>
void main(){
char *str1="powla";
char *str2="er";
printf("%s\b\b%s",str1,str2);
}

Output: power

Explanation:

\b escape sequence back the cursor one position left .We are using two /b so after writing str1 cursor is at the position of l of powal .So when it write er it will override the la so output will be power.

(3) What will be output of following c program?

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

Output: 16

(4)What is missing statement of in the following program?

#include<stdio.h>
Void main(){
int sort(int,int);
int i;
i=sort(5,6);
}

int sort(int a,int b){
int c;
c=a;
a=b;
b=c;
return a;
}

Answer:

Function sort returning a value but we are not using return value so there is weastge of two byte memory. So missing statement is, there should statement which uses the return value.

(5)Convert the following program c in term of if and else statement?

#include<stdio.h>
void main(){
int a=1,b=2,c=3;
if(a==5&&b==6&&c==7)
printf("india");
else
printf("pak");
}

Answer:

void main(){
int a=1,b=2,c=3;
if(a==1){
if(b==2){
if(c==3){
printf("india");
}
else{
printf("pak");
}
}
else{
printf("pak");
}
    }
else{
printf("pak");
}
}

(6)Draw the memory representation of following structure?

struct xxx{
char a;
int b;
char c;
};

Answer:

Memory representation:

(7) Write the following program in term of switch and case?

#include<stdio.h>
void main(){
int a=3;
if(x>2){
printf("INDIA IS BEST");
}
else{
printf("PAK IS BEST");
}
}

Answer:

if condition always return two value.
1 if condition is true.
0 if condition is false.
So program is

#include<stdio.h>
void main(){
int x=3;
switch(x>2){
case 0:printf("India is best");
break;
case 1:printf("Pak is best");
}
}

(8)What will be output of following c program?


#include<stdio.h> 
void main(){
int far *a=(int far*)0x50000011;
int far *b=(int far*)0x50010001;
int huge *c=(int huge*)0x50000011;
int huge *d=(int huge*)0x50010001;
if(a==b)
printf("I know C");
else
printf("I don't know C");
if(c==d)
printf("\nI know C");
else
printf("\nI don't know C");
}

Output:

I don’t know C
I known C
Explanation:

Far pointer always compares its whole far address. Since both or not equal so first output is: I don’t know C .Huge pointer always compare its physical address both c and d are representing same physical address so a and b are equal.

(9)What will be output of following c program?


#include<stdio.h> 
#define power(a) #a
void main(){
printf("%d",*power(432));
}

Output: 52

Explanation:

# is stringzinging operator. It makes the string constant of any data. So 432 is converted into “432” by macro power .Now *”432” means first char which is 4.Since we are using %d so it will print ASCII value of char 4 i.e. 52

(10)What will be output of following c program?


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

Output: -2
Explanation:
We are passing the array by xxx function. 1[ch] means *(ch+1) which is ch[1] =2.

(11) What is difference between a, b, c and in following declaration?

#define xxx char *
typedef char * yyy;
void main(){
yyy a,b;
xxx c,d;
}

Answer:

Both and b are char * type but c is char * type while d is char type.

(12) Write a c program to find the HCF of any two numbers?

Answer:


#include<stdio.h>
void main(){
int a,b,c;
scanf(“%d%d%d”,a,b,c);
while((c=a%b)!=0){
a=b;
b=c;
}
printf("%d",b);
}

(13)What will be output of following c program?


#include<stdio.h> 
void main(){
int a=5;{
a++;
}
printf("%d",a);
}

Output: 6

(14)What will be output of following c program?


#include<stdio.h> 
void main(){
int a=5;{
int a=7;
a++;
printf(“%d”,a);
}
printf("%d",a);
}

Output: 8 5
Explanation:

Scope of the auto variable is within {} if it is declared in {}.Also local variable has more priority than global variable.



4 comments:

abhimanipal said...

The tenth question is really good.....You learn some thing new everyday

NIDHI said...

thanks again

Anonymous said...

great

CProgramming said...

A very nice collection. I like it. Thanks.