473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Strange behavior on an if statement.

I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

Given the following:

#include <stdio.h>

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}
This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

What is going on here?

Thanks in advance.

Chad

Dec 3 '05 #1
6 1490
Chad wrote:
int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}
This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'


Well, how about some exact compiler versions? I'm pretty sure it doesn't
have anything to do with Suse vs FreeBSD but rather with compiler vs
compiler.

Anyhow, I suspect that one of the compilers is C99 aware, while the other
(the one on FreeBSD) is targetting C89. C89 requires that all variables
are declared at the beginning of the block they are used in, which is why
it chokes on the declaration of 'b'.

BTW: never compile without high warning levels (-Wall for gcc), you just
miss too many possible errors.

Uli

Dec 3 '05 #2
Chad wrote:
I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

Given the following:

#include <stdio.h>

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}
This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

What is going on here?


In "C Classic" all variable declarations in a block had
to appear at the beginning, before any executable statements.
The original 1989 C Standard kept that rule.

The new 1999 C Standard relaxed the rule, allowing you
to intermix declarations and statements in any order you
please (as long as variables are declared before you try to
use them).

It seems you're using two gcc versions, one that obeys
the older declarations-first rule and one that permits the
newer intermixed style. Some gcc versions support both
versions of the Standard, depending on the compile-time
options. Check your gcc versions, and gcc's documentation.

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 3 '05 #3
Chad wrote:
I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

Given the following:

#include <stdio.h>

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}
This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

What is going on here?


I think C99 allows this C++ like form:

/* declaration */
int a = 0;
/* processing */
if (a == 0) {
...
/* declaration */
int b = 0;
/* processing */
....
but all older C specs only allows:

/* declaration */
int a = 0;
int b = 0;
/* processing */
if (a == 0) {
....
So, unless your compiler conforms to C99. It can generate errors
because you declare variables after doing some processing (in this case
an if and a printf).

Since currently few compilers are C99, you should put all declarations
at the top of your function. The corrected code that should compile
everywhere:

#include <stdio.h>

int main(void) {
int a = 0;
int b = 2;

if(a == 0) {
fprintf(stderr, "exiting \n");
}
return 0;
}

Dec 3 '05 #4

Ulrich Eckhardt wrote:
Chad wrote:
int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}
This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'


Well, how about some exact compiler versions? I'm pretty sure it doesn't
have anything to do with Suse vs FreeBSD but rather with compiler vs
compiler.

Anyhow, I suspect that one of the compilers is C99 aware, while the other
(the one on FreeBSD) is targetting C89. C89 requires that all variables
are declared at the beginning of the block they are used in, which is why
it chokes on the declaration of 'b'.

BTW: never compile without high warning levels (-Wall for gcc), you just
miss too many possible errors.

Uli


Hmmm..... fascinating. Here is more on what is going on;
Suse Linux 9.1 is running gcc v 3.3.3. When I enable full warnings, I
get:
iffy.c: In function `main':
iffy.c:9: warning: unused variable `b'

FreeBSD 4.8 is running 2.95.3. When I enable full warnings, I still the
the exact same error message:
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

Interesting. Thanks.

Chad

Dec 3 '05 #5
On 3 Dec 2005 14:51:24 -0800, in comp.lang.c , "Chad"
<cd*****@gmail.com> wrote:
I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;


you're not allowed to do this in C89 - variable declarations may not
occur after statements.
You /can/ do this in C++ and in C99, which some compilers partially
support. So this explains your problem.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 4 '05 #6
Eric Sosman wrote:
"C Classic"


"Australian Rules C"

http://webstore.ansi.org/ansidocstor...AS+3955%2D1991

--
pete
Dec 4 '05 #7

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

Similar topics

36
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
2
by: Yaro | last post by:
Hi UDB 8.2.2 Win I am little confused about IDENTITY behavior. create table aaa( c1 integer not null generated by default as identity, c2 integer, primary key (c1)
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
0
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
23
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.