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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:34:02+00:00 2026-05-26T20:34:02+00:00

My code is as follows typedef struct { char name[15]; char country[10]; }place_t; int

  • 0

My code is as follows

typedef struct
{
 char name[15];
 char country[10];
}place_t;  

int main()
 {
 int d;
 char c;
 place_t place;
 printf("\nEnter the place name : ");
 scanf("%s",place.name);
 printf("\nEnter the coutry name : ");
 scanf("%s",place.country);
 printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
 scanf("%c",&c);
 printf("You entered %c",c);
 return 0;
 }

If I run the program, it prompts for place name and country name, but never waits for the character input from user.
I tried

fflush(stdin);
fflush(stdout);

Neither work.

Note : Instead of a character, if I write a similar code to get an integer or a float, it prompts for values and the code works just fine.

int d;
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf("%d",&d);

Why does this happen? Is there anything wrong in the code?

  • 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-26T20:34:03+00:00Added an answer on May 26, 2026 at 8:34 pm

    The problem is that scanf leaves the whitespace following entered non-whitespace characters in the stream buffer, which is what the scanf(%c...) then reads. But wait a second…

    In addition to being tricky to get right, such code using scanf is horribly unsafe. You’re much better off using fgets and parsing the string later:

    char buf[256];
    fgets(buf, sizeof buf, stdin);
    // .. now parse buf
    

    fgets always gets a full line from the input, including the newline (assuming the buffer is large enough) and you thus avoid the problem you’re having with scanf.

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

Sidebar

Related Questions

I have a code which uses bit-fields declared as follows typedef struct my{ const
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I have a struct defined in a header as follows: #define LC_ERR_LEN 300 typedef
I have a nested structure as follows: typedef struct { float mz_value; float int_value;
In rpc.h, the GUID structure is declared as follows: typedef struct _GUID { DWORD
I have something like this in my code typedef struct ts_fem_mesh { double **vertices;
Why can't I do something like this in c# (pseudo code follows) Interface1 {
I have code as follows: $(#item_select).change(function() { var params = $(#item_select option:selected).val(); $.post('/account/ar_form.php', {idata:
For some code as follows, opts, args = getopt.getopt(sys.argv[1:], c:, ... for o,v in

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.