This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
I got this code compiled in VC++ and GCC that produces different outputs and appreciate if someone can point me out where the things get wrong.
#include "stdio.h"
#define Cube(x) x*x*x
int main(void){
int x=5;
printf("%d\r\n", Cube(x++));
return 0;
}
In GCC, the displayed value is 210 (=5*6*7) and in VC++2010 its 125 (=5*5*5).
If I do this,
#include "stdio.h"
#define Cube(x) x*x*x
int main(void){
int x=5;
printf("%d\r\n", Cube(++x));
return 0;
}
VC++ prints 512 (=8*8*8) and GCC prints 392 (=7*7*8).
Appreciate if someone can say whats going on.
The line
printf("%d\r\n", Cube(x++));
is pre-processed to:
printf("%d\r\n", x++*x++*x++));
That is cause for undefined behavior.
printf("%d\r\n", ++x*++x*++x));
is also cause for undefined behavior.
See
Why the output of this program is 41? is it undefined behavior?
Why are these constructs (using ++) undefined behavior?
You can avoid the problem by converting Cube to a function. The program below is well behaved.
#include "stdio.h"
int Cube(int x)
{
return x*x*x;
}
int main(void)
{
int x=5;
printf("%d\r\n", Cube(x++));
printf("%d\r\n", Cube(++x));
return 0;
}
Related
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
I've tried writing this code and the two printf statements appear to give segmentation fault.
why isn't the output 5? what went wrong? and how to edit this code in a way such that it prints 5?
#include <stdio.h>
int* fun(int x){
x=5;
return &x;
}
int main() {
int x = 3;
int* p = fun(x);
printf("%d",*(fun(x)));
printf("%d",*p);
return 0;
}
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 1 year ago.
#include <stdio.h>
void main(void)
{
printf("hello");
}
I can’t seem to get the code to run. I tried using some extensions/terminal but the problem does not solve.
If you use int as the type of main, it would be a good idea to include a return statement. The following works in VS Code
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
You need int main (void), not void main(void).
See the Microsoft documentation.
This question already has answers here:
Can anybody tell me why this is happening in c language
(3 answers)
Closed 5 years ago.
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int a=10;
printf("%d"+1,a);
return 0;
}
Output. --- d
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int a=10;
printf("%d"+36,a);
return 0;
}
Output. --- p
Explain me why this happening... Whenever i change value of +1 it print different outputs...
The behaviour of your code is undefined.
"%d"+1 actually is adding 1 to a const char* pointer (the literal "%d" will decay to a const char* pointer under certain circumstances), which actually takes you to the d in that string literal! The printf formatter is therefore not appropriate for your arguments.
"%d"+36 is simply going to do very bad things indeed, since you don't own the memory 36 places on from the start of "%d"
Don't you mean something like printf("%d", a + 1); &c.?
Lastly, what the deuce is an Int? Didn't you mean int?
This question already has answers here:
Function returns value without return statement
(6 answers)
Closed 6 years ago.
I would like to know what is happening in this simple c code:
#include <stdio.h>
int something(int a, int b) {
int c = a * 3 + 4 * b;
}
int main()
{
int res = something(12, 54);
printf("%d\n", res);
return 0;
}
The function does not have the return, however "res" stores the value calculated in something.
Following I show the compilation command, the execution and the output:
$ gcc main.c
$ ./a.out
252
EDITED
I have changed the variables types from int to float, in relation to the #Paul Ogilvie comment, and it seems to return the last int stored:
#include <stdio.h>
float something(int a, int b) {
float res = a * 3 + 4 * b * 1.0;
}
int main() {
float res = something(12, 54);
printf("%d\n", res);
return 0;
}
$ gcc main.c
$ ./a.out
54
It is undefined behavior, see here:
Reaching the end of any other value-returning function is undefined behavior, but only if the result of the function is used in an expression.
Compiling your code with gcc -Wreturn-type gives the appropriate warning:
ret.c:4:1: warning: control reaches end of non-void function [-Wreturn-type]
This question already has answers here:
what is the return type of printf [closed]
(2 answers)
Closed 7 years ago.
Follwing code in C gives the output "hello5" ...how?
#include<stdio.h>
int main(){
int f = fun();
printf("%d",f);
return 0;
}
void fun(){
printf("hello");
}
What you see is undefined behavior.
The value f is never initialized and you are printing uninitialized variable which will lead to undefined behavior.
printf() returns number of characters successfully printed out so the count here is 5(hello) for printing hello.
You need to return this value if you want then you have defined behavior because you are initializing the variable f in main()
int func()
{
int j;
j = printf("hello");
return j;
}