J
Joona I Palaste said:Good catch!
You missed one too ;-)
((printf)(("%d\n"), (a)));
The syntax of if, for, and while, to name a few, C statements, requires
parenthesis and I don't think it makes them look like functions.
In said:As for typing "return (0);" being typoed "reutrn (0);" and not being
caught, then..
1. Every viewer/editor[1] I use for C has syntax highlighting that show the
return as obviously wrong.
2. I've always used a compiler that warns when you use a non-prototyped
function.
In said:Fao said:When I first started programming in C, I was asked to write a macro, which
would calculate the number of elements in an array, but couldn't use any
functions. I knew I could do it like this:
#define NUM_ELTS(x) (sizeof(x) / sizeof(x[0]))
However, at the time, the parentheses that I was taught to use made me think
that sizeof was a function.
I would write that as
#define NUM_ELTS(x) (sizeof(x) / sizeof((x)[0]))
References to macro arguments should always be enclosed in
parentheses; otherwise you can run into some really nasty problems
with operator precedence. Remember that the argument is expanded into
the text, not into the expression tree.
In said:OTOH, I've seen ([sur]real code in [sur]real system) something like:
#define return(x) \
{ \
printf("%d\n", x); \
/* 10 or so lines of "debugging" code */
return 0; \
}
In order to use this "facility", return *had* to be coded with parentheses
around value. If that was a good practice is beside the point on hand.
Dan Pop said:In <qchmb.9047$9E1.40254@attbi_s52> "Glen Herrmannsfeldt"
That's because something else is *usually* following the right
parenthesis. And even when there is nothing else, many people prefer
to write the semicolon on a separate line.
I would write that as
#define NUM_ELTS(x) (sizeof(x) / sizeof((x)[0]))
References to macro arguments should always be enclosed in
parentheses; otherwise you can run into some really nasty problems
with operator precedence. Remember that the argument is expanded into
the text, not into the expression tree.
Care to provide a concrete example of a *correct* NUM_ELTS() invocation
where the extra parentheses make any difference?
Nonetheless, it is a function. Several years ago I examined some CGlen said:Yes, and also the difference between languages with, and without, reserved
words.
PL/I doesn't have reserved words, so that procedure calls must either be in
an expression, such as part of an assignment statement, or from a CALL
statement.
But C already confuses the issue between statements and function calls.
The exit() function is similar to the STOP statement in PL/I and Fortran,
though it could be considered related to return. Though I do agree that
having return as a function call would be very strange. It does seem that
exit would make more sense as a statement than a function.
In said:But C already confuses the issue between statements and function calls.
The exit() function is similar to the STOP statement in PL/I and Fortran,
though it could be considered related to return.
Though I do agree that
having return as a function call would be very strange. It does seem that
exit would make more sense as a statement than a function.
In said:Nonetheless, it is a function. Several years ago I examined some C
startup and shutdown. The start: label set up stuff from the command
processor arguments, argc and argv, and then called
'exit(main(argc,argv))'. exit() saved main()'s return value and, after
'closing' the C environment, returned it to the command processor (an
int value). Of course this is annecdotal and may have nothing to do with
C today.
In said:[email protected] (Dan Pop) said:I would write that as
#define NUM_ELTS(x) (sizeof(x) / sizeof((x)[0]))
References to macro arguments should always be enclosed in
parentheses; otherwise you can run into some really nasty problems
with operator precedence. Remember that the argument is expanded into
the text, not into the expression tree.
Care to provide a concrete example of a *correct* NUM_ELTS() invocation
where the extra parentheses make any difference?
I can't think of one, and there may not be one, but I'd much rather
add the parentheses than take the time to convince myself that they're
not necessary in this particular instance.
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.