C basic questions


C basic interview questions and answers for freshers

(1)

void main(){
    char i;
    clrscr();
    for(i=120;i<=128;i++){
         printf("%d ",i);

    }
    getch();
}

Output: Infinite loop

(2)

void main(){
    int i;
    clrscr();
    for(i=0,i++,i<=5;i++,i<=2;i=0,i<=5,i+=3){
         printf("%d ",i);
    }
    getch();
}

Output: 2
(3)

void main(){
    int i=1;
    clrscr();
    for(;;){
         printf("%d ",i);
    }
    getch();
}

Output: Infinite loop
(4)

void main(){
    int i=2;
    clrscr();
    for(i=0;i<=3;i++){
         static int i;
         i=i+8;
    }
    printf("%d",i);
    getch();
}

Output: 4
(5)

extern int j;
void main(){
    int i=0;
    clrscr();
    for(i=0;i<=2;i+=1){
         int j=5;
         printf("%d ",j);
         j++;
    }
    getch();
}
int j=25;

Output: 5 5 5
(6)

extern int j;
void main(){
    int i=0;
    clrscr();
    for(i=0;i<=2;i+=1){
         int j=5;
         printf("%d ",j);
         j++;
    }
    getch();
}
int j=25;

Output: 5 5 5

C programming language basic questions and answers

17 comments:

Bhaskey said...

The answer of first question is 120 to 128,
how it can be infinite loop man!!

Unknown said...

i represent by charachter

Anonymous said...

u r amaz...

Anonymous said...

CAN ANY1 EXPLAIN ME THE OUTPUT FOR 6TH QUE

Unknown said...

please explain 2nd question out put

Unknown said...

for(i=0,i++,i<=5;i++,i<=2;i=0,i<=5,i+=3)

first i = 0, now i++ so i = 1
now loop-condition will be verified, so i++, i<=2, will be executed, so i = 2
now it will print 2 ,
now i=0, i<=5, i+=3, so i = 3 and loop condition verified, i = 4 and i<=2 fails so loop breaks, thus it prints 2 .

Anonymous said...

after 128 counter will roll over to -128 as its char

Unknown said...

#include
#include
void main()
{
clrscr();
int i,j;
for(i=1;i<=13;i++)
{
if(i<=3)
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
for(i=1;i<=16;i++)
{
if(i==3||i==5||i==12||i==14)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
for(i=1;i<=16;i++)
{
if(i==2||i==6||i==11||i==15)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
for(i=1;i<=16;i++)
{
printf("*");
}
printf("\n");
for(i=1;i<=16;i++)
{
if(i==1||i==6||i==10||i==16)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
for(i=1;i<=16;i++)
{
if(i==1||i==6||i==10||i==16)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
for(i=1;i<=16;i++)
{
if(i==1||i==6||i==10||i==16)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
for(i=1;i<=16;i++)
{
printf("*");
}
printf("\n");
textcolor(BLUE+BLINK);
textbackground(GREEN);
cprintf("! HOME SWEET HOME !");
printf("\n");
textcolor(RED+BLINK);
printf("\n");
cprintf("created by sonu chohan");


getch();
}

Unknown said...

please explain the answer of question 4 and 5

Unknown said...

please explain the answer or question 2 in simply&clearfuly.

Ankit ghiya said...

Question no.2 explanation : In for loop 1st part is the initialization part, and we can put multiple statement in for loop using comma operator. finally initialize part look like : for(i=0,i++,i<=5;
After this, the condition checking is done, and in condition checking we can put multiple condition's too also if any of the condition become false then for loop terminate,
After that the body of for loop will execute, and then the increment statement come in the picture followed by condition checking.
And like wise loop continue till the condition going true.


Unknown said...

intially j value is 25 outside the for loop... bt j is declared as 5 inside the for loop.. though j value is incremented at the end of the loop, the cntrl vl go back to the strtng line of the loop i.e j=5 (j value gets initialized again).. so j value will always b 5 nly irrespective of itzz incremnt as lng as the loop gets executed..

Unknown said...

Question no.2 explanation :data type of i is char we dont assign integer values so it is Infinite loop

Annwesha said...

it shows a declaration error when executed for the condition

Unknown said...

can any one explain 2Q ans

Unknown said...

Plz explain Q no 3,4,5 and 6 also

Deekshith Banjan said...

j=5, prints 5, increments to 6. Back to loop, again assigns j=5 and prints. so loop runs 3 times and each time the value of j is assigned 5 and printed.