Lecture 02 Simple C Programs

Joseph Haugh

University of New Mexico

Attributions

Some slides and code are from Soraya Abad-Mota and Joan Lucas

Simple C Program 1

// Author: Joseph Haugh

/* Forward slash and asterisk to open/close comments */
// Double slash for a single line comment

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
  • Very simple C program.
  • A program is a “plain text” document.
  • File suffix is .c.
  • Always put your name in a comment at the top of your programs.

Simple C Program 1

// Author: Joseph Haugh

/* Forward slash and asterisk to open/close comments */
// Double slash for a single line comment

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
  • Every program has a main function.
  • Functions have open and closing braces.
  • Functions are a series of statements.
  • Body of the function is indented.

Simple C Program 1

// Author: Joseph Haugh

/* Forward slash and asterisk to open/close comments */
// Double slash for a single line comment

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
  • Two types of comments:
    • Single-line: //
    • Multi-line: /* ... */
  • printf function is from the stdio.h standard library.
  • # is the preprocessor directive symbol.

Simple C Program 1

// Author: Joseph Haugh

/* Forward slash and asterisk to open/close comments */
// Double slash for a single line comment

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How are you today?\n");
    return 0;
}
  • Compile and execute the program.
  • Produces two lines of output.
  • printf = “formatted print”.
  • string is enclosed in double quotes (“).
  • \n indicates that a newline should be printed.

Simple C Program 2

// Author: Joseph Haugh

#include <stdio.h>

int main() {
    printf("He");
    printf("llo, wo");
    printf("rld!\n");
    return 0;
}
  • Same output as before.
  • Number of printf statements doesn’t necessarily determine lines of output.
  • \n is the newline.

Simple C Program 3

// Author: Joseph Haugh

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    printf("How  \\are/  \"you\"  today?\n");
    return 0;
}
  • " (double-quote) has a special meaning as a string delimiter.
  • \ (back-slash) has a special meaning as the escape char.
  • back-slash “turns off” the special meaning of the character following it.

Simple C Program 4

// Author: Joseph Haugh

#include <stdio.h>

int main() {
    // C is "stream oriented", each statement can
    // spill over many lines

    printf
              (
        "Hello world!\n"
        
        )
                  ; // every stmt ends with ;
    return 0;
}
  • C is “stream oriented”.
  • Stream oriented verse line oriented
  • The printf statement can span several lines in the source code file.
  • This is legal, but poor “style”, since this is much harder to read.
  • Every statement ends with a semi-colon.

Simple C Program 5

// Author: Joseph Haugh

#include <stdio.h>

int main() {
    // C is "stream oriented", each statement can
    // spill over many lines

    printf
              ( // strings cannot be split over a line
        "Hello,
         world!\n"
        
        )
                  ; // every stmt ends with ;
    return 0;
}
  • Strings cannot be split over a line.
  • Compiling this code produces errors.

Simple C Program 5

// Author: Joseph Haugh

#include <stdio.h>

int main() {
    // C is "stream oriented", each statement can
    // spill over many lines

    printf
              ( 
        "Hello, "  // adjacent strings are
        "world!\n" // combined into one string
                   // we do NOT use "+"
        )
                  ; // every stmt ends with ;
    return 0;
}
  • Two adjacent strings are combined into one String by C.
  • No need to use the + operator, as in:
    • "Hello" + "world"
  • The first homework will be assigned later today.