Ascii characters in C language

BobaMosfet

Joined Jul 1, 2009
2,211
@BobaMosfet
int x=6 ;
char c='6';
printf("\n%c",x);
printf("\n%d",x);

printf("\n%c",c);
printf("\n%d",c);

what am i telling to the compiler for each of variable declaration and 4 output statements?
Why don't you find out for yourself? It's just memory. When you put a value in quotes (double or single) you're telling the compiler to treat it as a string- meaning, use an ASCII value to represent each character, not its actual decimal value. When you don't, as with int, long, etc- you're getting it's actual value.

However, as I said, why don't you see for yourself:

Code:
char   c = 6;
char   a = '6';
int     i = 0;
unsigned char   memory[10];
unsigned char   *memAddr;

memAddr = (unsigned char*)((long)&c - 4L);                    // get the address of memory location 4 bytes before where 'c' is in memory
for(i=0;i<9;i++)
  memory[i] = *memAddr++;
memory[9] = 0x00;                                            // add a terminating byte (c-style)
printf("%X\n",memory);                                     // Output Hex for each character in String

memAddr = (unsigned char*)((long)&a - 4L);                    // get the address of memory location 4 bytes before where 'a' is in memory
for(i=0;i<9;i++)
  memory[i] = *memAddr++;
memory[9] = 0x00;                                            // add a terminating byte (c-style)
printf("%X\n",memory);                                     // Output Hex for each character in String
it's a bit rough, I apologize if there's an error, but it should give you the gist, and output something like this:

000132780632232057 // 0x06 is a 6
4318A7FE3623C49820 // 0x36 is a '6'

NOTICE that I am using 'char' (byte-sized aka '8-bit') sized variable types in all the above to help you see things in memory at a byte level. int's use 2 bytes, longs use 4 bytes (compiler defined- but that is typical for 32-bit systems). If you represented 6 in 8-bits, 16-bits, or 32-bits, it would be: 0x06, 0x0006, and 0x00000006.

This may help:

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6
 
Last edited:

402DF855

Joined Feb 9, 2013
271
For someone learning to program, just consider that in C '1' equals 49 (decimal) equals 0x31 (hexadecimal). Likewise a "byte" is 8 bits. Even experts will rarely see implementations where these relationships aren't true.

Extra credit: what is sizeof('1')?
 

Analog Ground

Joined Apr 24, 2019
460
For someone learning to program, just consider that in C '1' equals 49 (decimal) equals 0x31 (hexadecimal). Likewise a "byte" is 8 bits. Even experts will rarely see implementations where these relationships aren't true.

Extra credit: what is sizeof('1')?
I suspect this will throw a compiler or preprocessor error. '1' is not a type and cannot reserve storage. '1' is a constant. I am too lazy to try it.
 

Analog Ground

Joined Apr 24, 2019
460
In C, sizeof('1') is 4. In C++, it's 1.

Probably more correct to say that in C sizeof('1') == sizeof(int).
Also:
Probably more correct to say that in C sizeof('1') == sizeof(int) == sizeof(49).

Also, sizeof("123") returns 4 on any platform.

'1' is treated by sizeof as an expression and returns 4 because the default int type is 32 bits on your platform. Nothing to do with the usual use of '1' as the value assigned to a char. I didn't realize sizeof( ) would return more than just the size of a type. Thanks for the motivation to explore sizeof( ) further. I learned something.
 

402DF855

Joined Feb 9, 2013
271
Probably more correct to say that in C sizeof('1') == sizeof(int) == sizeof(49).
Not really, sizeof(49) is already known to be sizeof(int).
Also, sizeof("123") returns 4 on any platform.
This might confuse some folks. It returns 4 because that's how many bytes are required for the string literal. It's not because a pointer is 4 bytes. sizeof("12345678") returns 9.

Another wrinkle: sizeof('1') is 4 for C, 1 for C++. But sizeof(L'1') is 2 for both. (My compiler anyway.)
 

Analog Ground

Joined Apr 24, 2019
460
Not really, sizeof(49) is already known to be sizeof(int).


Another wrinkle: sizeof('1') is 4 for C, 1 for C++. But sizeof(L'1') is 2 for both. (My compiler anyway.)
The sizeof(49) was to reinforce how 49 and '1' are equivalent expressions and result in the same values. This is getting back to the beginning of the thread. The sizeof("123") was to illustrate how it evaluates a string and the null at the end must be accounted. I am curious about why 1 is returned for C++.
 

402DF855

Joined Feb 9, 2013
271
how 49 and '1' are equivalent expressions and result in the same values.
In C but not in C++.
I am curious about why 1 is returned for C++.
I suspect it was to correct the mistake made in C. Consider these functions from ctype.h:
C:
int    islower(int);
int    isprint(int);
int    ispunct(int);
int    isspace(int);
I'd rather these took "unsigned char" having been bitten by it a few times.
 
Top