This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
$void main()
{
int a=10,c;
c= ++a + ++a;
printf("%d",c);
}
this program Actualy Print Value Of c=24 but By Calculation we can say it should be
c=23 ,how it possible?
Your program has a bug -- you modify the same variable twice without an intervening sequence point. Fix the bug and the mystery will go away.
A very deep understanding of not just how the language works but how compilers work is required to understand why buggy code happens to do what it happens to do. I would just suggest not writing buggy code and, when you find a bug, simply fix it instead of trying to understand precisely why and how it broke.
My advice to you is to stop. You learned the right lesson -- code that triggers undefined behavior is unpredictable and frequently doesn't do what you might expect it to do. That's all you need to know about UB until you're an expert at using the language correctly.
'++' > '+'
Here post increment operation is done before.Since you gave it two times if does post increment two times so the value of 'a' becomes 12 and adds it up (12+12).So the final value is 24.
Related
This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
C - Off by one error, but no segmentation fault?
(3 answers)
No out of bounds error
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int array[10];
array[50] = 5;
printf("array[50] = %i \n", array[50]);
}
Above code works without a problem when is shouldn't. Why is that? This behaviour could make finding a potential bug in the program quite tricky.
The behaviour of an out of bounds array access such as this is undefined.
Undefined behaviour can manifest itself as the program working as you intended!
This does indeed make programming in C tricky, and the language draws criticism for being unsafe in this respect (cf. FORTRAN which grew up contemporaneously). Such pernicious behaviour can be obviated somewhat by adopting appropriate programming practices that have grown up since C was invented in the 1970s. Bounds checking software has also been invented which will weed out bugs such as this.
This question already has answers here:
Can you have a triple minus signs in C programming? What does it mean? [duplicate]
(5 answers)
Why doesn't a+++++b work?
(9 answers)
Closed 6 years ago.
I just started learning C Programming and I have a question based on expression evaluation.
If we have 3 variables, a,b, and c:
c=a+++b++;
c=a+++++b;
Why is the 1st expression valid and 2nd invalid?
It appears that the C compiler does interpret a+++ as a++ +, while +++b generates an error even if you put another variable before it.
In practice it is a very bad idea to write such expressions without spaces. You would confuse yourself when you come back to look at your code, and annoy anyone else looking at it :) Just rewrite:
a++ + b++
a++ + ++b
and everything will work as expected.
This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
int a=9,b=6,c=3;
printf("%d%d%d");
I executed this in code blocks 10.05. I got some garbage values. But in a website the output was given as 3 6 9. What is the correct one?
You will get garbage values, because you're not providing any arguments to the printf() call.
The correct code would be
printf("%d%d%d",c,b,a);
(to get the numbers in the order quoted)
The correct one is neither of the two you described. Since no values were passed to printf, only the formatters, whatever was on the stack at that moment (which is undefined) is passed.
What is the correct output of this statement?
This code invokes undefined behaviour and so there is no correct output. The output is undefined.
The code invokes undefined behaviour because the format string you passed to printf requires you to pass more parameters (3) than you supplied (0).
This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Debugging a program that crashes 10 times in different places
You are given a the source to a application which is crashing when
run. After running it 10 times in a debugger, you find it never
crashes in the same place. The application is single threaded, and
uses only the C standard library. What programming errors could be
causing this crash? How would you test each one?
Pointer error. You're trying to read/write an undefined pointer - do a find and replace for free(x) -> free(x);x=NULL.
Edit: Should also check for assignment; are you doing anything like this?
int *a;
a++;