[list=1][*]what's the problem with gets()?[*]when you take a char, and add more than 255 to it, does it start again at zero and continue counting?[*]is there a difference in bit operations between a char and an unsigned char?[/list=1]
[list=1][*]what's the problem with gets()?[*]when you take a char, and add more than 255 to it, does it start again at zero and continue counting?[*]is there a difference in bit operations between a char and an unsigned char?[/list=1]
1. it allows buffer overruns
2. it starts again at zero
3. the LSB? is used as a sign flag and the maximum values become -128 to 128, so i would say yes.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
2. Lets try it and see what happens:Originally posted by ygfperson
[list=1][*]what's the problem with gets()?[*]when you take a char, and add more than 255 to it, does it start again at zero and continue counting?[*]is there a difference in bit operations between a char and an unsigned char?[/list=1]
Code:#include <stdio.h> #include <ctype.h> int main(void) { unsigned char c; int i; for (i = 0, c = 0; i < 260; i++, c++) printf("%02x\t%c\n", c, isprint(c)?c:' '); return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I think you mean -128 to 127Originally posted by no-one
3. the LSB? is used as a sign flag and the maximum values become -128 to 128, so i would say yes.
what's the problem with gets()?
It's a bug and a security hole in many situations. gets() doesn't accept a length and so will keep writing off the end of the allocated space it's been told to write data into. In short: it can overrun the buffer.
when you take a char, and add more than 255 to it, does it start again at zero and continue counting?
Depends. The C standard doesn't (or didn't) specify whether or not a char is signed or unsigned. It also doesn't specify a maximum size so a 'char' type with the range from -16383 to +16383 is perfectly valid for example.
If the compiler you're using uses a signed 'char' type then overflowing and undeflowing causes undefined behaviour. If the compiler uses an unsigned 'char' type then overflowing/undeflowing it is acceptable.
However, because the signedness of the 'char' type isn't defined in C it is dangerous to assume that it is one sign or another. This is why signed char and unsigned char types exist.
(The signedness of the char type has no doubt been discussed at length in news:comp.std.c ad nausium and I doubt it will ever be set to be one or the other)
is there a difference in bit operations between a char and an unsigned char?
Yes. IIRC It is undefined behaviour to over or underflow any signed integer type. Shifting 'off the top' of a signed integer would as overflow. With unsigned integer types overflow and underflow are well defined and don't result in undefined behaviour.
Get rid of gets. Use fgets.
char name[ 20 ];
printf( "\nEnter name" );
fgets( name, sizeof( name ), stdin );
char buffer[ 20 ];
int number;
printf( "Enter a number" );
fgets( buffer, sizeof( buffer ), stdin );
sscanf( buffer, "%d", &number );
-k