Input and Output

printf()

  1. used to write to the standard output device, usually a monitor
    Example:
    printf("Hello there!");
    

  2. included in stdio.h, so use "#include <stdio.h>"
  3. can print the value of variables using placeholders
    Example:
    printf("My age is %d", MyAge);
      

    %c char
    %d int
    %f float
  4. Escape sequences must be used for special characters
    \n newline
    \t tab
    \" double quote
    \' single quote
    \? question mark
    \\ backslash

  5. printf() example program:
    
    #include <stdio.h>
    
    int main() {
    
       int year = 1999;
    
       printf("This program was written in %d.\n", year);
    
       return 0;
    
    }
    
    Source File | DOS Executable (run from DOS prompt)

scanf()

  1. used to read from the standard input device, usually a keyboard
    Example:
    scanf("%lf",&diameter);  /* wait for input and store it in 
                                a variable named diameter */
       

    %c char
    %d int
    %lf float
  2. Can be used with multiple inputs.
  3. scanf() example program:
    
    #include <stdio.h>
    
    int main() {
    
       int year;
    
       printf("Please enter the year>");
    
       scanf("%d", &year);
    
       printf("\n\nThe current year is %d.\n", year);
    
       return 0;
    
    }
    
    Source File | DOS Executable (run from DOS prompt)