Please explain this C program [duplicate] - c

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.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=11;
clrscr();
printf("%d");
getch();
}
Output=11
How the output is 11 even I am not mentioned the variable name in the printf function.

The 11 is on the stack because of the b variable, and your printf() function is looking on the stack for a value on the stack because that's where variables get passed.
If you add a c=47, you'll probably get 47. But this is undefined behavior.

This is called "undefined behavior", which means that program can do just about anything.
What is actually happening in this case is that both variables and function parameters are put on the stack. Since you aren't passing the parameter that printf is expecting, it ends up pulling something else off the stack, which is your b variable.
But because it is undefined behavior, if you had a different compiler, a different CPU, or even different compile options, such as a higher optimization level, you could get very different results.

Related

What will happen if I don't use return 0; In C [duplicate]

This question already has answers here:
Why does the main function work with no return value?
(2 answers)
What should main() return in C and C++?
(19 answers)
Closed 2 years ago.
Read all this to understand my problem: In my collage test there was a question like this:
What is the error in this code:
#include<stdio.h>
int main(){
printf("Hello World");
}
Answer of above code: compiling Error
But This is what I got when I try it practically:
As per books we have to return 0; when we use int main()
can any one give perfect or understandable Explanation.
Absence of return statement will never raise compiling error. Especially for main function, that handles differently. If it has type int but have doesn't return anything, a program acts like main returns 0.
Theoretically, it can be compiler-dependent, but in you case, it's likely an error in test.
You should return 0 but not doing that is not a compiling error.
UPD: as #EricPostpischil mentioned in comments, compilers don't raise an error because they have a mode set by default that allows to compile non-standard code, so question in your test is incomplete. It has to specify compiler you are using etc.

Difference between "void main" and "int main [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 7 years ago.
I study C both from textbooks and websites. Somewhere the sample codes begin the program execution by "void main" and somewhere they begin the function with "int main". What's the difference, really? They both give the same result. Is there anything that makes them slightly different? If so, please mention what does "void" does, and what "int" does.
The int main() and the void main() all do the same, which is process the main process of the program.
void main() it means that the functions main() does not return a value.
The difference is int main() is the only right way of calling it, since every program, when called, returns an "error message" that the OS interprets, in which case, closing the program with a return 0; tells the process calling your program that it ended without a problem.
The ANSI standardization of C says that the main function must return an integer. But if you program embedded systems, for instance, then you will use void main. Please se this question for elaboration: What should main() return in C and C++?

Why is this not giving a compile error [duplicate]

This question already has answers here:
C function defined as int but having no return statement in the body still compiles
(7 answers)
Closed 8 years ago.
#include <stdio.h>
int func()
{
}
int main(void)
{
printf("%d\n",func());
return 0;
}
the function "func()" is of "int" return type but is not returning anything. When the function is called in the print function, why is it giving an output 0? And why does it compile successfully although the function definition does not agree to the function code?
If you enable warnings you will see a diagnostic. Traditionally, C implicitly allowed all functions to return int. The behavior of the return value is undefined, so it is not guaranteed to be 0. The behavior could change on different compilers or on different platforms or with different optimization flags or even just by adding or removing other unrelated code. The reason you are probably seeing 0 is because you are running unoptimized and whatever previous value happens to be in the register or stack position is 0. This is pure chance, and relying on the behavior will ultimately result in bugs in your code.

Why printf() is printing 0 instead of 10 in following code? [duplicate]

This question already has answers here:
C++ global and local variables
(8 answers)
Closed 8 years ago.
If i compile and run the following code, it is printing 0 instead of 10.
#include<stdio.h>
main()
{
int Var=10;
{
char Var=Var;
printf("%d",Var);
}
}
Why this is printing 0 and why not 10 ?
Because in local declaration
char Var=Var;
the right occurrence of Var refers to the local Var, not the upper one. As Alk commented, this is undefined behavior to assign from an uninitialized variable.
So your declaration does not initialize Var at all, i.e. Var contains garbage. In your particular case, that garbage happens to be 0.
BTW, having two homonyms Var in the same function is really bad taste.
As this answer suggests, you should compile with gcc -Wall -Wshadow then you'll get some warnings on your code. (also add -g to get debug information to be able to debug with gdb)
Assuming you are using gcc, you would want to turn on -Wshadow (http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html).
This would pop up an error, at the inner variable having same name as outer. The zero is a random value. It could print any garbage in that place.
try this
#include<stdio.h>
main()
{
int Var1=10;
{
char Var=Var1;
printf("%d",Var);
}
}

Changing value of const int using using pointer [duplicate]

This question already has answers here:
Const variable changed with pointer in C
(5 answers)
Closed 9 years ago.
I wrote a C program in gcc and it's giving me very unexpected output. The code is :
#include<stdio.h>
int main(){
const int x=10;
int *p=&x;
*p=11;
printf("%d\n",*p);
printf("%d",x);
printf("\n%u\n",p);
printf("%u", &x);
}
Here output is:
11
10
37814068
37814068
Why do p and &x give the same address(37814068) but different values(5,10)??
Modifying a const variable (directly or through a pointer) invokes undefined behavior. You may not get the same result in another machine.
In the C Standard mofiying a constant is an undefined behaviour. It means that anything could happen it depends on the machine you're running and the compiler your using. In some cases constants are puted on read-only memory and modifying it's value will cause the program to crash.
Exemple of the error generated by the GCC compiler:
error: assignment of read-only location '* p'
*p = 11;
^

Resources