0

Ricently i was writing the code, and make a mistake like that:

int f () 
{
 . . .
}

int g;

int main()
{
 . . .
 if (f) { 
  // this is actually true
 }
}

Later, i've check and constructions like:

f;
g;

Does not give even any compiller errors. It is seems kind of logical, But what is the purpose of this? How it can be used? To check if some identifier is present? To force compiler not to optimize function or variable?

UPD: Just checked and behavior is the same using VS and minGW.

G0867532
  • 72
  • 1
  • 10
  • Use `volatile` If you want to force compiler to keep a variable without optimizing it away: All of this is discussed _[HERE](https://stackoverflow.com/questions/2219829/how-to-prevent-gcc-optimizing-some-statements-in-c)_ – ryyker Jan 10 '19 at 13:37
  • In VS a compiler gives a warning `function call missing argument list` (for function only). – montonero Jan 10 '19 at 13:41

1 Answers1

4

Functions decay to pointers to themselves, i.e. plain f is equal to &f. And when using a pointer in a condition like that, then it checks if the pointer is null or not.

In other words,

if (f) { ... }

is equal to

if (&f != NULL) { ... }

It might not make much sense in this case (as &f will always be non-null) but pointers to function are quite usable in other places.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Right. But what is the purpose of just writing `f; g; `or even `12;`? Why this is not an error? – G0867532 Jan 10 '19 at 13:36
  • 1
    @G0867532 it has no purpose. Modern compilers may issue a warning for this kind of stuff. – Jabberwocky Jan 10 '19 at 13:37
  • @Jabberwocky But why this is not even a warning? And why is is portable? – G0867532 Jan 10 '19 at 13:39
  • 2
    @G0867532 - Cause that's how the C grammar is designed. Most statements are in fact *expression statements* (`a=1` is also an expression). And well, `f`, `g` and `12` *are* valid expression, and can appear in expression statements. – StoryTeller - Unslander Monica Jan 10 '19 at 13:40
  • @G0867532 what is your platform? What are the compiler flags? It is portable because it's syntactically correct code. – Jabberwocky Jan 10 '19 at 13:40
  • 1
    @G0867532 Besides the statement keywords, a statement is just an expression followed by a semicolon. And it could be *any* expression. Some expressions (like assignment or calling function) makes sense, others (like plain `f`) does not. And for expressions that generate a result (which also happens if you call a function that doesn't return `void`), the result is simply ignored. Some compilers on some warning-levels will issue a warning, but it's not an error. – Some programmer dude Jan 10 '19 at 13:41
  • @Someprogrammerdude As far as i remember from theory of compilers, compiler should throw an error on expressions that have no sense, this is why i've asked this in a first place. But this is looks like rhis is not the case. – G0867532 Jan 10 '19 at 13:48
  • @G0867532 That's really language dependent. C allows any expression. – Some programmer dude Jan 10 '19 at 14:28