A question on argument passing

M

M. Azam

I thought this should work but it doesn't. When bin is passed argument
251 it get converted in to -5. If I changed bin to take an int, it works
fine. I was expecting it to work even with char, since 251 is less than
the max a char can handle. Why it is not working? Thanks.


#include<stdio.h>
void bin(char n) {
printf("n = %d\n", n);
if (n!=0) {
printf("%d",n%2);
bin(n/2);
}
printf("\n");
}
main() {
unsigned char val = 4;
printf("%d: ", val);
bin(val);

val = ~ val;
printf("%d: ", val);
bin(val);
}
 
E

Eric Sosman

M. Azam said:
I thought this should work but it doesn't. When bin is passed argument
251 it get converted in to -5. If I changed bin to take an int, it works
fine. I was expecting it to work even with char, since 251 is less than
the max a char can handle. Why it is not working? Thanks.

Probably because "since 251 is less than the max a char
can handle" is false. Try this:

#include <stdio.h>
#include <limits.h>
int main(void) {
printf ("%d <= char <= %d\n", CHAR_MIN, CHAR_MAX);
return 0;
}

This program will print different results on different C
implementations. Two common outcomes are

0 <= char <= 255
and
-128 <= char <= 127

but others are possible.
 
K

Keith Thompson

M. Azam said:
I thought this should work but it doesn't. When bin is passed argument
251 it get converted in to -5. If I changed bin to take an int, it works
fine. I was expecting it to work even with char, since 251 is less than
the max a char can handle. Why it is not working? Thanks.
[...]

The maximum value a char can handle is CHAR_MAX. It's likely that
CHAR_MAX is 127 on your system. Plain char can be either signed or
unsigned; apparently on your system it's signed.

On a typical system with 8-bit bytes, unsigned char has a range of
0..255, signed char has a range of -128..+127, and plain char has the
same range as either signed char or unsigned char. (On some rare
systems, signed char might have a range of -127..+127; on others,
bytes might be bigger than 8 bits.)
 
M

M. Azam

M. Azam said:
I thought this should work but it doesn't. When bin is passed argument
251 it get converted in to -5. If I changed bin to take an int, it
works fine. I was expecting it to work even with char, since 251 is
less than the max a char can handle. Why it is not working? Thanks.
[...]

The maximum value a char can handle is CHAR_MAX. It's likely that
CHAR_MAX is 127 on your system. Plain char can be either signed or
unsigned; apparently on your system it's signed.

On a typical system with 8-bit bytes, unsigned char has a range of
0..255, signed char has a range of -128..+127, and plain char has the
same range as either signed char or unsigned char. (On some rare
systems, signed char might have a range of -127..+127; on others, bytes
might be bigger than 8 bits.)

Yes, by default char is signed, when changed bin signature to take
unsigned char, it worked fine. Thanks for the response.

--Zam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top