C Programming Question

Status
Not open for further replies.

oSpIkEo

Seniorius Lurkius
33
HI, im studying this c tutorial on some page, if u want it i can display it later.

but here, i'm just gonna type out what i remember ( as in studied)
and when i compile it , get errors. im using ms visual c++ 5.0

#include <stdio.h>
int main()
{
long char a;

printf("Enter your name: ");
getc(a, stdin);
putchar(10); /* char 10 is newline */
printf("You entered your name as: ");
putc(a,stdout);
putchar(10);
return 0;
}
 

oSpIkEo

Seniorius Lurkius
33
well, i got this far. seems to compile and execute now, but when i put in my name, the rest gets truncated.

#include <stdio.h>
int main()
{
long double a;

printf("Enter your name: ");
a = getc(stdin);
putchar(10); /* char 10 is newline */
printf("You entered your name as: ");
putc(a,stdout);
putchar(10);
return 0;
}


what else can i use? i tried them all. long double , double , float... etc
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
Originally posted by skylark:





In Linux you'll get a warning, "the gets function is dangerous..." just ignore it



No, do not, I repeat do not ignore that warning. The gets() function is extremely dangerous and introduces a point of failure in your program. Do not use it. Ever. Use fgets() instead.



oSpIkEo, the char type in C only holds a single character, not a string. To store a string, you must use an array of char:



Code:
char x;    /* can store single characters only */

char a[6]; /* can store strings up to 5 characters long */



In C, all strings are terminated with a 0 value, so the size of the char array must be at least 1 more than the size of the string you are trying to store. In other words, if you store the string "Hello" to a char array, the actual contents of the array are {'H', 'e', 'l', 'l', 'o', 0}.



The reason gets() is dangerous is that it will attempt to store as many characters as you try to enter, regardless of the size of the target buffer. If you have a target buffer of 10 characters and someone enters a string that's 100 characters long, 90 of those characters will be stored past the end of your buffer and clobber whatever's there, causing a crash or other weird behavior (the Morris worm exploited this misfeature). fgets() allows you to specify how many characters to read, allowing you to prevent such problems.



Example (untested!):



Code:
#include <stdio.h>

#include <string.h>



int main (void)

{

    char name[34]; /* can hold names up to 32 characters long */

                   /* plus newline character and nul terminator */

    char *n;       /* used to find newline character in buffer */



    printf ("What's your name: ");

    fgets (name, sizeof name, stdin);

    if (!(n = strchr (name, '\n')))

    {

        /* 

        ** We didn't find a newline in the target buffer, 

        ** meaning the user typed in a name longer than 

        ** we can handle.  

        */

        printf ("That name's too long for me!\n");

    }

    else

    {

        /*

        ** Strip out the newline

        */

        *n = 0;

        printf ("Hi, %s\n", name);

    }



    return 0;

}



Normally you'll check the return value of fgets() to make sure the read actually succeeded.

[This message was edited by jbode on May 05, 2002 at 14:03.]
 

skylark

Ars Tribunus Militum
2,388
No, do not, I repeat do not ignore that warning. The gets() function is extremely dangerous and introduces a point of failure in your program. Do not use it. Ever. Use fgets() instead.

Actually I was simply repeating what my C prof said. He said that it was dangerous, "but in C everything is dangerous. If you don't like dangerous programming don't use C".
He didn't mention the problem you described.
I have used gets numerous times, never had a problem.
But I'm sorry, I admit I still have a lot to learn.
 

MrHat

Ars Scholae Palatinae
952
Actually I was simply repeating what my C prof said. He said that it was dangerous, "but in C everything is dangerous. If you don't like dangerous programming don't use C".

Lighting a match and throwing it into an aircraft fuel tank is dangerous. But everything about aircraft refueling is dangerous. If you don't like exploding, don't refuel your aircraft.

:p
 
Status
Not open for further replies.