LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-30-2012, 09:37 PM   #1
hunter_king
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
why gcc complained "‘else’ without a previous ‘if’"


hi all:
i wrote a very simple C code, but when i Compiled, gcc Complain that "error: ‘else’ without a previous ‘if’". here'is the code
Code:
#define TRUE 1
#define FALSE 0

int compare (char opt_1, char opt_2)
{
        if (opt_1 == '*' || opt_1 == '/')
                return TRUE;
        else
                return FALSE;
}
 
Old 12-31-2012, 04:01 AM   #2
business_kid
Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 6,375

Rep: Reputation: 557Reputation: 557Reputation: 557Reputation: 557Reputation: 557Reputation: 557
It obviously sees a problem with the if. placing of punctuation is vital.

I'm on a deferred year and unable to check mysefl. Is it if . . .else or if. . . then . . . else?
 
Old 12-31-2012, 05:17 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by hunter_king View Post
hi all:
i wrote a very simple C code, but when i Compiled, gcc Complain that "error: ‘else’ without a previous ‘if’".
There's nothing wrong with the code you posted; if GCC is indeed spewing an error, then it lies elsewhere.
 
Old 12-31-2012, 05:54 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,774

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
You've misunderstood something. I tried it, and got only a warning:

Code:
hunter_king.c: In function 'compare':
hunter_king.c:4: warning: unused parameter 'opt_2'
 
Old 12-31-2012, 07:52 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 778
Blog Entries: 2

Rep: Reputation: 199Reputation: 199
As it happens in this example the "else" is unnecessary.
Code:
if (opt_1 == '*' || opt_1 == '/')
    return TRUE;

return FALSE;
 
Old 12-31-2012, 08:17 AM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,084

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by hunter_king View Post
here'is the code
Since that code wouldn't give the error you described, I assume you simplified the actual code before posting (often a good practice) but failed to retest the simplified code to make sure it had the same error as the original (clearly a bad practice).

Consider this incorrect program similar to what you posted. With some compiler, I think it would give the error you reported (though with my copy of gcc it gives a different error). I'm intending this to show that the difference between what you posted and what you tested might be subtle.
Code:
#define TRUE 1;
#define FALSE 0;

int compare (char opt_1, char opt_2)
{
        if (opt_1 == '*' || opt_1 == '/')
                return TRUE;
        else
                return FALSE;
}
 
1 members found this post helpful.
  


Reply

Tags
gcc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] HELP ME! URGENT - Entered "exit" before previous command had completed run dajobi Linux - Newbie 3 03-18-2012 08:12 PM
Multiple issues including: "Previous Installation hasn't been completed" sisterdelirious Linux - Software 6 09-16-2010 04:49 AM
tar "Error exit delayed from previous errors" using "-C /" ToK Linux - Software 2 03-19-2008 06:49 AM
using raid1 with mdadm to "restore" system to previous state predder Linux - Enterprise 0 08-08-2006 07:57 PM
Porting to Fedora gcc 4.0 -parsing backward slash("\") comma(",") code_blew Programming 1 04-26-2006 04:07 PM


All times are GMT -5. The time now is 01:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration