The document discusses variables, constants, data types, and operators in programming. It explains that variables are used to store and manipulate data in a program and have a name, type, and value, while constants are values that do not change during program execution. The key data types covered are integer, floating point, character, and string. The document also describes arithmetic, relational, and logical operators that are used to perform calculations and comparisons on variable and constant values. Examples are provided to illustrate declaring variables and constants, using different data types, and applying operators in expressions.
2. OUTCOMES:
Explain constants and variables
Describe data types
Identify operators in programming
Explain different between formula and
expression
2
3. 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
4. CONSTANTS
are values that do not change during program
execution
Types of constants:
• Integer constant
• Floating point constant
• String constant
• Character constant
4
5. 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
6. 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
7. 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 code
b) Before a function name
7
8. DECLARATION FORM:
Type
variable_list
Data types Variable;
Example:
int no1,no2; int mark;
char name[20]; float amount, rate;
8
9. #include <stdio.h>
int main()
{
int a, b;
int result;
a=5;
b=2;
a=a+1;
result=a-b;
printf("nntt%d",result);
return 0;
}
4
Output:
9
10. Variables may be initialized by assigning
constants to
them either at the time of their declaration or at a
later
tiSmyentax to declare a
constant:
data type variable value
int days_of_week =
7;
10
11. Example:
int m, n =10;
char response = ‘n’;
char color [6] = ‘green’;
float rate, total = 0.0;
11
12. #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("nn Your name is %s",name);
printf (" with matrix number %s ", matrix);
printf (“n Your age is %d”, age);
return 0;
}
Output:
What is your name?
Ali
What 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 1035
Your age is 23
12
13. IDENTIFY THE SCOPE OF CONSTANT & VARIABLES
Integer constant
are the whole numbers that do not contain decimal
point
Example;
int number = 10;
0, -708
13
14. Floating point constant
a decimal number that contains the decimal point (.)
Example;
float number =
0.7564;
-12.0, 65.4
14
15. 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
16. 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
17. KEYWORDS IN C PROGRAMME
• every word in C language is a keyword or an
identifier
• keyword cannot be used as a variable name
Definition Name Type
char Character
unsigned char Unsigned character
signed char Signed character (same as char)
int Integer
unsigned int Unsigned integer
signed int Signed integer
short int Short integer
unsigned short int Unsigned short integer
signed short int Signed short integer (same as short
int)
long Long integer
long int Long integer (same as long)
signed long int Signed long integer (same as long
int)
unsigned long int Unsigned long integer
float Floating-point
double Double floating-point
17
18. DATA TYPES
Basic data types in C:
• Integer
• Character
• Floating point
• Double floating point
18
19. INTEGER
hold whole numbers
Keyword : int
Syntax: int variable_name;
Example:
int number;
int a, b, c;
19
20. CHARACTER
hold only a single character
Keyword : char
Syntax: char variable_name;
Example:
char name[30];
char huruf;
20
22. DOUBLE FLOATING POINT
will ensure a maximum accuracy in decimal point
Keyword : double
Syntax: double variable_name;
Example:
double tax;
double value;
22
23. 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
24. Arithmetic Operator
Most C programs perform arithmetic
calculations.
OPERATO
R
ACTION ALGEBRAIC
EXPRESSION
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 % s
24
25. Typical operator results. With a=7 and b=2,
the expression on the values on the right
EXPRESSION VALUE
a – b 5
a + b 9
a * b 14
a / b 3
a % b 1
25
26. 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
27. Example:
ALGEBRA C
m= (a+b+c+d+e)/5;
y=(m*x) + b;
a b c d e
5
m
y mx b
27
28. Relational Operators
Operators for data comparisons are
available
It’s called relational operators, and the task
is to compare data.
28
29. 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 Y
29
30. Example:
if a = 7 and b = 5,
then a < b yields 0 and a != b yields 1.
30
31. Logical operators
There 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
33. The first two logical operators, && and ||,
never appear by themselves.
They typically go between two or more
relational tests.
33
34. a b
a | | b
either a or b
must be true
a&&b
Both a and b
Must be true
! a
Produces the
opposite
relation
0 0 0 0 1
0 1 1 0 1
1 0 1 0 0
1 1 1 1 0
34
35. 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
36. 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
37. if ((sales > 5000) || (hrsWorked > 81))
{
bonus = 500;
}
The sales must be more than 5000, or the
hrsWorked must
be more than 81, before the assignment executes.
37