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;
}
Discussion:
24 comments Page 1 of 3.
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
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
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
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)
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.
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)
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
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
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 ?
{
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 ?
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......
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......
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
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
Padhu said:
2 decades ago
In C char types are treated as int by the compiler. Hence they can be used in place of int (<255 for unsigned). This is the reason why char can be used inside switch statement.
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.
Hence using char as data type we can store integer values up to 255.
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?
can anyone explain?
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers