Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

  • 1
  • 2
  • 3
  • 4
  • 5
  • 10
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

GCC errors with compiling C code.

Mon Nov 06, 2017 4:20 am

OK I'm starting a new thread that will deal with errors and other problems that I will run across and need help with solving as I'm learning C and GCC. The point of this thread is to keep my learning C thread nice and clean as that one deals with learning C and which beginners books and manuals are recommend and where to find them:
viewtopic.php?f=20&t=120229

I found this site and starting reading and using it and tried compiling this example code here with GCC:
http://www.loirak.com/prog/ctutor.php
if (numcandy > 1000)
{
   printf("The store does not have that much candy in stock!");
}
else
   .... print out the cost .....

I get this error in Bash when compiling:
numcandystock.c:1:1: error: expected identifier or ‘(’ before ‘if’
if (numcandy > 1000)
^~
numcandystock.c:5:1: error: expected identifier or ‘(’ before ‘else’
else
^~~~

I double checked to see if I typed this correctly and I did. I have no clue what errors means, and no idea on how to fix it.
Thank you.
 
NTMBK
Gerbil XP
Posts: 371
Joined: Sat Dec 21, 2013 11:21 am

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 4:46 am

Did you enclose that code in a function? For instance void main(){ } . The part you've quoted is a badly formed snippet on its own (especially that last line, which isn't C at all!), and won't compile. Try taking the sample from the top of the page, and modifying it with a conditional.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 5:07 am

Thanks. I'm going to have to reread those sections again, but slower this time as lately I've been reading stuff faster then I can understand and having to read over. :roll:

Would it help if I look at correct source files to learn by example?
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 6:42 am

whm1974 wrote:
Thanks. I'm going to have to reread those sections again, but slower this time as lately I've been reading stuff faster then I can understand and having to read over. :roll:

The compiler is pedantic and unforgiving. That's its job. :wink:

Unlike interpreted languages like Python, you can't just execute isolated fragments of code in C. All code must be part of a function, and your program's code execution begins at the top of the function main(). You can't compile anything until you've got a complete module, and you can't build a program until you've got a module with a main() function in it. Simple programs typically consist of a single module containing main() and any other functions it needs.

The fragment you tried to compile isn't a complete module, it's an isolated example of use of the if/else construct. Furthermore, the ".... print out the cost ....." bit is meant to be filled in with whatever code is used to print the cost; it isn't valid C code.

Typical structure of a simple C program:
#include header files for any prebuilt libraries used

declarations of global variables

definitions of functions (subroutines)

main() function

The main() function does not necessarily need to appear at the bottom, but that saves you the trouble of needing to "forward declare" the other functions it calls. (This is explained near the bottom of the page you linked,)

Once you get past the simple "newbie" concepts like this, something else to keep in mind: The compiler will sometimes get confused by syntax errors, rendering subsequent errors irrelevant/meaningless. So if a particular error isn't making sense, try fixing the error(s) above it and compiling again.

whm1974 wrote:
Would it help if I look at correct source files to learn by example?

Yes.

The examples further up the page you linked (immediately under "Your First Program" and "Data Types and Printf"), and further down (last 3 code blocks on the page) are complete, correct C source files.

Tip: If it doesn't start with some #includes and have a main() function somewhere, it's not a complete C program!
Nostalgia isn't what it used to be.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 7:15 am

just brew it! wrote:
whm1974 wrote:
Thanks. I'm going to have to reread those sections again, but slower this time as lately I've been reading stuff faster then I can understand and having to read over. :roll:

The compiler is pedantic and unforgiving. That's its job. :wink:

Unlike interpreted languages like Python, you can't just execute isolated fragments of code in C. All code must be part of a function, and your program's code execution begins at the top of the function main(). You can't compile anything until you've got a complete module, and you can't build a program until you've got a module with a main() function in it. Simple programs typically consist of a single module containing main() and any other functions it needs.

The fragment you tried to compile isn't a complete module, it's an isolated example of use of the if/else construct. Furthermore, the ".... print out the cost ....." bit is meant to be filled in with whatever code is used to print the cost; it isn't valid C code.

Typical structure of a simple C program:
#include header files for any prebuilt libraries used

declarations of global variables

definitions of functions (subroutines)

main() function

The main() function does not necessarily need to appear at the bottom, but that saves you the trouble of needing to "forward declare" the other functions it calls.

Once you get past the simple "newbie" concepts like this, something else to keep in mind: The compiler will sometimes get confused by syntax errors, rendering subsequent errors irrelevant/meaningless. So if a particular error isn't making sense, try fixing the error(s) above it and compiling again.

whm1974 wrote:
Would it help if I look at correct source files to learn by example?

Yes.

The examples further up the page you linked (immediately under "Your First Program" and "Data Types and Printf"), and further down (last 3 code blocks on the page) are complete, correct C source files.

Tip: If it doesn't start with some #includes and have a main() function somewhere, it's not a complete C program!

Thank you very much JBI, the last bit is very helpful. Thanks for pointing that out.
 
chuckula
Minister of Gerbil Affairs
Posts: 2109
Joined: Wed Jan 23, 2008 9:18 pm
Location: Probably where I don't belong.

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 8:11 am

Yeah... I'm assuming your original thread is serious and you aren't just trolling here but I highly recommend starting with an online tutorial instead of just throwing snippets of code into a text file.

There are a bunch of them out there but W3Schools is pretty well-known: https://www.w3schools.in/c-tutorial/

You'll note that their very first example includes the minimal #include <stdio.h> and a compilable main function (it doesn't specify argc & argv but that's acceptable for a program that does not take command line parameters).
4770K @ 4.7 GHz; 32GB DDR3-2133; Officially RX-560... that's right AMD you shills!; 512GB 840 Pro (2x); Fractal Define XL-R2; NZXT Kraken-X60
--Many thanks to the TR Forum for advice in getting it built.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 8:18 am

chuckula wrote:
Yeah... I'm assuming your original thread is serious and you aren't just trolling here but I highly recommend starting with an online tutorial instead of just throwing snippets of code into a text file.

The snippet he posted came from an online tutorial. He didn't realize the snippet couldn't be compiled on its own, since it was just an isolated example of using one particular type of C statement.

I think we're past that speedbump now. :wink:
Nostalgia isn't what it used to be.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 8:25 am

chuckula wrote:
Yeah... I'm assuming your original thread is serious and you aren't just trolling here but I highly recommend starting with an online tutorial instead of just throwing snippets of code into a text file.

There are a bunch of them out there but W3Schools is pretty well-known: https://www.w3schools.in/c-tutorial/

You'll note that their very first example includes the minimal #include <stdio.h> and a compilable main function (it doesn't specify argc & argv but that's acceptable for a program that does not take command line parameters).

Think you for taking for taking me seriously chuckula. You guys providing me with encouragement and moral support of and in itself is going a long way with helping me keeping at this.
 
chuckula
Minister of Gerbil Affairs
Posts: 2109
Joined: Wed Jan 23, 2008 9:18 pm
Location: Probably where I don't belong.

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 8:47 am

whm1974 wrote:
chuckula wrote:
Yeah... I'm assuming your original thread is serious and you aren't just trolling here but I highly recommend starting with an online tutorial instead of just throwing snippets of code into a text file.

There are a bunch of them out there but W3Schools is pretty well-known: https://www.w3schools.in/c-tutorial/

You'll note that their very first example includes the minimal #include <stdio.h> and a compilable main function (it doesn't specify argc & argv but that's acceptable for a program that does not take command line parameters).

Think you for taking for taking me seriously chuckula. You guys providing me with encouragement and moral support of and in itself is going a long way with helping me keeping at this.


I will say that even most online tutorials that at least show working code have a tendency to not explain the process of actually compiling the program to produce a functional executable very well. Especially when you leave the realm of simple programs that only include a single source code and header file and when you need to start linking in external shared libraries (e.g. the need for -lm for the basic math library as one trivial example).
4770K @ 4.7 GHz; 32GB DDR3-2133; Officially RX-560... that's right AMD you shills!; 512GB 840 Pro (2x); Fractal Define XL-R2; NZXT Kraken-X60
--Many thanks to the TR Forum for advice in getting it built.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Mon Nov 06, 2017 9:25 am

OK I did a couple of examples in the link chuckula pasted and compiled using gcc from bash. Thank you, that was very helpful.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:02 pm

OK two of errors I'm having are typos and skipping stuff I'm supposed to be typing in the text editor. I suppose this is rather common mistakes made by beginners? Well if nothing else I will certainly improve my typing skills. :D
 
Glorious
Gerbilus Supremus
Posts: 12343
Joined: Tue Aug 27, 2002 6:35 pm

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:19 pm

JBI wrote:
Unlike interpreted languages like Python, you can't just execute isolated fragments of code in C.


To be fair you can't always just do it in the REPL either because of indentation issues... :P
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:26 pm

whm1974 wrote:
OK two of errors I'm having are typos and skipping stuff I'm supposed to be typing in the text editor. I suppose this is rather common mistakes made by beginners? Well if nothing else I will certainly improve my typing skills. :D

Everyone makes typos. Catching the silly ones is part of the compiler's job; fixing those is easy. The more subtle ones which result in successful compilation but a buggy program are harder to find.
Nostalgia isn't what it used to be.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:30 pm

Glorious wrote:
JBI wrote:
Unlike interpreted languages like Python, you can't just execute isolated fragments of code in C.

To be fair you can't always just do it in the REPL either because of indentation issues... :P

You can do multi-line indented control structures in the interactive interpreter. But yeah it is kind of a PITA. Mostly I just use the interactive mode to make sure I understand how a particular library function works before incorporating it into my code. Or as a glorified calculator.
Nostalgia isn't what it used to be.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:33 pm

Glorious wrote:
JBI wrote:
Unlike interpreted languages like Python, you can't just execute isolated fragments of code in C.


To be fair you can't always just do it in the REPL either because of indentation issues... :P

Speaking of indentation, what is the standard practice? I'm tabbing 8 spaces because that is what Gedit is set for by default.
 
Redocbew
Minister of Gerbil Affairs
Posts: 2495
Joined: Sat Mar 15, 2014 11:44 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:37 pm

Ask five different developers, and you're likely to get seven different answers to that. All of them will be correct.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:49 pm

whm1974 wrote:
Speaking of indentation, what is the standard practice? I'm tabbing 8 spaces because that is what Gedit is set for by default.

Hard tabs set every 8 columns is the standard for Linux kernel development. Linus is very militant about enforcing it.

Most developers I know (myself included) use narrower tab stops (4 columns seems to be fairly common) unless we are hacking around in Linux kernel code.

In many development groups (Linux kernel devs being a notable exception) it is considered bad form to use "hard tab" characters; your editor should be set to insert the requisite number of spaces instead of a Tab character when the Tab key is hit. It is also considered bad form to re-indent someone else's code, unless the existing indentation is clearly messed up. IOW, if you're editing a module where the developer set his tab stops every 5 columns, don't go changing everything just because you normally use 4. This becomes critically important if you're doing collaborative development and are using a version tracking system like Subversion or Git (re-indented lines show up as changes even if the code itself is the same).

Also, if your editor has an option to automatically remove trailing spaces that you accidentally add at the end of lines, enable it.
Nostalgia isn't what it used to be.
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:50 pm

Tabs, representing 4 columns.

Easy indentation/de-indentation, and just big enough to be easily visible but not so much that 4-5 levels of indentation won't make lines start the next block over.

JBI, I'm wondering what the rationale for the "tabs should be spaces" thinking is. To me it's absurd, because the whole point of having a tab character is precisely so that the next person opening the file will instantly see it according to their indentation rules.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:54 pm

morphine wrote:
Tabs, representing 4 columns.

Easy indentation/de-indentation, and just big enough to be easily visible but not so much that 4-5 levels of indentation won't make lines start the next block over.

JBI, I'm wondering what the rationale for the "tabs should be spaces" thinking is. To me it's absurd, because the whole point of having a tab character is precisely so that the next person opening the file will instantly see it according to their indentation rules.

The rationale is that hard tab characters don't display the same in all environments. If you set tabs to 4 in your editor, but then view the file in another editor that defaults to 8 or in a bash shell, the indentation changes to 8.

A space is a space is a space, everywhere. The file looks the same no matter what tool you use to view/edit it.

I can see your point too, but then you get people who mix tabs and spaces, and it goes off the rails.
Nostalgia isn't what it used to be.
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 3:56 pm

Well duh, but people who mix tabs and spaces should be shot.

Which means that tabs are fine. ;)
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:00 pm

The majority of the places I've worked discouraged (and in at least a couple of cases prohibited) the use of "hard tabs". Except in makefiles, of course, where they are an essential part of the syntax... :o
Nostalgia isn't what it used to be.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:01 pm

I think we've just confirmed what Redocbew said. :wink:
Nostalgia isn't what it used to be.
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:05 pm

morphine wrote:
Well duh, but people who mix tabs and spaces should be shot.

Which means that tabs are fine. ;)

Agreed, so I will continue using tab since that is what I'm used to doing.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:07 pm

whm1974 wrote:
morphine wrote:
Well duh, but people who mix tabs and spaces should be shot.

Which means that tabs are fine. ;)

Agreed, so I will continue using tab since that is what I'm used to doing.

Also correlated with Redocbew's post, no matter what style you choose, if you get a job developing professionally odds are good you'll be asked to change it anyway.
Nostalgia isn't what it used to be.
 
DancinJack
Maximum Gerbil
Posts: 4494
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:08 pm

Also just to add, since I didn't see any, COMMENT YOUR CODE REGARDLESS OF HOW "EASY" YOU THINK IT IS.
i7 6700K - Z170 - 16GiB DDR4 - GTX 1080 - 512GB SSD - 256GB SSD - 500GB SSD - 3TB HDD- 27" IPS G-sync - Win10 Pro x64 - Ubuntu/Mint x64 :: 2015 13" rMBP Sierra :: Canon EOS 80D/Sony RX100
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:09 pm

just brew it! wrote:
The majority of the places I've worked discouraged (and in at least a couple of cases prohibited) the use of "hard tabs". Except in makefiles, of course, where they are an essential part of the syntax... :o

I wonder how many software installing from source issues are caused by beginners?
 
DancinJack
Maximum Gerbil
Posts: 4494
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:10 pm

whm1974 wrote:
just brew it! wrote:
The majority of the places I've worked discouraged (and in at least a couple of cases prohibited) the use of "hard tabs". Except in makefiles, of course, where they are an essential part of the syntax... :o

I wonder how many software installing from source issues are caused by beginners?


If I understand you correctly, not many because beginners usually don't get code into production.
i7 6700K - Z170 - 16GiB DDR4 - GTX 1080 - 512GB SSD - 256GB SSD - 500GB SSD - 3TB HDD- 27" IPS G-sync - Win10 Pro x64 - Ubuntu/Mint x64 :: 2015 13" rMBP Sierra :: Canon EOS 80D/Sony RX100
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:12 pm

Hold my Red Bull.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
whm1974
Emperor Gerbilius I
Topic Author
Posts: 6361
Joined: Fri Dec 05, 2014 5:29 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:14 pm

DancinJack wrote:
Also just to add, since I didn't see any, COMMENT YOUR CODE REGARDLESS OF HOW "EASY" YOU THINK IT IS.

That is good coding practice. When I start doing BASIC programming I plenty of problems dealing with run errors in source code I found due to people not commenting their code
 
Redocbew
Minister of Gerbil Affairs
Posts: 2495
Joined: Sat Mar 15, 2014 11:44 am

Re: GGC errors with compiling C code.

Tue Nov 07, 2017 4:15 pm

just brew it! wrote:
Also correlated with Redocbew's post, no matter what style you choose, if you get a job developing professionally odds are good you'll be asked to change it anyway.


Yeah, it does tend to vary. I often use two-space indents when writing new code. In addition to stripping trailing whitespace, a features that converts tabs to space is useful also. For new projects which have mismatched indents(WTF! I still can't get over this one.) that's usually one of the first things I'll do. For large projects which are already entrenched it pays to just get used to whatever the current standard practice is and just go with that.
Last edited by Redocbew on Tue Nov 07, 2017 4:19 pm, edited 1 time in total.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 10

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On