Thread: fgets works only with size > 1

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    23

    fgets works only with size > 1

    Hi,

    Why this code works:

    Code:
    void main(){
        char letter;
        char* str;
        printf("Enter char:");
        fgets(&letter,2,stdin);
        printf("\n letter = %c",letter);
    }
    and this one doesn't:
    Code:
    void main(){
        char letter;
        char* str;
        printf("Enter char:");
        fgets(&letter,1,stdin);             // the change is 1 instead of 2
        printf("\n letter = %c",letter);
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    This doesn't work as well:
    Code:
    void main(){
        char letter;
        char* str;
        fputs("Enter char:",stdout);
        fgets(&letter,1,stdin);
        fputs(&letter, stdout);
    }
    Last edited by DoK; 09-10-2017 at 07:52 AM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    You have not allocated any data space for "str".
    Code:
    char str[256];
    And "letter" can only contain one letter.

    Also we usually don't use fgets() to just get one char. We use getchar() instead, and putchar() to display it. Plus "letter" should be defined as an int, not a char.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why this code works:
    > char letter;
    > fgets(&letter,2,stdin);
    Only through pure dumb luck does this "work". Change things around a bit, and it can soon "not work".

    fgets() ALWAYS appends a \0 to whatever string it stores. So in this case, the \0 ends up overwriting someone else's memory.

    It's also time to stop using void main as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    > Why this code works:
    > char letter;
    > fgets(&letter,2,stdin);
    Only through pure dumb luck does this "work". Change things around a bit, and it can soon "not work".

    fgets() ALWAYS appends a \0 to whatever string it stores. So in this case, the \0 ends up overwriting someone else's memory.

    It's also time to stop using void main as well.

    Thank you very much!

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by rstanley View Post
    You have not allocated any data space for "str".
    Code:
    char str[256];
    And "letter" can only contain one letter.

    Also we usually don't use fgets() to just get one char. We use getchar() instead, and putchar() to display it. Plus "letter" should be defined as an int, not a char.

    Thanks

  7. #7
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Actually it is still not working :/

    This is the new code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void Ex1(){
        char letter;
        char* str;
        fputs("Enter char: ",stdout);
        letter = getchar();                     // it just skip this i think
        putchar(letter);                        // It doesn't print anything even if I do fflush(stdin);
    }
    
    void Ex2(){
        puts("Ex2");
    }
    
    int main(){
        char choice;
        while(1){
            fputs("#Please select the exercise (1-5, 0 to exit): ",stdout);
            choice = getchar();
    
            switch (choice) {
                case '0':
                    puts("Good bye");
                    return 0;
    
                case '1':
                    Ex1();
                    break;
    
                case '2':
                    Ex2();
                    break;
            }
        }
    
        return 0;
    }
    thanks for the help
    Last edited by DoK; 09-10-2017 at 09:00 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you type in
    1\n

    Then main() will read '1' and Ex1() will read '\n'.

    > It doesn't print anything even if I do fflush(stdin)
    Because fflush() doesn't work on input streams.

    If you want to clean up the input stream, then see the FAQ
    FAQ > Flush the input buffer - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    If you type in
    1\n

    Then main() will read '1' and Ex1() will read '\n'.

    > It doesn't print anything even if I do fflush(stdin)
    Because fflush() doesn't work on input streams.

    If you want to clean up the input stream, then see the FAQ
    FAQ > Flush the input buffer - Cprogramming.com

    Thanks again!
    Is it legit if add this function:
    Code:
    void clear_stdin(){
        int c;
        while((c = getchar()) != '\n' && c != EOF);
    }
    
    
    void Ex1(){
        char letter;
        char* str;
    
        clear_stdin();
        fputs("Enter char: ", stdout);
        letter = getchar();
        putchar(letter);
    }
    ?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looks good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    Looks good.
    You are the man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets after scanf, skips over fgets?
    By willane1965 in forum C Programming
    Replies: 1
    Last Post: 08-17-2014, 11:13 PM
  2. Confusion how fgets() works
    By Tripswitch in forum C Programming
    Replies: 3
    Last Post: 07-23-2014, 10:12 AM
  3. fgets without specifying size
    By Queue in forum C Programming
    Replies: 5
    Last Post: 09-17-2006, 06:02 PM
  4. fgets() array size memory concern
    By Sereby in forum C Programming
    Replies: 6
    Last Post: 07-28-2004, 10:50 PM
  5. fgets array size
    By stautze in forum C Programming
    Replies: 9
    Last Post: 04-29-2002, 01:25 PM

Tags for this Thread