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

  • Home
  • SEARCH
  • 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 6974107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:11:39+00:00 2026-05-27T17:11:39+00:00

the code is as follows: #include <stdio.h> main() { int m=123; int n =

  • 0

the code is as follows:

#include <stdio.h>
main()
{
    int m=123;
    int n = 1234;
    short int a;
    a=~0;
    if((a>>5)!=a){
        printf("Logical Shift\n");
        m=0;
    }
    else{
        printf("Arithmetic Shift\n");
        m=1;
    }
    scanf("%d",&a);
    printf("%d\n", m);
}

after the line scanf("%d",&a); the value of m becomes 0.

I know it may be caused by the scanf: a’s type is short and the input’s type is int. But How can this affect the value of m ?

Thanks a lot !

  • 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-27T17:11:40+00:00Added an answer on May 27, 2026 at 5:11 pm

    The most likely reason for m being 0 in your snippet is because you assign m to have this value in the body of your if-statement, but since the code contains undefined behavior no one can say that for sure.


    The likely story about passing a short* when scanf expects an int*

    Assuming sizeof(short) = 2 and sizeof(int) == 4.

    When entering your main function the stack on which the variables reside would normally look something like the below:

      _
     |short int (a)   : scanf will try to read an int (4 bytes).
     |_ 2 bytes       : This part of memory will most
     |int       (n)   : likely be overwritten
     |                :..
     |
     |_ 4 bytes
     |int       (m)
     |
     |
     |_ 4 bytes
    

    When you read a %d (ie. an int) into the variable a that shouldn’t affect variable m, though n will most likely have parts of it overwritten.


    Undefined Behavior

    Though it’s all a guessing game since you are invoking what we normally refer to as "undefined behavior" when using your scanf statement.

    Everything the standard doesn’t guarantee is UB, and the result could be anything. Maybe you will write data to another segment that is part of a different variable, or maybe you might make the universe implode.

    Nobody can guarantee that we will live to see another day when UB is present.


    How to read a short int using scanf

    Use %hd, and be sure to pass it a short*.. we’ve had enough of UB for one night!

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

Sidebar

Related Questions

I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);
My code is as follows: #include <stdio.h> struct MyData { int id; char msg[255];
I had the following code : #include<stdio.h> void main() { int * a; int
My program is as follows; #include <stdio.h> #include <string.h> int main() { char string[]
Tried the following code : #include<stdio.h> int main() { int *p,*q; p = (int
First, I wrote a c++ code as follows: #include <cstdio> int main() { int
I have witten a simple c program as follows: #include <stdio.h> #include <regex.h> int
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
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
I have code as follows: $(#item_select).change(function() { var params = $(#item_select option:selected).val(); $.post('/account/ar_form.php', {idata:

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.