fgets

Discussion in 'C Programming' started by Bill Cunningham, Apr 24, 2011.

  1. 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
     
    Bill Cunningham, Apr 24, 2011
    #1
    1. Advertising

  2. On Apr 23, 8:46 pm, "Bill Cunningham" <> 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
     
    Gregory Pietsch, May 5, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Wilkins

    cin before fgets

    Charles Wilkins, Aug 7, 2003, in forum: C++
    Replies:
    4
    Views:
    732
    Charles Wilkins
    Aug 7, 2003
  2. Mike Mimic

    fgets and newline

    Mike Mimic, May 15, 2004, in forum: C++
    Replies:
    4
    Views:
    8,055
    John Harrison
    May 15, 2004
  3. DJP
    Replies:
    7
    Views:
    7,475
    glen herrmannsfeldt
    Oct 21, 2004
  4. Replies:
    6
    Views:
    1,255
    Mark McIntyre
    Jun 25, 2003
  5. Eigenvector

    fgets and problems reading into array

    Eigenvector, Jul 26, 2003, in forum: C Programming
    Replies:
    12
    Views:
    641
    Glen Herrmannsfeldt
    Jul 29, 2003
Loading...

Share This Page