2.1 Brief History of C: Overview To C Language
2.1 Brief History of C: Overview To C Language
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.
Timeline:
11
Chapter 2
2.2.1 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.
Example. ‘a’
‘I’
‘+’
‘2’
“De La Salle University”
“a string with double quotes \” within”
“a single backslash \\ is in this string”
12
Overview to C Language
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.
Incorrect Identifier
1name
3to4
Incorrect Identifier
printf
scanf
13
Chapter 2
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.
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.
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.
14
Overview to C Language
15
Chapter 2
7
5
10
1
11
In C, evaluation of logical and relational expressions return 0 for false and 1 (or any
nonzero value) for true.
The following shows the logical operators and their corresponding truth
tables.
16
Overview to C Language
Example.
salary < MIN_SALARY || dependents > 5
temperature > 90.0 && humidity > 0.90
n >= 0 && n <= 100
!(0 <= n && n <= 100)
Operator Precedence
() highest
!, unary +, -
*, /, %
binary +, -
<, <=, >, >=
==, !=
&&
|| lowest
6.0
1.0
1
1
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.
2
b – 4ac b*b–4*a*c
a+b
(a + b) / (c + d)
c+d
1
2 1 / (1 + x * x)
1+x
a x –(b + c) a * -(b + c)
17
Chapter 2
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
18
Overview to C Language
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
19
Chapter 2
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
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.
20