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.

Chandan rathore said:   1 decade ago
Write the same program in c++. It will give you different result. It will show the ascii value of corresponding digit.

can anyone explain?
(1)

Govind said:   1 decade ago
@Omkumar.

In first programme you put a semicolon right after the for statement.

for (;i<=5;i++) ;

This means that for loop doesn't have any statements to run.

So even if the for loop condition is TRUE nothing is gonna happen.

For loop will run without printing anything until the condition is TRUE. Once the condition is FALSE (i.e., When i=6) it will come out of the for loop and print i.

Thanks.
(1)

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 ?

Narendra said:   1 decade ago
in the program j=1 but not j='1';
if j='1' then ascii value of 1 is assigned to j and while loop executes.
in the above program we just convert char to int using format strings in printf statement it is possible in c......

Kiran said:   1 decade ago
Hi!

j is character data type and its range is 0 to 255 and all these are included as integer data type. So, the character data type is treated as integer data type also. And it evalutes same as int data type.
Step1:
while(j < 5) is evalutes as while(1 < 5) is true.
the printf statement executes 1
then j = j+1 evalutes to j =2 Until the while statement becomes fails.
When j = 5 , while statement fails.
The result is
1 2 3 4 only
(1)


Post your comments here:

Your comments will be displayed after verification.