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 1 of 3.

Narendra.n.h said:   1 decade ago
This above statement will print value along with comma. For example:

int j=9;
printf ("%d, ", j) ;

Output:9,
(2)

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)

Sonamuthu said:   1 decade ago
While loop execute 4 times so will print 1, 2, 3, 4.
(1)

Sanjoy said:   1 decade ago
printf ("%d, ", j) ;.

Why there is a comma after the symbol d?
(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)

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)

Dev said:   12 months ago
Here, it will be int i=1.

Ashok said:   2 decades ago
while(1<5) it is true enters the loop
prints j values as 1
j value is incremented by 1 i.e now j value is 2
while(2<5) it is true enters the loop
prints j values as 2
j value is incremented by 1 i.e now j value is 3
while(3<5) it is true enters the loop
prints j values as 3
j value is incremented by 1 i.e now j value is 4
while(4<5) it is true enters the loop
prints j values as 4
j value is incremented by 1 i.e now j value is 5
while(5<5) it is false exits from loop

Shashank Mishra said:   2 years ago
How can be char j =1?
1 is an integer so j should be int datatype.

Jat said:   3 years ago
Anyone, explain the meaning of %d in this printf("%d, ", j).


Post your comments here:

Your comments will be displayed after verification.