Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 51

The Shirpur Education Soceity’s

R. C. Patel Polytechnic, Shirpur


First Year Diploma In Computer Engineering
Semester-II
Subject Code – 17212 (PIC)

Programming In C

Subject In-charge– Prof. V. I. Memon


2 – More Basic Concepts of C
✯ Data Types
✯ Variables
✯ Writing programs based on this session
✯ Operators
✯ Writing programs based on operators
✯ Expressions
✯ Writing useful programs in C

Course Content VC-ICP (Vocational Course on Introduction to C Programming


DATA TYPES
✯ “A data type refers to a named group of data which share similar properties and
have common name.”
✯ For example: Numeric, Alphabetic, Alphanumeric, etc.
 Name of a person or thing or place contain alphabets, i.e. alphabetic data
type.
 Address of place can contain alphabets as well as digits, i.e. alphanumeric
data type.
 Age of a person is represented using digits, i.e. numeric data type.
✯ A data type determines how much storage should be allocated to a variable. It
also determines the permissible operations on variables.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Classification of Data Types

Data Types
Basic Derived User-defined
• Character (char) • Arrays • Structure (struct)
• Integer (int) • Functions • Union (union)
• Float (float) • Pointers • Enumeration (enum)
• Double • Type Definition
(double) (typedef)
• Void (void)

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Basic (Primitive/Fundamental) Data Types
Keyword Size Size
Data Type in in Range Use
used
bytes Bits

Character char 1 8 0 to 255 Store a single character

Integer int 2 16 -32768 to +32767 Stores whole numbers


(-215 to +215-1)

Single precision float 4 32 3.4E-38 to Stores real numbers with


floating point 3.4E+38 single precision floating point

Double precision double 8 64 1.7E-308 to Stores real numbers with


floating point 1.7E+308 double precision floating point

Valueless void 0 0 Valueless Stores no value

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Data Type Modifiers
✯ “A type modifier modifies the base type to yield a new type.”
✯ It modifies the range and the arithmetic properties of the base type.
✯ The type modifiers and the corresponding keywords available in C are:
 Signed (signed) – positive and negative numbers
 Unsigned (unsigned) – positive numbers
 Short (short) – 16 bit numbers
 Long (long) – 32 bit numbers

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Variants of Basic Data Types
Data Type Size in Access Range
bytes Specifier
char 1 %c -128 to +127
signed char 1 %c -128 to +127
unsigned char 1 %c 0 to 255
int 2 %i, %d -32768 to +32768
signed int 2 %i, %d -32768 to +32768
short int 2 %i, %d -32768 to +32768
signed short int 2 %i, %d -32768 to +32768
signed 2 %i, %d -32768 to +32768
short 2 %i, %d -32768 to +32768
unsigned int 2 %u 0 to 65535
unsigned short int 2 %u 0 to 65535
unsigned 2 %u 0 to 65535
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Variants of Basic Data Types (continued)
Size in Access
Data Type bytes Specifier Range

long int 4 %li, %ld -2147483648 to +2147483647

signed long int 4 %li, %ld -2147483648 to +2147483647

long 4 %li, %ld -2147483648 to +2147483647

unsigned long int 4 %lu 0 to 4294967295

float 4 %f 3.4E-38 to 3.4E+38

double 8 %lf 1.7E-308 to 1.7E+308

long double 10 %Lf 3.4E-4932 to 1.1E+4932

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


char Data Type
✯ A character constant is a single character enclosed within a pair of single quotes.
✯ For example: ‘A’, ‘$’, ‘@’, ‘4’, these are valid characters, while writing “A” is invalid.
✯ Each character constant contains an integer value which is provided by the ASCII
(American Standard Code for Information Interchange) character set.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


ASCII Chart
Char Value Char Value Char Value Char Value
Special chars. 0-32 ) 41 ; 59 ] 93

! 33 * 42 <  60 ^ 94

“ 34 + 43 = 61 _ 95

# 35 , 44 >  62 ‘ 96

$ 36 - 45 ? 63 a-z 97-122

% 37 . 46 @ 64 { 123

& 38 / 47 A-Z 65-90 | 124

‘ 39 0-9 48-57 [ 91 } 125

( 40 : 58 \ 92 Special chars. 126-255


Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
int Data Type
✯ The int data type is used to store whole numbers, which are a set of non-
negative integers including 0. A int data type takes 2 byte for storage.
✯ For ex: Valid integer numbers are: 250, 15033, 28216, +18000, 0, 32763, etc.
Invalid integer numbers are: 4,442 (comma not allowed), -31.49
(decimal not allowed), 40000 (out of range), etc.
✯ While storing an int number, the number is converted into its equivalent binary
number and then stored in the allocated two bytes (16 bits).

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


float Data Type
✯ The float data type is used to store real numbers that have a single-precision
floating-point. Floating-point numbers are numbers that contain a decimal
point (fractional value).
✯ In the single precision floating point numbers are expressed in the following
format: Sign, Exponent & Mantissa.

✯ For ex: Valid floating-point numbers are: -216.218, 2.43130E+4, 0.002323E+6.


Invalid floating-point numbers are: 4,4 (comma not allowed), -3.1.4
(multiple decimal points not allowed), -999999999.999999 (out of
range), etc.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


float Data Type (continued)
✯ The character E stands for exponent. We can use the scientific notation in
situations where the given data is either too large or too small.
✯ For example: if the specified number is 0.000000000017, then we must use the
scientific notation to represent this number. In scientific notation, we can write
the number as either 1.7 x 10-11 or as 1.7 E-11.
✯ Floating-point numbers use the IEEE format to represent mantissa and
exponents. According to the IEEE format, a floating point value in its binary
form is known as a normalized form.
✯ In the normalized form, the exponent is adjusted in such a way that the binary
point in the mantissa always lies to the right of the most significant non-zero
digit.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


float Data Type (continued)
✯ For example: 5.32 can be converted in IEEE normalized form as:
5 = 101, 0.32 = 0101 0.32 x 2 = 0.64 0
5/2 = 1 (remainder) 0.64 x 2 = 1.28 1
2/2 = 0 0.28 x 2 = 0.56 0
1/2 = 1 0.56 x 2 = 1.12 1
✯ Thus, the binary equivalent of 5.32 = 101.0101
✯ The normalized form of binary number obtained by adjusting the exponent until
the decimal point is to the right of most significant 1.
✯ Therefore, the normalized binary equivalent = 1.010101 x 22.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


double Data Type
✯ The double data type is used to store real numbers that have a double-
precision floating­point.
✯ The precision is related with the accuracy of data. When we require more
accuracy in representing the floating-point numbers, we must use the double
data type.
✯ A float data type stores a maximum of 6 digits after the decimal point whereas
a double data type stores 16 digits after the decimal point.
✯ The double precision floating point numbers are expressed in the following
format: Sign, Exponent & Mantissa. As shown below:

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


void Data Type
✯ The void data type stores no values and therefore you cannot perform any
operations on a variable that is declared as void. It is the only non-specific data
type in C.
✯ Unlike all other data types, the void data type neither has a size nor any
permissible range.
✯ It is primarily used in three cases:
 To specify the return type of a function (when function returns no value)
 To specify the parameters of the function (when function accepts no
arguments from the caller)
 To create generic pointers

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Variables
✯ “A variable is a named location in memory that is used to hold a value that
can be easily accessed and modified by the program.”
✯ “A variable is an entity whose value can vary (i.e. change) during the execution of
program.”
✯ The variables are the names provided to values to identify the programming
elements. As a variable is used to identify a value; therefore variables are also
called identifiers.
✯ While writing variable names, all the rules given for identifiers should be
followed.
✯ Each variable has a name and data type.
✯ For example, the following statement declares a variable i if the type int:
int i;

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Variables (continued)
✯ There are two values associated with the variable:
 Its data value stored at some location in memory. This is sometimes
referred to as a variable’s rvalue (i.e., its read value).
 Its location value; i.e., the address in memory at which its data it’s stored.
This is sometimes referred to as a variable’s lvalue (i.e., its location value).

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Declaration of Variables
✯ All variables must be declared before they can be used. The general form of a
declaration is: data_type variable_list;
✯ Here, data_type must be a valid data type plus any modifiers, & variable_list
may consist of one variable name or more than one variable names separated by
commas.
✯ In C, variable declaration always ends with a semicolon. Here are some
declarations:
char c
int i, j, k;
short int si;
unsigned int ui;
float f;
double balance_sheet, profit, loss;
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Declaration of Variables
✯ Declaration gives an introduction of a variable to compiler and its properties like
scope, range of values and memory required for storage.
✯ The variable can store single value at a time in the allocated memory to it.
✯ In C variables can be declared at various places in the program but two things
must be kept in mind. First, variables should be declared before using them.
Second, variables should be declared closest to their first point of use to
make the source code easier to maintain.
✯ In C variables are declared at three basic places as follows:
 When a variable is declared inside a function it is known as a local variable.
 When a variable is declared in the definition of function parameters it is
known as formal parameter.
 When the variable is declared outside all functions, it is known as a global
variable.
✯ A variable cannot be of type void.
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Initializing of Variables
✯ Once we have declared a variable, the next step is to assign a value to that
variable. This process of assigning a value to a variable is called as initialization
of a variable.
✯ If a variable is not initialized then it contains a garbage value.
✯ We can assign a value to a variable using the = (assignment operator). The syntax
for initializing a variable is as follows: - variable_name = value;
✯ We can initialize a variable by using two methods. In the first method, we can
specify an initial value to a variable within the declaration part itself. In the
second method, the initial value is assigned to the variable in the execution
section of the C program.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Initializing of Variables
✯ The following are some examples of declaring and initializing a variable in a
single statement:
int y = 3;
float salary = 0.0;
char ch = ‘A’;
double tax = 0.1362;

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Initializing of Variables
✯ We can assign a single initial value to multiple variables using a single
assignment statement. This type of initialization requires more than one
assignment operator. Therefore, such assignments are called multiple
assignments.
int a = 20;
int b = 20;
int c = 20;
✯ These three statements can be rewritten as:
int a, b, c;
a = b = c = 20;

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Operators
✯ “An operator is a symbol that specifies the mathematical, logical, or relational
operation to be performed.”
✯ The operators specify the type of operation that needs to be applied. The values
that are operated on by operators are called operands.

Classification of Operators
Based on No. of Based on Role of
Operands Operators
• Unary • Arithmetic
• Binary • Logical
• Ternary • Relational
• Bitwise
• Assignment

1. Arithmetic Operators
+ : Addition * : Multiplication ++ : Increment (pre & post)
- : Subtraction / : Division -- : Decrement (pre & post)
    % : Modulus      

✯ For example: Consider three variables declared as, int a=9, b=3, result;
Operation Operator Syntax Comment Result
Multiply * a*b result = a * b 18
Divide / a/b result = a / b 3
Addition + a+b result = a + b 12
Subtraction - a–b result = a – b 6
Modulus % a%b result = a % b 0
✯ While performing modulo division, the sign of the result is always the sign of the
first operand (the dividend). Therefore,
16 % 3 = 1 -16 % 3 = -1
16 % -3 = 1 -16 % -3 = -1
16 % 3.0 16.5% 4 compiler error
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Increment & Decrement Operators
✯ ++ and -- are unary operators which perform increment & decrement operation.
✯ The increment operator (++) is used to add 1 to its operand and the
decrement operator (--) is used to subtract 1 from its operand.
✯ In other words, a = a + 1 is the same as ++a or a++ and
a = a – 1 is the same as --a or a--.
✯ These operators can be applied only to variables by using either the prefix form
(i.e. pre-increment/decrement) or postfix form (post-increment /decrement).
✯ Writing 2++, 2--, --2 or ++2 will cause error. It cannot modify a constant, a
variable is required.
✯ When a prefix form (++x or --x) is used, the value of the variable is either
incremented or decremented first and then assigned. However, in the postfix
form, the value is used and after the assignment operator has performed the
operation, the value is incremented or decremented.
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Increment & Decrement Operators

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


2. Logical Operators
&& : Logical AND || : Logical OR ! : Logical NOT

✯ A logical operator is used to compare or evaluate logical and relational


expressions. The Truth table of logical operators:
A B A && B A || B !A
0 (false) 0 (false) 0 (false) 0 (false) 1 (true)
1 (true) 0 (false) 0 (false) 1 (true) 0 (false)
0 (false) 1 (true) 0 (false) 1 (true) 1 (true)
1 (true) 1 (true) 1 (true) 1 (true) 0 (false)
✯ Logical AND operator is used to simultaneously evaluate two conditions or
expressions with relational operators. If expressions on both the sides (left and
right side) of the logical operator is true then the whole expression is true.
✯ For example: (a > b) && (a > c)
The expression to the left is (a < b) and that on the right is (b > c). The whole
expression is true only if both expressions are true, i.e., if a is greater than b & c.
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
2. Logical Operators (continued)
✯ Logical OR operator is used to simultaneously evaluate two conditions or
expressions with relational operators. If one or both the expressions on the left
side and right side of the logical operator is true then whole expression is true.
✯ For example: (a > b) || (a > c)
The expression to the left is (a < b) and that on the right is (b > c). The whole
expression is true if either b is greater than a or b is greater than c.
✯ Logical NOT operator operator takes a single expression and negates the value
of the expression. That is, logical NOT produces a zero if the expression evaluates
to a non-zero value and produces a 1 if the expression produces a zero. In other
words, it just reverses the value of the expression.
✯ For example: int a = 10, b; b = !a;
Now the value of b = 0. This is because value of a = 10. !a = 0.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


2. Logical Operators (continued)
Operator Description or Action Example Return Value
&& Logical AND 5 > 3 && 5 < 10 1
|| Logical OR 8 > 5 || 8 < 2 1
! Logical NOT 8! = 8 0

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


3. Relational Operators
<  : Less than <= : Less than or equal to == : Equal to
>  : Greater than >= : Greater than or equal to != : Not equal to
✯ Relational operators compare two operands and return in terms of true (1) or
false (0).
Operator Meaning Example Return Value
<  Less than 3<5 1
>  Greater than 7>9 0
<= Less than or equal to 100 <= 100 1
>= Greater than or equal to 50 >= 100 0
== Equal to 3 == 3 1
!= Not equal to 3 != 3 0
✯ The operands of a relational operator must evaluate to a number. Characters are
considered valid operands since they are represented by numeric values in the
computer system. So, if we say, 'A'<'B', where A is 65 and B is 66 then the result
would be 1 as 65<66.
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
4. Bitwise Operators
& : Bitwise AND ~ : Bitwise NOT/Complement <<  : Left shift
| : Bitwise OR ^ : Bitwise Exclusive-Or >>  : Right shift
✯ As the name suggests, bitwise operators are those operators that perform
operations at bit level.
✯ The bitwise operators expect their operands to be integers and treat them
as a sequence of bits. They can be applied on operands of type char,
short, int, long, whether signed or unsigned.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


4. Bitwise Operators (continued)
✯ The bitwise AND operator (&) same as logical AND operation, i.e., the
bitwise AND operator compares each bit of its first operand with the
corresponding bit of its second operand. If both bits are 1, the
corresponding bit in the result is 1 and 0 otherwise.
✯ For example: 10101010 = x
& 01010101 = y
00000000 = x & y
✯ In a C program, the & operator is used as follows:
int a = 10, b = 20;
int c = a & b;
c = 0;

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


4. Bitwise Operators (continued)
✯ The bitwise OR operator ( | ) same as logical OR operation, i.e., the
bitwise-OR operator compares each bit of its first operand with the
corresponding bit of its second operand. If one or both bits are 1, the
corresponding bit in the result is 1 and 0 otherwise.
✯ For example: 10101010 = x
| 01010101 = y
11111111 = x | y
✯ In a C program, the | operator is used as follows:
int a = 10, b = 20;
int c = a | b;
c = 30;

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


4. Bitwise Operators (continued)
✯ The bitwise XOR operator (^) same as logical XOR operation, i.e., the
bitwise XOR operator compares each bit of its first operand with the
corresponding bit of its second operand. If one of the bits is 1, the
corresponding bit in the result is 1 and 0 otherwise.
✯ For example: 10101010 = x
^ 01010101 = y
11111111 = x ^ y
✯ In a C program, the ^ operator is used as follows:
int a = 12, b = 20;
int c = a ^ b;
c = 24;

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


4. Bitwise Operators (continued)
✯ The bitwise NOT operator (~), or complement, is a unary operation
that performs logical negation on each bit of the operand.
✯ By performing negation of each bit, it actually produces the 1s
complement of the given binary value.
✯ Bitwise NOT operator sets the bit to 1 if it was initially 0 and sets it to 0 if
it was initially.
✯ For example: ~10101011 = 01010100
✯ In a C program, the ~ operator is used as follows:
int a = 10;
int b = ~a;
b = 5

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


4. Bitwise Operators (continued)
✯ The bitwise Left Shift operator (<<), when we apply a left-shift, every
bit in x is shifted to the left by one place. So, the MSB (most significant bit)
of x is lost, the LSB (least significant bit) of x is set to 0.
✯ For example: If we have x = 0001 1101,
then x << 1 produces 0011 1010
✯ If we observe carefully, we will notice that shifting once to the left
multiplies the number by 2. Hence, multiple shifts of 1 to the left, results
in multiplying the number by 2 over and over again.
✯ Therefore if we have x << 4, then it produces 1010 0000
✯ In a C program, the << operator is used as follows:
int a = 5, b = 2;
int c = a << b;
c = 20
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
4. Bitwise Operators (continued)
✯ The bitwise Right Shift operator (<<), when we apply a right-shift,
every bit in x is shifted to the right by one place. So, the LSB of x is lost, the
MSB of x is set to 0.
✯ For example: If we have x = 0001 1101,
then x >> 1 produces 0000 1110
✯ If we observe carefully, we will notice that shifting once to the left
multiplies the number by 2. Hence, multiple shifts of 1 to the left, results
in multiplying the number by 2 over and over again.
✯ Therefore if we have x >> 4, then it produces 0000 0001
✯ In a C program, the >> operator is used as follows:
int a = 5, b = 2;
int c = a >> b;
c = 1
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
5. Assignment Operators
= : Assignment /= : Divide & assign &= : Bitwise AND & assign
+= : Add & assign %= : Modulo & assign |= : Bitwise XOR & assign
-= : Subtract & assign <<= : Bitwise left shift & assign ^= : Bitwise OR & assign
*= : Multiply & assign >>= : Bitwise right shift & assign      

✯ An assignment operator (=) is used to assign a constant or a value of one


variable to another.
✯ Arithmetic Assignment operators are a combination of arithmetic and the
assignment operator. With this operator, the arithmetic operation
happens first and then the result of the operation is assigned.
✯ The general format for an assignment operator is: var = expression
✯ For example: x = 10;
sum = x + y;
a = b = c = 10;
a + b = x (invalid in C)
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
5. Assignment Operators (continued)
✯ C language supports a set of shorthand assignment operators of the form:
variable op= expression, where op is a binary arithmetic operator.
✯ The shorthand assignment is of the form op1 op= op2, where op1 and op2 are
operands and op= is a shorthand assignment operator. It is a shorter way of
writing op1 = op1 op op2.
✯ For example: a /= 2 is equivalent to a = a / 2
b &= 5 is equivalent to b = b & 5
c <<= 2 is equivalent to c = c << 2

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


6. Miscellaneous Operators
sizeof() : Size of operator : Returns the size of a variable.
* : Value of operator : Pointer to a variable.
& : Address of operator : Returns the address of a variable.
. : Dot operator : Direct member access.
–> : Arrow operator : Indirect member access
, : Comma operator : Decides the sequence of operation.
?: : Conditional expression : Returns either a true or a false value.
[] : Array Index : Represents array index.
() : Expression : Determines expression evaluation order

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


6. Miscellaneous Operators (continued)
✯ The operator sizeof is a unary operator used to calculate the size of data types.
✯ This operator can be applied to all data types.
✯ The sizeof operator is used to determine the amount of memory space that the
variable/ expression/ data type will take.
✯ A sizeof expression returns an unsigned value that specifies the space in bytes
required by the data type, variable, or expression.
✯ For example: sizeof(char) returns 1
sizeof(int) returns 2
float f;
sizeof(f) returns 4

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


6. Miscellaneous Operators (continued)
✯ The comma operator ( , ) in C takes two operands i.e. comma operator is binary
operator.
✯ It works by evaluating the first and discarding its value, and then evaluates the
second and returns the value as the result of the expression.
✯ Comma separated operands when chained together are evaluated in left-to-right
sequence with the right most value yielding the result of the expression.
✯ Among all the operators, the comma operator has the lowest precedence.
✯ For example: x = (a, b); if a=2, b=3 then x = 3
✯ The following statement assigns the value of b to x, then increments a, and then
increments b.
int a = 2, b = 3, x = 6;
x = (++a, b += a);
Now, the value of x = 6.
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Operator Precedence
✯ The precedence of operators determines a rank for the operators. The higher an
operator's precedence, the higher “binding” it has on the operands.
✯ Associativity determines the grouping of operations among operators of the
same precedence.
✯ In the expression a * b / c, since multiplication and division have the same
precedence we must use the associativity to determine the grouping. These
operators are left associative which means they are grouped left to right as if the
expression was (a * b) / c.
✯ The operators' order of precedence from highest to lowest and their associativity
is shown in this table:

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Operator Precedence Chart
Operator Type Operator Associativity
Primary Expression Operators ( expr), [index], ., –>, expr++, expr-- left-to-right
Unary Operators *, &, +, -, !, ~, ++expr,  --expr , (typecast), sizeof right-to-left
*, /, %
+, -
>>, <<
<, >, <=, >=
==, !=
Binary Operators left-to-right
&
^
|
&&
||
Ternary Operator ?: right-to-left
Assignment Operators =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= right-to-left
Comma Introduction to C , left-to-right
VC-ICP (Vocational Course on Introduction to C Programming
Expressions
✯ In simplest terms expression is a statement made up of data and operators
operating upon that data which is evaluated by compiler.
✯ Expression by definition is any valid combination of operators, constants,
functions and variables.
✯ Expressions in C are basically operators acting on operands. Statements like
a = b + 3, ++z and 300 > (8 * k) are all expressions.
✯ Strictly speaking, even a single variable or constant can be considered an
expression.

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Type Casting (Implicit) Rules
✯ An arithmetic operation between an integer and integer always yields an integer
result.
✯ An operation between a real (float) and real always yields a real result.
✯ An operation between an integer and real always yields a real result. In this
operation the integer is first promoted to a real and then the operation is
performed. Hence the result is real.
✯ For example: Operation Result   Operation Result
5/2 2   2/5 0
5.0 / 2 2.5   2.0 / 5 0.4
5 / 2.0 2.5   2 / 5.0 0.4
5.0 / 2.0 2.5   2.0 / 5.0 0.4

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming


Type Casting (Implicit) Rules
✯ It may so happen that the type of the expression and the type of the variable on
the left-hand side of the assignment operator may not be same. In such a case the
value of the expression is promoted or demoted depending on the type of the
variable on left-hand side of =.
✯ For example: Observe the results of the arithmetic statements shown in table
below. It has been assumed that k is an integer variable and a is a real variable.
Res   Res   Res   Res
Instruction ult Instruction ult Instruction ult Instruction ult
     
k=5/2 2 k=2/5 0 a=5/2 2.0 a=2/5 0.0
     
k = 5.0 / 2 2 k = 2.0 / 5 0 a = 5.0 / 2 2.5 a = 2.0 / 5 0.4
     
k = 5 / 2.0 2 k = 2 / 5.0 0 a = 5 / 2.0 2.5 a = 2 / 5.0 0.4
     
k = 5.0 / 2.0 2 k = 2.0 / 5.0 0 a = 5.0 / 2.0 2.5 a = 2.0 / 5.0 0.4
Introduction to C VC-ICP (Vocational Course on Introduction to C Programming
Expression Evaluation
✯ D = a * b / c. (a = 5, b = 4, c = 3)
D=a*b/c . . . given expression
D=5*4/3 . . . operation: replace variables
D = 20 / 3 . . . operation: *
D=6 . . . operation: /

✯ D=a+b*c (Ans: 17)


✯ X=3*4+5*6 (Ans: 42)
✯ X=3*(4%5)/2 (Ans: 6)
✯ i=2*3/4+4/4+8-2+5/8 (Ans: 8)

Introduction to C VC-ICP (Vocational Course on Introduction to C Programming

You might also like