473,686 Members | 3,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ANSI C syntax ?

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

Mar 10 '07 #1
127 5471
bz****@hotmail. com wrote:
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.

Mar 10 '07 #2
bz****@hotmail. com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 10 '07 #3
SRR
On Mar 10, 2:06 pm, "santosh" <santosh....@gm ail.comwrote:
bz8...@hotmail. com wrote:
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.

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?

Mar 10 '07 #4
SRR wrote:
On Mar 10, 2:06 pm, "santosh" <santosh....@gm ail.comwrote:
bz8...@hotmail. com wrote:
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.


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.

Mar 10 '07 #5
bz****@hotmail. com writes:
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 ] )
Mar 10 '07 #6
SRR
On Mar 10, 5:37 pm, Zhou <zhouyan1...@gm ail.comwrote:
bz8...@hotmail. com writes:
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.
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 -

Mar 10 '07 #7
SRR wrote:
On Mar 10, 2:06 pm, "santosh" <santosh....@gm ail.comwrote:
bz8...@hotmail. com wrote:
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.


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.

Mar 10 '07 #8
Zhou wrote:
>
.... snip ...
>
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.
C99 is ANSI C. You mean not valid for C90.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #9
CBFalconer <cb********@yah oo.comwrites:
Zhou wrote:
>>
... snip ...
>>
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.

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...
Mar 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6551
by: Salisha Khan | last post by:
I have an instance method that takes an ansi string. Dim foo As Object foo(String a) The problem is Strings in VB .NET are unicode while in order for the method to work it must accept an Ansi string. I can convert to an ansi byte array but i can't pass a byte array, the method will only accept a string. I can't use DllImport or Declare because the method is not static. I couldn't find
3
5923
by: Jack | last post by:
Hi I am just trying to get a feel for what oracle SQL syntax there is that would not be covered at all in anyway from a functionality perspective in the standard ANSI SQL.. Any input is appreciated.. Thank you, Jack
3
2215
by: ColdCanuck | last post by:
Help! I'm trying to understand the new ANSI join syntax (after many years of coding using the old style). I am now working with an application that only understands ANSI syntax so I am struggling. My first (old style syntax) SQL statement below produces 60 rows: SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT, A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
5
1410
by: celsius | last post by:
Hi all, please forgive me if this already posted many times. i was reading peter van der linden's book expert C programming. on page number 188,he is discussing about implementing finite state machine in C. he explains as follows :- there are several ways to implement finite state
1
10973
by: ehchn1 | last post by:
Hi, Just curious. Would you use ANSI style table joining or the 'old fashion' table joining; especially if performance is the main concern? What I meant is illustrated below: ANSI Style select * from a join b on a.id = b.id
83
11597
by: sunny | last post by:
Hi All What is C99 Standard is all about. is it portable, i mean i saw -std=C99 option in GCC but there is no such thing in VC++.? which one is better ANSI C / C99? can i know the major difference between C99 & ANSI C standards?
6
4033
by: dunleav1 | last post by:
I have an application that uses the old join syntax instead of the SQL92 standards join syntax. I need to justify changing the code to the new standard. Is there any performance issue related to using the old syntax that are documented? Are there any other issues that I use to justify a code upgrade?
0
1237
by: Gary | last post by:
Is the above available? I've been looking at ISO/IEC 9075-2:2003 and the dynamic SQL syntax contained there works fine on Oracle when embedded in a COBOL application but I get errors such as: 43 SQL0104N An unexpected token "'inlist'" was found following "ALLOCATE". Expected tokens may include: "<identifier>".
2
2758
by: unauthorized | last post by:
Hi guys. I've been trying to compile the pcre library for use with the MSVC compiler, but the code wouldn't compile because it needed the func strtoq which isn't part of my C(++) library. Anyway, I figured I may as well get down and dirty with it and write a quick replacement (and it worked fine) but once I tried compiling it as a .c source, bad things happened: Compiler log: ------ Build started: Project: pcre, Configuration: Debug Win32...
0
8581
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8514
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6439
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.