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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:19:46+00:00 2026-05-14T06:19:46+00:00

Today I found the following: #include <stdio.h> int main(){ char x = 255; int

  • 0

Today I found the following:

#include <stdio.h>

int main(){
char x = 255;
int z = ((int)x)*2;

printf("%d\n", z); //prints -2

return 0;

}

So basically I’m getting an overflow because the size limit is determined by the operands on the right side of the = sign??

Why doesn’t casting it to int before multiplying work?

In this case I’m using a char and int, but if I use “long” and “long long int” (c99), then I get similar behaviour. Is it generally advised against doing arithmetic with operands of different sizes?

  • 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-14T06:19:46+00:00Added an answer on May 14, 2026 at 6:19 am

    char can be either signed or unsigned, depending on your compiler.

    In your case, it appears to be signed, and 255 is outside the range it can represent (likely, it can only represent numbers from -128 to 127).

    So the problem occurs when you assign 255 to your char variable – this results in an implementation-defined value, which in your case, appears to be -1.

    When you multiply -1 by 2, you get -2. No mystery there. The cast to (int) does nothing – types narrower than int are always promoted to int or unsigned int before any calculations are done with them.

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

Sidebar

Related Questions

I was looking at a css file today and found the following rule set:
I was playing around with include and extend today and found something I don't
The following is a failing unittest explaining a bug I found today: [TestMethod] public
Later today, I was scrolling through ejhon.com slides and I found out the following:
I was going through an article today when it mentioned the following: We've found
Today i tested my website on seo tool site and found following error: Your
Engaging in reverse engineering I found today the following snipet: <script id=idea type=text/x-jqote-template> <![CDATA[
I found the following method in our code base today, and it strikes me
Today I found the following disturbingly ambiguous situation in our code base: class Base
Today I found a strange jquery selector in the following code: $(this).find(+div.parent).hide(); I've searched

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.