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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:32:46+00:00 2026-06-13T08:32:46+00:00

#include<stdio.h> int main() { unsigned short a=-1; printf(%d,a); return 0; } This is giving

  • 0
#include<stdio.h>
int main()
{
unsigned short a=-1;
printf("%d",a);
return 0;
}
  1. This is giving me output 65535. why?
  2. When I increased the value of a in negative side the output is (2^16-1=)65535-a.
  3. I know the range of unsigned short int is 0 to 65535.
    But why is rotating in the range 0 to 65535.What is going inside?

    #include<stdio.h>
    int main()
    {
    unsigned int a=-1;
    printf("%d",a);
    return 0;
    }
    
  4. Output is -1.
  5. %d is used for signed decimal integer than why here it is not following the rule of printing the largest value of its(int) range.
  6. Why the output in this part is -1?
  7. I know %u is used for printing unsigned decimal integer.

    Why the behavioral is undefined in second code and not in first.?

    This I have compiled in gcc compiler. It’s a C code
    On my machine sizeof short int is 2 bytes and size of int is 4 bytes.

  • 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-13T08:32:48+00:00Added an answer on June 13, 2026 at 8:32 am

    In your implementation, short is 16 bits and int is 32 bits.

    unsigned short a=-1;
    printf("%d",a);
    

    First, -1 is converted to unsigned short. This results in the value 65535. For the precise definition see the standard “integer conversions”. To summarize: the value is taken modulo USHORT_MAX+1.

    This value 65535 is assigned to a.

    Then for the printf, which uses varargs, the value is promoted back to int. varargs never pass integer types smaller than int, they’re always converted to int. This results in the value 65535, which is printed.

    unsigned int a=-1;
    printf("%d",a);
    

    First line, same as before but modulo UINT_MAX+1. a is 4294967295.

    For the printf, a is passed as an unsigned int. Since %d requires an int the behavior is undefined by the C standard. But your implementation appears to have reinterpreted the unsigned value 4294967295, which has all bits set, as as a signed integer with all-bits-set, i.e. the two’s-complement value -1. This behavior is common but not guaranteed.

    • 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() { unsigned char c; c = 300; printf(%d,c); return 0; }
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int
#include<stdio.h> int main() { int a,b; a=a+b; printf(%d,a); return 0; } what should be
#include<stdio.h> void function(int); int main() { int x; printf(Enter x:); scanf(%d, &x); function(x); return
I have the following program #include <stdio.h> int main(void) { unsigned short int length
#include<stdio.h> int main() { unsigned int a=6; int b=-20; (a+b>6)?puts(>6):puts(<=6); return 0; } The
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
#include<stdio.h> #include<string.h> int main() { unsigned char *s; unsigned char a[30]=Hello world welcome; memcpy(s,&a,15);

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.