Some odd place to declare

R

Ravi

Is this valid in C99:

int main(void)
{
printf("%s\n", char *p = foo());
return 0;
}

char *foo()
{
return "abc";
}
 
S

santosh

"]
Is this valid in C99:

int main(void)
{
printf("%s\n", char *p = foo());
return 0;
}

char *foo()
{
return "abc";
}[/QUOTE]

No. It will not compile. Even if you correct it to do so, it still
invokes undefined behaviour.
 
K

Kenny McCormack

"]
Is this valid in C99:

int main(void)
{
printf("%s\n", char *p = foo());
return 0;
}

char *foo()
{
return "abc";
}

No. It will not compile. Even if you correct it to do so, it still
invokes undefined behaviour.[/QUOTE]

I would imagine that the truth of the later assertion depends very much
on exactly what specific changes are made in order to get it to compile.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Kenny said:
santosh <[email protected]> said:
"]
Is this valid in C99:

int main(void)
{
printf("%s\n", char *p = foo());
return 0;
}

char *foo()
{
return "abc";
}

No. It will not compile. Even if you correct it to do so, it still
invokes undefined behaviour.

I would imagine that the truth of the later assertion depends very much
on exactly what specific changes are made in order to get it to compile.[/QUOTE]

Correct, more so considering the question is about C99, and C99 does not
allow for implicit function declarations. Using an undeclared identifier,
such as printf or foo here, is a syntax error in C99. Trying to fix that by
declaring char *printf(); or something equally silly obviously means the
behaviour is undefined, but assuming you don't do that, I see no UB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top