Saturday, December 20, 2008

A Simple C Program


/* Latihan. 2.1: lat02_01.c
A first program in C */
#include <stdio.h>

int main()

{
printf( "Welcome to C!\n" );

return 0;
}
Welcome to C!


  • Comments

  • - Text surrounded by /* and */ is ignored by computer
    - Used to describe program
  • #include <stdio.h>

  • - Preprocessor directive
    * Tells computer to load contents of a certain file
    - <stdio.h> allows standard input/output operations
  • int main()

  • - C++ programs contain one or more functions, exactly one of which must be main
    - Parenthesis used to indicate a function
    - int means that main "returns" an integer value
    - Braces ( { and } ) indicate a block
    * The bodies of all functions must be contained in braces
  • printf( "Welcome to C!\n" );

  • - Instructs computer to perform an action
    * Specifically, prints the string of characters within quotes (" ")
    - Entire line called a statement
    * All statements must end with a semicolon (;)
    - Escape character (\)
    * Indicates that printf should do something out of the ordinary
    * \n is the newline character
  • return 0;

  • - A way to exit a function
    - return 0, in this case, means that the program terminated normally
  • Right brace }

  • - Indicates end of main has been reached
  • Linker

  • - When a function is called, linker locates it in the library
    - Inserts it into object program
    - If function name is misspelled, the linker will produce an error because it will not be able to find function in the library

No comments: