Slide 1 ITEC113 Algorithms and Programming Techniques

C programming: Variables, Expressions Part I

Slide 2 ### Objectives

- To understand what variables are
- initialization/garbage values
- naming conventions

- To learn about the frequently used data types
- To understand the components of
- an assignment Statements
- arithmetic expressions

- To learn about
- frequently used operators,
- operator precedence

Slide 3 ### VARIABLES

- Variables are basic data objects manipulated in a program.
- Each variable has to be declared before use.
- Each variable has a name and a data type.
- You can give initial value (variable initialization) on variable declaration.
Examples:

int x;

char gender;

float avg;

float sum=0;

char name[10];

int *fp;

Slide 4 ### VARIABLES

- Variable declaration allocates a cell in the main memory whose size is determined by the data type
- For example for int 4 bytes are used, for double 8 bytes are used

- When the variable is created in the main memory it contains garbage value
- This is due to the existence of 1’s and 0’s in the memory. 1 means high voltage, 0 means low voltage.

- It is a good idea to initialize variables before first usage.
- A variable name is the symbolic representation of the memory location that is allocated on variable declaration

Slide 5 ### Rules on Variable Names:

- DO NOT use reserved words as variable names
(e.g. if, else, int, float, case, for, …).

- The first character has to be a letter or underscore. It can not be a numeric digit.
The second and the other characters of the name can be any letter, any number, or an underscore “_”.

Examples

Some valid names:

my_name, m113_1, salary, bluemoon, _at

Some invalid names:

my name, my-name , 1stmonth , salary! , guns&roses ,

Slide 6 ### Tradition on Variable Names:

These are NOTrules but you can increase the quality of your program by using them!

- Select related and meaningful names indicating tasks of the variables.
- Do not use variable names that exceed 8 characters.
- Use small case letters for variable names.
- Upper case letters are mostly used in the names of symbolic constants.

Slide 7 ### Variable Declaration:

- Variable declaration is used to introduce the system to the variables that the programmer decides to use on the rest of the program.
- On variable declaration,
- variable name,
- data type
are declared.

- Also you can give the initial valueof the variable on its declaration.
Example : intk ;

intm=15;

floatfnumber= 1.75;

charch=’w’ ;

Slide 8 ### Data Types of the Variables :

- A variable data type specifies:
- The kind of value a variable can store
- The set of operations that can be applied to the variable

- There are 3 main different data types and their derivations for declaration in ANSI–C.

Slide 9 ### Data Types and Sizes : (Continued)

- Integers(int) :
- Integers are all numeric values that have no fractional or decimal components.
- Integer numbers may be positive or negative.
Examples :

13 7 –6 208 1024

- C compiler allocates 4 bytes (32 bits) to an integer (int) variable.
- An integer variable can store values in the range
- –32,768 through 32,767
- Derived Integers:
- short, long and unsigned are data types derived from int, and used to keep integer values.
- The sizes of long and short is differentiated from int.
- The data type unsigned is used only with positive integers.

Slide 10 ### Data Types and Sizes : (Continued)

- The sizes of long and short is differentiated from int.
- The data type unsigned is used only with positive integers.

Slide 11 ### Data Types and Sizes : (Continued)

Real Numbers:

Slide 12 ### Data Types and Sizes : (Continued)

- We can use Scientific Notation to represent real numbers that are very large or very small in value.
- The letters e or E is used to represent times 10 to the power.
Example:

- 1.23 x 10 5is represented in C as 1.23e5or 1.23e+5or 1.23E5
- 1 x 10 -9is represented in C as 1e-9

Slide 13 ### Data Types and Sizes : (Continued)

Character : ( char )

Slide 14 ### Data Types and Sizes : (Continued)

Categories of characters :

- Alphabetic Letters :
- ( Upper case : ‘A’ , ‘B’, …….. ‘Z’ )
- ( Lower case : ‘a’ , ‘b’, …….. ‘z’ )

- Numeric digits
- ( ‘1’,’2’,’3’,…………,’9’,’0’ )

- Special Characters
- ( blank, ‘+’,’#’,’-‘,’_’,……..)

- Control Characters

Slide 15 ### Assignment Statements

stores values 1 and 0 to cnt and sum.

stores character ‘Y’ to ch

stores computational results to sum and avg

Slide 16 ### Expressions

- Arithmetic Expressions involve arithmetic operators such as *,+,-,/,%:
- Relational Expressions involve relational operators that compare two values such as >,<,== etc:
- Logical Expressions involve the logical andandor operators && and || and are used to combine relational expressions:
- Example: ( a > b && c == 7 )

Slide 17 ### Arithmetic Expressions

In the Assignment Statement:

M = a * 5 + b % 4 ;

- The expression to the right of the assignment operator ( = ) involves an arithmetic operation that combines arithmetic operands with arithmetic operators.
- The most commonly used arithmetic operators are:
- Addition (+) Operator
- Subtraction (-) Operator
- multiplication (*) Operator
- division (/) Operator
- remainder (%) Operator

For real or integer numbers

For integer numbers only

Slide 18 ### Operator Precedence Rules

- Arithmetic expressions inside parentheses are executed first (left to right).
- Unary operators ( minus signs and plus signs) are executed before multiplications, divisions and remainder operations.
- Additions and subtractions are executed last.

parentheses

-ve and +ve signs

Mult. Div., and mod.

Add and subtract

Slide 19 ### Operator Precedence Rules:Examples

- ? =3 + 5* 4
- Evaluated as 3 +(5*4) and the result is 23

- ? = 8 / (5 – 2)
- Evaluated as 8 /3 and the result is 2

- ? = 8 + 12 % 5
- Evaluated as 8 +(12%5) and the result is 10

- ? = 6 * 5 / 2 + 2
- Evaluated as ( (6*5)/2)+ 2 and the result is 17

- ? = 9 – 4 + 2 * 6
- Evaluated as 9 – 4 + (2*6) and the result is 17

- ? = 1 + 2 * (3 + 4)
- Evaluated as 1 +(2 * (3+4)) and the result is 15

- ? = 5 * 2 + 9 % 4
- Evaluated as (5*2) + (9 % 4) and the result is 11

- ? = 5 * 2 % ( 7 – 4)
- Evaluated as (5 * 2)%(7 – 4) and the result is 1

Slide 20 ### That’s it for now!Next Lecture: More on variables, data types and expressions