Chapter 2
Chapter 2
Chapter 2
hours = 40.0;
Variables - declaring
var name memory loc
main() hours 40.0 4278
{ payrate 20.0 427C
float hours; 4280
float payrate; grosspay ?
float grosspay; j ? 4284
int j;
hours = 40.0;
payrate = 20.00;
Variables - declaring
var name memory loc
main() hours 40.0 4278
{ payrate 20.0 427C
float hours;
float payrate; grosspay 800.00 4280
float grosspay; j ? 4284
int j;
note: referencing a
variable only "reads"
hours = 40.0; it (non-destructive).
payrate = 20.00; Assigning to a
grosspay = hours * payrate variable overwrites
whatever was there
(destructive).
Variables - declaring
var name memory loc
main() hours 40.0 4278
{
float hours; payrate 20.0 427C
float payrate; grosspay 800.00 4280
float grosspay; j 5 4284
int j;
note: referencing a
hours = 40.0; variable only "reads"
payrate = 20.00; it (non-destructive).
grosspay = hours * payrate Assigning to a
variable overwrites
j = 5;
whatever was there
(destructive).
Variables - declaring
var name memory loc
main() hours 40.0 4278
{
float hours; payrate 20.0 427C
float payrate;
float grosspay; grosspay 800.00 4280
int j; j 5 6 4284
hours = 40.0; note: referencing a
payrate = 20.00; variable only "reads"
grosspay = hours * payrate it (non-destructive).
j = 5; Assigning to a
j = j + 1;
variable overwrites
}
whatever was there
(destructive).
Rules for Naming Variables in C
In C, variable names are built from:
the letters of the alphabet (A-Z, or a-z)
the digits 0 through 9
the underscore. (no other special characters!)
A variable name must start with a letter or an underscore.
Spaces are not allowed in variable name.
Only the 32 characters of a variable name are significant.
Identifiers are case sensitive: sum≠SUM≠Sum≠sUM
A variable name must not be the same as a reserved
word.
Reserved words / Key Words in C and C++
void 0 valueless
TYPE
DATA
EXAMPLES
TYPES
char ‘A’ ‘b’ ‘$’ ‘9’
void valueless
For example:
scanf ( “ %s %d %f”, &name, &age, &per );
Another example:
x = 6 / 2 + 1 - 3 + 8 * 4;
x = 33;
x = 6 / (2 + 1) - (3 + 8) * 4;
x = -42;
Arithmetic Assignment Operator
+= Addition Arithmetic Assignment Operator
-= Subtraction Arithmetic Assignment Operator
*= Multiplication Arithmetic Assignment
Operator
/= Division Arithmetic Assignment Operator
%= Remainder Arithmetic Assignment Operator
For Example:
total = total + number;
total + = number;
Arithmetic Assignment Operator
int a = 4, b = 2, c = 36 ;
a += b ; /* This adds b to a, a = ? */
int a = 4, b = 2, c = 36 ;
a += b ; /* This adds b to a, a = ? */
[ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
c /= a + b ; /* What is value of c now?
*/
Arithmetic Assignment Operator
Example of arithmetic assignment operators:
int a = 4, b = 2, c = 36 ;
a += b ; /* This adds b to a, a = ? */
[ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
c /= a + b ; /* What is value of c now? */
[ Answer: c = c / (a + b), and a = 6 now,
so c = 36 / (6 + 2), so c = 36 / 8 or c = 4 ]
Relational Operators
Operator: Meaning:
Output:
Is age less than 20 ? 1
Is age less than 20 ? 0
Increment/Decrement Operators
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
c = b-- - ++a ;
/* What are the values of a, b, c now? */
Increment/Decrement Operators
Examples of increment and decrement operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
(Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now? */
Increment/Decrement Operators
Examples of increment and decrement operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
(Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now? */
(Answers: a = 6, b = 0, c = -5)
Logical Operators
Homework
1. Write a program that inputs your age in years and then
calculate the total number of months, weeks and days.
2. Write a program that inputs marks for 5 subjects and then
calculate the total marks obtained and the average
3. Write a program that inputs a number and checks if it is
even or odd (Hint: use remainder operator)
4. Write a program that takes temperature in Fahrenheit as
input and convert it into Celcius
5. Write a program that takes input the coefficients (a,b,c) of a
quadratic equation and calculate its roots x1 and x2 with the
following formulae: