User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

MagPi 47 - An Introduction to C - Page 62

Sat Jul 02, 2016 3:05 am

In the article An Introduction to C, Simon Long presents a Hello World program in C. Unfortunately, Simon presents the main function with a return type of void, which as far as I know has never been the correct signature for main in any of the C standards. While a number of compilers will accept this code and produce the expected result, I think it would be better to have an introduction to C article that presents the signature of main that conforms to the standard.

If you compile the Hello World program

Code: Select all

#include <stdio.h>

void main (void)
{
    /* A print statement */
    printf ("hello world!\n");
}
with gcc with warnings turned on you get a the following warning

Code: Select all

% gcc -Wall -o myprog hello.c
hello.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main (void)
      ^
There is a good discussion on stackoverflow, What should main() return in C and C++? which presents good information on this topic.

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: MagPi 47 - An Introduction to C - Page 62

Sat Jul 02, 2016 3:29 am

It's an intro to C programming, so it's kept simple. I suspect return/exit codes and parameter passing will be explored in future installments.

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: MagPi 47 - An Introduction to C - Page 62

Thu Jul 14, 2016 7:35 pm

Hi AndyD,

Yes, you're correct:

Code: Select all

int main(void)
or

Code: Select all

int main(int argc, char **argv)
are the standard forms. If main does not return an int then my Linux OS puts 1 into $?, which normally signals an error to the OS. The use of the return value is mentioned in https://www.raspberrypi.org/magpi/issues/3/ page 22.

------

Simon is a highly efficient Linux guru, used to hacking through all sorts of code. Therefore, he is probably planning to introduce some interesting projects. We will have to see what comes next...

Best regards,

Will

User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: MagPi 47 - An Introduction to C - Page 62

Thu Jul 14, 2016 9:40 pm

Thanks @williamhbell,

There is no doubt we have all benefited from Simon Long's work on the user interface!

It is a common misconception that void main is a valid signature for the main function in C and C++. I am pretty sure there are a number of C programming books that use void main. I can see that it may be considered harmless to present void main(void) as a valid signature, but my opinion is that given the popularity of the Raspberry Pi, this may be the introduction to C of a large number of future software engineers and it would be great if we could help to stop this misconception. However, there are probably more important things to teach the next generation of software engineers.

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 375
Joined: Fri Dec 26, 2014 11:02 am

Re: MagPi 47 - An Introduction to C - Page 62

Sat Jul 16, 2016 3:51 pm

I'm the author of the series of articles in question, so I thought I'd explain myself...

Yes, in the example in the first episode, I have shown a simplified prototype for main. For the record, in 25 years of writing C professionally, I have yet to come across a compiler which considers main returning void to be an error, and actually, for a lot of real-world C programming (embedded systems, standalone PC applications etc), nothing is ever done with the returned value anyway.

Had I decided to show the strictly correct prototype for main in the first episode of an introduction to C, I would have needed to explain what an int is, which would mean I would have needed to explain variable types. I'd also have needed to explain what an array of string pointers is (argv), and by the time I'd done that, I would a) have used up all the allotted 1000 words and b) scared off every beginner who was reading it!

As some have suggested above, I do indeed show and explain the correct prototype for main later in the series, but I considered it to be an unnecessary complication in the first instalment. If you know enough about C to know that it's wrong, you probably don't need to be reading a series called "an introduction to programming in C"... ;)

ejolson
Posts: 3246
Joined: Tue Mar 18, 2014 11:47 am

Re: MagPi 47 - An Introduction to C - Page 62

Fri Oct 14, 2016 7:14 am

spl23 wrote:As some have suggested above, I do indeed show and explain the correct prototype for main later in the series, but I considered it to be an unnecessary complication in the first instalment.
As finding common denominators is difficult when adding fractions, well-intentioned primary school teachers have been known to teach

2/3 + 3/4 = 5/7

Learning this first, while easier, is likely to make the final goal of learning arithmetic harder.

A good case can be made for learning simple things first; however, learning wrong things that need to be unlearned does not make learning simpler. A program not returning 0 on success breaks makefiles, shell scripts and any program that checks the return value. For example, rc.local aborts when any program run at startup returns with a nonzero error code.

Struggling with these problems and others until more than half way through the book, and then discovering those problems come from intentionally being taught the wrong thing, could be frustrating. Many students barely have the patience to learn something once. When correct is as simple as "int main()" maybe it is better to learn it right the first time.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23082
Joined: Sat Jul 30, 2011 7:41 pm

Re: MagPi 47 - An Introduction to C - Page 62

Fri Oct 14, 2016 9:51 am

ejolson wrote:
spl23 wrote:As some have suggested above, I do indeed show and explain the correct prototype for main later in the series, but I considered it to be an unnecessary complication in the first instalment.
As finding common denominators is difficult when adding fractions, well-intentioned primary school teachers have been known to teach

2/3 + 3/4 = 5/7

Learning this first, while easier, is likely to make the final goal of learning arithmetic harder.

A good case can be made for learning simple things first; however, learning wrong things that need to be unlearned does not make learning simpler. A program not returning 0 on success breaks makefiles, shell scripts and any program that checks the return value. For example, rc.local aborts when any program run at startup returns with a nonzero error code.

Struggling with these problems and others until more than half way through the book, and then discovering those problems come from intentionally being taught the wrong thing, could be frustrating. Many students barely have the patience to learn something once. When correct is as simple as "int main()" maybe it is better to learn it right the first time.
Your examples are apples and oranges. One is a technique, one is minor change to a minor issue. With the main() issue, you are keeping it simple to start with (but still valid), then introducing a more complex version as requried. Perhaps an example would be teaching Newtonian mechanics, prior to teaching what actually happens.

Although, tbh, if this is the worst thing that comes out of Simon's efforts, good for him!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
PeterO
Posts: 4880
Joined: Sun Jul 22, 2012 4:14 pm

Re: MagPi 47 - An Introduction to C - Page 62

Fri Oct 14, 2016 10:06 am

jamesh wrote: Perhaps an example would be teaching Newtonian mechanics, prior to teaching what actually happens.
My example would be how I was taught Chemistry at school. At the start of the A-level course we were told to forget pretty much everything from O-level because it was a gross over-simplification. Subsequently I got a "D" at A-level :(
As you say the "main" issue is really quite minor.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23082
Joined: Sat Jul 30, 2011 7:41 pm

Re: MagPi 47 - An Introduction to C - Page 62

Fri Oct 14, 2016 1:08 pm

PeterO wrote:
jamesh wrote: Perhaps an example would be teaching Newtonian mechanics, prior to teaching what actually happens.
My example would be how I was taught Chemistry at school. At the start of the A-level course we were told to forget pretty much everything from O-level because it was a gross over-simplification. Subsequently I got a "D" at A-level :(
As you say the "main" issue is really quite minor.
PeterO
I got an E. I really didn't get chemistry at that level.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

ejolson
Posts: 3246
Joined: Tue Mar 18, 2014 11:47 am

Re: MagPi 47 - An Introduction to C - Page 62

Fri Oct 14, 2016 1:25 pm

spl23 wrote:For the record, in 25 years of writing C professionally, I have yet to come across a compiler which considers main returning void to be an error
Clang/LLVM is such a C compiler.

Code: Select all

$ cat main.c
#include <stdio.h>

void main (void)
{
    printf("Hello world!\n");
}
$ clang main.c
main.c:3:1: error: 'main' must return 'int'
void main (void)
^
1 error generated.
On the other hand

Code: Select all

$ gcc -Wall main.c
main.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
$ ./a.out
Hello world!
$ echo $?
13
The fact that gcc produces a warning and a main routine that mysteriously returns the number of characters printed by the printf function is not an indication of valid C code. While explaining why this happens is an interesting exercise in the implementation of architecture-specific C function-call semantics, it is not easier then explaining that the operating system requires the main routine to return an exit code which can be examined by typing "echo $?" after running the program.

User avatar
PeterO
Posts: 4880
Joined: Sun Jul 22, 2012 4:14 pm

Re: MagPi 47 - An Introduction to C - Page 62

Fri Oct 14, 2016 2:43 pm

If gcc can compile it I'm happy to call it valid as it knows far more than I do about valid vs invalid code.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 375
Joined: Fri Dec 26, 2014 11:02 am

Re: MagPi 47 - An Introduction to C - Page 62

Sat Oct 15, 2016 3:55 pm

ejolson wrote:A program not returning 0 on success breaks makefiles, shell scripts and any program that checks the return value.
If someone is capable of including a program they have written in a makefile, I would wager a large sum of money that they already know an awful lot more about C than I describe in my book... ;)
ejolson wrote:On the other hand

Code: Select all

$ gcc -Wall main.c
main.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
$ ./a.out
Hello world!
$ echo $?
13
The fact that gcc produces a warning and a main routine that mysteriously returns the number of characters printed by the printf function is not an indication of valid C code.
The fact that you had to include "-Wall" to get a warning indicates that this is not a significant issue. By default (i.e. without -Wall), gcc on RPi does not generate a warning on main returning void, for the simple reason that it is a harmless violation of the spec rather than something which causes a real problem. For the code examples in the book, a return code is unnecessary.

As I have explained previously, this book is an introduction to C for absolute beginners, not a definitive guide that will turn someone into a C expert. As a result, it includes some relatively harmless simplifications - this is one of them. And for the record, I write a lot of C, and my main functions invariably return void - it may not (strictly speaking) be correct, but it's yet to cause me a problem in real life...

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

Re: MagPi 47 - An Introduction to C - Page 62

Tue Oct 18, 2016 9:35 am

Simon,
thank you for your efforts writing this great C tutorial - I love it!
I have to agree that for some educational reasons one will have to simplify some details which will not mean that they could not be elaborated then at a more advanced level.
tbh, I don't like some posts written here when they sometimes sound to me sort of nitpicking (which then perhaps could - but hopefully shouldn't and wouldn't - demotivate the author for either future enterprises), but some points of view might be worth mentioning for a later edition or a codicil.
(not meant to be offensive to any one)

ejolson
Posts: 3246
Joined: Tue Mar 18, 2014 11:47 am

Re: MagPi 47 - An Introduction to C - Page 62

Thu Oct 20, 2016 10:39 pm

spl23 wrote:As I have explained previously, this book is an introduction to C for absolute beginners, not a definitive guide that will turn someone into a C expert.
The fact that your book is written for beginners is why I'm interested in it. In case it wasn't clear from my previous posts, I like the book very much and appreciate you writing it.

I teach an introductory course on numerical mathematics. Half the students have never written a program. We use C because Fortran is not widely used outside of numerical work. The students write code to perform a specific calculation and then change the code if they want to solve a different problem. To manage which output goes with which program we start using makefiles right after hello world.

I posted a question in the C programming section that got ignored because people were discussing advanced topics. I see that your book is licenced under Creative Commons and claimed to be open source on the website. I would like to make some simple changes for my students. Are there easily-modified source files available for the book or should I use a pdf editor?

ejolson
Posts: 3246
Joined: Tue Mar 18, 2014 11:47 am

Re: MagPi 47 - An Introduction to C - Page 62

Tue Nov 01, 2016 5:26 pm

ejolson wrote:I see that your book is licenced under Creative Commons and claimed to be open source on the website. I would like to make some simple changes for my students. Are there easily-modified source files available for the book or should I use a pdf editor?
It appears that Xournal will work well enough for changing "void main" to "int main" where needed. I greatly appreciate this book being released under a creative commons license, which allows changes to be made for my class and so the examples compile using clang. Any help would be appreciated.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23082
Joined: Sat Jul 30, 2011 7:41 pm

Re: MagPi 47 - An Introduction to C - Page 62

Tue Nov 01, 2016 5:33 pm

ejolson wrote:
spl23 wrote: I teach an introductory course on numerical mathematics. Half the students have never written a program. We use C because Fortran is not widely used outside of numerical work. The students write code to perform a specific calculation and then change the code if they want to solve a different problem. To manage which output goes with which program we start using makefiles right after hello world.
Brave. Makefiles are evil.

(says someone who has been using them on and off for 30 years)

Surely there must be a better way than introducing people to the syntactically challenged, formatting challenged, and semantically challenged makefile format?

I would have put more challenged in there, but I think I covered everything.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
PeterO
Posts: 4880
Joined: Sun Jul 22, 2012 4:14 pm

Re: MagPi 47 - An Introduction to C - Page 62

Tue Nov 01, 2016 5:57 pm

jamesh wrote: Surely there must be a better way than introducing people to the syntactically challenged, formatting challenged, and semantically challenged makefile format?
CMake makes make easier :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ejolson
Posts: 3246
Joined: Tue Mar 18, 2014 11:47 am

Re: MagPi 47 - An Introduction to C - Page 62

Tue Nov 01, 2016 6:26 pm

jamesh wrote:Surely there must be a better way than introducing people to the syntactically challenged, formatting challenged, and semantically challenged makefile format?
Students seem to understand easily the idea of a recipe that requires certain ingredients and a procedure to combine those ingredients to create something. Avoiding implicit rules helps.

I always anticipate trouble with tabs and remind them. This year there were no problems with tabs, but a surprising number of difficulties with students not paying attention to the difference between uppercase versus lowercase characters.

The advantage of make over other build systems is that it is a standard and general purpose tool that can also be used for generating data files, updating graphs and formatting reports. Early versions of Unix even used make to populate the /dev directory. Being able to use makefiles for many different tasks offsets the irritation of tabs in the syntax.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23082
Joined: Sat Jul 30, 2011 7:41 pm

Re: MagPi 47 - An Introduction to C - Page 62

Wed Nov 02, 2016 10:30 am

PeterO wrote:
jamesh wrote: Surely there must be a better way than introducing people to the syntactically challenged, formatting challenged, and semantically challenged makefile format?
CMake makes make easier :-)

PeterO
Ah yes.....!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

Re: MagPi 47 - An Introduction to C - Page 62

Thu Nov 03, 2016 12:52 pm

for beginners, make or makefile should be explained so that they can understand the basics.

But after having explained that make and makefile in a tutorial, one should proceed using an IDE like Geany (which already has been introduced in the MagPi tutorial) for compile, debug, build, and run, which is far better and far more convenient then especially for beginners IMO.

ejolson
Posts: 3246
Joined: Tue Mar 18, 2014 11:47 am

Re: MagPi 47 - An Introduction to C - Page 62

Thu Nov 03, 2016 6:28 pm

Stiller wrote:for beginners, make or makefile should be explained so that they can understand the basics.

But after having explained that make and makefile in a tutorial, one should proceed using an IDE like Geany (which already has been introduced in the MagPi tutorial) for compile, debug, build, and run, which is far better and far more convenient then especially for beginners IMO.
What seems convenient for beginners can make the learning curve steeper later on. This is often the case when special-purpose beginner-friendly tools are substituted for general-purpose tools that can be used for other things. As a case in point, I've had multiple students ask if they could use Microsoft Word for writing their C programs because they already knew how to use that editor. Fortunately Geany is general purpose enough to also edit LaTeX, bash scripts and gnuplot scripts as well as html, configuration and normal text files. While it seems silly to edit .bashrc using Geany, having to learn an entirely different editor to do the same thing would make the task much more difficult. Therefore, I don't see Geany as a problem. I like the book very much. My only concern is the use of "void main" which doesn't even compile with clang.

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

Re: MagPi 47 - An Introduction to C - Page 62

Thu Nov 03, 2016 7:26 pm

The point is:
Using an IDE gives a quick learning success and a big motivation to carry on.
Arduino shows how it works.
Beginners are dispairing about the command line mess.

peterlite
Posts: 720
Joined: Sun Apr 17, 2016 4:00 am

Re: MagPi 47 - An Introduction to C - Page 62

Thu Nov 03, 2016 8:22 pm

Have you reviewed the code in https://www.raspberrypi.org/magpi/issue ... ials-c-v1/? Is it standard enough to work in Linux Mint? I ask because I am looking at writing a program to be compiled and used on both.

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 375
Joined: Fri Dec 26, 2014 11:02 am

Re: MagPi 47 - An Introduction to C - Page 62

Thu Nov 03, 2016 11:01 pm

peterlite wrote:Have you reviewed the code in https://www.raspberrypi.org/magpi/issue ... ials-c-v1/? Is it standard enough to work in Linux Mint? I ask because I am looking at writing a program to be compiled and used on both.
It's all standard ANSI C - it should work perfectly well on any version of Linux that uses the gcc compiler.

There are two errors I am aware of in the code examples - details are at viewtopic.php?f=106&t=109621#p1054301

User avatar
DougieLawson
Posts: 35560
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MagPi 47 - An Introduction to C - Page 62

Fri Nov 04, 2016 4:41 pm

Stiller wrote:for beginners, make or makefile should be explained so that they can understand the basics.
It would be better to explain cmake and use that to create complex Makefiles that can be used but don't need to be examined in detail. I'm a big fan of cmake as it's realtively sane compared to plain makefiles.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “The MagPi”