[Courses] C Programming For Absolute Beginners, Lesson 2: Comments Digest

Carla Schroder carla at bratgrrl.com
Tue Feb 21 00:35:33 UTC 2012


Hey all, this week's lesson is in two parts. This part is some copy-and-paste 
from the comments for Lesson 1. There was a lot of great information so here 
are some of the highlights. Remember, you can always read the course archives 
online at http://mailman.linuxchix.org/pipermail/courses/

best,
Carla

##########

When you have a #include file surrounded by 
angle brackets, it indicates to look for the file according to the 
include path.  The compiler typically has a built in include path but 
you can extended via command line arguments or environment variables:

gcc -I/usr/local/include  will add /usr/local/include to the front of 
the include path.

When you do a #include with double quotes (#include "file.h") that 
means to search the same directory as the file which is doing the 
#include.

For example, if /home/kathryn/src/foo.c  does

#include "foo.h"

then the compiler will search in /home/kathryn/src for foo.h and if it 
doesn't find it, it will search the include path as normal.

##########

> is it good practice to
> use the 'void' keyword?


For little programs like this, there's no need to bother with
returning error codes, so you're going to pretend that main() doesn't
return anything, and so it actually is good coding practice to say

void main()

One reason it's good practice is that you can set the compiler to
tell you about warnings -- things that might be errors in your code
but they're not bad enough to keep you from compiling it. Most good
C programmers include flags like -Wall (show all warnings) when they
compile C, and if you try that on your program without declaring
main() to be void, you'll get a nice juicy warning about it:

$ cc -o foo -Wall foo.c
foo.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
foo.c: In function ‘main’:
foo.c:5:1: warning: control reaches end of non-void function [-Wreturn-type]

What it's saying in plainer English is "Hey, dummy, you said main was
supposed to return an int, but you never actually return one anywhere!"

So for the purposes of a beginning class, there's no reason at all
to worry about it. But if you want to get into good habits from the
very beginning, you can either use "void main()", or else say
"int main()" and then end your program with a "return 0;"
And if you leave off the "int", C will assume it, but it's good
practice to include it if you're going to be returning something.

################

> I played with unicode characters in printf:

> #include <stdio.h>
> main()
> {
> printf( "\nI \u2764 C!\n\n" );
> }

> but on compiling I got the following error:

> $ gcc -o iheart iheart.c
> iheart.c: In function ‘main’:
> iheart.c:4:9: warning: universal character names are only valid in C++ and 
C99 [enabled by default]

> The output of 'iheart' is correct, so I guess I should ignore this?


Since you're trying to do something that's not in the standard character 
set, you're using a code for Extended ASCII ("\u2764" in the printf() line)

> #include <stdio.h>
> main()
> {
> printf( "\nI \u2764 C!\n\n" );
> }

The warnings from the compiler means "hey wait, that's a universal 
character name from the Extended ASCII set -- not all C compilers are 
going to support that. However, I have a language standard called C99 
that does support it, and I turn it on by default because I'm gcc and 
I'm smart! So you're ok for now, but I just wanted to let you know that 
in case you decide to compile this with a non-Extended-ASCII-something 
in the future."

> $ gcc -o iheart iheart.c
> iheart.c: In function ‘main’:
> iheart.c:4:9: warning: universal character names are only valid in C++
> and C99 [enabled by default]
>
> The output of 'iheart' is correct, so I guess I should ignore this?

I'm not sure how other compilers are about handling extended ASCII, but 
with gcc you can use:

gcc -o iheart -Wall -std=c99 iheart.c

...to specify usage of the language -st(an)d(ard) C99, and that will 
make the warning go away.

##########

This Stackoverflow discussion goes into some interesting minutiae on the finer 
points of cc vs. gcc:

Invoking GCC as “cc” versus “gcc”
http://stackoverflow.com/questions/939989/invoking-gcc-as-cc-versus-gcc

############

        Re C89, C99 (and C90 and C11), thanks for mentioning 
these. I had no idea these specs existed. I'd only known of 
"K&R C" and "ANSI C". 

    Briefly (I hope I can be brief), when I use a search 
engine to look for "C programming C 89", the top-most link is 
to a wikipedia page for ANSI C. 
http://en.wikipedia.org/wiki/ANSI_C 
    The "89" part refers to the year, 1989. Best I can tell, 
it's this spec people refer to when they talk about ANSI C. 
    There's a C90 that seems to have tweaked the C89 spec a 
bit. 
    There's a C99 that "adopted the ISO/IEC 9899:1999 standard." 
This seems to add a significant set of new features to the 
language. 
http://en.wikipedia.org/wiki/C99 

    C11, done in 2011, seems to have added more changes to the 
C programming language specification. According to the wikipedia 
page for C11, 
http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29 
C11 is "the current standard for the C programming language." 


    On a more directly practical point, I have been assuming that 
C Programming for Absolute Beginners will be an intro to ANSI C, 
otherwise known as C89. 
    I can happily just eat what's given me, but I'm curious to 
know if this course will cover C99 and/or C11 features. 

############


Hi Jim and everyone,

This page shows the status of gcc for the new C standards:
http://gcc.gnu.org/onlinedocs/gcc/Standards.html
I also spent some quality time with man gcc, and now I have a headache because 
there are all these exceptions and ifs, ands, buts, and whatevers. The default 
is gnu90, which is C90 with some GNU extensions. That's what we're going to 
use in this course. Using the gcc options that support the newer standards is 
simple; the hard part is reading and understanding them. So I'll probably not 
address them in this course.

Carla

#####################
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
ace Linux nerd
author of Linux Cookbook,
Linux Networking Cookbook,
Book of Audacity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Courses mailing list