C – Operators and Expressions
In the C programming language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted
The types in C can be classified as follows:
|S.N.||Types and Description|
They are arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.
They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
|3||The type void:
The type specifier void indicates that no value is available.
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
The array types and structure types are referred to collectively as the aggregate types. The type of a function specifies the type of the function’s return value. We will see basic types in the following section, whereas, other types will be covered in the upcoming chapters.
Following table gives you details about standard integer types with its storage sizes and value ranges:
|Type||Storage size||Value range|
|char||1 byte||-128 to 127 or 0 to 255|
|unsigned char||1 byte||0 to 255|
|signed char||1 byte||-128 to 127|
|int||2 or 4 bytes||-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647|
|unsigned int||2 or 4 bytes||0 to 65,535 or 0 to 4,294,967,295|
|short||2 bytes||-32,768 to 32,767|
|unsigned short||2 bytes||0 to 65,535|
|long||4 bytes||-2,147,483,648 to 2,147,483,647|
|unsigned long||4 bytes||0 to 4,294,967,295|
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:
printf(“Storage size for int : %d \n”,sizeof(int));
When you compile and execute the above program it produces the following result on Linux:
Storage size for int : 4
Following table gives you details about standard floating-point types with storage sizes and value ranges and their precision:
|Type||Storage size||Value range||Precision|
|float||4 byte||1.2E-38 to 3.4E+38||6 decimal places|
|double||8 byte||2.3E-308 to 1.7E+308||15 decimal places|
|long double||10 byte||3.4E-4932 to 1.1E+4932||19 decimal places|
The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. Following example will print storage space taken by a float type and its range values:
printf(“Storage size for float : %d \n”,sizeof(float));
printf(“Minimum float positive value: %E\n”, FLT_MIN );
printf(“Maximum float positive value: %E\n”, FLT_MAX );
printf(“Precision value: %d\n”, FLT_DIG );
When you compile and execute the above program, it produces the following result on Linux:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
The void Type
The void type specifies that no value is available. It is used in three kinds of situations:
|S.N.||Types and Description|
|1||Function returns as void
There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status);
|2||Function arguments as void
There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void);
|3||Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc(size_t size ); returns a pointer to void which can be casted to any data type.
The void type may not be understood to you at this point, so let us proceed and we will cover these concepts in the upcoming chapters.
C – Operators and Expressions
- The symbols which are used to perform logical and mathematical operations in a C program are called C operators.1
- These C operators join individual constants and variables to form expressions.
- Operators, functions, constants and variables are combined together to form expressions.
- Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.
Types of C operators:
C language offers many types of operators. They are,
- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bit wise operators
- Conditional operators (ternary operators)
- Increment/decrement operators
- Special operators
Arithmetic Operators in C:
- C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.
Assignment operators in C:
- In C programs, values for the variables are assigned using assignment operators.
- For example, if the value “10″ is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
- Other assignment operators in C language are given below.
|Simple assignment operator||=||sum=10||10 is assigned to variable sum|
|Compound assignment operators||+=||sum+=10||This_is_same_as_sum=sum+10…………|
|-=||sum-=10||This is same as sum = sum-10|
|*=||sum*=10||This is same as sum = sum*10|
|/+||sum/=10||This is same as sum = sum/10|
|%=||sum%=10||This is same as sum = sum%10|
|&=||sum&=10||This is same as sum = sum&10|
|^=||sum^=10||This is same as sum = sum^10|
Relational operators in C:
- Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
|1||>||x > y||x is greater than y|
|2||<||x < y||x is less than y|
|3||>=||x >= y||x is greater than or equal to y|
|4||<=||x <= y||x is less than or equal to y|
|5||==||x == y||x is equal to y|
|6||!=||x != y||x is not equal to y|
Logical operators in C:
- These operators are used to perform logical operations on the given expressions.
- There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
|1||&&||logical AND||(x>5)&&(y<5)||It returns true when both conditions are true|
|2||||||logical OR||(x>=10)||(y>=10)||It returns true when at-least one of the condition is true|
|3||!||logical NOT||!((x>5)&&(y<5))||It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false
Bit wise operators in C:
- These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
- Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >> (right shift).
|x||y||x|y||x & y||x ^ y||Operator_symbol||Operator_name|
Conditional or ternary operators in C:
- Conditional operators return one value if condition is true and returns another value is condition is false.
- This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
- In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.
C – Increment/decrement Operators
- Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
Increment operator : ++ i ; i ++ ;
Decrement operator: – – i ; i – – ;
Difference between pre/post increment & decrement operators in C:
- Below table will explain the difference between pre/post increment and decrement operators in C.
|1||Pre increment||++i||Value of i is incremented before assigning it to variable i.|
|2||Post-increment||i++||Value of i is incremented after assigning it to variable i.|
|3||Pre decrement||– –i||Value of i is decremented before assigning it to variable i.|
|4||Post_decrement||i– –||Value of i is decremented after assigning it to variable i.|
Special Operators in C:
- Below are some of special operators that C language offers.
|1||&||This is used to get the address of the variable.
Example : &a will give address of a.
|2||*||This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
|3||Sizeof ()||This gives the size of the variable.
Example : size of (char) will give us 18u.
Posted By-: Vissicomp Technology Pvt. Ltd.
Website -: http://www.vissicomp.com