is this a gcc extention or standard?

S

steve yee

#define M(i, j) ({ ++i; j; })

int main(int argc, _TCHAR* argv[])
{
int i = 0;
int j = M(i, 3);
return 0;
}

the above code can compile with gcc, but can't compile with most other
compilers. is it a gcc extension, or standerd feature?
 
R

Richard Heathfield

steve yee said:
#define M(i, j) ({ ++i; j; })

int main(int argc, _TCHAR* argv[])
{
int i = 0;
int j = M(i, 3);
return 0;
}

the above code can compile with gcc, but can't compile with most other
compilers. is it a gcc extension, or standerd feature?

It's an extension. The macro syntax is non-standard, and the _TCHAR type is
non-standard.

If most compilers can't compile it, that's a pretty reasonable clue that
it's non-standard! (The whole point - or at least /a/ whole point - of
standard code is that you can use any compiler you like.)
 
D

Dave Vandervies

#define M(i, j) ({ ++i; j; })

int main(int argc, _TCHAR* argv[])
{
int i = 0;
int j = M(i, 3);
return 0;
}

the above code can compile with gcc, but can't compile with most other
compilers. is it a gcc extension, or standerd feature?

If most other compilers don't like it, then either it's a GCC extension
or the other compilers you're using are broken.

What does gcc -W -Wall -ansi -pedantic say?


dave
 
P

Peter Nilsson

Richard said:
steve yee said:
#define M(i, j) ({ ++i; j; })

int main(int argc, _TCHAR* argv[])
{
int i = 0;
int j = M(i, 3);
return 0;
}

the above code can compile with gcc, but can't compile with most other
compilers. is it a gcc extension, or standerd feature?

It's an extension. The macro syntax is non-standard,

True, but there is a standard alternative...

#define M(i, j) (++(i), j)

Of course, I only _know_ this is a valid alternative because I know the
particular
gcc extension. [There are other forms on the extension which are not so
easy
to replicate in macro form.]
and the _TCHAR type is non-standard.

In the strictest sense, the identifier is standard in the sense that it
belongs
to a class of identifiers which are reserved for the implementation.

That said, it's use is obviously non-portable.
If most compilers can't compile it, that's a pretty reasonable clue that
it's non-standard!

More precisely, if most _conforming_ compilers (read... compilers
invoked in
conforming mode) can't compile it, it's a reasonable clue that it's
non-standard.
Many of the compilers I use will not compile trigraphs _unless_ they
are
invoked in conforming mode (or with trigraphs enabled.)
 
R

Richard Heathfield

Peter Nilsson said:
More precisely, if most _conforming_ compilers (read... compilers
invoked in
conforming mode) can't compile it, it's a reasonable clue that it's
non-standard.

If it's not playing to C rules, it's not topical here in clc, so it is fair
within the context of this newsgroup to take it as read that it will be
invoked in conforming mode.
 
M

Martin Ambuhl

steve said:
#define M(i, j) ({ ++i; j; })

int main(int argc, _TCHAR* argv[])
{
int i = 0;
int j = M(i, 3);
return 0;
}

the above code can compile with gcc,

Really? gcc tells me:
3: error: expected declaration specifiers or '...' before '_TCHAR'
4: warning: 'main' takes only zero or two arguments
: In function 'main':
6: warning: ISO C forbids braced-groups within expressions
6: warning: unused variable 'j'
: At top level:
3: warning: unused parameter 'argc'
but can't compile with most other
compilers. is it a gcc extension, or standerd feature?

It is fairly common to expect a legal declaration of main, so gcc's
flagging as an error is not really a gcc extension.

Braced groups in expressions are illegal in standard C, so gcc's
diagnostic is not really a gcc extension.

Helpful messages about unused variables are not mandated by the
standard, so I guess you could call those gcc extensions.
 
S

steve yee

sorry, for gcc, the code should be:

#define M(i, j) ({ ++i; j; })

int main(int argc, char* argv[])
{
int i = 0;
int j = M(i, 3);
return j;
}
What does gcc -W -Wall -ansi -pedantic say?

bash-2.03$ gcc -W -Wall -ansi -pedantic t.c
t.c: In function 'main':
t.c:7: warning: ISO C forbids braced-groups within expressions
t.c:9: warning: control reaches end of non-void function
bash-2.03$ gcc -v
Using built-in specs.
Target: sparc-sun-solaris2.8
Configured with: ../src-gcc/configure --prefix=/opt/gcc
--with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld
--enable-languages=c,c++
Thread model: posix
gcc version 4.1.1 20060421 (prerelease)
 

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
474,177
Messages
2,570,954
Members
47,507
Latest member
codeguru31

Latest Threads

Top