CS156: Intro to C, Part I

Spring 2018

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

% cp ~cs156/pub/hello_world.c .
% ls -l
total 4
-r-------- 1 cs156 class 84 Apr 20 09:56 hello_world.c
% cat hello_world.c
#include <stdio.h> 
int main() { 
    printf("Hello, world!\n"); 
    return 0; 
} 
% c11 -Wall hello_world.c
% ls -l
total 24
-rwx------ 1 cs156 class 18048 Apr 20 09:56 a.out
-r-------- 1 cs156 class    84 Apr 20 09:56 hello_world.c
% ./a.out
Hello, world!

Example Continued, using -o

% cp ~cs156/pub/hello_world.c .
% ls -l
total 4
-r-------- 1 cs156 class 84 Apr 20 09:56 hello_world.c
% cat hello_world.c
#include <stdio.h> 
int main() { 
    printf("Hello, world!\n"); 
    return 0; 
} 
% c11 -Wall -o hello hello_world.c
% ls -l
total 24
-rwx------ 1 cs156 class 18048 Apr 20 09:56 hello
-r-------- 1 cs156 class    84 Apr 20 09:56 hello_world.c
% ./hello
Hello, world!

Get the order correct!

% cp ~cs156/pub/hello_world.c .
% c11 -Wall -o hello hello_world.c
% ls -l
total 24
-rwx------ 1 cs156 class 18048 Apr 20 09:56 hello
-r-------- 1 cs156 class    84 Apr 20 09:56 hello_world.c
% c11 -Wall -o hello_world.c hello
hello: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.fini+0x0): first defined here
hello: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.data+0x0): first defined here
hello:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o:(.rodata+0x0): first defined here
hello:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
hello: In function `_dl_relocate_static_pie':
(.text+0x30): multiple definition of `_dl_relocate_static_pie'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text[.text.group]+0x0): first defined here
hello: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text+0x0): first defined here
hello: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/8/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
hello:(.data+0x8): first defined here
/bin/ld: error in hello(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
% ls -l
total 20
-rwx------ 1 cs156 class 18048 Apr 20 09:56 hello

Disaster results from -o in the wrong order!

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-01-06T13:56

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building