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

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......

Pratik Gupta said:   1 decade ago
thanks @padhu

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.

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?


Post your comments here:

Your comments will be displayed after verification.