| 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. | 
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:
return 0 statement. Seems like we are trying to
	 give something back, and it is an integer. Maybe if we modified
	 our main function definition:
	 int main() Ok, now we are saying that our
	 main function will be returning an integer! So remember,
	 you should always explicitly declare the return type on the
	 function!#include <stdlib.h> to our includes.
	 Let's change our return statement to 
	 return EXIT_SUCCESS;. That is much more readable.
        int printf(const char *format, ...); printf returns
	an int. The man pages say that printf returns the number of
	characters printed. Now you wonder, who cares? Why should you care
	about this? It is good programming practice to ALWAYS check for
	return values for important function calls. It will not only make your
	program more readable,
	but in the end it will make your programs less error prone./* */.
	The comment begins with /* and ends with 
	*/.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!
      
        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.
      
     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.
  
 
    
    