ANSI C syntax ?

B

bz800k

Hi

Does this code satisfy ANSI C syntax ?

void function(void)
{
int a = 2;

a = ({int c; c = a + 2;}); /* <<-- here !! */
printf("a=%d\n", a);
}


Thanks !
tsuyoshi
 
S

santosh

Hi

Does this code satisfy ANSI C syntax ?

void function(void)
{
int a = 2;

a = ({int c; c = a + 2;}); /* <<-- here !! */
printf("a=%d\n", a);
}

No. Neither is it valid C99. You can't put a statement block within an
expression context.
 
R

Richard Heathfield

(e-mail address removed) said:
Hi

Does this code satisfy ANSI C syntax ?

void function(void)
{
int a = 2;

a = ({int c; c = a + 2;}); /* <<-- here !! */

No. The right operand of an assignment-operator must (eventually) be an
expression that yields a value. A compound statement does not yield a
value.
 
S

SRR

No. Neither is it valid C99. You can't put a statement block within an
expression context.


Even I did think that its against Ansi C standards as compound
statements dont yield any value. But when I compile it it compiles
well and executes to give a result of 4.
I am using Dev-CPP compiler and I am using Windows OS.I'm surprised to
find that not even warnings are generated though I expected an error.
Is it a bug with the compiler?
 
G

Guest

SRR said:
Even I did think that its against Ansi C standards as compound
statements dont yield any value. But when I compile it it compiles
well and executes to give a result of 4.
I am using Dev-CPP compiler and I am using Windows OS.I'm surprised to
find that not even warnings are generated though I expected an error.
Is it a bug with the compiler?

It is not a bug with the compiler. GCC (which is used by Dev-CPP) is
not a conforming compiler for any standardised language unless
specific options are passed to it; at the minimum, to get a C
compiler, use the -ansi (or -std=c89) and -pedantic (or -pedantic-
errors) options.
 
Z

Zhou

Hi

Does this code satisfy ANSI C syntax ?

void function(void)
{
int a = 2;

a = ({int c; c = a + 2;}); /* <<-- here !! */
printf("a=%d\n", a);
}


Thanks !
tsuyoshi

I think this is invalid for ANSI C, since ANSI C require each
declaration for variable to be in place before all execution
statement.

But however it will work on, compiled by gcc. But if you try
Microsoft's compiler, it won't work, since they have no interest to
implement the new C99 syntax.

The compiler did not give any warning or error it is because the
compiler suppose such syntax, namely, declaration after the execution
code. For instance

for( int i = 0; i < count; i++ )

the code above is not valid for ANSI C but valid for C99, and gcc
think it is OK.

in ANSI C it should be;

int i;

/* some other code perhaps */

for( i = 0; i < count; i++ )

The reason that your code give the result a = 4 is below:

when C find the assignment "=", it require its right operand to return
a value, and assign this value to the left operand.

In this case it find a block, "{...}". It will execute this block of
code and get the value by this execution. Every assignment will return
a value, which is the value it assigned to its left operand. So after

c = a + 2;

been executed, the first "=" get the value returned by "c = a + 2",
namely, 4. This is what you got.

To illustrate this more clearly, see below:

#include <dirent.h>

/* some codes */

DIR *dp;

if( ( d = opendir( argv[ 1 ])) == NULL )

/* other codes */

This will work though the left operand of "==" is an assignment
statement, not a variable or constant. Because
d = opendir( argv[ 1 ])

will return the value of

opendir( argv[ 1 ] )
 
S

SRR

I think this is invalid for ANSI C, since ANSI C require each
declaration for variable to be in place before all execution
statement.

But however it will work on, compiled by gcc. But if you try
Microsoft's compiler, it won't work, since they have no interest to
implement the new C99 syntax.

The compiler did not give any warning or error it is because the
compiler suppose such syntax, namely, declaration after the execution
code. For instance

for( int i = 0; i < count; i++ )

the code above is not valid for ANSI C but valid for C99, and gcc
think it is OK.

in ANSI C it should be;

int i;

/* some other code perhaps */

for( i = 0; i < count; i++ )

The reason that your code give the result a = 4 is below:

when C find the assignment "=", it require its right operand to return
a value, and assign this value to the left operand.

In this case it find a block, "{...}". It will execute this block of
code and get the value by this execution. Every assignment will return
a value, which is the value it assigned to its left operand. So after

c = a + 2;

been executed, the first "=" get the value returned by "c = a + 2",
namely, 4. This is what you got.
This explanation will not be correct in this respect.
In C assignment is an expression and therefore evaluate to the
assigned value.
But statements are not expressions!!
In the code given in the question, a compound statement is enclosed
within paranthesis and is "assigned" to an int variable!
Note that a statement is just a statement and does not yield any
value!!
To illustrate this more clearly, see below:

#include <dirent.h>

/* some codes */

DIR *dp;

if( ( d = opendir( argv[ 1 ])) == NULL )

/* other codes */

This will work though the left operand of "==" is an assignment
statement, not a variable or constant. Because
d = opendir( argv[ 1 ])

will return the value of

opendir( argv[ 1 ] )- Hide quoted text -

- Show quoted text -
 
C

CBFalconer

S

santosh

SRR said:
Even I did think that its against Ansi C standards as compound
statements dont yield any value. But when I compile it it compiles
well and executes to give a result of 4.

I am using Dev-CPP compiler and I am using Windows OS.I'm surprised to
find that not even warnings are generated though I expected an error.
Is it a bug with the compiler?

No. gcc, unless otherwise instructed, compiles it's own C-like
language, called GNU C. GNU C provides numerous extensions over ISO C,
and is far more forgiving of less than correct constructs. To compile
your code with strict conformance to C90, use the -ansi and -pedantic
flags. For partial conformance to C99, use -std=c99 and -pedantic. It
also helps to set diagnostic output to a reasonable level with -Wall
and -Wextra at a minimum.

Consult the gcc reference, either an online info page, or at the gcc
website, for all the details for harnessing gcc. It's a powerful
compiler and it's options are well worth a detailed study. It'll
really help you get the most out of your compilation process.
 
Z

Zhou Yan

CBFalconer said:
C99 is ANSI C. You mean not valid for C90.

ANSI C is referred to C89.
In 1989 ANSI published the first C standard.
In 1990 ISO adopt this C89 as ISO 9899:1990, so called C90
In 1999 ISO published the ISO 9899:1999, so called C99

ANSI only published C89 and C99 is ISO C.

When reffered to ANSI C we always refer to C89 or equivalent, C90.

For instance, in GCC, -ansi is the same as -std=c89 option.

The last thing...we should not discuss the Standard in this
newsgroup...
 
R

Richard Heathfield

santosh said:
Erm, isn't ANSI C synonymous with C90?

Not any more. C89 and ANSI C were once synonymous, and C90 and ISO C
were once synonymous. But C99 is the current C Standard as far as both
ANSI and ISO are concerned.

(And C90 is the current C Standard as far as almost everyone else is
concerned.)
 
S

santosh

The last thing...we should not discuss the Standard in this
newsgroup...

Wherever did you get that wild idea from?

We *do* discuss the C Standards in this group. In fact, that's the
only thing we discuss :)
 
S

santosh

Richard said:
santosh said:


Not any more. C89 and ANSI C were once synonymous, and C90 and ISO C
were once synonymous. But C99 is the current C Standard as far as both
ANSI and ISO are concerned.

So ANSI adopted ISO's C99 version of the Standard?

Thanks for clearing up that confusion.

<snip>
 
R

Richard Heathfield

santosh said:
Zhou Yan wrote:


Wherever did you get that wild idea from?

What's so wild about it? He's right.
We *do* discuss the C Standards in this group. In fact, that's the
only thing we discuss :)

No. The C Standard is discussed in comp.std.c. Here, we discuss
programming in C. The C Standard defines C, which is why it crops up so
much here, but the topic of the comp.lang.c group is the /usage/ of the
language, not its definition.
 
R

Richard Heathfield

santosh said:
Richard Heathfield wrote:


So ANSI adopted ISO's C99 version of the Standard?

Yes. It took them a little while, but they finally adopted it some time
in April or May 2000. I can't quite recall what took them so long -
probably something as silly as only one of them having a pen with which
to sign the relevant paperwork, and he was ill on the day of the
meeting. Or perhaps the dog ate their courtesy copy of C99 and they had
to beg ISO for a new one. I don't know.
 
M

Mark McIntyre

ANSI C is referred to C89.

No. ANSI also ratified C99, and copies of the standard distributed in
the US bear the ANSI logo. Indeed folk round here would mostly
understand ANSI C and ISO C to be synonyms, especially in a USAnian
context.
When reffered to ANSI C we always refer to C89 or equivalent, C90.

No.
For instance, in GCC, -ansi is the same as -std=c89 option.

Mayve, but thats a gcc-ism not a C feature.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Z

Zhou Yan

SRR said:
This explanation will not be correct in this respect.
In C assignment is an expression and therefore evaluate to the
assigned value.
But statements are not expressions!!
In the code given in the question, a compound statement is enclosed
within paranthesis and is "assigned" to an int variable!
Note that a statement is just a statement and does not yield any
value!!

So, I see that I am wrong...but could you please kindly explain why
that code will produce the out put "a = 4", I managed to compile that
code fragment and really got "a = 4", which make me think about that
the right oprand {...} yield a value 2.
If it is just

a = ( c = 4 );

that is clearly what happend. But does that code

a = ( { int c; c = 2 + a; } );

also give us a = 4?

Thanks
 
D

Default User

Mark said:
especially in a USAnian
^^^^^^^

The above is not a word. The term you're searching for is "American".
If we're going to be pummeling the non-native speakers for using silly
abbreviations, then we ought to refrain from doing the same ourselves.




Brian
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top