(Lecture 3) Fundamentals of C Programming Language - 2025
(Lecture 3) Fundamentals of C Programming Language - 2025
Programming Language
✓ Chapter 2: “C Fundamentals”
3
Learning Objectives
▪ To understand basic elements used to construct simple
C statements.
▪ To differentiate identifiers and Keyword in C language
▪ To understand basic data type in C language
▪ To understand Variables and Array in C language
▪ To understand how to declare variables and array in C
▪ To understand Expressions and Statement in C with
examples
4
Outlines
▪ Introduction
▪ The C Character Set
▪ Identifiers and Keywords
▪ Data Types
▪ Variables and Arrays
▪ Declarations
▪ Expressions
▪ Statements
5
Introduction
▪ There are basic elements used to construct a simple
C statement, which include
– the C character set, identifiers & keywords, data types,
constants, variables and arrays, declarations, expressions
and statements.
▪ This week we will see how these basic elements can
be combined to form more comprehensive program
components in C programming language.
6
The C Character Set
▪ UPPERCASE characters: letters A to Z,
▪ lowercase - letters a to z
▪ certain special characters
– The special characters are listed below.
7
THE C CHARACTER SET
❖ Most versions of the language also allow certain other
characters, such as @ and $, to be included within strings and
comments.
8
IDENTIFIERS and KEYWORDS
❖ Identifiers are names that are given to various program
elements, such as variables, functions and arrays.
10
Examples 1
11
Example 2
The following are are NOT valid Identifiers
4th The first character must be a letter
“x” illegal character (“)
order-no illegal Character (-)
error flag illegal character (blank space)
12
Example 3 Scenario
▪ Consider as identifiers:
file_manager and file_management
– both are grammatically valid.
– Some compilers may be unable to distinguish between
them because the first eight letters are the same for each
identifier.
– Therefore, only one of these identifiers should be used in a
single C program.
13
Example 4 Scenario
❖ A C program is being written to calculate the future value
of an investment.
– The identifiers value or future_value are appropriate
symbolic names.
– However, v or fv would probably be too brief, since the intended
representation of these identifiers is not clear.
▪ On the other hand, the identifier
future_value_of_an_investment
– would be unsatisfactory because it is too long and cumbersome.
14
Keyword
❖Keyword are reserved words that have standard,
predefined meanings in C.
15
Standard Keywords in C
16
DATA TYPES
❖The basic data type in C
Data Description Typical Memory
Type Requirement
Int Integer quantity 2 byte or one
word
char Single character 1 byte
Float floating-point number (i.e., a number containing a 1 word (4
decimal point and/or an exponent) byte)
Double double-precision floating-point number (i.e., more 2 words
2 words (8 bytes) significant figures, and an
exponent which maybe larger in magnitude)
17
Data Type Qualifiers
❖ The basic data types can be augmented by the use of the data type
qualifiers.
19
CONSTANTS
There are four basic types of constants in C.
1. integer constants,
2. floating-point constants,
3. character constants and
4. string constants (there are also enumeration Constants),
✓ integer and floating-point constants represent numbers often
referred to collectively as numeric-type constants
20
Rules apply to all numeric-type constants
1. commas and blank spaces cannot be included within the
constant.
2. The constant can be preceded by a minus (-) sign if desired.
– Actually the minus sign is an operator that changes the sign of a
positive constant, though it can be thought of as a part of the
constant itself.
3. The value of a constant cannot exceed specified minimum
and maximum bounds.
– For each type of constant, these bounds will vary from one C
compiler to another.
21
Constant: Integer Constants
❖ An integer constant is an integer-valued number
consists of a sequence of digits.
▪ If the constant contains two or more digits, the first digit must be
something other than 0.
23
Example 5
▪ The following decimal integer constants are written
incorrectly for the reasons stated:
12,245 illegal character (,)
36.0 illegal character (.)
10 20 30 illegal character (blank space)
123-45-6789 illegal character (-)
0900 the first digit cannot be a zero
24
Constant: Floating-Point Constants
A floating-point constant is a
base- 10 number that contains
either a decimal point or an
exponent (or both).
25
Example 6
Several valid floating-point constants are shown below:
0. 1. 0.2 827.602
50000. 0.000743 12.3 315.0066
2E-8 0.006e-3 1.6667E+8 2121212e12
26
Example 7
The following are NOT valid floating-point constants
for the reasons stated.
1 Either a decimal point or an exponent must be present.
27
Constant: Character Constants
❖A character constant is a single character,
enclosed in apostrophes (i.e., single quotation
marks).
29
Representing Characters in Bytes
(using coding scheme)
❖ Each character on the computer
keyboard is represented by a binary
number (in Byte).
▪ This means that when, for example you press
the ‘A’ key, the binary number 01000001 is sent
to the CPU as a data input instruction.
▪ The CPU will then check the ASCII Table to see
which character this is before
sending/outputting it in the desired memory or
output location.
▪ On the ASCII Table, ‘A’ has an ASCII value 65
which is ‘01000001’ in binary form.
30
Example 8
▪ Several character constants and their corresponding
values, as defined by the ASCII character set, are shown
below (see pg 31 of the main textbook)
Constant ASCII Value BINARY DIGIT (BIT) VALUE
'A' 65 01000001
‘x' 120 01111000
'3' 51 00110011
'7' 55 00110111
‘!‘ 33 00100001
31
Representing Characters in Bytes using coding scheme:
ASCII - American Standard Code for Information Interchange
32
Computers Use Binary System To Represent & Compute Data
33
Escape Sequences
▪ Certain non-printing characters, as well as the backslash (\) and
the apostrophe (‘), can be expressed in terms of escape
sequences.
▪ An escape sequence always begins with a backward slash (\) and
is followed by one or more special characters.
▪ For example, a line feed (LF), which is referred to as a newline
in C, can be represented as \n.
▪ Such escape sequences always represent single characters,
even though they are written in terms of two or more characters.
34
The commonly used escape sequences
35
Constant: String Constants
❖ A string constant consists of any number of consecutive
characters (including none), enclosed in (double) quotation
marks.
\t (horizontal tab)
\n (newline)
▪ This special character is not visible when the string is displayed
38
String Constants
39
String Constants
❖ How many Character are in this string constant?
44
Example 11: A C program contains the following lines
int a, b, c;
char d;
a = 3;
b = 5;
c = a+b;
d = ‘a’;
a = 4;
b = 2;
c = a-b;
d = ‘W’;
45
Example 11:
Explanation a C program contains the following lines
Every C statement end with a semicolon
int a, b, c; The first two lines are type declarations, which state that a, b and c are integer variables,
char d; and that d is a char-type variable.
a = 3; Next four lines: the integer quantity 3 is assigned to a, 5 is assigned to b, and the quantity
b = 5; represented by the sum a + b (i.e., 8) is assigned to c. The character 'a' is then assigned
c = a+b; to d. In the third line, the values of the variables a and b are accessed simply by writing
d = ‘a’; the variables on the right-hand side of the equal sign.
a = 4; The last four lines redefine the values assigned to the variables as follows: the integer
b = 2; quantity 4 is assigned to a, replacing the earlier value, 3; then 2 is assigned to b,
c = a-b; replacing the earlier value, 5; then the difference between a and b (i.e., 2) is assigned to
d = ‘W’; c, replacing the earlier value, 8.
Finally, the character ' W ' is assigned to d, replacing the earlier character, ' a ' .
46
Array
❖ The array is another kind of variable that is used extensively in C.
▪ An array is an identifier that refers to a collection of data items that all have
the same name.
▪ The data items must all be of the same type (e.g., all integers, all characters, etc.).
▪ The individual array elements are distinguished from one another by the value that is
assigned to a subscript.
47
Array
▪ Suppose that x is a 10-element array.
▪ The first element is referred to as x[0],the second as x[1],and
so on. The last element will be x[9].
– The subscript associated with each element is shown in square
braces.
▪ The value of the subscript for the first element is 0, the value
of the subscript for the second element is 1, and so on.
▪ For an n-element array, the subscripts always range from 0 to n-1 .
48
Array
49
DECLARATIONS
❖ A declaration associates a group of variables with a
specific data type.
▪ All variables must be declared before they can appear in executable
statements.
▪ A declaration consists of a data type, followed by one or more variable
names, ending with a semicolon.
▪ Typical values are two bytes for each short integer variable, and four bytes (one
word) for each ordinary integer variable.
▪ The maximum permissible values of a, b and c will be smaller than the
maximum permissible values of p and q when using a compiler of this type.
52
Example 14
➢ A C program contains the following type declarations.
int a, b;
unsigned x, y;
▪ The unsigned variables x and y can represent values that are twice as large as
the values represented by a and b.
53
Example 15
➢ A C program contains the following type declarations.
float cl, c2, c3;
double root1, root2; (This is more common)
long float rootl, root2;
54
Example 16
➢ A C program contains the following type declarations.
▪ Initial values can be assigned to variables within a type
declaration
int c = 12;
char star = "*";
float sum = 0.;
double factor = 0.21023e-6;
55
Example 16: Explanation
int c = 12;
char star = "*";
float sum = 0.;
double factor = 0.21023e-6;
57
Example 18
58
Example 19
➢ A C program contains the following type declaration.
char text [ ] = "California";
➢ The declaration could also have been written by explicitly specified the size of the array as
char text [ll] = "California";
➢ If the size is too small, e.g.,
char text[10] = "California";
✓ the characters at the end of the string (in this case, the null character) will be lost.
➢ If the size is too large, e.g.,
char text[20] = "California";
✓ the extra array elements may be assigned zeros, or they may be filled with meaningless
characters.
59
EXPRESSIONS
▪ An expression represents a single data item, such as a
number or a character.
62
Example 20: Explanation
1. addition operator (+). This expression represents the sum of the values
assigned to the variables a and b.
a + b 2. involves the assignment operator (=). In this case, the expression
x = y causes the value represented by y to be assigned to x.
c = a + b 3. the value of the expression (a + b) is assigned to the variable c.
4. have the value 1(true) if the value of x is less than or equal to
x <= y the value of y. Otherwise, the expression will have the value 0(false).
x == y 5. is a test for equality not an assignment expression. Thus, the
expression will have the value 1 (true) if the value of x is equal to
++i the value of y. Otherwise, the expression will have the value 0 (false)
6. the value of the variable i is increased by 1 (i.e., incremented).
Thus, the expression is equivalent to i = i +1. The operator ++,
indicates incrementing, is called a unary operator
63
STATEMENTS
❖A statement causes the computer to carry out
some action.
70
SYMBOLIC CONSTANTS
▪ where name represents a symbolic name, typically written in uppercase
letters, and
▪ text represents the sequence of characters that is associated with the
symbolic name.
▪ Note that text does not end with a semicolon, since a symbolic constant
definition is not a true C statement.
▪ Moreover, if text were to end with a semicolon, this semicolon would be
treated as though it were a part of the numeric constant, character
constant or string constant that is substituted for the symbolic name.
71
Example 21
✓ Then,
area = 3.141593*radius*radius;
72
Example 22
#define CONSTANT 6.023E23
int c =5;
. . .
printf("CONSTANT = %f", c);
printf("CONSTANT = %f”, CONSTANT);
75
Exercise 3
➢ Write appropriate declarations and assign the given initial values for
each group of variables and arrays.
(a) Floating-point variables: a = -8.2, b = 0.005
Integer variables: x = 129, y = 87, z = -22
Character variables: c1 = ‘w’ , c2 = ‘&’
(6) Double-precision variables: d1 = 2.88x10-8, d2 = -8.4x105
Integer variables: u = 711 (octal), v = ffff (hexadecimal)
(c) Long integer variable: big = 123456789
Double-precision variable: c = 0.3333333333
Character variable: eol = newline character
(d) One-dimensional character array: message= 'ERROR’
76
Exercise 4
77
Exercise 5
➢ Identify whether each of the following statements is an expression statement, a compound statement or a control
statement.
(e) {
(a) a*(b+c);
++x ;
(d) if(x>0)
(b) while (a < 100)
{ {
d = a * (b + c ) ; { y = 2.0;
++a; ++x ; z = 6.0;
} if(x>0) }
y = 2.0; else
(c) if(x>0) {
else
y = 2.0;
y = 3.0; y = 3.0;
else
y = 3.0; printf("%f”, y); z= 9.0;
} }
}
78
Exercise 6
➢ Write an appropriate definition for each of the following
symbolic constants, as it would appear within a C program.
79
Exercise 7
80
Exercise 8
81
Exercise 9
82
Exercise 10
83