C Programming/Intro exercise

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Obtaining a compiler Index Next: Preliminaries

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 line that's doing the work we're interested in is

    printf("Hello World!\n");

#include <stdio.h> is a preprocessor directive. Preprocessor directives 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 functions. Although, in many cases, they serve similar purposes but 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 with the 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! These will be described in the chapters that follow.

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 hello.c and then compile it with gcc. Just type:

gcc hello.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 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 gcc should warn about many types of suspicious code that are likely to be incorrect.

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

On IDEs[edit]

If you are using an 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. The process is roughly the same on all IDEs.

Previous: Obtaining a compiler Index Next: Preliminaries