chapter 2
TRANSCRIPT
OUTCOMES:
Explain constants and variables
Describe data types
Identify operators in programming
Explain different between formula and
expression
2
VARIABLES
a meaningful name of data storage location in computer
memory
Variable’s characteristics:
• has a name
• has a type
• holds a value that you assign to the variable
3
CONSTANTS
are values that do not change during program execution
Types of constants:
• Integer constant
• Floating point constant
• String constant
• Character constant
4
RULES OF DECLARING VARIABLE & CONSTANT
• Variable names can be short as a single letter or as long as 31 character
• Names must begin with a letter of alphabet
• Variables only contain letters, numbers and underscore (_) character
• Variables cannot have same name as C command or function
• Variables can be formed by freely combining the letters, digits and underscore
5
EXAMPLE:
Valid Invalid Comment
c “c” Illegal character
Totalx2 2totalx Illegal first character
Monthly_rate
Monthly-rate
Illegal character (-)
AGE AGE@ Illegal character @
SALARYPAY SALARY PAY
Illegal blank
6
DECLARE CONSTANT & VARIABLES
Variables hold different types of data:
• Numeric (integer, floating point, double floating point)
• Character
• String
Variables can be defined in 2 places:a) After the opening brace of a block of codeb) Before a function name
7
DECLARATION FORM:
Data typesVariable;
Typevariable_list
Example:
int no1,no2; int mark;char name[20]; float amount, rate;
8
#include <stdio.h>int main(){
int a, b;int result;
a=5;b=2;a=a+1;result=a-b;
printf("\n\n\t\t%d",result);return 0;}
4
Output:
9
Variables may be initialized by assigning constants to
them either at the time of their declaration or at a later
time
Syntax to declare a constant:
int days_of_week = 7;
data type variable value
10
#include <stdio.h>main(){char name[50], matrix[10];int date, month, year, age;int yearnow=2013;
printf("What is your name?\n");scanf( "%s", &name );printf("What is your matrix number?\n");scanf( "%s", &matrix );printf("What is your date of birth?\n");scanf( "%d", &date);printf("What is your month of birth?\n");scanf( "%d", &month);printf("What is your year of birth?\n");scanf( "%d",&year);
age = yearnow-year;printf("\n\n Your name is %s",name);printf (" with matrix number %s ", matrix);printf (“\n Your age is %d”, age);return 0;}
What is your name?AliWhat is your matrix number?1035
What is your date of birth?10
What is your month of birth?3
What is your year of birth?1990
Your name is Ali with matrix number 1035Your age is 23
Output:
12
IDENTIFY THE SCOPE OF CONSTANT & VARIABLES
Integer constantare the whole numbers that do not contain decimal point
Example;
int number = 10;0, -708
13
Floating point constanta decimal number that contains the decimal point (.)
Example;
float number = 0.7564;-12.0, 65.4
14
String constant• always enclosed in double quotation marks (“
and “)• a single space, a word, or a group of words
between double quotation marks is a C string constant
Example;
char name[6]= “AHMAD”;“2.0”, “X”
15
Character constant• should be enclosed within single quotation
marks (‘ and ‘)• all the alphabetic, numeric and special
character can be character constant
Example;
char numeric= ‘C’;‘X’, ‘0’
16
KEYWORDS IN C PROGRAMME
• every word in C language is a keyword or an identifier
• keyword cannot be used as a variable nameDefinition Name Type
char Characterunsigned char Unsigned charactersigned char Signed character (same as char)int Integerunsigned int Unsigned integersigned int Signed integershort int Short integerunsigned short int Unsigned short integersigned short int Signed short integer (same as
short int)long Long integerlong int Long integer (same as long)signed long int Signed long integer (same as
long int)unsigned long int Unsigned long integerfloat Floating-pointdouble Double floating-pointlong double Long double floating-point
17
INTEGER
hold whole numbers
Keyword : int
Syntax: int variable_name;
Example:
int number;int a, b, c;
19
CHARACTER
hold only a single character
Keyword : char
Syntax: char variable_name;
Example:
char name[30];char huruf;
20
FLOATING POINT
contain decimal points
Keyword : float
Syntax: float variable_name;
Example:
float value;Float gross_pay;
21
DOUBLE FLOATING POINT
will ensure a maximum accuracy in decimal point
Keyword : double
Syntax: double variable_name;
Example:
double tax;double value;
22
UNDERSTAND OPERATORS & EXPRESSION
• C math operators are symbols for multiplying, dividing,
• adding and subtracting and as well as for other
• operations
• are not always mathematical, but most of it is
• mathematical
• expression – combining operators, variables and
constants
23
Arithmetic OperatorMost C programs perform arithmetic
calculations.
OPERATOR
ACTION ALGEBRAIC EXPRESSIO
N
C EXPRESSI
ON+ Addition f + 7 f + 7
- Subtraction p – c p – c
* Multiplication bm b * m
/ Division x/y or or x÷y
x / y
% Remainder or Modulus
r mod s r % s24
EXPRESSION
VALUE
a – b 5a + b 9a * b 14a / b 3a % b 1
Typical operator results. With a=7 and b=2, the expression on the values on the right
25
Parentheses are used to group terms in C expressions in much the same manner as in algebraic expressions. For example, to multiply a times the quantity B+C, we write:
A*(B+C)
26
Relational Operators
Operators for data comparisons are availableIt’s called relational operators, and the task is to compare data.
28
Standard algebraic equality
operator or relational operator
C equality or
relational operator
Example of C condition Meaning of C
condition
Equality operators= == X == Y X is equal to Y≠ != X != Y X is not equal to Y
Relational operators> > X > Y X is greater than Y< < X < Y X is less than Y≥ >= X >= Y X is greater than
or equal to Y≤ <= X <= Y X is less than or
equal to Y29
Logical operatorsThere may be times when you need to test more than one set of variables.
You can combine more than one relational test in a compound relational test by using C’s logical operators
31
The first two logical operators, && and ||, never appear by themselves.
They typically go between two or more relational tests.
33
a ba | | b
either a or b must be true
a&&bBoth a and bMust be true
! aProduces the
opposite relation
0 0 0 0 1
0 1 1 0 1
1 0 1 0 0
1 1 1 1 0
34
Example:
The true and false on each side of the operators
represent a relational if test.
The following statements, for example, are valid if
test that use logical operators (sometimes called
compound relational operators).
35
if ((a<b) && (c>d))
{
printf(“Results are invalid”);
}
If a is less than b, and c is greater than d, print results are invalid to the screen.
36