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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:16:35+00:00 2026-05-16T12:16:35+00:00

I am getting a weird problem while using scanf() to store data into a

  • 0

I am getting a weird problem while using scanf() to store data into a union.

Here’s my code

#include <stdio.h>

union Student
{
    float score;
    char grade;
};

int main(void)
{
    union Student jack;

    printf("Enter student score : ");
    scanf("%f", &jack.score);
    printf("Score : %f", jack.score);

    jack.score=0;

    printf("Enter student grade : ");
    scanf("%c", &jack.grade);
    printf("Grade : %c", jack.grade);

}

I get the following output

searock@searock-desktop:~/Desktop$ ./union
Enter student score : 12
Score : 12.000000Enter student grade : Grade :

but if I change my code to:

#include <stdio.h>

union Student
{
    float score;
    char grade;
};

int main(void)
{
    union Student jack;

    printf("Enter student grade : ");
    scanf("%c", &jack.grade);
    printf("Grade : %c\n", jack.grade);

    printf("Enter student score : ");
    scanf("%f", &jack.score);
    printf("Score : %f\n", jack.score);

}

It gives me the exact output [correct output]. I know this is not a good example, but can someone explain me what’s going wrong?

Modified Code : Add \n before format string. [scanf(“\n%c”, &ch);]

#include <stdio.h>

    union Student
    {
        float score;
        char grade;
    };

    int main(void)
    {
        union Student jack;

        printf("Enter student score : ");
        scanf("%f", &jack.score);
        printf("Score : %f", jack.score);

        jack.score=0;

        printf("Enter student grade : ");
        scanf("\n%c", &jack.grade);
        printf("Grade : %c", jack.grade);

    }
  • 1 1 Answer
  • 4 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-16T12:16:35+00:00Added an answer on May 16, 2026 at 12:16 pm

    In the first example, the first scanf() reads a number up to but not including the newline (assuming you don’t type just blanks and newlines; if you do, it will continue waiting for input until you provide a number or a non-number – where blanks etc do not count as either a number or a non-number). Then the second scanf() with the ‘%c‘ format specifier does not skip white space (unlike most other format specifiers) and reads the newline (assuming you typed a newline immediately after the end of the number; if you typed something else – white space or a letter, perhaps, it reads that character), completely ignoring anything you typed on the second line. (Indeed, when I run the code, it does not wait for me to enter anything for the second lot of input. Try typing “3.14+C” or “3.14C” and a newline.)

    In the second example, the first scanf() reads the first character. The second scanf() skips over white space, including newlines, until it finds either a number or something that is definitively not a number (like a letter).

    This is a beautiful demonstration of why most people avoid scanf(); it is very difficult to provide satisfactory control over it. You would be better off reading lines (perhaps using fgets(); definitely not using gets()) and then parsing them with sscanf(). You would then get sane behaviour for both examples.

    Note that your problem is completely unrelated to the use of a union; your code is fine in the way that is working. Your problem is all tied up with the use of scanf().

    Minor recommendation: when printing lines of output – other than prompts – include a newline at the end of the format string.

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

Sidebar

Related Questions

While using sqlite database in android I am getting a weird problem. I am
I've got a weird problem here: I'm getting a 500 error code from Apache
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
Getting a weird problem when uploading a spreadsheet via C#. Here's an example of
I am getting a weird problem while parsing a double value in managed C++.
I'm getting a weird problem in memory deallocation. I have the following code for
ok so I'm just getting started in scala.. ran into a weird problem with
I'm getting a weird problem in GlassFish 2.1 - I'm a beginner with this
I am Getting a weird problem. I am making an App which should run

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.