0% found this document useful (0 votes)
15 views53 pages

L2-Constants, Data Types and Operators

The document discusses various C programming concepts including data types, variables, constants, operators, and input/output functions. It covers integer, floating point, character, and string data types as well as arithmetic, relational, logical, and assignment operators. The document also discusses variable declaration and definition, constants, keywords, identifiers, and input functions like scanf.

Uploaded by

Abror md Fayiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views53 pages

L2-Constants, Data Types and Operators

The document discusses various C programming concepts including data types, variables, constants, operators, and input/output functions. It covers integer, floating point, character, and string data types as well as arithmetic, relational, logical, and assignment operators. The document also discusses variable declaration and definition, constants, keywords, identifiers, and input functions like scanf.

Uploaded by

Abror md Fayiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Constants, Data Types and

Operators
Programming language
• Every program instruction must confirm
precisely to the syntax rules of the
language.

• C has its own vocabulary and grammar.

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.

• C programs are written using these tokens and


the syntax of the language.

6
C Tokens…

7
Keyword
• Every C word is classified as either a keyword
or an identifier.

• All keywords have fixed meanings and these


meanings cannot be changed.

• Example: auto, break, char, void etc.,

8
Keyword (Cont…)

9
Identifiers
• Identifiers refer to the names of variables,
functions and arrays.

• They are user-defined names and consist of a


sequence of letters and digits, with a letter as a
first character.

• Both uppercase and lowercase letters are


permitted. The underscore character is also
permitted in identifiers.
10
Constants
Constants in C refer to fixed values that do not change during the
execution of a program.

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.

Note: Embedded spaces, commas and non-


digit characters are not permitted between
digits.
Example: 1) 15,750
2)$1000
13
Constants(Cont.)
• Octal Constant
An octal integer constant consists of any combination
of digits from the set 0 through 7, with a leading 0.
Eg: 1) 037 2) 0435

• 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,

A real number may also be expressed in exponential or


scientific notation.

Eg:215.65 may be written as 2.1565e2


16
Constants(Cont.)
• Single Character Constant
A single character constants contains a single
character enclosed within a pair of single
quote marks.
Eg: ’5’
‘X’
‘;’

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.

These character combinations are known as


escape sequences.

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’;

• C permits multiple assignments in one line.


Example:
initial_value=0; final_value=100;

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

o When both the operands in a single arithmetic


expression are integers, the expression is called an integer
expression , and the operation is called integer arithmetic.

o During modulo division the sign of the result is always


the sign of the first operand.

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.

Eg: 1) 15 / 10.0 = 1.5

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.

&& (logical AND)


|| (logical OR)
! (logical NOT)

Eg: 1) if(age > 55 && sal < 1000)


2) if( number < 0 || number >100)

39
LOGICAL OPERATORS

#include <stdio.h>

int main() {
int x = 5;
int y = 3;

// Returns 1 (true) because 5 is greater than 3 AND 5 is


less than 10

printf("%d", x > 3 && x < 10);


return 0;
}

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);

This statement first assigns the value 10 to x, then assigns 5 to y, and


finally assigns 15(i.e, 10+5) to value.
The Size of Operator
The size of is a compiler time operator and, when used with an operand,
it returns the number of bytes the operand occupies.

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

Expressions are evaluated using an assignment statement of the form

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.

For example, consider the given expression


int x = 5 + 4 * 2;

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.

Operator associativity can either be left-to-right or right-to-left. Means if an


expression contains two or more operators of same precedence. Then they
are evaluated in either left to right or right to left order.

Consider the below expression


int x = 5 * 4 / 4 % 3;

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?

C programming language divides the problem into smaller


modules called functions or procedures each of which handles
a particular responsibility. The program which solves the entire
problem is a collection of such functions.

52
THANK YOU

53

You might also like