Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:18:22+00:00 2026-06-09T05:18:22+00:00

I wan to read two char so I do the following code: main() {

  • 0

I wan to read two char so I do the following code:

main() {
    char a,b;

    printf("\nEnter the first char.. ");
    a=getchar();
    printf("\nEnter the second char.. ");
    b=getchar();
    b=getchar();       //<--I've to add this sentence because the previously doesn't work

    printf("\nFirst char --> %c",a);
    printf("\nSecond char --> %c",b);

}

The code should not work but it does. The output is the correct:

First char –> z

Second char –> z (‘z’ or the char there I wan to)

If I remove the second getchar() then the output is:

First char –> z

Second char –> (Here there is no char)

How can I remove the second getchar() without get any error?

Because fflush(stdin) before getchar() doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:18:23+00:00Added an answer on June 9, 2026 at 5:18 am

    As indicated by the other answers, the problem was caused by the carriage return.

    There are many ways solve this, some more complex than others. Therefore I will indicate the simplest method I’ve found:

    As the GNU website indicate the most appropriate function for this is getc(stream) and I quote:

    This is just like fgetc, except that it is permissible (and
    typical) for it to be implemented as a macro that evaluates the stream
    argument more than once. getc is often highly optimized, so it is
    usually the best function to use to read a single character.

    I put it into practice but it still have the CR problem, so I printed the result of this function and notice that it return 10 when CR.
    After this I tried to get functionality and clarity for the code. Doing everything possible for this, now my code is the following:

    main() {
        char a,b;
    
        printf("\nEnter the first char.. ");
        if ((a=getc(stdin))==10) a=getc(stdin);
        printf("\nEnter the second char.. ");
        if ((b=getc(stdin))==10) b=getc(stdin);
    
        printf("\nFirst char --> %c",a);
        printf("\nSecond char --> %c",b);
    
    }
    

    And the nice output:

    First char –> z

    Second char –> z (‘z’ or the char there I wan to)


    I’ve learned that all the programs need they own solution, different than the others, and that is the reason (I think) for that are so many functions. What I can say is “Always have to adapt the program. Maybe this sentence works for me but is not guaranteed that gonna work for other codes.”

    Thanks to all the guys that helped me!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I wan't to add 1 to an integer. This will only be
I wan't to implement the following code - the check if the pointer is
I wan't to register the OnMouseOver and OnMouseOut-Event for an Image from the Code
This is my first time using this site so sorry for any bad formatting
I wan't to add a subclass of UIViewController to a NSMutableDictionary which should be
I'm far from good at jQuery, to start with. I have read these two
I have read all that stuff about time saving and I wan't to create
I wan't to change the background color of a div dynamicly using the following
I wan't to load some data from my XML file using this function: public
I was using java mail to read Gmail as following - Session session =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.