Chapter 2: Introduction to Language Semantics & Program Flow By Mr. Samwel Tarus
Overview Introduction to Character set C Tokens Management of Input and output in C language
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 + - * / = % & # ! ? ^ “ ‘ ~ \ | < > ( ) [ ] { } : ; . , _
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
Cont’d…
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
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:
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
Constants Values that do not change during the execution of a program. C supports several types of constants: eg
Primary constants
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
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
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
Data types C supports four classes of data types namely Primary or fundamental data types User-defined data types Derived data types Empty data set
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).
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
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:
Logical Operators Combine or evaluate logical and relational expressions. Logical AND (&&) Logical OR (||) Logical NOT (!) Nb : logical operators evaluate to either true of false
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
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
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;
Bitwise Operators To manipulation data / values at bit level. Eg : & Bitwise And | Bitwise Or ! Bitwise Not << Bitwise Shift left >> Bitwise Shift Right ~ Ones Complement
Comma operator Used to separate variables, constants, expressions in a program. Eg , int a,b,c ; {2,4,6,1,8,4}
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);
Program examples (Objective is mgt of I/O) A program to find the perimeter of a rectangle A program to compute simple Interest for the sum invested for any number of years. A program that reads the name of a student and find the total marks of four subjects. A program that checks the size of memory occupied variables in a program. Read on operator precedence and associativity
Control Structures / Statements Directs the flow of execution in a program. C language control statement are divided into three categories: Decision control structures Looping control structures 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
Decision Control Structures Facilitate the action of branching Examples of structures / statements; if statement if ... else statement nested if … else statement switch case statement
Looping Control structures Facilitate the action of repetition / iteration Examples: for loop do … while loop while loop
Jumping Control Structures Facilitate the action of jumping to some other point in the program: Examples: break statement continue statement goto statement
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; }
Cont’d …
Program example (for if control statement) Write a program to check if any input number is a an even number. Write a program to check if any input number is less than zero. Nb : also draw a flowchart of the same
Program demo for if .. else Program to check if any input number is either odd or even number Program to check if any input number is divisible by 5 Nb : also draw a flowchart for the same
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
Switch case switch( exp ) { case 1: statement; statement; break; case 2: statement; statement; break; case n: statement; statement; break; default: statement; statement; break; }
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.
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