1. Overview to C Language
11
2.1 Brief History of C
C’s ancestors include Combined Programming Language (CPL), Basic Combined
Programming Language (BCPL), B, and ALGOL 68. CPL was developed at
Cambridge University in the early 1960’s. BCPL is a simple systems language
developed by Martin Richards in 1967 [SEBE96]. A systems language is a
programming language used to create operating systems (like Windows and DOS)
and compilers (like Visual Basic). ALGOL68 or Algorithmic Language, on the other
hand, was developed for scientific applications.
The first high-level language implemented under the UNIX operating system
was designed and implemented in 1970 in Bell Laboratories by Ken Thompson. This
was B, which was based on BCPL.
Neither BCPL nor B is a typed language. Being untyped means that all data
are considered machine words, which may be simple but leads to many
complications. Because of this complication and several other problems, it led to the
development of a new typed language based on B. Originally called NB (New B) but
later named C, it was designed and implemented by Dennis Ritchie at Bell
Laboratories in 1972. In some cases through BCPL, and in other cases directly, C
was influenced by ALGOL 68.
For more than a decade since C’s inception, the only standard was the book by
Kernighan and Ritchie. Over time, several implementers added different features thus
creating different versions of C. Between 1982 and 1988, ANSI produced a new
official description of C which included many of the features that implementors had
already incorporated into the language.
2.2 Types of Data
A C program is composed of a sequence of characters that are grouped by a compiler
into identifiable tokens. These tokens can be classified as literals, identifiers, and
keywords.
Timeline:
1963 1967 1968 1970 1972
CPL BCPL ALGOL68 B C
2. Chapter 2
12
2.2.1 Literals
Literals are classified under numeric and non-numeric literals.
2.2.1.1 Numeric Literals
Numeric literals can be further subdivided into whole numbers and real numbers.
Whole numbers are also called integers. These types of numbers do not contain any
fractional part and it should not have a decimal point. Real numbers, on the other
hand, are also called floating point numbers. These numbers have fractional parts and
it could also be expressed using scientific notation.
Exponential representation may be used to represent real numbers. An
exponential representation consists of an integer or real value followed by e then an
integer value in the exponential base 10.
Example. 2.5e2 is the same as 2.5 x 10
2
, which is equivalent to 250.0
2e4 is the same as 2 x 10
4
, which is equivalent to 20000.0
2e0 is the same as 2 x 10
0
, which is equivalent to 2.0
2.e5 is the same as 2.0 x 10
5
, which is equivalent to 200000.0
In defining numeric literals, the following rules should be followed:
1. No comma.
2. No space between the unary sign (+ or -) and the digits.
3. Must begin and end with a digit.
2.2.1.2 Non-Numeric Literals
Non-numeric literals may be in the form of a character or a series of characters. A
series of characters is called a string. A string should be enclosed in double quotes,
while a character is enclosed in single quotes. Some special characters may be used,
but it should be preceded by the escape character (backslash).
Example. ‘a’
‘I’
‘+’
‘2’
“De La Salle University”
“a string with double quotes ” within”
“a single backslash is in this string”
3. Overview to C Language
13
Other escape characters are listed below.
Escape Character Meaning
a alert
t tab
0 null character
’ single quote
% percent symbol
2.2.2 Identifiers
Identifiers are composed of a sequence of letters, digits, and the special character _
(underscore). Identifiers are defined by the programmer, thus they should be
descriptive. Avoid using names that are too short or too long. Limit the identifiers
from 8 to 15 characters only.
Some rules must be followed for defining identifiers. They are as follows:
1. It must consist only of letters, digits, and underscores.
Correct Example Incorrect Example
_duh x-1
thisisanidentifier2 num#
a large num
num_1 y?
2. An identifier cannot begin with a digit.
Incorrect Identifier
1name
3to4
3. An identifier defined in the C standard library should not be redefined.
Incorrect Identifier
printf
scanf
4. Identifiers are case sensitive; meaning uppercase is not equal to the
lowercase.
Example. ans ≠ Ans ≠ aNs ≠ anS ≠ ANs ≠ ANS
4. Chapter 2
14
2.2.2.1 Variables
Variables are identifiers that can store a value that can be changed. These can be of
different data types. They can store literals or some type of structured data.
Structured data can contain one or more values or data.
2.2.2.2 Constants
Constants are identifiers that can store a value that cannot be changed. Usually,
constant identifiers are all capital letters with underscores between each word to
distinguish them from variables.
2.2.3 Keywords
Keywords are words that have a strict meaning in C. These are special words
“reserved” by the programming language for processing, thus these may not be
redefined by the programmer. Some keywords are listed below.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
2.3 C Expressions
In the C language, expressions may either be arithmetic or logical, but the result
would be an integer or a real value.
2.3.1 Arithmetic Expressions
The following table will show the different operators and their uses. The rule shows
the type of operands where the operator can be used and the resulting data type given
the operands.
5. Overview to C Language
15
Operator Meaning Rule Example
+ addition integer + integer = integer
integer + real = real
real + integer = real
real + real = real
5 + 2 = 7
5 + 2.0 = 7.0
5.0 + 2 = 7.0
5.0 + 2.0 = 7.0
- subtraction integer - integer = integer
integer - real = real
real - integer = real
real - real = real
5 - 2 = 3
5 - 2.0 = 3.0
5.0 - 2 = 3.0
5.0 - 2.0 = 3.0
* multiplication integer * integer = integer
integer * real = real
real * integer = real
real * real = real
5 * 2 = 10
5 * 2.0 = 10.0
5.0 * 2 = 10.0
5.0 * 2.0 = 10.0
/ division integer / integer = integer
integer / real = real
real / integer = real
real / real = real
5 / 2 = 2
5 / 2.0 = 2.5
5.0 / 2 = 2.5
5.0 / 2.0 = 2.5
% remainder integer % integer = integer 5 % 2 = 1
-5 % 2 = -1
5 % -2 = 1
-5 % -2 = -1
In evaluating arithmetic expressions, the following rules should follow:
1. Parenthesis First. Evaluate expressions that are enclosed in parenthesis.
If there are nested parenthesis, evaluate from inside out.
2. Operator Precedence. Evaluation of operators should follow a hierarchy
of priorities. Evaluate expressions with higher priority operators first.
unary +, - (positive and negative) highest
*, /, %
binary +, - (addition and subtraction) lowest
3. Associativity Rule. If expression to be evaluated have operators that are in
the same precedence level, evaluate the expression from left to right.
6. Chapter 2
16
Example. z = 8 a = 3 b = 9 w = 2 y = -5
z – ( a + b / 2 ) + w * -y
8 3 9 2 -5
4
7
5
10
1
11
2.3.2 Logical and Relational Expressions
In C, evaluation of logical and relational expressions return 0 for false and 1 (or any
nonzero value) for true.
The relational and equality operators are shown below.
Relational Operators Equality Operators
< less than == equal
<= less than or equal != not equal
> greater than
>= greater than or equal
The following shows the logical operators and their corresponding truth
tables.
Logical Operator Meaning Truth Table
&& and true && true = true
true && false = false
false && true = false
false && false = false
|| or true || true = true
true || false = true
false || true = true
false || false = false
! not !(true) = false
!(false) = true
7. Overview to C Language
17
Example.
salary < MIN_SALARY || dependents > 5
temperature > 90.0 && humidity > 0.90
n >= 0 && n <= 100
!(0 <= n && n <= 100)
Rules in evaluating logical and relational expressions are similar with
evaluating arithmetic expressions. These operators also follow precedence rules. The
updated list is as follows:
Operator Precedence
( ) highest
!, unary +, -
*, /, %
binary +, -
<, <=, >, >=
==, !=
&&
|| lowest
Example. flag = 0 y = 4.0 z = 2.0 x = 3.0
!flag || ( y + z >= x – z )
0 4.0 2.0 3.0 2.0
6.0
1.0
1
1
1
2.3.3 Converting Mathematical Formula to C Expression
To solve mathematical problems using the computer, the formula should be translated
to the programming language to be used. In this case, arithmetic operations should be
in C expressions.
Example.
b
2
– 4ac b * b – 4 * a * c
a+b
c+d
(a + b) / (c + d)
1
1+x
2 1 / (1 + x * x)
a x –(b + c) a * -(b + c)
8. Chapter 2
18
2.3.4 Converting English Conditions to C Expression
Solutions to problems may sometimes depend on a set of conditions. To use the
computer to solve such problems, these conditions should be converted to the C.
Example.
x and y are greater than z x > z && y > z
x is equal to 1.0 or 3.0 x == 1.0 || x == 3.0
x is in the range of z to y, inclusive z <= x && x <= y
x is outside the range of z to y !(z <= x && x <= y)
z > x || x > y
Self Evaluation Exercises
1. Determine if the following identifiers are valid or invalid.
a) L8r
b) star*ting
c) num_Values
d) 4u
e) one_i_aren’t
2. Evaluate the following expressions
a.) 10 + 23 % (17 – 4 * 2) / (24 – (7 + 15 % 2))
b.) 150 - (-6 + 8 * 4 – 22 % 4) – (5 – (15.2 / 2))
c.) (7 == 7.0) && ( 15 > 3) || !((7 >4) || (7 > 3))
d.) (8 > 13 % 3) || (7 > 22 % 3) && (5 == 30 / 6)
3. Convert the following mathematical equations to C expressions without adding
unnecessary parenthesis
a.) 1 + X
1 + 1
6 8
b.) (X)(Y)(Z)
(X
2
– Y
2
) + Y
4 + X
2 – Z
4. Convert the following statements to C expressions
a.) X is neither 6 nor 8
b.) X is any number except 1, 2, and 3
c.) REVENUE is at most 80% of SALES
d.) contestant’s HEIGHT is at least 175 cm and AGE is between 18 and 23,
inclusive
e.) X is between 100 and 200, exclusive, except 120, 130, and 180
9. Overview to C Language
19
5. Write the C statement that will convert an amount in dollars to its peso equivalent.
Assume that PhP1 is equal to $51.75.
Chapter Exercises
1. Determine if the following identifiers are valid or invalid.
a) 3id
b) 1_i_am
c) R3D3
d) int
e) per-capita
f) o_no_o_no
g) me_to-2
h) xYshouldI
i) phone#
j) MAX_SPEED
k) G
l) __yes
2. Determine if the following are valid or invalid whole number literals.
a) -10500
b) 435
c) 2,020
d) +50,000
e) 21.5
3. Determine if the following are valid or invalid real literals
a) 2.34e2
b) 15e-0.3
c) 125
d) 34,500.99
e) 0.005
4. Determine if the following are valid or invalid character literals.
a) ‘M’
b) ‘n1’
c) ‘’
d) ‘”’
e) ‘+’
f) ‘&’
5. Given x = 2.0 and y = 3.0, evaluate the following:
a) 2 – 4 * 3 + 26 / 2
b) (3 + 4) * x / 2 + y
c) 5 + 6.6 / 2.2 * 0.5
10. Chapter 2
20
6. Given i = 1, j = 2, k = 3, x = 5.5, and y = 7.7, evaluate the following whether they
yield a value of TRUE/FALSE:
a) i < (j – k)
b) (x – y) <= ((j – k) – 1)
c) (k + j) != (i + 1 * 4)
d) ! (1 == 1)
e) i && j
f) i == j && i + j == k || y == x + 2
g) –i <= j – k && !j
7. Assume the values a = 1, b = 2, c = 0, d = 5.0, and e = 25. What is the output of the
following:
a) a + b * c && a
b) 3 / a + b / e || c
c) 10 + 15 || 0 && 5 > 3 + 3
d) 1 + 2 > 3 * 4 || 5 && 3 > 4 == 0
e) 1 % 2 + 1 == 0 + 1 && 2
8. Convert the following conditions to C expressions:
a) Commission = (sales – sales X .10) * .25
b) Commission = (sales – sales X 10/100) * 25/100
c) Interest = Amount X Rate
d) Semiannual Interest = 600/10%
e) age is from 18 to 21 inclusive
f) water is less than 1.5 and also greater than 0.1
g) year is divisible by 4
h) speed is not greater than 55
i) y is greater than x and less than z
j) w is either equal to 6 or not greater than 3
9. Write the C statement that will compute for the area of a triangle given the base and
the height.
10. Write the C statement that will convert a Fahrenheit (F) measure to a Celsius (C)
measure. (C = 5/9 x (F – 32))
11. Write the C statement that will convert an amount in peso to its dollar equivalent.
Assume that PhP1 is equal to $51.75.
12. Write the C statement(s) that will compute the least number of Php5 and Php1 coins
given an amount. Example: There are 3 Php5 and 2 Php1 in Php17.