Expression in C

Expression:-
An expression is a combination of variables, constants and operators arranged according to the syntax of the language.
C can handle any complex expression with ease. It is little bit different from algebraic expression.
Algebraic Expressions C Expressions
axb-cxd a*b-c*d
(m+n)(a+b) (m+n)*(a+b)
Evaluation of expression:
We can evaluate an expression by using assignment statement.
As:- Variable = Expression.
e.g. : Temp = ((f * cos(x)/sin (y))+(g * sin(x)/cos(y)))
All relevant variables must be assigned values before the evaluation of the expression.

Type conversion in expression:-
To effectively develop C programs, it would be necessary for you to understand the rules that are used for the implicit conversion of operands.
If an expression contains an operation between an int and a float, the int would be automatically promoted to a float before carrying out of operation.

Automatic type conversion:-
If the operands are of different types the lower type is automatically converted to the higher type before the operation proceeds
The result is of the higher type.
Given below is the sequence of rules that are applied by evaluating expressions:-
Operator 1 Operator 2 Result
Long Double any Long Double
Double any Double
Float any Float
Unsigned Long Int any Unsigned Long In
Long Int any Long Int
Unsigned Int any Unsigned Int
Final result of an expression to the type of the variable on the left of the assignment signed before assigning the value to it.
However, the following changes are introduced during the final assignment:-
1. Float to Int causes truncation of the fractional part.
2. Double to float causes rounding of digits.
3. Long int to int causes dropping of the excess higher order bits
Type Casting:
Casting a value is a forcing a type conversion in a way that is different from the automatic conversion and this process is called type cast.
This should be clear from the following examples:
float a;
int x=6,y=4;
a=x/y;
printf("the value of a =%f",a);
Its output will be:- the value of a =1.000000.
Here the answer is 1.000000, not 1.5 because 6 & 4 both are integer & variable a is of float type.
The general form of casting is: (type_desired) expression;
here type_desired : standard C data types and
expression: constant, variables or an expression.
example of type casting:-
int p=30,s=4;
float r;
r=(float)p/s;
printf("value of r = %f", r);
Its output is :- value of ratio = 7.500000.
Type Definition using typedef:-
C allows us to create data types via the typedef statement.The format of typedef statement is:- "typedef data type new_type_name;"
Example:
typedef int rak;
rak bat1,bat2; /*this statement is equivalent to int bat1,bat2*/

Comments

Popular posts from this blog

Components of C language

Decision making using if statement

How to make payment for final certificate, migration, provisional for the students of last semester of ptu?