language design - C function call followed by a comma separator -
this question has answer here:
- what comma operator , do? 8 answers
i reading material errors should avoided when writing c programs , came across following code:
#include <stdio.h> void foo(int param) { printf("foo called\n"); printf("%d\n",param); } int main() { return foo,(1); } the code above build without errors , warnings (it show warning when -wall activated) when run small program nothing displayed. function foo not called because of comma separator.
my question why c standard allow such syntax? shouldn't compiler issue error in case? in context syntax used in real use case?
thanks in advance,
pd: i'm using gcc 4.8.3
edit:
couldn't compiler in case detect situation , issue error instead of warning (as said appears when -wall enabled)
c simple, minimalist language programmer supposed know they're doing.
in c, types convert each other quite , original c didn't have function declarations -- programmer supposed know how call each function , parameters.
c programming language unix ,
"unix not designed stop doing stupid things, because stop doing clever things."—doug gwyn (https://en.wikiquote.org/wiki/unix)
as bonus, if compiler doesn't try smart, compilation can fast.
c++ takes different approach this.
as practical applications of , operator, take at: https://en.wikipedia.org/wiki/comma_operator
the comma operator has relatively limited use cases. because discards first operand, useful first operand has desirable side effects. further, because used outside of specific idioms, , mistaken other commas or semicolon, potentially confusing , error-prone. nevertheless, there circumstances commonly used, notably in loops , in sfinae (http://en.cppreference.com/w/cpp/language/sfinae). embedded systems may have limited debugging capabilities, comma operator can used in combination macro seamlessly override function call, insert code before function call.
Comments
Post a Comment