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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:25:25+00:00 2026-06-11T19:25:25+00:00

I did some C programming couple decades ago. I am trying to re-learn the

  • 0

I did some C programming couple decades ago. I am trying to re-learn the language. I wrote this. I get something unexpected. When I change the ‘short int’ to ‘int’, it seems to work. Can anyone look at my code to see if anything is wrong with it or is this a compiler issue. I am using gcc on Linux.

#include <stdio.h>

int main(void) {

    short int age = 0;
    short int num_1_yr_olds = 0;
    short int num_2_and_3_yr_olds = 0;
    short int num_above_3_yr_olds = 0;

    while(1) {

        printf ("Enter age: ");
        scanf ("%d", &age);

        if (age < 1) {
            break;
        }

        switch (age) {
            case 1:
                ++num_1_yr_olds;
                break;
            case 2:
            case 3:
                ++num_2_and_3_yr_olds;
                break;
            default:
                ++num_above_3_yr_olds;
                break;
        }
    }
    printf ("Number of 1 year olds = %d\n", num_1_yr_olds);
    printf ("Number of 2 and 3  year olds = %d\n", num_2_and_3_yr_olds);
    printf ("Number above 3 year olds = %d\n", num_above_3_yr_olds);
}

Input

Enter age: 1
Enter age: 1
Enter age: 1
Enter age: -1

Output

Number of 1 year olds = -1
Number of 2 and 3  year olds = 0
Number above 3 year olds = 0

num_1_yr_olds value is getting messed up. I expected a 3, I get a -1. The value of num_1_yr_olds becomes a -1 regardless of input.

  • 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-11T19:25:26+00:00Added an answer on June 11, 2026 at 7:25 pm

    Your problem lies here:

    short int age = 0;
    :
    scanf ("%d", &age);
    

    You really need to ensure that your data types match your format string. The correct format specifier for a short int is %hd, not %d.

    Some compilers will actually check this and warn you.

    What’s likely to be happening is that the misalignment of data and format string is causing the short int to become the “wrong” value hence the counts are being screwed up.


    In more depth, for a two’s complement, little-endian architecture like x86, scanning an int into a short may put the least significant half into age and the most significant half into num_1_year_olds (if it’s adjacent to age in memory).

    Graphically, it may be clearer to think of it like this:

                             shorts in memory
                         +-----------------------+
    What scanf("%hd") =< |          age          | \
      will write to.     +-----------------------+  = What scanf("%d")
                         |    num_1_year_olds    | /    will write to.
                         +-----------------------+
                         | num_2_and_3_year_olds |
                         +-----------------------+
                         | num_above_3_year_olds |
                         +-----------------------+
    

    So, when you enter 1, age becomes 1 and num_1_year_olds becomes 0.

    Each time you do that, it increases num_1_year_olds because age is 1 but that will be overwritten by the scanf the next time you get input.

    When you finally enter -1 (all 1 bits in two’s complement), age becomes -1 and so does num_1_year_olds.

    Then, because age is less than one, the loop breaks, and the values are what you see: {-1, 0, 0}.

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

Sidebar

Related Questions

10 years ago I did some game programming and used OpenGL for the 3d
I'm trying to learn C by reading C Programming Language, 2nd Edition . I
Did some googling and couldn't find a clear answer on this. My assumption is
I did some programming for reading the data from Active Directory such as user
So I've never done any assembly programming (although I did some reading/reasoning out the
I took Computer Networking last semester and did some C programming in linux (using
I did some searching on this site and on google as well. But i
I am getting into GUI programming and did some research. Now not everything is
I did some programming with Java and C# and they both have great GUI
I did find some discussion on this topic, but it was beyond my current

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.