3. Character set
• Collection of letters, digits, white spaces and special characters that are used to
form instructions in a program / used to represent information.
• The characters that can be used to form words, numbers and expressions depend
upon the computer on which the program is run. E.g
• LOWERCASE LETTERS a-z
• UPPER CASE LETTERS A-Z
• DIGITS 0, 1, 2, …. 9
• WHITE SPACES (Blank spaces)
• SPECIAL CHARACTERS
+ - * / = % & # ! ? ^
“ ‘ ~ | < > ( ) [ ]
{ } : ; . , _
4. C Tokens
Basic element recognized by the compiler.
Source-program text that the compiler does not break down into
component elements.
The smallest individual units that acts as basic building blocks in
program development.
E.g: Keywords, Identifiers (Variables), Constants, Operators
6. Keywords
Reserved words whose meaning is understood by the compiler.
Predefined words that serve as basic building blocks in program development.
They have standard, fixed, predefined meanings in C. These meanings cannot be
changed.
It cannot be used as an identifier.
All the keywords are written in lower case
The standard keywords are
Int, float, auto, break, continue, return, else, if, long, switch, default, do, whiule,
static, short, void, goto, sizeof, volatile, for char
7. Variable/Identifiers
Anything that changes in the execution of the program.
Named location in the memory that has a value.
Identifiers refer to the names of variables, functions and arrays.
These are user-defined names and consist of a sequence of letters and digits.
x = 3
x can hold different values at different times x is known as a variable
Variables are user-defined data types:
8. Rules regarding variable formation:
Variable names must not be keywords
Variable names must not have a white space in between
Variable names are case sensitive
Variable names can start or be separated with an underscore
Variable names must not begin with a digit
9. Constants
• Values that do not change during the execution of a program.
• C supports several types of constants: eg
11. Constants Explained
Integer constants:
sequence of digits (whole numbers) eg, 2, 78,45,0, 1,
Occupy 2 bytes in memory
Given by keyword int
Management of i/o is %d
Float Constants:
Sequence of digits with decimal points eg, 12.5, 0.003, 34.890
numbers that contain fractional part are called real or floating-point constants.
Occupies 4 bytes of memory
Given by the keyword float
Management of i/o is %f
12. Cont’d …
Character Constant:
Any single character enclosed in single quotes eg, ‘a’, ‘5’, ‘ ‘ ‘$’
Occupies 1 byte of memory
Given by the keyword char
Management of i/o is %c
String constant:
A string constant is a sequence of characters enclosed in double quotes.
The characters may be letters, numbers, special characters, blank space.
Ex: “KSR” “College of Arts and Science”
Occupies total number of characters + 1
Given by the keyword char
Management of i/o is %s
13. Escape Sequence / Charcters
Character combinations consisting of a backslash () followed by a letter or by
a combination of digits.
Used to format o/p
Examples:
a Alert
n New Line
t Tab
r Carriage Return
b Backspace
o Null
14. Data types
C supports four classes of data types namely
1. Primary or fundamental data types
2. User-defined data types
3. Derived data types
4. Empty data set
15. Primary data types
All C compilers support four fundamental data types namely;
integer (int),
character (char),
floating point (float) and
double precision floating point (double).
16. Operators and Expressions
• Operator: Symbol which helps the user to command the computer to do a
certain mathematical or logical manipulations.
• Sign or symbol that is used to manipulate two or more variables, constants,
expressions etc.
• Operators are used in C language program to operate on data and variables.
C has a rich set of operators.
• Expresion:
• Collection of variables, constants operators and must evaluate to a value
• E.g x = (y+2) < 9
19. Relational Operators
• Used to compare two or more variables, constants, and expressions in the
program: eg
• Used in making decisions, Always evaluate to either true or false:
20. Logical Operators
Combine or evaluate logical and relational expressions.
Logical AND (&&)
Logical OR (||)
Logical NOT (!)
Nb: logical operators evaluate to either true of false
21. Assignment Operator
(=), is used to assign the result of an expression to a variable.
C has a set of ‘shorthand’ assignment operators of the form
Var = value;
Var oper=value;
The Assignment Operator evaluates an expression on the right of the expression
and substitutes it to the variable on the left of the expression.
Example 1:
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
Example 2:
x + = 1 is same as x = x + 1
22. Increment and Decrement Operators
Use to increase or decrease the value of the variable by some value eg; ++ , --
The operator ++ adds 1 to the operand while -- subtracts 1.
Both are unary operators and can appear before the variable called prefix and
after the variable called postfix.
++a and --a (Prefix Increment / Decrement Operator)
a++ and a-- (Postfix Increment / Decrement Operator)
++a is equivalent to a = a + 1 or a += 1
--a is equivalent to a = a – 1 or a – = 1
23. Conditional Operators
Called ternary operator is denoted as ? :
Used to evaluate the value of an expression.
Syntax: res = (expression) ? True : false;
Eg; res = (x < y) ? 3 : 7;
24. Bitwise Operators
To manipulation data / values at bit level.
Eg:
& Bitwise And
| Bitwise Or
! Bitwise Not
<< Bitwise Shift left
>> Bitwise Shift Right
~ Ones Complement
25. Comma operator
Used to separate variables, constants, expressions in a
program.
Eg, int a,b,c;
{2,4,6,1,8,4}
26. Special Operators
& -Ampersand
- Address of operator
- Returns the address where the variable is residing in the memory
* -Asterik /Star
-Value at address operator
-Returns value contained in the address
Sizeof() - sizeof function operator
- Return the number of bytes occupied by a variable, constant in memory
Eg. x=sizeof(2);
27. Program examples (Objective is mgt of I/O)
1. A program to find the perimeter of a rectangle
2. A program to compute simple Interest for the sum invested for any number
of years.
3. A program that reads the name of a student and find the total marks of four
subjects.
4. A program that checks the size of memory occupied variables in a program.
Read on operator precedence and associativity
28. Control Structures / Statements
Directs the flow of execution in a program.
C language control statement are divided into three categories:
1. Decision control structures
2. Looping control structures
3. Jumping control structures
Branching: The process of taking the alternative course of action against the
other.
Looping: The process of repeating / iterating a block of statements any number of
times.
Jumping: Moving to a specified location / point in a program
29. Decision Control Structures
Facilitate the action of branching
Examples of structures / statements;
i. if statement
ii. if ... else statement
iii. nested if … else statement
iv. switch case statement
31. Jumping Control Structures
Facilitate the action of jumping to some other point in the program:
Examples:
i. break statement
ii. continue statement
iii. goto statement
32. If control structure
Control structure that considers only the true part of the condition;
Syntax:
Example 1;
if(condition)
Statement;
Example 2
if(condition)
{
Statement;
Statement;
Statement;
}
34. Program example (for if control statement)
a. Write a program to check if any input number is a an even number.
b. Write a program to check if any input number is less than zero.
c. Nb: also draw a flowchart of the same
37. Program demo for if .. else
a. Program to check if any input number is either odd or even number
b. Program to check if any input number is divisible by 5
Nb: also draw a flowchart for the same
40. Program demo for nested if … else
Write a program that reads the name and the marks scored by a
student, and to display the corresponding grade:
Use the given grading system:
< 0 marks Invalid marks
Between 0 and 39 grade = E, Fail
Between 40 and 49 Grade = D
Between 50 and 59 Grade = C
Between 60 and 69 Grade = B
Between 70 and 100 Grade = A
> 100 marks beyond the range
43. Switch case: Explanation
An expression must evaluate to a value. The value of the expression is
compared upon several cases in the switch case control structure.
The case that matches the value of an expression, is executed.
The break statement in each of the cases transfers the control of execution
outside the switch control structure.
The default option handles any mismatch in the control structure.
44. Program demo for switch case:
• Write a menu driven program to preform the following:
• Compute the perimeter of a rectangle
• Compute the sum of two numbers
Ie:
Main menu
1. perimeter
2. sum
Enter your choice