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]

Tradition dictates that we begin with a very simple program, which simply displays the characters "Hello, World!" on the screen and immediately exits.

#include <stdio.h>

int main(int argc, char** argv) {
  printf("Hello, World!\n");
  return 0;

The task itself sounds like it should be simple enough, and the truth of the matter is that most of the above code is what some refer to as boilerplate code. The line that's doing the work we're interested in is printf("Hello World!\n");.

#include <stdio.h> is an example of a macro. Loosely speaking, macros allow us to tell a part of the compiler - the preprocessor - to modify the code we've written before it is compiled. In this case, the include macro is retrieving C code that has already been written from the file stdio.h. Files used in this way are called header files and are saved with the .h extension. In this program, the only thing we needed from stdio.h was the printf function.

Both main and printf are examples of C's functions. Although they serve similar purposes, functions are quite different from macros. We'll be committing a great deal of discussion to each later. In computer science, the term function tends to be used a bit more loosely than in mathematics. For now, it suffices to say that functions let us define a complex process that we want to reference frequently.

Finally, we consider the last line, return 0;. When the operating system executes our program, it's useful to be able to let the OS know whether or not the program succeeded. We do this with an exit status, which we send to the operating system by C's return statement. In this case, we provide an exit status of "0" to indicate that execution succeeded without error. As our programs grow in complexity, we can use other integers as codes to indicate other types of errors. This style of providing exit statuses is a long standing convention.

You probably have a few more questions, like What's up with this int argc, char** argv business? That's weird. or Why is it int main instead of just main? Don't worry! We'll get to that in short order.

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