in Programming in C recategorized by
3,445 views
2 votes
2 votes
Predict the output of following C programs

1.
#include
int main()
{
    char a = '\012';
 
    printf("%d", a);
 
    return 0;
}

2.
#include
int main()
{
    char a = '012';
 
    printf("%d", a);
 
    return 0;
}

3.
#include
int main()
{
    char a = '\012';
 
    printf("%c", a);
 
    return 0;
}
in Programming in C recategorized by
3.4k views

4 Comments

Output
1)-10(why)
The character sequence \0 is octal escape sequence and Octal value of decimal  12 is 10.
2)-50(why)
Always print ASCII value of last character of String
3)-nothing print(why)
It print first character of your string
1
1

Answer 1: It will print 10 as acc to me it will consider it as octal number. when we print in decimal then it will print 10 

Answer 2:it is printing the ascii value of 2 but couldnt get the ans why

Answer 3: It will print nothing as it will terminate by seeing null character ('\0') at the begining.

0
0
Is there any reason why it will print the ascii value of last  character?
0
0
@Anjali_aspirant It prints the ASCII value when printed as a charater, and prints 10 when printed as decimal.

%d -> decimal value %c -> ASCII value
0
0

2 Answers

2 votes
2 votes

(1) #include
int main()
{
    char a = '\012';
     printf("%d", a);
     return 0;
}

The output is 10.

char a = '\012'; ==> Stores octal value of 12 to a , which is 001010.

printf("%d", a); ==> Printing it in decimal  will print 10.

(2) #include
int main()
{
    char a = '012';
    printf("%d", a);
    return 0;
}

The output is 50.

char a = '012'; ==> Here ASCII values of each character get stored (to the same location). 0 has ASCII value 48, then 1 with 49, finally 50 is stored for character 2. Now printing it in decimal gives 50 as output.

3. #include
int main()
{
    char a = '\012';
    printf("%c", a);
    return 0;

The output is Linefeed Character (or New Line)

char a = '\012'; ==> This is same as the first case where octal value for 12 (00001010) is stored in a. This has a decimal value 10, and the corresponding ASCII character is 'Line Feed'.
printf("%c", a); ==> Prints character value
So the output will go to New Line on printing 10 as a character.

edited by
0 votes
0 votes

(1) #include 
int main() 

    char a = '\012'; 
     printf("%d", a); 
     return 0; 
}

The output is 10.

char a = '\012'; ==> Stores octal value of 12 to a , which is 001010.

printf("%d", a); ==> Printing it in decimal  will print 10.

(2) #include 
int main() 

    char a = '012'; 
    printf("%d", a); 
    return 0; 
}

The output is 50.

char a = '012'; ==> Here ASCII values of each character get stored (to the same location). 0 has ASCII value 48, then 1 with 49, finally 50 is stored for character 2. Now printing it in decimal gives 50 as output.

3. #include 
int main() 

    char a = '\012'; 
    printf("%c", a); 
    return 0; 

The output is Linefeed Character (or New Line)

char a = '\012'; ==> This is same as the first case where octal value for 12 (00001010) is stored in a. This has a decimal value 10, and the corresponding ASCII character is 'Line Feed'. 
printf("%c", a); ==> Prints character value 
So the output will go to New Line on printing 10 as a character.