Sunday, January 4, 2009

Best basic Question of C programming with Explanation

(1)What will be output
void main()
{
clrscr();
printf("%d",sizeof(3.8));
getch();
}

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.
For more detail click here
(2) What will be output ?
void main()
{
char *str1="powla";
char *str2="er";
clrscr();
printf("%s\b\b%s",str1,str2);
getch();
}

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 ?
void main()
{
int a=270;
char *p;
p=(char *)&a;
clrscr();
printf("%d",*p);
getch();
}

output: 
16
(4)What is missing statement of in the following program ?
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;
}

Ans: 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)Write following in term of if and else :
void main()
{
int a=1,b=2,c=3;
clrscr();
if(a==5&&b==6&&c==7)
printf("india");
else
printf("pak");
getch();
}

Ans:
void main()
{
int a=1,b=2,c=3;
clrscr();
if(a==1)
{
if(b==2)
{
if(c==3)
{
printf("india");
}
else
{
printf("pak");
}}
else
{
printf("pak");
} }
else
{
printf("pak");
}
getch();
}
(6)Give the memory representation of
struct xxx
{
char a;
int b;
char c;
};
Ans: Memory representation :




More detail click here 
(7) Write the following program in term of switch and case ?
void main()
{
int a=3;
if(x>2)
{
printf(“INDIA IS BEST”);
}
else{
printf(“PAK IS BEST”);
}
}

Ans: if condition always return two value.
1 if condition is true.
0 if condition is false.
So program is
void main()
{
int x=3;
switch(x>2)
{
case 0:printf("India is best");
break;
case 1:printf("Pak is best");
}
getch();
}
(8) What will be output ?
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;
clrscr();
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");
getch();
}

Output:
I don’t know C
I known C
Explanation: far pointer always compare 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.


More detail click here
(9) What will be output ?
#define power(a) #a
void main()
{
clrscr();
printf("%d",*power(432));
getch();
}

Output : 
52
Explanation:
# is string zinging operator. It make 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
More detail click here
(10) What will be output ?
void main()
{
int arr[]={1,2,3,4,5,6};
void xxx(int[5]);
xxx(arr);
getch();
}
void xxx(int ch[5])
{
clrscr();
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;
}

Ans: 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 number ?
Ans:
void main()
{
int a,b,c;
scanf(“%d%d%d”,a,b,c);
clrscr();
while((c=a%b)!=0)
{
a=b;
b=c;
}
printf("%d",b);
getch();
}
(13) What will be output ?
void main()
{
int a=5;
{
a++;
}
clrscr();
printf("%d",a);
getch();
}

Ans :
6
(14) What will be output ?
void main()
{
int a=5;
{
int a=7;
a++;
printf(“%d”,a);
}
clrscr();
printf("%d",a);
getch();
}

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.

No comments:

Blog List