CSPP 51038 - Tutorial 1

Below, "K+R" = Kernighan and Ritchie text, "The C Programming Language"

What is C?

Developed at Bell Labs during the early 70s, for use with UNIX operating system

Widely used for Operating Systems, Compilers, Embedded Systems, and Scientific Computing

Getting Started - Hello World!

ex1.c   text
("text" is the same file but with a .txt extension)

This example has one function, called main. Note that the function name main has special significance: execution of a C program starts at main. (K+R, p. 6)

To compile our first C program:
# gcc ex1_hello.c -o ex1

Here, the -o indicates that output will be an executable named ex1. To run ex1:
# ./ex1

More info on compiling can be found on the Lab 1 FAQ

The first line of this program:
 #include stdio.h
is a preprocessor directive to include a header file from the C standard library.

What is the return type of main in this example?

Data Types in C

See K+R, section 2.2

What is the difference between these data types? Which are integer types?
int,float,double, long, long double, short, char

Between int and unsigned int?

With the exception of char, the length (ie, number of bytes) of each of these data types varies by machine. Side exercise: use sizeof operator to compare data type lengths:

ex2_datatypes.c   text

In this example, we use a typedef statement to create a new type uint, based on the existing primitive type unsigned int. We will see more of typedef later, especially with struct.

A variable of type char can be assigned either a character or integer value. See ASCII Character Table. ex2b_char.c   text

Printing variables with printf

Example 1 uses printf, a C standard library function that prints to standard output (ie, the terminal).

printf allows up to write variables (such as int) to standard out via conversion specifiers such as %d. See K+R Table 7.1 for a list of these conversion codes.

Can you find the mistakes in this program?
ex3_printf.c   text

Function calls

Our first example contained one function, the main method. Now lets add another function: ex4_functions.c   text

What is the return type of the function usd2can? The arguments? If you are new to functions, see K+R Section 4.1

Compare the above example with: ex4b_functions.c   text

In the second example, we have one global variable: float usd. A global variable can be used by any function with the file, which a local variable such as float can can only be used within the block of code where it is declared. (Note that Example 1 is the preferred way to write this program.)

Control Structures

Become familiar with: Exercise: re-write Example 2b with a while loop, and without break or continue.

K+R Chapter 3 has everything you need to know on control structures.

Getting started with Arrays

A simple array example: ex6_arrays.c   text

This is equivalent to: ex6b_arrays.c   text

What is different between these 2 examples?

Exercise



Written by Sonjia Waxmonsky, wax at cs uchicago edu, based on Jing Cao's lecture slides.