0% found this document useful (0 votes)
6 views83 pages

(Lecture 3) Fundamentals of C Programming Language - 2025

The document outlines the fundamentals of the C programming language, focusing on basic elements such as identifiers, keywords, data types, variables, and arrays. It provides learning objectives and a structured outline for understanding these concepts, along with examples and explanations of valid and invalid identifiers, constants, and escape sequences. The content is designed for a lecture in a programming course, emphasizing the construction of simple C statements and the importance of data types and variable declarations.

Uploaded by

osobsolape12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views83 pages

(Lecture 3) Fundamentals of C Programming Language - 2025

The document outlines the fundamentals of the C programming language, focusing on basic elements such as identifiers, keywords, data types, variables, and arrays. It provides learning objectives and a structured outline for understanding these concepts, along with examples and explanations of valid and invalid identifiers, constants, and escape sequences. The content is designed for a lecture in a programming course, emphasizing the construction of simple C statements and the importance of data types and variable declarations.

Uploaded by

osobsolape12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Fundamentals of C

Programming Language

Week 3  Lecture Note


The CSC121/COS121 Team @ 2025
2
RECOMMENDED Text
➢ Byron S. Gottfried “Theory and
Problems of Programming with
C” Second edition’ SCHAUM’S
Outline Series, McGRAW –HILL
(1996).

✓ 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.

❖ C uses certain combinations of these characters, such as:


\b as backspace
\n as newline
\t as horizontal tab
▪ These character combinations are known as escape sequences.

8
IDENTIFIERS and KEYWORDS
❖ Identifiers are names that are given to various program
elements, such as variables, functions and arrays.

❖ Identifiers consist of letters and digits, in any order,


except that the first character must be a letter.

❖ Uppercase and lowercase letters are not


interchangeable: it mean C language is Case sensitive.
9
IDENTIFIERS and KEYWORDS
▪ The underscore character ( _ ) is considered to be a
letter and can also be included in an identifier’s name.
– An underscore is often used in the middle of an identifier.
– An identifier may also begin with an underscore, though this is
rarely done in practice.
▪ An identifier can be arbitrarily long. (max. 31 Characters)
▪ Some compiler only recognises the first 8 characters

10
Examples 1

The following names are valid Identifiers:


x y12 sum_1
_temperature. names
area tax_rate TABLE

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.

▪ keywords can be used only for their intended purpose;


▪ keywords cannot be used as programmer-defined
identifiers (i.e. they are system defined)

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.

➢ They include: short, long, signed and unsigned

▪ For example, integer qualifiers can be defined as


short int,
long int
unsigned int
18
Take Note

✓ Every identifier that represents a number or a character


within a C program must be associated with one of the
basic data types before the identifier appears in an
executable statement.

▪ This is accomplished via a type declaration.

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.

❖ Integer constants can be written in three different number systems:


✓ decimal (base 10),
✓ octal (base 8) and
✓ hexadecimal (base 16).
22
decimal integer constant
▪ A decimal integer constant can consist of any combination of digits
taken from the set 0 through 9.

▪ If the constant contains two or more digits, the first digit must be
something other than 0.

▪ Several valid decimal integer constants are shown below.


0 1 743 5280 32787 9999

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.

1,000.0 Illegal character (,)

2E+10.2 The exponent must be an integer quantity (it cannot


contain a decimal point).

3E 10 Illegal character (blank space) in the exponent.

27
Constant: Character Constants
❖A character constant is a single character,
enclosed in apostrophes (i.e., single quotation
marks).

▪ EXAMPLE: Several character constants are shown below:


'A' 'X' '3' '?' ' '
✓ Notice that the last constant consists of a blank space
enclosed in apostrophes
28
Constant: Character Constants
▪ Character constants have integer values that are determined by the
computer's particular character set.

▪ Most computers, and virtually all personal computers, make use of


the ASCII (i.e., American Standard Code for Information
Interchange) character set, in which each individual character is
numerically encoded with its own unique 7-bit combination.

▪ (Hence a total of '2' = 128 different characters).

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.

▪ Several string constants are shown below:


"green" "Washington, D.C. 20005" "270-32-3456"
"$19.95" "THE CORRECT ANSWER IS: " " "
"2*( I+3)/J" " " "Line 1\nLine 2\nLine 3"
36
String Constants
▪ Note that the string constant
"Line 1\nLine 2\nLine 3"
▪ extends over three lines, because of the newline characters that are
embedded within the string.
▪ Thus, this string would be displayed as:
Line 1
Line 2
Line 3
• Also, notice that the string " " is a null(empty) string
37
String Constants
▪ The following string constant includes three special characters that are represented
by their corresponding escape sequences.

"\t To continue, press the \"RETURN\" key\n"

▪ The special characters are:

\t (horizontal tab)

\” (double quotation marks, which appears twice)

\n (newline)
▪ This special character is not visible when the string is displayed
38
String Constants

▪ The compiler automatically places a null


character (\0) at the end of every string
constant, as the last character within the string
(before the closing double quotation mark).

39
String Constants
❖ How many Character are in this string constant?

"\t To continue, press the \"RETURN\" key\n"


▪ It actually contains 38 characters. This includes
✓ five blank spaces,
✓ four special characters: 1 horizontal tab (\t), 2 quotation marks
(\”) and 1 newline (\n) represented by escape sequences,
✓ and the null character (\0) at the end of the string.
40
Note
❖character constant (e.g., 'A') and single-character
string constant (e.g. "A") are not equivalent.
• a character constant has an equivalent integer value: ‘A’
= 65
▪ a single-character string constant does not have an
equivalent integer value: "A” =/= 65. and,
▪ single-character string constant consists of two
characters
▪ the specified character followed by the null character (\0).
41
Example 10
❖ The character constant 'w' has an integer value of
119 in the ASCII character set. i.e. ‘w’ = 119
• It does not have a null character at the end.

❖ In contrast, the string constant "w" actually


consists of two characters:
✓ the lowercase letter w and the null character \0.
✓ This constant does not have a corresponding integer value.
42
VARIABLES AND ARRAYS
❖A variable is an identifier that is used to
represent some specified type of information
within a designated portion of the program.

▪ In its simplest form, a variable is an identifier that is


used to represent a single data item;
– i.e. a numerical quantity or a character constant.
43
VARIABLES AND ARRAYS
▪ The data item must be assigned to the variable at some point in the
program.
▪ The data item can then be accessed later in the program simply by
referring to the variable name.
▪ A given variable can be assigned different data items at various
places within the program.
▪ Thus, the information represented by the variable can change during
the execution of the program.
▪ However, the data type associated with the variable cannot change.

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 data items are represented by their corresponding array-elements


• (i.e., the first data item is represented by the first array element, 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

❖ There are several different ways to categorize arrays:


✓ integer arrays,
✓ character arrays,
✓ one dimensional arrays,
✓ multi-dimensional arrays

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.

▪ Each array variable must be followed by a pair of square brackets,


containing a positive integer which specifies the size (i.e., the
number of elements, e.g. student[5]) of the array.
50
Example 12
➢ A C program contains the following type declarations.
int a, b, c, Score[10];
float rootl, root2;
char flag, text[80];
▪ Thus, a, b and c are declared to be integer variables,
▪ Score is a 10-element int-type array with 10 integer scores
▪ rootl and root2 are floating-point variables,
▪ flag is a char-type variable and
▪ text is an 80-element, char-type array.
– Note the square brackets enclosing the size specification for text.
51
Example 13
➢ A C program contains the following type declarations.
Equivalent C program:
short int a, b, c;
short a, b, c;
long int r, s, t;
long r, s, t;
int p, q;
int p, q;

▪ 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.

▪ However, x and y cannot represent negative quantities.


✓ For example, if the computer uses 2 bytes for each integer quantity, then a
and b may take on values that range from -32768 to +32767,
✓ whereas the values of x and y may vary from 0 to +65535

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;

▪ With a particular C compiler, the double-precision variables rootl and root2


represent values that can vary (in magnitude) from approximately 1.7 x 10-308 to
1.7 x 10+308.
▪ However, the floating-point variables cl, c2 and c3 are restricted (in
magnitude) to the range 3.4 x 10-38 to 3.4 x 1038

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;

▪ Thus, c is an integer variable whose initial value is 12,


▪ star is a char-type variable initially assigned the character " *",
▪ sum is a floating-point variable whose initial value is 0 .
▪ factor is a double-precision variable whose initial value is 0.21023 x 10-6.
56
Example 17
➢ A C program contains the following type declaration.
char text[] = "California";

▪ This declaration will cause text to be an 11-element character array.


▪ The first 10 elements will represent the 10 characters within the word
California, and
▪ the 11th element will represent the null character (\0) which is automatically
added at the end of the string.

57
Example 18

➢ A C program contains the following type declaration.

char text[ll] = "California";

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.

▪ The expression may consist of a single entity, such as


a constant, a variable, an array element or a reference to
a function.

▪ It may also consist of some combination of such entities,


interconnected by one or more operators.
60
EXPRESSIONS
▪ The use of expressions involving operators is particularly common
in C, as in most other programming languages.

▪ Expressions can also represent logical conditions that are either


true or false.

▪ However, in C the conditions true and false are represented by the


integer values 1 and 0, respectively.

▪ Hence logical-type expressions really represent numerical


quantities.
61
Example 20
a + b
x = y
c = a + b
x <= y
x == y
++i

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.

▪ There are 3 different classes of statements in C.


1. expression statements,
2. compound statements and
3. control statements.
64
Statement: Expression statement
❖ An expression statement consists of an expression followed by a semicolon.
❖ The execution of an expression statement causes the expression to be
evaluated. ➢ The fourth expression statement causes the printf
function to be evaluated. This is a standard C
a = 3; library function that writes information out of the
c = a+b; computer
++i; ➢ In this case, the message Area = will be
printf("Area = %f”, area); displayed, followed by the current value of the
; variable area.
➢ Thus, if area represents the value 100, the
statement will generate the message Area = 100.
➢ The last expression statement does nothing, since
it consists of only a semicolon.
65
Statement: Compound statement
❖ A compound statement consists of several individual
statements enclosed within a pair of braces { }.
▪ A typical compound statement is shown below:
{
pi = 3.141593;
circumference = 2*pi*radius;
area = pi*radius*radius;
}
66
Statement: Control statements
❖ Control statements are used to create special program features,
such as logical tests, loops and branches.
– Many control statements require that other statements be embedded within them
while (count <= n)
{
printf('x = ');
scanf(“%f”, &x);
sum +=x;
++count;
}
67
Statement: Control statements
• This statement contains a compound
while (count <= n)
statement, which in turn contains four
{ expression statements.
printf(' x = '); • The compound statement will continue
scanf(“%f", &x); to be executed as long as the value of
sum +=x; count does not exceed the value of n.
• Note that count increases in value
++count;
during each pass through the loop.
}
68
SYMBOLIC CONSTANTS
▪ A symbolic constant is a name that substitutes for a sequence
of characters.
▪ The characters may represent a numeric constant, a character
constant or a string constant.
▪ Thus, a symbolic constant allows a name to appear in place
of a numeric constant, a character constant or a string.
▪ When a program is compiled, each occurrence of a symbolic
constant is replaced by its corresponding character
sequence.
69
SYMBOLIC CONSTANTS
▪ Symbolic constants are usually defined at the beginning
of a program.
▪ The symbolic constants may then appear later in the
program in place of the numeric constants, character
constants, etc. that the symbolic constants represent.
▪ A symbolic constant is defined by writing:
#define name text

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

#define TAXRATE 0.23 ✓ Notice that the symbolic names are


#define PI 3.141593 written in uppercase, to distinguish
#define TRUE 1 them from ordinary C identifiers.
#define FALSE 0
#define FRIEND "Susan" ✓ Also, note that the definitions do not
area = PI * radius * radius; end with semicolons

✓ Then,
area = 3.141593*radius*radius;

72
Example 22
#define CONSTANT 6.023E23
int c =5;
. . .
printf("CONSTANT = %f", c);
printf("CONSTANT = %f”, CONSTANT);

✓ The first print will output:


CONSTANT = 5

✓ The second print will output:


CONSTANT = 6.023E23
73
Exercises1
➢ Determine which of the following are valid identifiers.
▪ If invalid, explain why.
(a) record1 (e) $tax (i) name-and-address
(b) l record (f) name (j) 123-45-6789
(c) file_3 (g) name and address
(d) Return (h) name_and_address
74
Exercise 2
➢ Write appropriate declarations for each group
of variables and arrays.

(c) Integer variable: index


(a) Integer variables: p, q
Unsigned integer variable: cust_no
Floating-point variables: x, y, z
Double-precision variables: gross, tax, net
Character variables: a, b, c
(d) Character variables: current, last
Unsigned integer variable: count
(b) Floating-point variables: root1, root2
Floating-point variable: error
Long integer variable: counter (e) Character variables: first , last
Short integer variable: flag 80-element character array: message

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

➢ Explain the purpose of each of the following expressions.

(a) a-b (d) a>=b (f) a<(b/c)


(b) a*(b+c) (e) (a%5)==0 (g) --a
(c) d=a*(b+c)

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

You might also like