fgets

B

Bill Cunningham

I have been away from C for awhile and need a refresher. fgets 2nd
parameter takes an int but the 1st takes a string. When I compile what I
write everything works fine but this is what I type-

#include <stdio.h>

int main()
{
char p[5];
printf("enter code-> ");
fflush(stdout);
fgets (p,sizeof(int),stdin);
printf("%s\n",p);
return 0;
}

Everything's cool and works the way I want. But is there a similarity
with the 5 chars and the ints in fgets 2nd parameter? I shouldn've used 5
for int should I have?

Bill
 
G

Gregory Pietsch

    I have been away from C for awhile and need a refresher. fgets 2nd
parameter takes an int but the 1st takes a string. When I compile what I
write everything works fine but this is what I type-

#include <stdio.h>

int main()
{
    char p[5];
    printf("enter code-> ");
    fflush(stdout);
    fgets (p,sizeof(int),stdin);

What's the sizeof(int) for? The fgets function reads in a string. If
you want to convert the string to an integer, use atoi, atol, atoll,
strtol, strtoul, or strtoull. -- Gregory Pietsch
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top