Thread: general c rules

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    general c rules

    [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]

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    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.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: general c rules

    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]
    2. Lets try it and see what happens:
    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]

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally 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.
    I think you mean -128 to 127

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    44

    Re: general c rules

    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.

  6. #6
    Unregistered
    Guest
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Replies: 6
    Last Post: 11-01-2008, 12:01 PM
  3. Pls let me know the rules in C
    By ice_breaekr in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-28-2005, 06:27 AM
  4. Addition to the forum rules
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-08-2004, 05:31 PM