Thread: help with c

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    help with c

    i was working on c language.I wanted to compile this code

    Code:
    void main()
    {
    char inp[5];
        //char out[5];
        int i,n;
        //k=0;
        printf("Enter no. of characters in the word :");
        scanf("%d",&n);
        printf("Enter the word :");
        for(i=0;i<5;i++)
            scanf("%c",&inp[i]);
    
    
    for(i=0;i<5;i++)
            printf("%c",inp[i]);
    }
    the output i expected was simple my input word, but the output am getting has 1 word less than my input.

    like

    input-apple
    output-appl

    i have no idea y its happening.. someone please explain it to me..



  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%d",&n);
    Let's say you enter the number 5. You would type 5 then 'enter' (newline). The 5 is taken from the input buffer and put into 'n'. The newline is still floating in the buffer.

    Code:
    for(i=0;i<5;i++)
        scanf("%c",&inp[i]);
    This will read five characters from the input. However, the first character it reads will be the newline left over from above. So it will only read the next four characters.

    You can see this in the output - it prints a newline before the four letters in your example.

    One solution is to add a space in the "scanf()" before the %c, as follows:

    Code:
    scanf(" %c",&inp[i]);
    This will ignore the preceding whitespace (a newline, in this case) before reading the next character.

    --------

    Note that "main()" is generally supposed to return an integer. The standard form is:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        // code here
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    yeah..it is printing a new line.. but i didn't understood how or why the space in front of %c will solve the problem..

    and why is main() supposed to return an integer value??

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Becaseu %c, unlike %d, will not eat the newline automatically. (because it is a character!)
    By leaving a space, you say to scanf
    Code:
    Hey, scanf! Please ignore the newline and read the data I want to!
    Main returns a integer, in order to inform the rest of the world that everything went well.

    Check here for more.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by sahilpatel_13 View Post
    yeah..it is printing a new line.. but i didn't understood how or why the space in front of %c will solve the problem.


    Quote Originally Posted by Matticus View Post
    This will ignore the preceding whitespace (a newline, in this case) before reading the next character.
    Or, if you want further reading: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    and why is main() supposed to return an integer value??
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    --------

    (Small letters seem to have the opposite effect of all caps to me - I read it in a "whisper." Ha.)

  6. #6
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Code:
    int i,n;
        //k=0;
        printf("Enter no. of characters in the word :");
        scanf("%d",&n);
        printf("Enter the word :");
        for(i=0;i<5;i++)
            scanf("%c",&inp[i]);
    why do u call n, i think int n are useless,
    its better to making code like this,
    Code:
    int i,n=5;
    and in for loop using this
    Code:
    for(i=0;i<n;i++)
    it didnt solve the problem though but it looks better than this way,

Popular pages Recent additions subscribe to a feed