This question already has answers here:
How are arguments evaluated in a function call? [duplicate]
(2 answers)
Sequence points and partial order
(3 answers)
Closed 9 years ago.
in C this works as I expect
void foo(int a, int b ) { printf("%i,%i ",a,b ); }
int main()
{
int i=1;
foo(i++,i);
foo(i++,i);
foo(i++,i);
foo(i++,i);
return(0);
}
output: 1,2 2,3 3,4 4,5
Now, the following does not work as I would have guessed:
void foo(int a, int b ) { printf("%i,%i ",a,b ); }
int main()
{
int a[] = { 100,200,300,400,500,600,700 };
int i=0;
foo(a[i++],a[i]);
foo(a[i++],a[i]);
foo(a[i++],a[i]);
foo(a[i++],a[i]);
return(0);
}
It returns
100,100 200,200 300,300 400,400
Following the logic of the previous example, I would have expected
100,200 200,300 300,400 400,500
My first suspicion was that the increment was only called after the function call, however, as the first example shows, i is indeed incremented inside the function call, unless it is used as an index for the array.
I also tried the same using foo(*(a+(sizeof(int)i++)),(a+(sizeof(int)*i))); just to check, and it also acts like the second example.
The compiler is gcc version 4.6.3
My question is, why does it work when I'm using the variable i directly as the function parameter, but not when I use the variable i as an index for an array which is used as the function parameter?
Both of your code invokes undefined behavior. You may get either expected or unexpected result.
Compile your code with -Wall flag. Compiler should give the warning:
[Warning] operation on 'i' may be undefined [-Wsequence-point]
Read the C_FAQ - 3.8. It explains this issue pretty well.
Related
This question already has answers here:
func() vs func(void) in C99
(4 answers)
Difference between int main() and int main(void)?
(10 answers)
Closed 4 years ago.
What is the difference between these two programs?
The 1st one i am getting 4,3,2,1 and 2nd one is compilation error.
#include <stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
and
#include <stdio.h>
int main(void)
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
When you define a function like this:
int func() { ... }
It says that the function takes an indeterminate number of arguments and returns an int. So you can legally pass any number of arguments of any type (although you won't be able to access them).
When you define a function like this:
int func(void) { ... }
It says that the function takes no arguments. Attempting to pass any arguments to this function will result in a compile time error.
On a side note, recursively calling the main function is not a good idea. You're better off either calling another function which is recursive or just using a loop.
The appearance of a solitary void in a parameter list explicitly tells the compiler "this function takes no arguments".
In the first code example, calling main recursively is permitted since there is no argument list, which permits any number of arguments (this may have been changed in a more recent C standard than the one supported by your compiler; I forget the specifics).
Variables declared static are stored in the process' data section rather than in stack memory, so they persist beyond their scope and retain their value across function calls, so i decrements on each call until it reaches zero and your program hits the base case (don't enter the if statement), and terminates.
This question already has answers here:
Implicit int return value of C function
(7 answers)
Closed 6 years ago.
I am rather in doubts about getting some problems while coding with a few recursive functions. Now here I am giving a simple code with nested function call,so that I can point to my exact problem.
int main(void)
{
int i;
i=a();
printf("%d\n",i);
return 0;
}
int a()
{
return b();
}
int b()
{
return 9;
}
well,no problem at all,it gives output as 9. But if i redefine function a() as:
int a()
{
b();
int new=0; //not significant
}
Again it producing valid output, .i.e,
9
here, though I removed return keyword I was not getting any compilation error,neither the value of i in the output was wrong...(I expected garbage or something like that).
How these things are handled?
In your second piece of code:
int a()
{
b();
int new=0; //not significant
}
The return type of a() is int. However, in your code, you are not returning anything. This typically results in undefined behavior. So, you may get any value (you said you are getting 9, while using gcc on ideone, I am getting 0). It is undefined behavior, so no-one can say exactly what you'll get.
It will depend on the compiler, as pointed out in the SO Answer, but the compiler isn't obliged to document what happens, nor is it required to be consistent in the returned value. Different calls may return different values.
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.
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;
^
This question already has an answer here:
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 9 years ago.
void test(int x[static 10]);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11};
test(a);
return 0;
}
void test(int x[static 10])
{
printf("%d",x[9]);
}
I was looking for bizarre C statements. I found this one, But could not understand what is the use of static 10 in that statement. Is it same as int x[10]?
Another thing, you can use volatile also, in place of static e.g int x[volatile 10]
Anybody knows what is the use of this kinda declaration?
PS: Compiled using GCC 4.6.3,
It's a hint for the compiler telling that the x pointer parameter points to the first element of an array of at least 10 elements.
For example:
test(NULL); // undefined behavior