int main()
{
int value = 4321;
int *ptrVal = &value;
printf("%d %d",++value,(*(int*)ptrVal)--);
return 0;
}
How does pre-increment/post increment works in above print statement ?
And why is answer 4321 4321 ?
You are modifying the object value twice between two sequence points: you are invoking undefined behavior. Undefined behavior means your program can print 4321 4321, print 42 or even just crash.
A correct version of your program would be:
int value = 4321;
int *ptrVal = &value;
++value;
(*ptrVal)--; // no need to cast to int *
printf("%d %d", value, *ptrVal); // same as printf("%d %d", value, value);
Of course you don't need any temporary pointer to achieve this.
The code above is just broken. It is unspecified how it will work or what the answer will be. You need a sequence point between modifications or modifications and accesses.
Related
When you run the C code below, you get a different results almost everytime(None of them are altogether correct).
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
int s[10][4]={{202201,90,13,21},{202202,32,24,12},{202203,23,53,76},{202204,21,43,64},{202205,12,45,89},{202206,98,99,100},{202207,12,0,0},{202208,0,0,0},{202209,98,12,34},{202210,23,65,34}};
int ave[10],sum[10];
printf("No. MA EN CN\n");
for(i=0;i<10;i++)
{
for(j=0;j<4;j++)
printf("%4d",s[i][j]);
printf("\n");
}
for(i=0;i<10;i++)
{
for(j=1;j<4;j++)
sum[i]=s[i][j]+sum[i];
printf("%d\n",sum[i]);
}
return 0;
}
What's happening? What's the mechanics behind it? How can I fix it?
Both sum and ave are uninitialized arrays. That means that when you cumulative add to s[i] your first add to an uninitialized value which invokes Undefined Behaviour. Here you were just getting a random but valid value.
You need to ensure that s[i][j] is initialized to 0 before it is first used:
int ave[10],sum[10] = {0}; // initialize the full sum array to 0
or:
for(i=0;i<10;i++)
{
sum[i] = 0; // ensure an initial value of 0
for(j=1;j<4;j++)
sum[i]=s[i][j]+sum[i];
printf("%d\n",sum[i]);
}
The issue most likely is in this statement.
sum[i]=s[i][j]+sum[i];
The "sum" integer array hasn't been initialized prior to this statement, so the value "sum[i]" on the right-hand side of the statement probably holds undefined data. Either initialize your sum array or revise the statement if some other type of evaluation should occur.
I have a problem with a void pointer in use with a ternary condition operator.
In case of:
int x = 5;
void * pointer = &x;
printf("x: %d \n", 1 == 1 ? (*((int*)pointer)) : (*((char*)pointer)));
It writes to console the number 5, whitch is correct activity. But when I change in last line char to double:
printf("x: %d \n", 1 == 1 ? (*((int*)pointer)) : (*((double*)pointer)));
Then it writes 0 and I don't know why. I know I can use if-else instead of it but I'm just curious why it is working that way. Could anyone help me?
The result of a ternary expression is the common type of the two branches. In the case of one branch being an int and the other a double then the result will be a double as well.
And you don't print a double value, you attempt to print an int value. Mismatching format specifier and value will lead to undefined behavior.
For more information see e.g. this conditional operator reference.
You should use %lf instead of %d
int x = 5;
void * pointer = &x;
printf("x: %lf \n", 1 == 1 ? (*((int*)pointer)) : (*((char*)pointer)));
Does anyone know why my program prints -69 in below case? I expect it to print default/ garbage value of uninitialized primitive data types in C language. Thanks.
#include<stdio.h>
int a = 0; //global I get it
void doesSomething(){
int a ; //I override global declaration to test.
printf("I am int a : %d\n", a); //but I am aware this is not.
a = -69; //why the function call on 2nd time prints -69?
}
int main(){
a = 99; //I re-assign a from 0 -> 99
doesSomething(); // I expect it to print random value of int a
doesSomething(); // expect it to print random value, but prints -69 , why??
int uninitialized_variable;
printf("The uninitialized integer value is %d\n", uninitialized_variable);
}
What you have is undefined behavior, you can't predict the behavior of undefined behavior beforehand.
However it this case it's easy to understand. The local variable a in the doesSomething function is placed at a specific position on the stack, and that position does not change between calls. So what you're seeing is the previous value.
If you had called something else in between, you would have gotten different results.
Yeah.. Your function reuse two times the same segment of memory.. So, the second time you call "doesSomething()", variable "a" will be still "random".. For example the following code fill call another function between the two calls and you OS will give you differen segments:
#include<stdio.h>
int a = 0; //global I get it
void doesSomething(){
int a; //I override global declaration to test.
printf("I am int a : %d\n", a); //but I am aware this is not.
a = -69; //why the function call on 2nd time prints -69?
}
int main(){
a = 99; //I re-assign a from 0 -> 99
doesSomething(); // I expect it to print random value of int a
printf( "edadadadaf\n" );
doesSomething(); // expect it to print random value, but prints -69 , why??
int uninitialized_variable;
printf("The uninitialized integer value is %d\n", uninitialized_variable);
}
For this part I actually just need to scanf a value into a variable and then put it into an equation to spit out a number.
Analogue input (-5V to 5V):
1
e is -1073750280
the code:
printf("Analogue input (-5V -5V):\n");
scanf("%d",e);
printf("e is: %d \n", e);
The number that e prints out as changes every time I run the program. I figure it has to do with memory but I cannot figure out what.
these are the variable declarations:
uint16_t *pointer;
int e,d,i;
C passes all of its function parameters by value, not by reference. That means functions cannot modify their parameters directly:
void NoChange(int i) {
printf("Before: %d\n", i);
i = 10; // Changes only the local copy of the variable.
printf("After: %d\n", i);
}
main() {
int n = 1;
printf("Start: %d\n", n);
NoChange(n);
printf("End: %d\n", n);
}
Output:
Start: 1
Before: 1
After: 10
End: 1
If you want a function to change the contents of a variable, you need to pass its address. Then the function can modify the data at that address, which effectively modifies the variable:
void Change(int *i) {
printf("Before: %d\n", *i);
*i = 10; // Changes the memory that i points to.
printf("After: %d\n", *i);
}
main() {
int n = 1;
printf("Start: %d\n", n);
Change(n);
printf("End: %d\n", n);
}
Output:
Start: 1
Before: 1
After: 10
End: 10
So in order for the scanf() function to store data in a variable, you need to pass it the address of that variable, like this:
int e;
scanf("%d", &e);
scanf("%d", &e); is the correct answer as others have pointed out. The scanf function needs a pointer to where you want it to store the data, otherwise it doesn't know where your variable is.
Since scanf was expecting a pointer it converted the uninitialized value stored inside e to a pointer and stored the result there. This is undefined behaviour, a phrase you will see in the C/C++ section a lot, and you shouldn't do it.
Also, since scanf did nothing to your variable, printf was printing the uninitialized value of e, which is why you were getting the unexpected result.
scanf("%d",&e);
You are missing &.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
#include <stdio.h>
int main()
{
int x=100;
x=x++;
printf("x : %d\n",x); //prints 101
return 0;
}
What is the reason for output 101? I think output should be 100.
This is Undefined Behaviour, due to Sequence Points.
Between consecutive "sequence points" an object's value can be
modified only once by an expression
The end of the previous epxression x=100; is one sequence point, and the end of x=x++; is another.
Basically, your expression has no intermediate 'sequence points', yet you're modifying the value of X twice. the result of this is Undefined Behaviour: Basically, anything could happen: You could get 100, 101 or 42...
Here is what I believe you're looking for:
int main(){
int x=100;
printf("x : %d\n",x++); // Will print 100 and then increment x
return 0;
}
You're incrementing x before printing it - so this is the reason for the output 101.
You're doing the same operations as x = x; x++;
You're incrementing x after assigning it to x, effectively:
The x=x++ effectively becomes (assign x to x prior to increment) then (increment x)
This is going to give the same effect as if you were to wrote:
x = x;
++x; // Increment after the assignment
This should leave x as 101 after the x=x++; line.
You're incrementing the same x you're printing - here it doesn't matter whether post- or pre-incrementing.
x=x++ would produce the same result as x=++x.
If you would like to assign it another object do this:
#include<stdio.h>
int main(){
int x=100;
int y=x++;
printf("y : %d\n",y); //prints 100
printf("x : %d\n",x); //prints 101
return 0;
}
This is what is happening
#include <stdio.h>
int main()
{
int x=100;
x=x++; // This is original code however I have rewritten this as below
x=x;
x++;
printf("x : %d\n",x); //prints 101
return 0;
}
as you can see
x=x++;
can be rewritten as
x=x;
x++;
hence the result no surprises ...
The effect is that the value of x won't change.
x++ works like this:
Take a reference of the value.
Allocate a new integer and store the value into it.
Increment the reference.
Return the temporary integer.
In C++ it would look like this:
int PostIncrement(int& x)
{
int y = x;
x = x + 1;
return y;
}
The operator precedence is not lost in this way and the assignment is done after the increment.