473,732 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1502
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
3451
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 print 2.
3
2360
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
2
1801
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
8532
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
2
2681
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 across some wierd loop behavior. With the pasted code I receive the output that follows. I realize that the code is broken, because the inner loop fails to reset j for each iteration of the outer loop (the fix is commented out). I also know that...
0
3572
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 requests database, and query execution time exceeds 30 seconds, the following error reported: ===
6
2271
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 the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When adding toolbar button: tbButton.Click += new...
23
1623
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
160
5867
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
2230
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 ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
8944
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
8773
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
9445
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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
6733
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
6030
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();...
1
3259
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

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.