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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:48:08+00:00 2026-06-15T14:48:08+00:00

#include<stdio.h> int main(void) { unsigned short a,e,f ; // 2 bytes data type unsigned

  • 0
#include<stdio.h>
int main(void)
{
    unsigned short a,e,f ;    // 2 bytes data type
    unsigned int temp1,temp2,temp4; // 4 bytes data type
    unsigned long temp3; // 8 bytes data type
    a=0xFFFF;
    e=((a*a)+(a*a))/(2*a); // Line 8
    //e=(((unsigned long)(a*a)+(unsigned long)(a*a)))/(unsigned int)(2*a);    

    temp1=a*a;
    temp2=a*a;
    temp3=(unsigned long)temp1+(unsigned long)temp2; // Line 14
    temp4=2*a;

    f=temp3/temp4;

    printf("%u,%u,%lu,%u,%u,%u,%u\n",temp1,temp2,temp3,temp4,e,f,a);
    return(1);
}

How do I fix the arithmetic (At Line 8 by appropriate typecasting of intermediate results) so that overflows are taken care of ? Currently it prints 65534 instead of expected 65535.

Why is the typecast necessary for Line 14 ?

  • 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-15T14:48:10+00:00Added an answer on June 15, 2026 at 2:48 pm

    You have to promote the types before performing the overflowing operation. In line 8 that would be multiplication, so

    e = ((unsigned) a * a + (unsigned) a * a) / (2 * (unsigned) a); 
    

    Note that promoting only one operand of such symmetrical operations as * is enough. You can use (unsigned) a * (unsigned) a if you wish, but (unsigned) a * a will work as well.

    This will take care of multiplication, which will no longer overflow. However, now the addition will overflow. While 32-bit unsigned is enough for a * a, it is not enough for a * a + a * a. For that you’ll need unsigned long (assuming it is larger). You can formally promote the first operand of + to unsigned long

    e = ((unsigned long) ((unsigned) a * a) + (unsigned) a * a) / (2 * (unsigned) a); 
    

    (again, promoting only the first operand of + is enough, meaning that the second multiplication can be left in unsigned).

    The above looks a bit too convoluted, and to make it look cleaner you can use unsigned long in the first multiplication from the very beginning

    e = ((unsigned long) a * a + (unsigned) a * a) / (2 * (unsigned) a); 
    

    or you can just use unsigned long everywhere to make it look even cleaner

    e = ((unsigned long) a * a + (unsigned long) a * a) / (2 * (unsigned long) a); 
    

    The very same problem appears in your temp1 = a * a; lines. They will overflow for the very same reason. You have to do

    temp1 = (unsigned) a * a;
    temp2 = (unsigned) a * a;
    

    to avoid overflow, i.e. promote a before multiplication.

    And that is exactly what you correctly do in line 14, i.e. promote operands of + before addition, although promoting only one operand is perfectly sufficient

    temp3 = (unsigned long) temp1 + temp2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following program #include <stdio.h> int main(void) { unsigned short int length
#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;
Consider this C code: #include stdio.h int main(void) { int count = 5; unsigned
I have the following code #include<stdio.h> #include<string.h> int main (void) { unsigned int pqr
I have this simple program #include <stdio.h> int main(void) { unsigned int a =
#include <stdio.h> #include <limits.h> int main(void){ printf("Type Size Min Max\n----------------------------------------------------------------------\n"); printf("%-20s%-10d%-20ld%-20ld\n", "long", sizeof(long), LONG_MIN,
#include <stdio.h> int main(void){ unsigned a[3][4] = { {2,23,6,7}, {8,5,1,4}, {12,15,3,9} }; printf(%u,*((int*)(((char*)a)+4))); return
The following program has undefined behavior: #include <stdio.h> int main(void) { unsigned int x
#include<stdio.h> int main(void) { signed int a=-1; unsigned int b=1; int c= a+b; printf(%d\n,c);

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.