L2-Constants, Data Types and Operators
L2-Constants, Data Types and Operators
Operators
Programming language
• Every program instruction must confirm
precisely to the syntax rules of the
language.
2
Character Set
C characters are grouped into the following
categories.
– Letters
– Digits
– Special Characters
– White Spaces
3
Character Set…
4
White Space
• Blank Space
• Horizontal Tab
• New Line
• etc
5
C Tokens
• In C programs, the smallest individual units
are known as tokens.
6
C Tokens…
7
Keyword
• Every C word is classified as either a keyword
or an identifier.
8
Keyword (Cont…)
9
Identifiers
• Identifiers refer to the names of variables,
functions and arrays.
11
Constants(Cont.)
Integer Constants
An integer constant refers to a sequence of digits.
There are three types integers-
decimal,
octal and
hexa decimal
12
Constants(Cont.)
• Decimal Constant
Eg:123, -321 etc.
• Hexadecimal Constant
A sequence of digits preceded by 0x or 0X is considered
as hexadecimal integer.
They may also include alphabets A through F or a
through f (10-15).
Eg: 1) 0X2 2) 0x9F 3) 0Xbcd
14
printf function
printf(“Integer values\n\n”);
printf(“%d”, 32);
printf(“ Total Value = %d”, 32 + 8);
printf(“ Total Value = %f”, 32.5);
printf(“ Total Value = %c”, ‘B’);
printf(“Total value = %ld”, 10L);
printf(“Total value =%lf”, 234.45);
printf(“Name is =%s”, “Arif”);
15
Constants(Cont.)
• Real Constants
Certain quantities that vary continuously, such as
distances, heights etc., are represented by numbers
containing functional parts like 17.548.Such numbers are
called real(or floating point)constants.
Eg:0.0083,-0.75 etc,
17
Constants(Cont.)
• String Constants
A string constant is a sequence of characters
enclosed in double quotes.
The characters may be letters, numbers,
special characters and blank space.
Eg:”Hello!”
“1987”
“?….!”
18
Constants(Cont.)
• Backslash Character Constants
C supports special backslash character
constants that are used in output functions.
19
Backslash Character Constants
20
Variable
• A variable is a data name that may be used to
store a data value.
• A variable may take different values at
different times of execution and may be
chosen by the programmer in a meaningful
way.
• It may consist of letters, digits and underscore
character.
Eg: 1) Average 2) Height
21
Rules for defining variables
• They must begin with a letter. Some systems
permit underscore as the first character.
• ANSI standard recognizes a length of 31
characters. However, the length should not be
normally more than eight characters.
• Uppercase and lowercase are significant.
• The variable name should not be a keyword.
• White space is not allowed.
22
Data types
ANSI C supports four classes of data types.
• Primary or Fundamental data types.
• User-defined data types.
• Derived data types.
• Empty data set.
23
24
Data types(Cont.)
25
Data types(Cont.)
26
Declaration Of Variables
• It tells the compiler what the variables name is.
• What type of data the variable will hold.
• The syntax is
data-type v1,v2…..vn;
Eg:1. int count;
float ratio, total;
27
Assigning Values To Variables
• The syntax is-
variable_name = constant
Eg:1) int a = 20;
2) balance = 75.84;
3) yes = ’x’;
28
/* Test1.c: This is my first C program */
#include <stdio.h>
int main()
{
float avg = 0.0;
float a = 1;
float b = 16;
clrscr ( );
/* This is the average of two floating numbers*/
avg = a + b;
printf ("avg:");
printf (“ %f ", avg/2);
return 0;
}
29
Reading from keyboard
Integer number:
scanf("%d",&number);
Float number:
scanf("%f",&number);
Character number:
scanf("%c",&number);
30
This is the Sum of two integer numbers from keyboard
# include <stdio.h>
int main( )
{
int num1, num2, sum = 0;
printf ("Enter Numbers\n");
scanf("%d%d", &num1, &num2);
sum = num1 + num2;
printf (“ Sum = %d\n", sum);
return 0;
}
31
OPERATORS OF C
C operators are classified into a number of categories.
They include:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
32
ARITHMETIC OPERATORS
The operators are
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulo division)
Eg: 1) a-b 2) a+b 3) a*b 4) p%q
• The modulo division produces the remainder of an integer
division.
• The modulo division operator cannot be used on floating point
data.
• Note: C does not have any operator for exponentiation.
33
Integer Arithmetic
Integer Arithmetic
That is
-14 % 3 = -2
-14 % -3 = -2
14 % -3 = 2
34
Real Arithmetic
• An arithmetic operation involving only real
operands is called real arithmetic. If x and y
are floats then we will have:
1) x = 6.0 / 7.0 = 0.857143
2) y = 1.0 / 3.0 = 0.333333
• The operator % cannot be used with real
operands.
35
Mixed-mode Arithmetic
• When one of the operands is real and the
other is integer, the expression is called a
mixed-mode arithmetic expression and its
result is always a real number.
36
RELATIONAL OPERATORS
• Comparisons can be done with the help of relational
operators. The expression containing a relational
operator is termed as a relational expression. The
value of a relational
• expression is either one or zero.
1) < (is less than)
2) <= (is less than or equal to)
3) > (is greater than)
4) >= (is greater than or equal to)
5) = = (is equal to)
6) != (is not equal to)
37
Simple program using relational operator
# include <stdio.h>
int main( ) {
int p1 = 30, p2 = 40;
if ( p1 > p2 ) {
printf( “ Person 1 is greater than Person 2 \n”);
}
if ( p1< p2 ) {
printf ( “ Person 2 is greater than Person 1\n”);
}
return 0; }
38
LOGICAL OPERATORS
C has the following three logical operators.
39
LOGICAL OPERATORS
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
40
ASSIGNMENT OPERATORS
The usual assignment operator is ‘=’.In addition, C has a
set of ‘shorthand’ assignment operators of the form,
v op = exp;
Eg:1. x += y+1;
This is same as the statement
x=x+(y+1);
Shorthand Assignment Operators
• a + =1 means a = a + 1
• a - = 1 means a = a – 1
• a *= n + 1 means a = a * (n+1)
• a /= n + 1 means a = a / (n+1)
• a %= b means a = a % b
41
INCREMENT AND DECREMENT OPERATORS
C has two very useful operators that are not generally found in
other languages.
These are the increment and decrement operator:
++ and --
The operator ++ adds 1 to the operands
while - - subtracts 1.
It takes the following form: m = 5;
• ++m; or m++
so m = ?
• --m; or m-- after this statement m = ?
42
CONDITIONAL OPERATOR
• A ternary operator pair “ ? : ” is available in C to construct
conditional expression of the form:
exp1 ? exp2 : exp3;
• Here exp1 is evaluated first. If it is true then the expression
exp2 is evaluated and becomes the value of the expression. If
exp1 is false then exp3 is evaluated and its value becomes the
value of the expression.
Eg:1) if(a>b)
x = a;
else
x = b;
Can be written as x = ( a > b) ? a : b ; 43
BITWISE OPERATORS
44
BITWISE OPERATORS
#include <stdio.h>
int main() {
int x =7;
x & = 5;
printf("%d", x);
return 0;
}
45
SPECIAL OPERATORS
The Comma Operator
The comma operator can be used to link the related expressions
together.
Eg: value = (x = 10, y = 5, x + y);
Eg: 1) m = sizeof(sum);
2) n = sizeof(long int)
3) k = sizeof(235L)
46
EXPRESSIONS
The combination of operators and operands is said to be an
expression.
ARITHMETIC EXPRESSIONS
An arithmetic expression is a combination of variables, constants, and
operators arranged as per the syntax of the language.
Eg 1) a = x + y;
EVALUATION OF EXPRESSIONS
variable = expression;
Eg:1) x = a * b – c;
2) y = b / c * a;
47
PRECEDENCE OF ARITHMETIC OPERATORS
Precedence of an operator specifies its priority compared to other operator. If
an expression contain different types of operator, then precedence of
operators specifies the order of evaluation each operator.
The result of above expression is 13 instead of 18. Since * operator has higher
precedence than + operator. Hence, + operator is evaluated after * operator.
High priority * / %
• Low priority + -
48
ASSOCIATIVITY OF OPERATORS
If an expression contains more than one operator with same precedence. Then
operator precedence along with its associativity defines the order of
evaluation of expression.
49
/*A Simple Expression Program*/
int main( )
{
float a, b, c, y, x, z;
a = 9;
b = 12;
c = 3; OUTPUT
x = a – b / 3 + c * 2 – 1; x = 10.000000
y = a – b / (3 + c) * (2 – 1); y = 7.000000
z = a – (b / (3 + c) * 2) – 1; z = 4.000000
printf(“ x = %f \n ” , x);
printf(“ y = %f \n ” , y);
printf(“ z = %f \n ” , z);
return 0;
}
Casting a Value
C performs type conversion automatically. However, there are
instances when we want to force a type conversion in a way that is
different from the automatic conversion.
The general form of a cast is:
(type-name)expression
Example:
float z;
int x , y ;
x = 10;
y = 3;
z = (float) x / y ;
51
Why is C called a structured programming language?
52
THANK YOU
53