codeblocks gives incorrect result as output - c

My code :
#include <stdio.h>
int main(){
int a = 5;
int b,c;
if (a==4){
b = 6;
c = 7;
}
printf("%d %d\n",b,c);
}
I know result should be 0 0, but codeblocks giving answer 50 8. I tried online compiler and I got answer 0 0. So what is problem with codeblocks? I am using latest version of codeblocks and gcc c compiler. How can I avoid this kind of problems in future?

The values of b and c variables are garbage because if() condition become false. It means, it is undefined behaviours.
C11 section 6.7.9 Initialization :
Paragraph 10:
If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate.

I know result should be 0 0
Your guess is wrong
The variables b,c are automatic (storage specifier), uninitialized variables -> indeterminate values -> no guarantee on their values -> UB!
In your case you got 50,8 you might as well get other values / print garbage/ crash...

The values for b and c are undefined in this program as they dont get initialised before they get printed

Okay looking at your code and analyzing it step by step,
#include <stdio.h>
int main(){
int a = 5;//You've assigned the value of 5 to integer a.
int b,c;//You've declared two integer variables "b" and "c".
if (a==4)/*Compiler checks whether the if condition is true,which in your case is not*/
{
b = 6;/*Assigning value 6 to the b variable but no effect as the if condition of your's is not satisfied*/
c = 7;/*The same above condition applies here.*/
}
printf("%d %d\n",b,c);/*Here your trying to print the values of b and c,
But remember you have not given them any value. So the compiler automatically prints the garbage value(random value). It need not be 0 all the time.*/
return 0;//You forgot this step btw:)
}

Related

Why a syntax like "a=b--- // a newline, line ends without ;" does not produce syntax error?

What is the output of this program and how?
#include<stdio.h>
int main(){
int a=0,b=10;
a=b---
printf("the value of b=%d",b);
printf("the value of a=%d",a);
return 0;
}
In your code, writing
a=b---
printf("the value of b=%d",b);
is same as
a = b---printf("the value of b=%d",b);
which is an expression with undefined behavior, as an attempt is made to change and use the value of variable b in two subexpressions without a sequence point in between. Hence, the output of this code cannot be justified.
Without the above problem, in general, the syntax is similar to
x = (y--) - (<return value of printf() call>)
which is a perfectly valid syntax.
Note:
Why a = b---<something> is treated as a = (b--) - <something> and not a = b - -- <something> is due to the maximal munch rule.
Strictly speaking, as others have said since this is undefined behaviour, the result could be anything . And while they're right, it doesn't explain why you get this specific answer in this specific compiler.
In practice b will usually be printed as 9, or 10 depending on whether the compiler does the decrememnt first or the printf first.
printf returns the number of characters printed. Which I think is 16 or 17 in this case.
So this is like writing a = (b--) - 16; b is 10 before the decrement and it's a post decrement so this is the value used.

can syntax of while loop be a variable with no value assigned to it in the beginning?

int fact,num[55];
k=0;
while(fact)
{
num[k++] = fact%1000;
fact/=1000;
}
What does this code mean?
If fact is initialized to 1 then how does the above code run? i.e int fact=1;
What does the following code mean?
This means your code invokes undefined behavior (as you are accessing the uninitialized variable fact) .
If fact is initialized to 1 then how does the above code run? i.e int fact=1;
Loop will iterate only once. 1/1000 = 0 and hence fact will become false after first iteration.

Output is not giving garbage value

Good evening guys,
This is a little program shown below. I'm seeking for garbage value of b in every execution, but get the same answer.
The code is simple as follows:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a = 300, b,c;
if (a>=400)
b=300;
c=200;
printf("\n%d\n%d",b,c);
getch();
}
The o/p in Codeblocks is as follow
2
200
but if I remove the line
b=300;
keeping the semicolon, then it gives garbage values.
Why is this contradiction happening?
Reading uninitialized variable follow below rules,
Static variable are by default initialized to zero means local static or file scope variable (global one).
Non-static variables which local to function are indeterminate. Reading them prior to assigning a value results in undefined behavior. compiler is free to do any thing. It can be zero, it can be the value that was in there, it can crash the program. you have absolutely no guarantees.
It will simply give you the last value that was stored in that position on the stack (or in that register if the variable happens to be stored in a register).
Also undefined might be different between different compilers.
Here 2 is garbage only, you should print the value of b before 'if-statement' and see if it matches with the last print statement. If it matches then both are garbage values and no change occurred to them because of the 'if-statement'.
The question about how compiler generates a garbage value is unpredictable/random.
Explanation:
Step 1: int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.
Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be failed.
Step 3: c = 200; here variable c is initialized to '200'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.

why does this C code outputs 1

Is there any reason why:
void function_foo(){
int k[8];
function_math(k, 8);
}
void function_math(int *k, int i){
printf("value: %d", k[i]);
}
The main execute function_foo();
The output will be 1? There's no initialization for elements of matrix k.
Maybe something with the length of int in memory?
I am new to C concepts, the pointers and everything.
It is undefined behaviour to evaluate k[8], since k only has 8 elements, not 9.
There is little point arguing about the consequences of undefined behaviour. Anything could happen. Your program is not well-formed.
(Note that it would even be undefined behaviour to evaluate k[0], ..., k[7], since they are unini­tia­lized. You have to write to them first, or initialize the array, such as int k[8] = { 1, 2 };.)
This is the value which is at the memory position after the last element of your declared array.
If you run this code in a week again, it could be 42 or anything else which is stored at this time on this specific memory address. Also a segmentation fault could be possible in this case.
You are stepping out of the bounds of the array k.
To access the last element of k try using function_math(k, 7)
The array is also not initialized so the values inside will be undefined.

Variable inside if block is assigned to zero even if the if expression evaluates to false in C

#include<stdio.h>
int main(void) {
int a = 123,b,c;
if(a>300) {
b=200;
}
c=4556;
printf("b = %d\n",b);
return 0;
}
Now as if block evaluates to false so if block should not get executed, but the code is printing value of b as 0.
Moreover if the value of c isn't assigned after the if block is over then the code shows some garbage value for b.
Can anyone tell me why is this happening like this, as far as I know b must show some garbage value for the first case also.
Unless you initialise your variables, you will not know for sure what their values are so this is not remotely surprising. If you want your b variable to contain a meaningful value, then initialise it with that value.

Resources