Relational operator and the precedence of operators [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 2 years ago.
Improve this question
I executed a code in the C language, However I am unable to understand its output.
#include <stdio.h>
int main()
{
int a=5;
int b= ++a + 0!=0;
printf("%d %d",++a, b);
return 0;
}
The output for the above program is
7 1
I am unable to understand why it is so.

Order of operations causes this to be treated as:
int b = (((++a) + 0) != 0);
Therefore:
int b = (6 != 0);
6 isn't 0, so that has a value of true aka 1.
int b = 1;

Related

Integer Overflows in C and Defenses Against Numeric Errors [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
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.

Try to multiple huge int but get product of 1 [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 4 years ago.
Improve this question
I watched a tutorial which explain int:
but I just get 1:
Enter a:999999999999999999999999999999999999999999
Enter b: 9999999999999999999999999999999999999999999999999999999
a * b = 1
$ ./a.out
Enter a:87999999999999999999999999999999999999999999999999999999999
Enter b: 89999999999998798774334999999999994378969869869869458639534934578365
product = 1
What's the problem?
The reason for you getting 1 instead of -129542144 is that you multiply
999999999999999999999999999999999999999999 and 89999999999998798774334999999999994378969869869869458639534934578365
instead of 300000 and 200000.
Try the following short program you will get the result in the example, -129542144.
#include <stdio.h>
int main()
{
int a = 300000;
int b = 200000;
printf("%d\n", a*b);
return 0;
}
This is assuming that the datatype is int and that it is at least 32 bits.

Can someone please explain me this code? [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 4 years ago.
Improve this question
Can someone please explain me the concept of [\] here?
#include<stdio.h>
int f(int t[\]){
return t[0\] + t[2\];
}
int main(void){
int i,a[\] = {-2,-1,0,1,2};
i = f(a+2);
printf("%d",i);
return 0;
}
I see no reason for '\' in your C code. Maybe it is some leftover from copying the code since it is consistently proceeding the ] closing brackets.
If you remove '\' from your program it will compile and the function f(a+2)
will give you the sum of third and fifth element in the array a[].
#include<stdio.h>
int f(int t[])
{
return t[0] + t[2];
}
int main(void){
int i,a[] = {-2,-1,0,1,2};
i = f(a+2); // (a+2) -> { 0, 1, 2 }
printf("%d",i);
return 0;
}
Output:
2

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;

Why does this print 1? [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 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
int var=0;
for(; var++; printf("%d",var));
printf("%d", var);
}
Please explain to me this C code. How is the output 1?
You might be confused because of the wrong code indentation. Your code is:
for(; var++; printf("%d",var))
;
printf("%d", var);
So you always get the output of the second printf. As var is initialized to 0 and var++ (the for-condition) is always executed, you end up with var==1.

Resources