C Problems and Solutions

1. What is wrong in the following problem
#include
main() {
int i,j;
j = 10;
i = j++ - j++;
printf("%d %d", i,j);
}
Ans: 0, 12

2. What is the output of the following problem
#include
main() {
int j;
for(j=0;j<3;j++)
foo();
}
foo() {
static int i = 10;
i+=10;
printf("%d\n",i);
}

Ans: i is static variable, so output is

20
30
40

3. What is wrong in the following code

#include
#include

main()
{
char *c;
c = "Hello";
printf("%s\n", c);
}

Ans: Hello, The code is successfully running

4. What is the final value of i and how many times loop is executed ?

#include
main()

{
int i,j,k,l,lc=0;
/* the input is given as 1234 567 */
printf("Enter the number string:<1234 567 >\n");
scanf("%2d%d%1d",&i,&j,&k);
for(;k;k--,i++)
for(l=0;l printf("%d %d\n",i,l);}
printf("LOOPS= %d\n", lc-1);
}
Ans: i = 16, and loop is executed for 169 times

5. What is the output of the following program

#include
main() {
union {
int a;
int b;
int c;
} u,v;
u.a = 10;
u.b = 20;
printf("%d %d \n",u.a,u.b);
}

Ans : 20 20
Read More