Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing char by integer qualifier

Tags:

c++

c

I am trying to execute the below program.

#‎include‬ "stdio.h" 
#include "string.h" 

void main()
{ 
    char c='\08'; 
    printf("%d",c); 
} 

I'm getting the output as 56 . But for any numbers other than 8 , the output is the number itself , but for 8 the answer is 56.

Can somebody explain ?

like image 361
Aravindan Chandrasekaran Avatar asked Aug 12 '13 09:08

Aravindan Chandrasekaran


4 Answers

A characters that begins with \0 represents Octal number, is the base-8 number system, and uses the digits 0 to 7. So \08 is invalid representation of octal number because 8 ∉ [0, 7], hence you're getting implementation-defined behavior.

Probably your compiler recognize a Multibyte Character '\08' as '\0' one character and '8' as another and interprets as '\08' as '\0' + '8' which makes it '8'. After looking at the ASCII table, you'll note that the decimal value of '8' is 56.


Thanks to @DarkDust, @GrijeshChauhan and @EricPostpischil.

like image 59
Maroun Avatar answered Sep 27 '22 18:09

Maroun


The value '\08' is considered to be a multi-character constant, consisting of \0 (which evaluates to the number 0) and the ASCII character 8 (which evaluates to decimal 56). How it's interpreted is implementation defined. The C99 standard says:

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

So if you would assign '\08' to something bigger than a char, like int or long, it would even be valid. But since you assign it to a char you're "chopping off" some part. Which part is probably also implementation/machine dependent. In your case it happens to gives you value of the 8 (the ASCII character which evaluates to the number 56).

Both GCC and Clang do warn about this problem with "warning: multi-character character constant".

like image 29
DarkDust Avatar answered Sep 27 '22 18:09

DarkDust


\0 is used to represent octal numbers in C/C++. Octal base numbers are from 0->7 so \08 is a multi-character constant, consisting of \0, the compiler interprets \08 as \0 + 8, which makes it '8' whose ascii value is 56 . Thats why you are getting 56 as output.

like image 20
Umer Farooq Avatar answered Sep 27 '22 16:09

Umer Farooq


As other answers have said, these kind of numbers represent octal characters (base 8). This means that you have to write '\010' for 8, '\011' for 9, etc.

There are other ways to write your assign:

char c = 8;
char c = '\x8'; // hexadecimal (base 16) numbers 
like image 44
tohava Avatar answered Sep 27 '22 18:09

tohava