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

11 comments:

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

    ReplyDelete
    Replies
    1. after 128 counter will roll over to -128 as its char

      Delete
  2. u r amaz...

    ReplyDelete
  3. CAN ANY1 EXPLAIN ME THE OUTPUT FOR 6TH QUE

    ReplyDelete
  4. Replies
    1. 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 .

      Delete
  5. #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();
    }

    ReplyDelete
  6. please explain the answer of question 4 and 5

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

    ReplyDelete
  8. 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.


    ReplyDelete