Widely used for Operating Systems, Compilers, Embedded Systems, and Scientific Computing
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?
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:
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
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
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.)
K+R Chapter 3 has everything you need to know on control structures.
This is equivalent to: ex6b_arrays.c text
What is different between these 2 examples?