CS156

CS156: Intro to C, Part I

Fall 2017

What Is A Program

See this page as a slide show

CS156 WhatIsAProgram: What is a Program?

What is a Program?

What is a Programming Language?

Syntax vs. Semantics

What is C?

What is Compilation?

An Example C Program

int main() {                       
    return 0;                       
}

All C programs have a main function.

The type of main

The ANSI standard says that main() returns int. It doesn't matter if void works for another compiler. We teach ANSI C in this class, not Microsoft C or GNU C or any particular compiler.

An Example C Program

int main() {                       
    /*  There may
        be one or more lines
        in this type of comment.
    */              
    return 0;  // Comment to the end of line  
}

Programs may have comments.

An Example C Program

#include <stdio.h>                  // Preprocessor Directive
int main() {                        // Function
    // Print Hello, world           // Comment
    printf("Hello, world!\n");      // Function Call
    return 0;                       // Function return value
}

Declarations appear at the top of the program

An Example C Program

#include <stdio.h>                  // Preprocessor Directive
int main() {                        // Function
    // Print Hello, world           // Comment
    printf("Hello, world!\n");      // Function Call
    return 0;                       // Function return value
}

Functions defined by the header file are used in the program.

Creating a C Program in Unix

gcc: The GNU C Compiler

Common c11 Options

Example Continued

% ls
hello_world.c
% cat hello_world.c
#include <stdio.h> 
int main() { 
    printf("Hello, world!\n"); 
    return 0; 
} 
% c11 hello_world.c
% ls -l
total 16
-rwx------  1 cs156 class 4705 Feb 20 12:15 a.out
-rw-------  1 cs156 class   95 Feb 20 12:10 hello_world.c
% ./a.out
Hello, world!

Example Continued, using -o

% ls
hello_world.c
% cat hello_world.c
#include <stdio.h> 
int main() { 
    printf("Hello, world!\n"); 
    return 0; 
} 
% c11 -o hello hello_world.c
% ls -l
total 16
-rwx------  1 cs156 class 4705 Feb 20 12:15 hello
-rw-------  1 cs156 class   95 Feb 20 12:10 hello_world.c
% ./hello
Hello, world!

Modified: 2017-02-20T11:36

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building