C Programming/Intro exercise

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: A taste of C Index Next: Preliminaries

Introductory Exercises[edit]

On GCC[edit]

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:


or, If you are using Cygwin.


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:

indicates that the compiler is supposed to generate an object file, which can be later linked to other files to form a final program.
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).
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.
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.

the classical hello world program[edit]

The basic hello world program, from the K+R book on C, is often worth memorising, just for the structure of the main function which accepts switches, just like gcc is a program with a main function that accepts switches.

#include <stdio.h>
#include <stdlib.h>
int main( int argc, char** argv) {
  printf("Hello World! \n");
  return 0;

The commented program below is basically the same, with some variations that have the same effect. e.g. a "** argv" , or "pointer to pointers" , is the same as *x[] or "array of pointers" ; and "exit(0)" does the same as "return 0" for the main function.

Note: It is a good chance to say that we usually return or exit a function with the 0 code when all of the commands executed successfully. e.g. in our Hello World program, all the commands before "return(0)", or "exit(0)" executed with no error. You will notice that this convention is very common, especially in the main function.

/* Hello World */
this gives include statement, brings in the header file stdio.h ,
located often on unix systems at the directory /usr/include/,
and includes  the printf() function, as well as others, snprintf, scanf, getchar, getline.
In C, functions that are exported have their "signatures" - function name and parameter list -
listed in header files for exporting, and then the same signatures are defined in another file,
often of the same name, to be compiled once into an object file, and on unix systems often reside
in /usr/lib/   with file names like stdlib.a  or stdlib.so , often as soft links to versioned files
e.g. stdlib.1.3.so , 
#include <stdio.h>
  this gives the standard library, which has functions 
  such as rand() random number generation (e.g. for games)
  malloc() and free()  for dynamic heap memory allocation as opposed to stack memory allocation.
  stack memory can be allocated by declaring variables and arrays at the start of a function , including
  the main function, and will be destroyed when the function exits.
#include <stdlib.h>
 the next line is the standard expected function name "main" and argument list of the first function
 to be executed for this program when the compiled program is executed.
 the first argument is the number of arguments, and the second argument is an array of pointers to
 arrays of characters (strings) which contain arguments .e.g.  "-?" , "-v" , "-c" 
int main(int n_args, char* args[]) {
  printf("Hello World!");  //  outputs a string without formatting.
  exit(0);  //  stdlib.h function to exit with a code, if executed from  say a bash shell script, 0 will be
            //  returned  , which can be used inside a shell conditional if statement.

On IDEs[edit]

If you are using a proprietary 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.

One can also find opensource IDE's like Eclipse, Netbeans or Qt Creator. The process will be the same as in a proprietary IDE.

Previous: A taste of C Index Next: Preliminaries