Hi
Given 4 contiguous bites, how the computer know if that is an ASCII character or integer value?
Thanks

I don't understand. I guess it depends on the data type of the variable that you're storing the bytes in.

> how the computer know if that is an ASCII character or integer value?
It doesn't.
That's for you (as a programmer) to decide, based on how you try and use that data.

I always thought the computer treated both as numbers and it was up to the program displaying the numbers to determine the value as a character in some format such as ASCII.

For example, a char datatype typically used to store ASCII characters can also be used to simply store the values 0-255 (the ASCII range) in languages such as C (and I believe C++ also).

That's just my understanding of it at least.

Thanks for your answeres
Just like I thought. The computer doesn't know nothing itself. We have to tell it what is what and how to do it. We can tell it that it is an ASCII character by : char variable=37;
or integer variable by : int variable=37;
isn't right?
Thanks

A char is just a small integer in C.

char c = 'a';
printf( "%d\n", c );

will print the numeric value of the small int, even though it would seem to be more appropriate to print it using %c

Likewise unsigned char c = 0xEA; really is just a small integer with no obvious displayable character. You would use this for example to extract information from a binary file one byte at a time.

Can you do this without any problem?

int value = 'A';
printf( "%c", value );

I was told that mixing types is bad.

Can you do this without any problem?

int value = 'A';
printf( "%c", value );

I was told that mixing types is bad.

there's nothing wrong with mixing char and int per se, although whether or not its 'bad' depends entirely on context.

an int must always be at least as large as a char, just as a general rule. Both int and char are integral types, although, the default representation of a char (when output as text) is as a character rather than a number.

It becomes "bad" in situations where you might attempt to fit a value into a variable when the variable isn't big enough to hold it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.