Basic Elements of C

Preprocessor Directives

  1. Definition: commands handed off to the C preprocessor (which modifies the code before it is compiled)
  2. #include is used to add libraries of functions to a C program
    Example:
    #include <stdio.h>
    

  3. #define creates a constant whose value may not be changed
    Example:
    #define PI 3.14159
    

The main() Function

  1. main() is the entry point for execution for all C programs
  2. In ANSI C, main() is an int, meaning it returns an integer to the operating system (usually 0)
    Example:
    int main() {
    
        ... code goes here ...
        ... code goes here ...
    
    return 0;
    }
    

Variables

  1. Declared either before main() (global) or inside a function (local)
  2. Some valid variable types:
    char used for characters
    short -32,768 to 32,767
    unsigned short 0 to 65535
    long -2,147,483,648 to 2,147,483,647
    int (16-bit) -32,768 to 32,767
    int (32-bit) -2,147,483,648 to 2,147,483,647
    float  
    double  
  3. Multiple variables can be assigned with one type declaration
  4. A variable can be assigned a value at the time of declaration
    Example:
    int MyAge;
    double cost, distance = 4.3;
    

Comments

  1. C style: /* Comments in here */
  2. C++ style: // Comments until the end of the line
    Example:
    int MyAge;   // This is the my age variable
    double cost, distance = 4.3;  /* Create a variable
                                   of type double for 
                                   cost and distance,
                                   setting distance equal
                                   to 4.3 */
    

Other

  1. Lines end with a semicolon
  2. Capitalization does matter!
  3. C source code files usually end in ".c"