1) Base of hexadecimal number system? Answer : 16 2) Universal gate in digital logic? Answer : NAND 3) Memory type that is non-volatile? Answer : ROM 4) Basic building block of digital circuits? Answer : Gate 5) Device used for data storage in sequential circuits? Answer : Flip-flop 6) Architecture with shared memory for instructions and data? Answer : von Neumann 7) The smallest unit of data in computing? Answer : Bit 8) Unit that performs arithmetic operations in a CPU? Answer : ALU 9) Memory faster than main memory but smaller in size? Answer : Cache 10) System cycle that includes fetch, decode, and execute? Answer : Instruction 11) Type of circuit where output depends on present input only? Answer : Combinational 12) The binary equivalent of decimal 10? Answer : 1010 13) Memory used for high-speed temporary storage in a CPU? Answer : Register 14) Method of representing negative numbers in binary? Answer : Two's complement 15) Gate that inverts its input signal? Answer : NOT 16)...
C program important points
Operators And Expressions
C language supports many types of operators, which can be used with variables and constants to form expressions. These operators can be divided into the following major groups:
*Arithmetic operators
* Relational operators
* Equality operators
* Logical operators
* Unary operators
* Conditional operator
* Bitwise operators
* Assignment operators
* Comma operator
* Sizeof operator
We will now discuss all these operators.
Arithmetic Operators
Consider three variables declared as,
int a=9, b=3, result;
We will use these variables to explain arithmetic operators.
Below Table shows the arithmetic operators, their syntax, and usage in C language.
Table : Arithmetic operators
In Table a and b (on which the operator is applied) are called operands. Arithmetic operators can be applied to any integer or floating point number. The addition, subtraction, multiplication, and division (+, –, *, and /) operators are the usual arithmetic operators, so you are already familiar with these operators.
However, the operator % might be new to you. The modulus operator (%) finds the remainder of an integer division. This operator can be applied only on integer operands and cannot be used on float or double operands.
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
When both operands of the division operator (/) are integers, the division is performed as an integer division. Integer division always results in an integer result. So, the result is always rounded-off by ignoring the remainder. Therefore,
9/4 = 2 and –9/4 = –3
Except for modulus operator, all other arithmetic operators can accept a mix of integer and floating point numbers. If both operands are integers, the result will be an integer; if one or both operands are floating point numbers then the result would be a floating point number.
All the arithmetic operators bind from left to right. Multiplication, division, and modulus operators have higher precedence over addition and subtraction operators. Thus, if an arithmetic expression consists of a mix of operators, then multiplication, division, and modulus will be carried out first in a left to right order, before any addition and subtraction can be performed. For example,
3 + 4 * 7
= 3 + 28
= 31
Relational Operators
A relational operator, also known as a comparison operator, is an operator that compares two values or expressions. Relational operators return true or false value, depending on whether the conditional relationship between the two
operands holds or not.
Table: Relational Operator
For example, to test if x is less than y,
relational operator < is used as x < y. This
expression will return true value if x is less than y; otherwise the value of the expression will be false. C provides four relational operators which are illustrated in Table. These operators are evaluated from left to right.
Equality Operators
C language also supports two equality operators to compare operands for strict equality or inequality. They are equal to (==) and not equal to (!=) operators. The equality operators have lower precedence than the relational operators.The equal-to operator (==) returns true (1) if operands
on both sides of the operator have the same value; otherwise, it returns false (0). On the contrary, the not-equal-to operator (!=) returns true (1) if the operands do
not have the same value; else it returns false (0). Below table summarizes equality operators.
Table: Equality Operator
Logical Operators
C language supports three logical operators. They are logical AND (&&), logical OR (||), and logical NOT (!). As in case of arithmetic expressions, logical expressions are evaluated from left to right.
Logical AND (&&)
Logical AND is a binary operator, which simultaneously evaluates two values or relational expressions. If both the operands are true, then the whole expression is true. If both or one of the operands is false, then the whole expression evaluates to false. The truth table of logical AND operator is given in below table For example,(a < b) && (b > c)The whole expression is true only if both expressions are true, i.e., if b is greater than a and c.
Table: Truth table for logical AND
Logical OR (||)
Logical OR returns a false value if both the operands are false.Otherwise it returns a true value. The truth table of logical OR
operator is given in below table. For example,(a < b) || (b > c)The whole expression is true if either b is greater than a or b is greater than c or b is greater than both a and c.
Table: Truth table for logical OR
Logical NOT (!)
The logical NOT operator takes a single expression and produces a zero if the expression evaluates to a non-zero value and produces a 1 if the expression produces a zero. The truth table of logical NOT operator is given in below table. For example,int a = 10, b; b = !a; Now the value of b = 0. This is because value of a = 10. !a = 0. The value of !a is assigned to b, hence the result.
Unary Operators
Unary operators act on single operands. C language supports three unary operators. They are unary minus, increment, and decrement operators.
Unary Minus (–)
Unary minus operator negates the value of its operand. For example, if a number is positive then it becomes negative when preceded with a unary minus operator. Similarly, if the number is negative, it becomes positive after applying the unary minus operator. For example,
int a, b = 10;
a = –(b);
The result of this expression is a = –10, because variable b has a positive value. After applying unary minus operator (–) on the operand b, the value becomes –10, which indicates it has a negative value.
Increment Operator (++) and Decrement (––)
The increment operator is a unary operator that increases the value of its operand by 1. Similarly, the decrement operator decreases the value of its operand by 1. For example, – –x is equivalent to writing x = x – 1.
The increment/decrement operators have two variants: prefix and postfix. In a prefix
expression (++x or – –x), the operator is applied before the operand while in a postfix expression (x++ or x––), the operator is applied after the operand.
An important point to note about unary increment and decrement operators is that ++x is not same as x++. Similarly, – –x is not the same as x– –. Although, x++ and ++x both increment the value of x by 1, in the former case, the value of x is returned before it is incremented. Whereas in the
latter case, the value of x is returned after it is incremented. For example,
int x = 10, y;
y = x++; is equivalent to writing
y = x;
x = x + 1;
Whereas y = ++x; is equivalent to writing
x = x + 1;
y = x;
The same principle applies to unary decrement operators. Note that unary operators have a higher precedence than the binary operators. And if in an expression we have more than one unary operator then they are evaluated from right to left.
Conditional Operator
The syntax of the conditional operator is
exp1 ? exp2 : exp3
exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of the expression, otherwise exp3 is evaluated and becomes the result of the expression. For example,
large = (a > b) ? a : b
The conditional operator is used to find the larger of two given numbers. First exp1, that is a > b, is evaluated. If a is greater than b, then large = a, else large = b. Hence, large is equal to either a or b, but not both.
Conditional operators make the program code more compact, more readable, and safer to use as it is easier to both check and guarantee the arguments that are used for evaluation. Conditional operator is also known as ternary operator as it takes three operands.
Bitwise Operators
As the name suggests, bitwise operators perform operations at the bit level. These operators include: bitwise AND, bitwise OR, bitwise XOR, and shift operators.
Bitwise AND
Like boolean AND (&&), bitwise AND operator (&) performs operation on bits instead of bytes, chars, integers, etc. When we use the bitwise AND operator, the bit in the first operand is ANDed with the corresponding bit in the second operand. The truth table is the same as we had seen in logical AND operation. 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 & 01010101 = 00000000
Bitwise OR
When we use the bitwise OR operator (|), the bit in the first operand is ORed with the corresponding bit in the second operand. The truth table is the same as we had seen in logical OR operation. 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 | 01010101 = 11111111
Bitwise XOR
When we use the bitwise XOR operator, the bit in the first operand is XORed with the corresponding bit in the second operand. The truth table of bitwise XOR operator
is shown in below table. 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 ^ 01010101 = 11111111
Table: Truth table for bitwise XOR
Bitwise NOT (~)
The bitwise NOT or complement is a unary operator that performs logical negation on each bit of the operand. By performing negation of each bit, it actually produces
the one’s 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 1. For example,
~10101011 = 01010100
Shift Operators
C supports two bitwise shift operators. They are shift left (<<) and shift right (>>). The syntax for a shift operation can be given as
operand op num
where the bits in the operand are shifted left or right depending on the operator (left, if the operator is << and right, if the operator is >>) by number of places denoted by num. For example, if we have
x = 0001 1101 then x << 1 produces 0011 1010
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. Therefore, if we have x
= 0001 1101, then
x << 3 gives result = 1110 1000
On the contrary, 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 gives result = 0000 1110
Similarly, if we have x = 0001 1101, then
x >> 4 gives result = 0000 0001
Assignment Operators
In C language, the assignment operator is responsible for assigning values to the variables. While the equal sign (=) is the fundamental assignment operator, C also supports other assignment operators that provide shorthand ways to represent common variable assignments.
When an equal sign is encountered in an expression, the compiler processes the statement on the right side of the sign and assigns the result to the variable on the left side. For example,
int x;
x = 10;
assigns the value 10 to variable x. The assignment operator has right-to-left associativity, so the expression a = b = c = 10;is evaluated as
(a = (b = (c = 10)));
First 10 is assigned to c, then the value of c is assigned to b. Finally, the value of b is assigned to a. Below table contains a list of other assignment operators that are supported by C.
Table: Assignment Operator
Comma Operator
The comma operator, which is also called the sequential-evaluation operator, takes two operands. It works by evaluating the first expression and discarding its value, and then evaluates the second expression and returns the value as the result of the expression.Comma-separated expressions 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.
Therefore, when a comma operator is used, the entire expression evaluates to the value of the right expression. For example, the following statement first increments a, then increments b, and then assigns the value of b to x.
int a=2, b=3, x=0;
x = (++a, b+=a);
Now, the value of x = 6.
sizeof Operator
sizeof is a unary operator used to calculate the size of data types. This operator can be applied to all data types. When using this operator, the keyword sizeof is followed by a type name, variable, or expression. The operator returns the size of the data type, variable, or expression in bytes. That is, the sizeof operator is used to determine the amount of memory space that the data type/variable/expression will take.
When a type name is used, it is enclosed in parentheses, but in case of variable names and expressions, they can be specified with or without parentheses. A sizeof expression returns an unsigned value that specifies the size of the space in bytes required by the data type, variable, or expression. For example, sizeof(char) returns 1, that is the size of a character data type. If we have,
int a = 10;
unsigned int result;
result = sizeof(a);
then result = 2, that is, space required to store the variable a in memory. Since a is an integer, it requires 2 bytes of storage space.
Operator Precedence Chart
Below table lists the operators that C language supports in the order of their precedence (highest to lowest). The associativity indicates the order in which the operators of equal precedence in an
expression are evaluated.
Table: Operator precedence chart
Examples of Expressions Using the Precedence Chart
If we have the following variable declarations:
int a = 0, b = 1, c = –1;
float x = 2.5, y = 0.0;
then,
(a) a && b = 0
(b) a < b && c < b = 1
(c) b + c || ! a
= ( b + c) || (!a)
= 0 ||1
= 1
(d) x * 5 && 5 || ( b / c)
= ((x * 5) && 5) || (b / c)
= (12.5 && 5) || (1/–1)
= 1
(e) a <= 10 && x >= 1 && b
= ((a <= 10) && (x >= 1)) && b
= (1 && 1) && 1
= 1
(f) !x || !c || b + c
= ((!x) || (!c)) || (b + c)
= (0 || 0) || 0
= 0
(g) x * y < a + b || c
= ((x * y) < (a + b)) || c
= (0 < 1) || –1
= 1
(h) (x > y) + !a || c++
= ((x > y) + (!a)) || (c++)
= (1 + 1) || 0
= 1
Write a program to calculate the area of a circle.
#include <stdio.h>
#include <conio.h>
int main()
{
float radius;
double area;
clrscr();
printf("\n Enter the radius of the circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf(" \n Area = %.2lf", area);
return 0;
}
Output
Enter the radius of the circle : 7
Area = 153.86