C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 19)
19.
What will be the output of the program?
#include<stdio.h>
int main()
{
    char j=1;
    while(j < 5)
    {
        printf("%d, ", j);
        j = j+1;
    }
    printf("\n");
    return 0;
}
1 2 3 ... 127
1 2 3 ... 255
1 2 3 ... 127 128 0 1 2 3 ... infinite times
1, 2, 3, 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
24 comments Page 2 of 3.

Nellaiappan N said:   9 years ago
Thanks for your answer @Padhu.

Srk said:   1 decade ago
%i means it will print integral type values like octal,decimal and hexadecimal.

Sudheer said:   1 decade ago
What is meaning of %i in c language I mean it declares which data type can any one tell me?

Gunjan dhawas said:   1 decade ago
here j is char which is of 1 byte. In 1 byte we can have number from 0 to 255.(2^8=256)as 1 byte=8 bits.

Hence using char as data type we can store integer values up to 255.

Omkumar said:   1 decade ago
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d,", i);
return 0;
}

Answer for above question is 6

char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;

Answer for ths is 1 2 3 4. In first program why we are not mentioning 12345 ?

Hammad said:   1 decade ago
j=j+1 is same as j++ post increment so
If(1<5)
printf("%d,",j);
j++;
It keep on increasing unless it encounters the condition 4<5 when it become 5<5 it fails then ans is 1,2,3,4

Pratik said:   1 decade ago
@narendra good explanation.

Thanks.

Panchanan rauta said:   1 decade ago
char is one of integral type when we assign an integer value into a char then it is treated as like integer.so the ans is as like below
step1 while(j<5) 1<5 yes value y=1 print
step2 j=2 2<5 yes value y=2 print
step3 j=3 3<5 yer value y=3 print
step4 j=4 4<5 yes value y=4 print
step5 j=5 5<5 no come out of loop so the values are 1,2,3,4

Vivek kumar said:   1 decade ago
Could not understand.

Please help me.

Sameer said:   1 decade ago
Thanks narendra.

Using which format strings in printf statement we convert char to int ?


Post your comments here:

Your comments will be displayed after verification.