C Programming/Intro exercise

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: A taste of C Index Next: Preliminaries

[edit] Introductory Exercises

[edit] On GCC

If you are using a Unix(-like) system, such as GNU/Linux, Mac OS X, or Solaris, it will probably have GCC installed. Type the hello world program into a file called first.c and then compile it with gcc. Just type:

gcc first.c

Then run the program by typing:

./a.out

or, If you are using Cygwin.

a.exe


You should now see your very first C program


There are a lot of options you can use with the gcc compiler. For example, if you want the output to have a name other than a.out, you can use the -o option. The following shows a few examples:

-c
indicates that the compiler is supposed to generate an object file, which can be later linked to other files to form a final program.
-o
indicates that the next parameter is the name of the resulting program (or library). If this option is not specified, the compiled program will, for historic reasons, end up in a file called "a.out" or "a.exe" (for cygwin users).
-g3
indicates that debugging information should be added to the results of compilation.
-O2 -ffast-math
indicates that the compilation should be optimized.
-W -Wall -fno-common -Wcast-align -Wredundant-decls -Wbad-function-cast -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes
indicates that gcc should warn about many types of suspicious code that are likely to be incorrect.
-E
indicates that gcc should only preprocess the code; this is useful when you are having trouble understanding what gcc is doing with #include and #define, among other things.


All the options are well documented in the manual page for GCC.

[edit] On IDEs

If you are using a commercial IDE you may have to select console project, and to compile you just select build from the menu or the toolbar. The executable will appear inside the project folder, but you should have a menu button so you can just run the executable from the IDE.

Previous: A taste of C Index Next: Preliminaries