Quick Navigation Bar
overview :: introduction :: basic c
[ toc | forums ]
  Note: If the document URL does not begin with https://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version.

C Programming :: Introduction

C Programming :: Hello, World

Let's give a go at a very simple program that prints out "Hello World" to standard out (usually your monitor). We'll call our little program hello.c.

        #include <stdio.h>

	main() {
	  printf("Hello, world!\n");
	  return 0;

What's all this junk just to print out Hello, world!? Let's see what's happening:

Seems like trying to figure all this out is just way too confusing. Let's break things up one at at time:

Let's see our new improved code!

#include <stdio.h>
#include <stdlib.h>

/* Main Function
 *   Purpose: Controls program, prints Hello, World!
 *   Input: Command-Line arguments
 *   Output: Returns Exit Status

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

The key point of this whole introduction is to show you the fundamental difference between correctness and understandability. Both sample codes produce the exact same output in "Hello, world!" However, only the latter example shows better readability in the code leading to code that is understandable. All codes will have bugs. If you sacrifice code readability with reduced (or no) comments and cryptic lines, the burden is shifted and magnified when your code needs to be maintained.

Document what you can. Complex data types, function calls that may not be obvious, etc. Good documentation goes a long way!

C Programming :: A Glimpse at Compilation

You will most likely be using the GNU C compiler, gcc in a Linux or *nix-like environment. Some environments will have the CC variable set which tends to be suitable when using Makefiles.

A superficial look at compiling would entail: gcc file.c But is this what you really want? We get a file called a.out in our directory. This is because you aren't specifying the correct compiler options. Taking the theory of "that which is learned first is learned best", compile your programs using strict and pedantic flags. This will not only catch more errors at compile time, but will potentially help you create a program with less errors at run time. The recommended options to include while compiling are:

So here is an example command line:

    gcc -ansi -pedantic -Wall -O2 -o hello hello.c

This produces an executable called hello in the directory where hello.c resides.

C Programming :: Introduction Summary

  1. Program with understandability in mind. Speed in writing the program may pay off now, but writing a program with readability and understandability will pay off in the future.
  2. Take advantage of the compiler's smarts! Use the strict pedantic options.
  3. Note: From here on out, major topics will be highlighted in blue and subtopics will be highlighted in black.

Notice: Please do not replicate or copy these pages and host them elsewhere. This is to ensure that the latest version can always be found here.

Disclaimer: The document author has published these pages with the hope that it may be useful to others. However, the document author does not guarantee that all information contained on these webpages are correct or accurate. There is no warranty, expressed or implied, of merchantability or fitness for any purpose. The author does not assume any liability or responsibility for the use of the information contained on these webpages.

If you see an error, please send an email to the address below indicating the error. Your feedback is greatly appreciated and will help to continually improve these pages.

© 1999-2023 Alfred Park (fred [ANTISPAM-REMOVE-THIS] AT randu.org)

Valid XHTML 1.0! Valid CSS!