Reputation: 1
Why doesn't the code below get compiled ? For sake of brevity, I would like the code to be written in this manner, which seems syntactically OK, but Linux gcc compiler complains
#include <stdio.h>
void fn(int in, char ch, char* str);
int main()
{
fn(int i2 = 20, char ch2 = 'Z', char* str2 = "Hello");
printf("in2 = %d, ch2 = %c, str2 = %s\n", in2, ch2, str2);
return;
}
void fn(int in, char ch, char* str)
{
printf("int = %d\n", in);
printf("ch = %c\n", ch);
printf("str = %s\n", str);
return;
}
Upvotes: 0
Views: 163
Reputation: 27129
Because in c89 (ANSI C) you can declare variables only at the beginning of a block.
int main()
{
int i2 = 20; char ch2 = 'Z'; char* str2 = "Hello";
fn(i2, ch2,str2);
printf("in2 = %d, ch2 = %c, str2 = %s\n", in2, ch2, str2);
return;
}
EDIT
In c99, even thought you can in other parts, you can't decalre variables inside of expressions (like a function call).
Upvotes: 2
Reputation: 5924
You should declare your variables outside the function call and everything will be OK.
Upvotes: 0