Professional Documents
Culture Documents
ng
Introduction to C Programming
Language
(Week 7)
The CSC121 Team
Learning Objectives
At the end of this lecture students should
understand:
– The structure of a C-program
– What variables, operators, expressions, statements,
compiler, keywords, header files, data types, input and
output functions are in C programming language
2
Introduction
There are many different languages that can be
used to program a computer.
The most basic of these is machine language
– It is a collection of very detailed, cryptic instructions
that control the computer’s internal circuitry
3
Introduction
Very few computer programs are actually written
in machine language because
– Machine language is very cumbersome to work with
– Machine language program written for one type of
computer cannot be run on another type of computer
without significant alterations
4
Introduction
Usually a computer program will be written in
some high-level language, whose instruction is
more compatible with human languages and
human thought processes.
Most of these are general-purpose languages such
as C.
5
Advantages of High-Level languages over Machine
Language
Simplicity
– It greatly simplifies the task of writing complete,
correct programs
Uniformity
– The rules for programming in a particular high-level
language are much the same for all computers
6
Advantages of High-Level languages over Machine
Language
Portability
– A program written for one computer can generally be
run on many different computers with little or no
alteration
7
Converting High-Level languages to Machine Language
8
Compiler
A compiler or interpreter is itself a computer
program.
It accepts a program written in high-level language
(e.g., C) as input, and generates a corresponding
machine-language as output
9
Compiler
The original high-level program is called the
source program, and the resulting machine-
language program is called the object program
10
Introduction to C
C is a general-purpose, structured programming
language
– General purpose in the sense that it can be used for
systems programming (e.g. for writing operating
systems) as well as for applications programming
(e.g. for writing programs to solve specific problems)
11
Introduction to C
C is a general-purpose, structured programming
language
– Structured in the sense that its instructions consist of
terms that resemble algebraic expressions, augmented
by certain English keywords such as if, else, for, do and
while
12
Structure of a C program
Every C program consists of one or more modules called
functions.
– One of the functions must be called main.
17
/* program to calculate the area of a circle */
printf("Radius = ? ");
Output function Statements
scanf("%f", &radius);
Input function
area = 3.14159 * radius * radius;
printf("Area = %f", area);
}
21
Data Types
The basic data types can be augmented by the use
of the data type qualifiers short, long, signed
and unsigned.
– For example, integer quantities can be defined as
short int, long int or unsigned int.
22
Operators
C includes a large number of operators which fall into
the following category:
– Arithmetic operators
– Unary operators
– Relational and logical operators
– Assignment operators
– The conditional operator
23
Arithmetic Operators
There are five arithmetic operator in C. They are
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division
24
Unary Operators
This class of operators act on a single operand to
produce a new value.
The next slide gives some examples
25
The various Unary Operators
Operator Name How it is written Example of Use
Unary minus A minus sign is made to preceed a -743,
numerical constant -0.2,
-3 * (x + y)
Pre-increment The increment operator (++) is placed ++counter
before the numeric variable
Post-increment The increment operator (++) is placed counter++
after the numeric variable
Pre-decrement The decrement operator (++) is placed --counter
before the numeric variable
Post-decrement The decrement operator (++) is placed counter--
after the numeric variable
26
Relational and logical Operators
There are four relational operators in C. They are:
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
27
Relational and logical Operators
Closely associated with the relational operators are
the following equality operators
Operator Meaning
== Equal to
!= not equal to
28
Relational and logical Operators
The relational and equality operators are used to
form logical expressions, which represent
conditions that are either true or false.
– The resulting expression will be of type integer, since
true is represented by integer 1 and false is represented
by the value 0
29
Examples of Relational Operators Usage
30
Examples of Relational Operators Usage
Expression Interpretation Value
i<j true 1
(i + j) >= k true 1
(j + k) > (i + 5) false 0
k != 3 false 0
j == 2 true 1
31
Assignment Operators
There are several different assignment operators in
C but the most commonly used assignment
operator is =.
Assignment expressions that make use of this
operator are written in the form
identifier = expression
32
Assignment Operators
Where identifier generally represents a variable,
and expression represents a constant, a variable or
a more complex expression.
Examples include
a=3
x=y
area = length * width
33
Assignment Operators
It is important to note that the assignment operator
= and the equality operator == are distinctly
different
34
The Conditional Operator
Simple conditional operations can be carried out
with the conditional operator (? :)
It is written in the form:
expression 1 ? expression 2 : expression 3
35
Using the Conditional Operator
In the conditional expression shown below, assume
that i is an integer variable:
(i < 0) ? 0 : 100
The expression thus reads if i is less than 0 then
value is 0 otherwise 100
36
Statements
A statement causes the computer to carry out some
action.
There are three different classes of statements in C
namely:
– Expression statements
– Compound statements
– Control statements
37
Expression Statements
Several expression statements are shown below:
a = 3;
c = a + b;
++i;
printf(“Area = %f”, area);
38
Compound Statements
They consist of several individual statements
enclosed with a pair of braces { }.
– The individual statements may themselves be
expression statements, compound statements or control
statements
Unlike an expression statement, a compound
statement does not end with a semicolon.
39
Example of a Compound Statement
{
pi = 3.141593;
circumference = 2 * pi * radius;
area = pi * radius * radius;
}
40 40
Control Statements
Control statements are used to create special
program features, such as
– Logical tests/branches: if-then, if-then-else
– Loops: for, while, do..while
41
Keywords
These are reserved words with predefined
meanings in C.
They cannot be used as programmer-defined
identifiers
42
Figure 2: Standard keywords in C
43
Header files
C is composed of several standard libraries.
Each library has a header also known as Header file.
It contains prototypes (used to describe the content of the
library) for all the functions available in that particular
library.
Some header files in C programming language are listed
in the table that follows.
44
Header files
Header files Description
<ctype.h> Used to test characters for certain properties.
Used for converting lowercase to uppercases vice versa.
<float.h> Contains the floating point size limits of the system.
<errno.h> Used to report error conditions.
<limits.h> Contains the integral size limit of the System.
<math.h> Used for mathematical operations
45 45
Header files
Header files Description
<stdio.h> Used for standard input/output operations
<stdlib.h> Used for conversion of text to numbers vice versa, memory
allocation and other utility operations
<string.h> Used for string processing operations
<time.h> Used for time and date manipulations and operations.
46 46
Output functions
The output functions include:
– putchar: allow single characters to be displayed to the
monitor
– printf: displays single characters, numerical values and
strings to the monitor
– puts: facilitates the output of strings to the monitor
47
The putchar Function
In a general, a function reference can be written as
putchar(character variable)
Example: a C program could contain the following
statements
char c;
.....
putchar(c);
48
The printf Function
The printf function is written as
printf(control string, arg1, arg2, . . . , argn)
Where control string refers to a string that contains
formatting information, and arg1, arg2, . . . , argn
are arguments that represent the individual output
data items
49
The printf Function Example
#include <stdio.h>
#include <math.h>
main() /* print several floating-point numbers */
{
float i = 2.0, j = 3.0;
printf("%f %f %f %f", i, j, i+j, sqrt(i+j));
}
50 50
The puts Function
The puts function offers a simple alternative to
printf
#include <stdio.h>
main() /* read and write a line of text */
{
char line[80];
gets(line);
puts(line);
}
51
Input functions
The input functions are:
– getchar: allow single characters to be obtained from the
keyboard
– scanf: allows single characters, numerical values and
strings to be obtained from the keyboard
– gets: facilitates the input of strings from the keyboard
52
The getchar Function
In general terms, a function reference can be written as
character variable = getchar();
Example: a C program contains the following statements
char c;
…..
c = getchar();
53
The scanf Function
The scanf function is written as
scanf(control string, arg1, arg2, . . ., argn)
Where control string refers to a string containing
certain required formatting information,
arg1, arg2, . . ., argn are arguments that represent
the individual input data items
54
The scanf Function
The control string consists of individual groups of
characters, with one character group for each input data
item.
Each character group must begin with a percent sign (%).
– A single character group consists of the percent sign, followed
by a conversion character which indicates the type of the
corresponding data item.
55
Conversion Character Meaning
c Data item is a single character
d Data item is a decimal integer
e Data item is a floating-point value
f Data item is a floating-point value
g Data item is a floating-point value
h Data item is a short integer
i Data item is a decimal, hexadecimal or octal integer
o Data item is an octal integer
s Data item is a string followed by a whitespace character
u Data item is an unsigned decimal integer
x Data item is a hexadecimal integer
56
scanf Example
#include <stdio.h>
main( )
{
char item[20];
int partno;
float cost;
.....
scanf("%s %d % f", item, &partno, &cost);
.....
}
57 57
The gets Function
The gets function offers a simple alternative to
scanf
#include <stdio.h>
main() /* read and write a line of text */
{
char line[80];
gets(line); // read a line of input
puts(line);
}
58
Question 1: Find the results of the following
equations by writing a C program given the
values A = 12, B = 3, C = 6, D = 2:
1. F = A + B/C - D ^2
2. F = (A + B)/ C - D ^2
3. F = A + B/(C - D ^ 2)
4. F = (A + B)\ D ^ 2
59
Question 2:
Roger would like to know the average of his test
scores. Write an equation that would calculate the
average given five test scores.
Write a C program to solve this problem.
60