Introduction to the C Programming Language
Science & Technology Support
High Performance Computing
Ohio Supercomputer Center
1224 Kinnear Road
Columbus, OH 43212-1163
Table of Contents
•
•
•
•
•
•
•
•
•
Introduction
C Program Structure
Variables, Expressions, &
Operators
Input and Output
Program Looping
Decision Making Statements
Array Variables
Strings
Math Library Functions
•
•
•
•
•
•
•
•
•
User-defined Functions
Formatted Input and Output
Pointers
Structures
Unions
File Input and Output
Dynamic Memory Allocation
Command Line Arguments
Operator Precedence Table
2
C Programming
Introduction
•
Why Learn C?
3
C Programming
Why Learn C?
•
•
•
•
•
•
•
•
•
Compact, fast, and powerful
“Mid-level” Language
Standard for program development (wide acceptance)
It is everywhere! (portable)
Supports modular programming style
Useful for all applications
C is the native language of UNIX
Easy to interface with system devices/assembly routines
C is terse
4
C Programming
C Program Structure
•
•
•
•
•
Canonical First Program
Header Files
Names in C
Comments
Symbolic Constants
5
C Programming
Canonical First Program
•
The following program is written in the C programming language:
#include main() {
/* My first program */ printf("Hello World! \n");
}
•
•
C is case sensitive. All commands in C must be lowercase.
C has a free-form line structure. End of each statement must be marked with a semicolon. Multiple statements can be on the same line. White space is ignored. Statements can continue over many lines.
6
C Programming
Canonical First Program Continued
#include
main()
{
/* My first program */ printf("Hello World! \n");
}
•
The C program starting point is identified by the word main().
•
This informs the computer as to where the program actually starts. The parentheses that follow the keyword main indicate that there are no arguments supplied to this program (this will be examined later on).
The two braces, { and }, signify the begin and end segments of the program. In general, braces are used throughout C to enclose a block of statements to be treated as a unit. COMMON ERROR: unbalanced number of open and close curly brackets!
•
7
C Programming
More on the Canonical First Program
#include main() {
/* My first program */ printf("Hello World! \n");
}
•
The purpose of the statement #include is to allow the use of the printf statement to provide program output. For each function built into the language, an associated header file must be included. Text to be displayed by printf() must be enclosed in double quotes. The program only has the one printf() statement.
• printf() is actually a function (procedure) in C that is used for printing variables and text. Where text appears in double quotes "", it is printed without modification. There are some exceptions however. This has to do with the \ and % characters. These characters are modifiers, and for the present the
\ followed by the n character represents a newline character.
8
C Programming
Canonical First Program Output & Comments
•
Thus the program prints
Hello World!
•
•
And the cursor is set to the beginning of the next line. As we shall see later on, what follows the \ character will determine what is printed (i.e., a tab, clear screen, clear line, etc.)
/* My first program */
Comments can be inserted into C programs by bracketing text with the /* and
*/ delimiters. As will be discussed later, comments are useful for a variety of reasons. Primarily they serve as internal documentation for program structure and functionality.
9
C Programming
Header Files
•
•
•
•
Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement.
Standard header files are provided with each compiler, and cover a range of areas: string handling, mathematics, data conversion, printing and reading of variables, etc.
To use any of the standard functions, the appropriate header file should be included.
This is done at the beginning of the C source file. For example, to use the function printf() in a program, the line
#include
should be at the beginning of the source file, because the declaration for printf() is found in the file stdio.h. All header files have the extension .h and generally reside in the /usr/include subdirectory.
#include
#include
#include "mylib.h"
The use of angle brackets informs the compiler to search the compiler’s include directories for the specified file. The use of the double quotes "" around the filename informs the compiler to start the search in the current directory for the specified file.
10
C Programming
Names in C
•
Identifiers in C must begin with a character or underscore, and may be followed by any combination of characters, underscores, or the digits 0-9. summary exit_flag i Jerry7
Number_of_moves
_id
•
You should ensure that you use meaningful (but short) names for your identifiers. The reasons for this are to make the program easier to read and self-documenting. Example: distance = speed * time;
•
Some users choose to adopt the convention that variable names are all lower case while symbolic names for constants are all upper case.
Keywords are reserved identifiers that have strict meaning to the C compiler.
C only has 29 keywords. Example keywords are: if, else, char, int, while
•
11
C Programming
Comments
•
The addition of comments inside programs is desirable. These may be added to
C programs by enclosing them as follows,
/*
Computational Kernel: In this section of code we implement the
Runge-Kutta algorithm for the numerical solution of the differential Einstein Equations.
*/
•
Note that the /* opens the comment field and the */ closes the comment field. Comments may span multiple lines. Comments may not be nested one inside the another.
/* this is a comment. /* this comment is inside */ wrong */
•
In the above example, the first occurrence of */ closes the comment statement for the entire line, meaning that the text wrong is interpreted as a C statement or variable, and in this example, generates an error.
12
C Programming
Why use comments?
•
•
•
Documentation of variables and functions and their usage
Explaining difficult sections of code
Describes the program, author, date, modification changes, revisions…
Best programmers comment as they write the code, not after the fact.
13
C Programming
Symbolic Constants
•
•
•
•
•
Names given to values that cannot be changed. Implemented with the
#define preprocessor directive.
#define N 3000
#define FALSE 0
#define PI 3.14159
#define FIGURE "triangle"
Note that preprocessor statements begin with a # symbol, and are NOT terminated by a semicolon. Traditionally, preprocessor statements are listed at the beginning of the source file.
Preprocessor statements are handled by the compiler (or preprocessor) before the program is actually compiled. All # statements are processed first, and the symbols (like N) which occur in the C program are replaced by their value
(like 3000). Once this substitution has taken place by the preprocessor, the program is then compiled.
In general, preprocessor constants are written in UPPERCASE. This acts as a form of internal documentation to enhance program readability and reuse.
In the program itself, values cannot be assigned to symbolic constants.
14
C Programming
Use of Symbolic Constants
•
Consider the following program which defines a constant called TAXRATE.
#include
#define TAXRATE 0.10 main ()
{
float balance; float tax; balance = 72.10; tax = balance * TAXRATE; printf("The tax on %.2f is %.2f\n",balance, tax);
}
The tax on 72.10 is 7.21
•
The whole point of using #define in your programs is to make them easier to read and modify. Considering the above program as an example, what changes would you need to make if the TAXRATE was changed to 20%?
15
C Programming
Use of Symbolic Constants
•
•
Obviously, the answer is one, where the #define statement which declares the symbolic constant and its value occurs. You would change it to read
#define TAXRATE 0.20
Without the use of symbolic constants, you would hard code the value 0.20 in your program, and this might occur several times (or tens of times).
16
C Programming
Variables, Expressions, and Operators
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Declaring Variables
Basic Format
Basic Data Types: Integer
Basic Data Types: Float
Basic Data Types: Double
Basic Data Types: Character
Expressions and Statements
Assignment Operator
Assignment Operator Evaluation
Initializing Variables
Initializing Variables Example
Arithmetic Operators
Increment/Decrement Operators
Prefix versus Postfix
•
•
•
•
•
•
•
•
•
•
Advanced Assignment Operators
Precedence & Associativity of
Operators
Precedence & Associativity of
Operators Examples
The int Data Type
The float and double Data
Types
The char Data Type
ASCII Character Set
Automatic Type Conversion
Automatic Type Conversion with
Assignment Operator
Type Casting
17
C Programming
Declaring Variables
•
•
A variable is a named memory location in which data of a certain type can be stored. The contents of a variable can change, thus the name. User defined variables must be declared before they can be used in a program. It is during the declaration phase that the actual memory for the variable is reserved. All variables in C must be declared before use.
Get into the habit of declaring variables using lowercase characters.
Remember that C is case sensitive, so even though the two variables listed below have the same name, they are considered different variables in C. sum Sum
•
The declaration of variables is done after the opening brace of main(). main() { int sum;
•
It is possible to declare variables elsewhere in a program, but lets start simply and then get into variations later on.
18
C Programming
Basic Format
•
The basic format for declaring variables is data_type var, var, …;
•
where data_type is one of the four basic types, an integer, character, float, or double type. Examples are int i,j,k; float length,height; char midinit;
19
C Programming
Basic Data Types: INTEGER
•
INTEGER: These are whole numbers, both positive and negative. Unsigned integers(positive values only) are also supported. In addition, there are short and long integers. These specialized integer types will be discussed later.
•
The keyword used to define integers is int •
An example of an integer value is 32. An example of declaring an integer variable called age is int age;
20
C Programming
Basic Data Types: FLOAT
•
FLOATING POINT: These are numbers which contain fractional parts, both positive and negative, and can be written in scientific notation.
•
The keyword used to define float variables is float •
Typical floating point values are 1.73 and 1.932e5 (1.932 x 105). An example of declaring a float variable called x is float x;
21
C Programming
Basic Data Types: DOUBLE
•
DOUBLE: These are floating point numbers, both positive and negative, which have a higher precision than float variables.
•
The keyword used to define double variables is double •
An example of declaring a double variable called voltage is double voltage;
22
C Programming
Basic Data Types: CHAR
•
CHARACTER: These are single characters.
•
The keyword used to define character variables is char •
Typical character values might be the letter A, the character 5, the symbol “, etc. An example of declaring a character variable called letter is char letter;
23
C Programming
Expressions and Statements
•
An expression in C is some combination of constants, variables, operators and function calls. Sample expressions are: a + b
3.0*x - 9.66553 tan(angle) •
Most expressions have a value based on their contents.
•
A statement in C is just an expression terminated with a semicolon. For example: sum = x + y + z; printf("Go Buckeyes!");
24
C Programming
The Assignment Operator
•
In C, the assignment operator is the equal sign = and is used to give a variable the value of an expression. For example: i=0; x=34.8; sum=a+b; slope=tan(rise/run); midinit='J'; j=j+3;
•
When used in this manner, the equal sign should be read as “gets”. Note that when assigning a character value the character should be enclosed in single quotes. 25
C Programming
The Assignment Operator Evaluation
•
In the assignment statement a=7; •
two things actually occur. The integer variable a gets the value of 7, and the expression a=7 evaluates to 7. This allows a shorthand for multiple assignments of the same value to several variables in a single statement. Such as x=y=z=13.0;
26
C Programming
Initializing Variables
•
C Variables may be initialized with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialized to 10. int count = 10;
•
In general, the user should not assume that variables are initialized to some default value “automatically” by the compiler. Programmers must ensure that variables have proper values before they are used in expressions.
27
C Programming
Initializing Variables Example
•
The following example illustrates the two methods for variable initialization:
#include
main () { int sum=33; float money=44.12; char letter; double pressure; letter='E'; /* assign character value */ pressure=2.01e-10; /*assign double value */ printf("value of sum is %d\n",sum); printf("value of money is %f\n",money); printf("value of letter is %c\n",letter); printf("value of pressure is %e\n",pressure);
}
•
which produces the following output: value value value value
of of of of sum is 33 money is 44.119999 letter is E pressure is 2.010000e-10
28
C Programming
Arithmetic Operators
•
The primary arithmetic operators and their corresponding symbols in C are:
Negation
%
*
Addition
+
Division
•
Modulus
Multiplication
•
-
/
Subtraction
-
When the / operator is used to perform integer division the resulting integer is obtained by discarding (or truncating) the fractional part of the actual floating point value. For example:
1/2
0
3/2
1
The modulus operator % only works with integer operands. The expression a%b is read as “a modulus b” and evaluates to the remainder obtained after dividing a by b. For example
7 % 2
1
12 % 3
0
29
C Programming
Increment/Decrement Operators
•
In C, specialized operators have been set aside for the incrementing and decrementing of integer variables. The increment and decrement operators are
++ and -- respectively. These operators allow a form of shorthand in C:
++i;
--i;
•
is equivalent to is equivalent to
i=i+1; i=i-1; The above example shows the prefix form of the increment/decrement operators. They can also be used in postfix form, as follows i++; i--;
is equivalent to is equivalent to
i=i+1; i=i-1; 30
C Programming
Prefix versus Postfix
•
•
•
The difference between prefix and postfix forms shows up when the operators are used as part of a larger expression.
– If ++k is used in an expression, k is incremented before the expression is evaluated. – If k++ is used in an expression, k is incremented after the expression is evaluated. Assume that the integer variables m and n have been initialized to zero. Then in the following statement a=++m + ++n; m 1, n
1, then a
2
whereas in this form of the statement a=m++ + n++; a 0 then m
1, n
1
31
C Programming
Advanced Assignment Operators
•
A further example of C shorthand are operators which combine an arithmetic operation and a assignment together in one form. For example, the following statement k=k+5; can be written as k += 5;
•
The general syntax is variable = variable op expression;
•
can alternatively be written as variable op= expression;
•
common forms are:
+=
-=
*=
Examples: j=j*(3+x); a=a/(s-5);
j *= 3+x; a /= s-5;
•
/=
%=
32
C Programming
Precedence & Associativity of Operators
•
The precedence of operators determines the order in which operations are performed in an expression. Operators with higher precedence are employed first. If two operators in an expression have the same precedence, associativity determines the direction in which the expression will be evaluated.
•
C has a built-in operator hierarchy to determine the precedence of operators.
Operators higher up in the following diagram have higher precedence. The associativity is also shown.
- ++ -* / %
+ =
R
L
L
R
L
R
R
L
33
C Programming
Precedence & Associativity of Operators Examples
•
This is how the following expression is evaluated
1 + 2 * 3 - 4
1 + 6 - 4
7 - 4
3
•
The programmer can use parentheses to override the hierarchy and force a desired order of evaluation. Expressions enclosed in parentheses are evaluated first. For example:
(1 + 2) * (3 - 4)
3 * -1
-3
34
C Programming
The int Data Type
•
A typical int variable is in the range +-32,767. This value differs from computer to computer and is thus machine-dependent. It is possible in C to specify that an integer be stored in more memory locations thereby increasing its effective range and allowing very large integers to be stored. This is accomplished by declaring the integer variable to have type long int. long int national_debt;
• long int variables typically have a range of +-2,147,483,648.
• There are also short int variables which may or may not have a smaller range than normal int variables. All that C guarantees is that a short int will not take up more bytes than int.
• There are unsigned versions of all three types of integers. Negative integers cannot be assigned to unsigned integers, only a range of positive values. For example unsigned int salary;
• typically has a range of 0 to 65,535.
35
C Programming
The float and double Data Types
•
As with integers the different floating point types available in C correspond to different ranges of values that can be represented. More importantly, though, the number of bytes used to represent a real value determines the precision to which the real value is represented. The more bytes used the higher the number of decimal places of accuracy in the stored value. The actual ranges and accuracy are machine-dependent.
•
The three C floating point types are: float double long double
•
In general, the accuracy of the stored real values increases as you move down the list.
36
C Programming
The char Data Type
•
Variables of type char take up exactly one byte in memory and are used to store printable and non-printable characters. The ASCII code is used to associate each character with an integer (see next page). For example the
ASCII code associates the character ‘m’ with the integer 109. Internally, C treats character variables as integers.
37
C Programming
ASCII Character Set
Ctrl
^@
^A
^B
^C
^D
^E
^F
^G
^H
^I
^J
^K
^L
^M
^N
^O
^P
^Q
^R
^S
^T
^U
^V
^W
^X
^Y
^Z
^[
^\
^]
^^
^_
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Code
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
SOH
ST
SLE
CS1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SIB
ESC
FS
GS
RS
US
Decimal
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Char sp !
"
#
$
%
&
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
Decimal
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Char sp !
"
#
$
%
&
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
Decimal
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Char
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
Decimal
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Char
`
a b c d e f g h I j k l m n o p q r s t u v w x y z {
|
}
~
DEL
38
C Programming
Automatic Type Conversion
•
•
•
How does C evaluate and type expressions that contain a mixture of different data types? For example, if x is a double and i an integer, what is the type of the expression x+i In this case, i will be converted to type double and the expression will evaluate as a double. NOTE: the value of i stored in memory is unchanged.
A temporary copy of i is converted to a double and used in the expression evaluation. This automatic conversion takes place in two steps. First, all floats are converted to double and all characters and shorts are converted to ints. In the second step “lower” types are promoted to “higher” types. The expression itself will have the type of its highest operand. The type hierarchy is as follows long double double unsigned long long unsigned
39
int
C Programming
Automatic Type Conversion with Assignment Operator
•
Automatic conversion even takes place if the operator is the assignment operator. This creates a method of type conversion. For example, if x is double and i an integer, then x=i; • i is promoted to a double and resulting value given to x
•
On the other hand say we have the following expression: i=x; •
A conversion occurs, but result is machine-dependent
40
C Programming
Type Casting
•
Programmers can override automatic type conversion and explicitly cast variables to be of a certain type when used in an expression. For example,
(double) i
•
will force i to be of type double. The general syntax is
(type) expression
•
Some examples,
(char) 3 + 'A' x = (float) 77;
(double) k * 57
41
C Programming
Input and Output
•
•
•
•
•
•
•
Basic Output printf Function
Format Specifiers Table
Common Special Characters for Cursor Control
Basic Output Examples
Basic Input
Basic Input Example
42
C Programming
Basic Output
•
Now, let us look more closely at the printf() statement. In a previous program, we saw this example print("value of sum is %d\n",sum);
•
which produced this output: value of sum is 33
•
The first argument of the printf function is called the control string. When the printf is executed, it starts printing the text in the control string until it encounters a % character. The % sign is a special character in C and marks the beginning of a format specifier. A format specifier controls how the value of a variable will be displayed on the screen. When a format specifier is found, printf looks up the next argument (in this case sum), displays its value and continues on. The d character that follows the % indicates that a (d)ecimal integer will be displayed. At the end of the control statement, printf reads the special character \n which indicates print the new line character.
43
C Programming
printf Function
•
General form of printf function printf(control string,argument list);
•
where the control string consists of 1) literal text to be displayed, 2) format specifiers, and 3)special characters. The arguments can be variables, constants, expressions, or function calls -- anything that produces a value which can be displayed. Number of arguments must match the number of format identifiers. Unpredictable results if argument type does not “match” the identifier.
44
C Programming
Format Specifiers Table
•
The following table show what format specifiers should be used with what data types:
Specifier
Type
%c
character
%d
decimal integer
%o
octal integer (leading 0)
%x
hexadecimal integer (leading 0x)
%u
unsigned decimal integer
%ld
long int
%f
floating point
%lf
double or long double
%e
exponential floating point
%s
character string
45
C Programming
Common Special Characters for Cursor Control
•
Some common special characters for cursor control are:
\n
newline
\t
tab
\r
carriage return
\f
form feed
\v
vertical tab
\b
backspace
\”
Double quote (\ acts as an “escape” mark)
\nnn
octal character value
46
C Programming
Basic Output Examples
printf(“ABC”); printf(“%d\n”,5); printf(“%c %c %c”,’A’,’B’,’C’); printf(“From sea ”); printf(“to shining “); printf (“C”); printf(“From sea \n”); printf(“to shining \n“); printf (“C”); leg1=200.3; leg2=357.4; printf(“It was %f miles”,leg1+leg2); num1=10; num2=33; printf(“%d\t%d\n”,num1,num2); big=11e+23; printf(“%e \n”,big); printf(“%c \n”,’?’); printf(“%d \n”,’?’); printf(“\007 That was a beep\n”);
ABC (cursor after the C)
5 (cursor at start of next line)
A B C
From sea to shining C
From sea to shining
C
It was 557.700012 miles
10
33
1.100000e+24
?
63 try it yourself
47
C Programming
Basic Input
•
There is a function in C which allows the programmer to accept input from a keyboard. The following program illustrates the use of this function.
#include
main()
{
int pin; printf("Please type in your PIN\n"); scanf("%d",&pin); printf("Your access code is %d\n",pin);}
•
What happens in this program? An integer called pin is defined. A prompt to enter in a number is then printed with the first printf statement. The scanf routine, which accepts the response, has a control string and an address list.
In the control string, the format specifier %d shows what data type is expected.
The &pin argument specifies the memory location of the variable the input will be placed in. After the scanf routine completes, the variable pin will be initialized with the input integer. This is confirmed with the second printf statement. The & character has a very special meaning in C. It is the address operator. (Much more with & when we get to pointers…)
48
C Programming
Basic Input Example
#include
main()
{
int pin; printf("Please type in your PIN\n"); scanf("%d",&pin); printf("Your access code is %d\n",pin);}
•
A session using the above code would look like this
Please type your PIN
4589
Your access code is 4589
•
The format identifier used for a specific C data type is the same as for the printf statement, with one exception. If you are inputting values for a double variable, use the %lf format identifier.
•
White space is skipped over in the input stream (including carriage return) except for character input. A blank is valid character input.
49
C Programming
Program Looping
•
•
•
•
•
•
•
•
•
•
•
•
•
Introduction to Program Looping
Relational Operators
Relational Operators Table for Loop for Loop Example for Loop Diagram
General Comments about for Loop
General Comments about for Loop Continued while Loop while Loop Example do while Loop do while Loop Example do while Loop Example: Error Checking
50
C Programming
Introduction to Program Looping
•
Program looping is often desirable in coding in any language to have the ability to repeat a block of statements a number of times. In C, there are statements that allow iteration of this type. Specifically, there are two classes of program loops -- unconditional and conditional. An unconditional loop is repeated a set number of times. In a conditional loop the iterations are halted when a certain condition is true. Thus the actual number of iterations performed can vary each time the loop is executed.
51
C Programming
Relational Operators
•
Our first use of these operators will be to set up the condition required to control a conditional loop. Relational operators allow the comparison of two expressions. Such as a < 4
•
which reads a “less than” 4. If a is less than 4, this expression will evaluate to
TRUE. If not it will evaluate to FALSE.
•
Exactly what does it mean to say an expression is TRUE or FALSE? C uses the following definition
– FALSE means evaluates to ZERO
– TRUE means evaluates to any NON-ZERO integer(even negative integers)
52
C Programming
Relational Operators Table
•
The following table shows the various C relational operators
Operator
Example
==
Equal to
count == 10
!=
Not equal to
flag != DONE
<
Less than
a < b
end_of_list
>=
•
Meaning
Greater than or equal to
lap >= start
The relational operators have a precedence below the arithmetic operators.
53
C Programming
for Loop
•
The for loop is C’s form of an unconditional loop. The basic syntax of the for statement is, for (initialization expression; test expr; increment expr) program statement;
•
Here is an example sum=10; for (i=0; i