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 6682223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:40:51+00:00 2026-05-26T04:40:51+00:00

I read input from the keyboard using scanf() in a while loop and print

  • 0

I read input from the keyboard using scanf() in a while loop and print back on the screen using printf():

#include <stdio.h>
#include <conio.h>

int main(void)
{
    char ch;

    printf("enter your name");

    while(ch!='/n')
    {
         scanf("%c",&ch);
         printf("%c",ch);
    }
    getch();
    return 0;
}

I want to know how printf() prints the value on screen, because when the user presses the enter key, the program is out of the loop.

  • 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-05-26T04:40:52+00:00Added an answer on May 26, 2026 at 4:40 am

    It is not a good idea to check the value of ch (in while loop) before assigning it a value.

    • First before entering loop, it checks whether ch equals "\n" which it does not (hopefully).

      while(ch!='/n')
      
    • It enters the loop. scanf() executes, waits for input. Suppose, you enter "a" and then press ENTER.

      scanf("%c",&ch);
      
    • As a whitespace character is encountered, it continues to next line, which prints the character entered earlier.

    • Then you check again: this time ch==a, so it enters the loop again.
    • Now, scanf() is executed again, the input buffer already has "\n", it is read.

      scanf("%c",&ch); //This time INPUT BUFFER already has "\n"
                       //so it wont ask for user input.
      
    • "\n" read earlier, gets printed.

    • Now, ch == "\n", so the loop exits.

    The catch was that even though your read "a" from the input, the input buffer already had "\n" the second time the loop was executed. To avoid situations like that you should use

    scanf("%c\n", &ch)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading from the standard input using the read() system call but there's a
I am trying to read the input from a keyboard which i will use
I'm trying to read input from the terminal. For this, I'm using a BufferedReader.
What I want to do is the following: read in multiple line input from
I am trying to read values from an input file in Perl. Input file
I need to read some data from an input file and plot a graph
I want to read the file path from html input type=file (the entry selected
I want to read UTF-8 input in Perl, no matter if it comes from
My program reads a list of integers from user input [ keyboard] and calculates
I am trying to read input from a socket line by line in multiple

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.