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
Meera said:
2 decades ago
j is a declared as a char data type.Then how the while loop executes?and how we are assigning that int value to char type?
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.
Pratik Gupta said:
1 decade ago
thanks @padhu
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)
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......
Sameer said:
1 decade ago
Thanks narendra.
Using which format strings in printf statement we convert char to int ?
Using which format strings in printf statement we convert char to int ?
Vivek kumar said:
1 decade ago
Could not understand.
Please help me.
Please help me.
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
Pratik said:
1 decade ago
@narendra good explanation.
Thanks.
Thanks.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers