Center For Advanced Studies in Engineering
Center For Advanced Studies in Engineering
Studies in Engineering
Programming in C
Lecture2
1
C Techniques
z Variables
z Operators
z Decisions
z Loops
2
Variable Definition
int x;
X
}
5
Values and Variables
z Basic Types:
z Integers (signed and unsigned)
z short
z int
z float
z double
z Characters
z Boolean
6
Basic Types: int,float,char
z Integers (int)
0 1 1000 -1 -10 666
z Floating point numbers (float)
1.0 .1 1.0e-1 1e1
z Characters (char)
’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’
7
int x;
C Variables float sum, product;
8
What Does a Variable
Declaration Do?
int apartmentNumber;
float tax_rate_Y2K;
char middleInitial;
11
Identifiers
z VALID
apartment_number tax_rate_Y2K
PrintHeading ageOfHorse_
z NOT VALID
apartment# 2000TaxRate
Age-Of-Cat day of week
VARIABLE DECLARATIONS
char middleInitial ;
char letter ;
int apartmentNumber;
13
Assignment Statement
z An assignment statement is used to put a value into a
variable.
z The previous value is replaced
z Syntax:
<variable> = <expression>;
15
The char type
z char stores a character variable
z We can print char with %c
z A char has a single quote not a double quote.
void main()
{
char a, b;
17
Printing variables and constants to the
screen with printf
• Use the following conversion specifications:
return 0;
} 20
Operators
z Arithmetic Operators
z Increment/ Decrement Operators
z Relational Operators
21
Arithmetic Operators
z + = add
z - = subtract
z * = multiply
z / = division
z % = modulus - remainder
22
Modulus
z produces remainder
z only on integer division
z division by zero = error
z Example:
ans = 13% 5;
ans=3
23
Precedence
24
Arithmetic Assignment Operators
z All the arithmetic operators can be combined
with the equal sign:
z += addition assignment operator
z -= subtraction assignment operator
z *= multiplication assignment operator
z /= division assignment operator
z %= remainder assignment operator
25
Examples
z total= total + number;
z total += number;
z d= d-a;
z d-=a;
z Shorthand Assigns:
x*=3 (multiply x by 3)
x+=5 (add 5 to x)
x-=10 (subtract 10 from x)
x/=2 (halve x)
26
Increment/Decrement Operators
z ++ (increment)
z -- (decrement)
z int a;
z a++; ++a;
z a--; --a;
27
C Operators
z There is a subtle difference between x++ and ++x.
z ++x will increment the variable before it does anything
else with it, x++ will increment after any assignments.
x = 10; x = 10;
y = ++x; y = x++;
z In the first two cases x is set to 11, but in the first this is
done before the assignment.
28
Relational\Logical Operators
z C defines these logical operators:
z < less than
z > greater than
z <= less than or equal to
z >= greater than or equal to
z == equal to
z != not equal to
z int age;
z age = 15;
z Printf(“Is age less than 21? %d”, age<21);
z age = 51;
z Printf(“Is age equal to 51? %d”, age==50);
30
z Arithmetic operators have a higher
precedence than relational operators, that is ,
are evaluated before relational operators.
31
Compound statements
z && and
z || or
z ! not
32
SUMMARY
33