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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:15:13+00:00 2026-06-02T20:15:13+00:00

int ch; do { ch = getchar(); printf(ch = %d\n,ch); }while(ch != ‘\n’ &&

  • 0
    int ch;
    do
    {
            ch = getchar();
            printf("ch = %d\n",ch);
    }while(ch != '\n' && ch != EOF);

In this case the output for input 1234 is

    ch = 49
    ch = 50
    ch = 51
    ch = 52
    ch = 10

But on giving it like this

    char chs[100] = {0};
    unsigned int u = 0;
    int sign = 1,k=0;
    if(scanf("%[- 0-9]",&chs[0])== 1)
    {
             int ch;
             do
             {
              ch = getchar();
              printf("ch =  %d\n",ch);
             }while(ch != '\n' && ch != EOF);

In this case output for input = 1234 is

    ch =10

Why is this happening here ???
Can someone please explain … Why isn’t the output same as before ??

How scanf is exactly working here ?

  • 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-02T20:15:15+00:00Added an answer on June 2, 2026 at 8:15 pm

    The call to scanf reads data from the stream. When you later try to read from the stream again by using getchar the characters that were read by scanf have already been consumed and are not read again.

    Also, see the documentation for scanf for valid values of the format string.

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

Sidebar

Related Questions

#include <stdio.h> int main() { char read = ' '; while ((read = getchar())
The following code is showing strange behaviour. While giving the input if I press
#include <stdio.h> // copy input to output // my version int main() { int
int max(int N, ...){ int* x = &N; x = x + 1; int
Right now I'm trying this: #include <stdio.h> int main(int argc, char *argv[]) { if
I found this piece of code on Wikipedia. #include <stdio.h> int main(void) { int
I've got problem with this peace of code, it should change lower case letters
#include stdafx.h #include stdio.h #include math.h int main() { float c; printf(Type c); scanf(%f,
I understand what is happening when I do this more explicitly using getchar() ,
I was confused by the getchar() function, so I searched this great website and

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.