Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fgets

Reply
Thread Tools

fgets

 
 
Bill Cunningham
Guest
Posts: n/a
 
      04-24-2011
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


 
Reply With Quote
 
 
 
 
Gregory Pietsch
Guest
Posts: n/a
 
      05-04-2011
On Apr 23, 8:46*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * 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

> * * 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[URGENT] fgets reading last line in file twice DJP C++ 7 10-21-2004 09:23 AM
fgets and newline Mike Mimic C++ 4 05-15-2004 06:19 AM
cin before fgets Charles Wilkins C++ 4 08-07-2003 10:14 PM
fgets and problems reading into array Eigenvector C Programming 12 07-29-2003 02:20 AM
Problems with fgets and reading in a number FakeAddress@NoSpam.com C Programming 6 06-25-2003 09:18 PM



Advertisments