Integer Overflows in C and Defenses Against Numeric Errors [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to research various integer overflow scenarios in C and I was wondering does the C language provide any defenses against numeric errors and are there any additional classes or libraries in the C language that can help with that? Also, can anyone give me an example of code that results in an integer overflow in C?

No, there are no defenses.
This overflows:
#include <limits.h>
#include <stdio.h>
const int a = INT_MAX - 2;
const int b = INT_MAX - 2;
printf("%d + %d = %d\n", a, b, a + b);
When I tested it it printed -6, but anything could happen I guess.

Related

Need to understand this If statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to understand more about this if condition the two sides of comparison and how it is compared:
int main()
{
unsigned short i;
if (i == '9' * 256 + '5')
{
/* Do stuff */
}
}
How are these compared?
Formally the behaviour of your code is undefined as you are reading the uninitialised variable i.
'9', 256, and '5' are all int types in C. So the right hand side is evaluated in int arithmetic, with the potential for overflow (it will not overflow with ASCII encoding).
i will be converted to an int type prior to the comparison.

Can I make a float number in a C program always round up [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Can I make a float number in a C program always round up
You can use the ceil() function. For example:
#include <stdio.h>
#include <math.h>
int main () {
float val1 = 1.6;
printf ("Round up to %.1lf\n", ceil(val1));
return(0);
}

Write c program to find mean of two numbers without using division [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;

Unexpected Behaviour of C program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Below is a C program which wants to multiply an integer by 5 using bitwise operation. But when i run this program it gives unexpected output. I know there is something i messing up which i could not see. Any help regarding this guys and girls ?
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int FiveTimes(int a)
{
int t;
t = a<<2 + a;
return t;
}
int main()
{
int a = 1, b = 2,c = 3;
PrintInt(FiveTimes(a));
PrintInt(FiveTimes(b));
PrintInt(FiveTimes(c));
return 0;
}
This is a question of "operator precedence": "<<" has a lower priority than "+" - so your code actually calculates a << (2 + a), while it should be (a << 2) + a. The latter is the fix.

I need to write a C program that adds two numbers each of 100+ digits? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Need to write a C program that adds two numbers each of 100+ digits..
I don't want the method of using arrays to do this.
Please suggest me how to store this numbers(atleast of 512 bit sized) and do the arithmetic operations?
You could use an arbitrary precision arithmetic library, such as GMP for that.
A quick C example:
#include <gmp.h>
mpz_t a, b;
const char *huge_decimal_num1 = "46819294521564960351683095841209562359068";
const char *huge_decimal_num2 = "6904120584864540916814056801234572451249681";
mpz_init_set_str (a, huge_decimal_num1, 10);
mpz_init_set_str (b, huge_decimal_num2, 10);
mpz_add (a, a, b); // a = a + b
printf("%s + %s = %s\n",
huge_decimal_num1, huge_decimal_num2, mpz_get_str (NULL, 10, a));

Resources