UNIT-II: Types, Operators and Expressions (6-Mark Questions with Answers)
Q1. Explain the different types of tokens in C with suitable examples.
Answer: Tokens in C are the smallest individual units in a program. They are categorized into:
1. Keywords: Reserved words with special meaning (e.g., int , return ).
2. Identifiers: Names for variables, functions (e.g., sum , main ).
3. Constants: Fixed values that do not change (e.g., 10 , 'A' ).
4. Strings: Sequence of characters enclosed in double quotes (e.g., "Hello" ).
5. Special Symbols: Characters with special meanings (e.g., ; , { , } ).
6. Operators: Symbols that perform operations (e.g., + , - , == ).
Q2. Differentiate between keywords, identifiers, and constants in C.
Answer:
• Keywords: Predefined and reserved (e.g., if , else ). Cannot be used as names.
• Identifiers: User-defined names for variables, functions (e.g., total , main ).
• Constants: Values that don’t change during execution (e.g., 3.14 , 100 ).
Q3. Write a short note on special symbols used in C programming.
Answer: Special symbols in C have special meanings:
• { } : Block of code
• ( ) : Function call, parameters
• [ ] : Array index
• ; : Statement terminator
• , : Separator
• # : Preprocessor directive
• * : Pointer declaration or multiplication
• . , -> : Access structure members
Q4. Explain basic data types in C with examples and their sizes.
Answer:
1. int – Integer, 4 bytes, e.g., int a = 5;
2. float – Decimal number, 4 bytes, e.g., float b = 3.5;
1
3. double – Large precision float, 8 bytes, e.g., double c = 3.14159;
4. char – Character, 1 byte, e.g., char ch = 'A';
Sizes may vary based on system architecture.
Q5. What are format specifiers? Explain with examples.
Answer: Format specifiers tell printf and scanf the data type:
• %d – Integer
• %f – Float
• %lf – Double
• %c – Character
• %s – String Example:
int a = 5;
printf("%d", a);
Q6. Write a program in C to demonstrate the use of `` operator.
Answer:
#include<stdio.h>
int main() {
printf("Size of int: %lu\n", sizeof(int));
printf("Size of float: %lu\n", sizeof(float));
printf("Size of double: %lu\n", sizeof(double));
printf("Size of char: %lu\n", sizeof(char));
return 0;
}
Q7. Explain variable declaration and initialization in C with syntax and example.
Answer:
• Declaration: Tells the compiler about the variable. Syntax: data_type variable_name;
• Initialization: Assigns a value at the time of declaration. Syntax: data_type variable_name =
value; Example:
2
int a; // declaration
float b = 5.0; // declaration + initialization
Q8. Classify different types of operators in C with examples.
Answer:
1. Arithmetic: + , - , * , / , %
2. Relational: == , != , > , < , >= , <=
3. Logical: && , || , !
4. Assignment: = , += , -= , etc.
5. Bitwise: & , | , ^ , ~ , << , >>
6. Unary: ++ , -- , - , !
7. Ternary: ? :
Q9. Differentiate between arithmetic and relational operators with examples.
Answer:
• Arithmetic Operators: Perform mathematical operations. Example: a + b , a * b
• Relational Operators: Compare values and return boolean results. Example: a > b , a == b
Q10. Explain bitwise operators in C with examples.
Answer: Bitwise operators operate on binary representations:
• & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Right Shift) Example:
int a = 5, b = 3;
printf("a & b = %d", a & b);
Q11. Discuss the use of assignment operators in C. Give examples.
Answer: Assignment operators assign values to variables:
• = assigns value: a = 5;
• += , -= , *= , /= , %= for shorthand operations. Example:
3
int a = 10;
a += 5; // a = a + 5
Q12. What are logical operators? Explain their working with an example.
Answer: Logical operators:
• && (AND): true if both operands are true
• || (OR): true if at least one operand is true
• ! (NOT): true if operand is false Example:
int a = 5, b = 10;
if (a < b && b < 20) {
printf("True");
}
Q13. Explain increment and decrement operators with their types and examples.
Answer:
• Increment (++) increases value by 1
• Decrement (--) decreases value by 1 Types:
• Prefix: ++a , --a
• Postfix: a++ , a--
Q14. Write a program to show the difference between prefix and postfix increment/decrement.
Answer:
#include<stdio.h>
int main() {
int a = 5;
printf("Prefix: %d\n", ++a); // 6
a = 5;
printf("Postfix: %d\n", a++); // 5, then becomes 6
return 0;
}
4
Q15. Explain the use of the conditional (ternary) operator with an example.
Answer: Syntax: condition ? expr1 : expr2; Example:
int a = 5, b = 10;
int max = (a > b) ? a : b;
printf("Max: %d", max);
Q16. What is operator precedence and associativity? Explain with an example.
Answer:
• Precedence: Determines which operator is evaluated first.
• Associativity: Direction of evaluation (left to right or right to left). Example:
int a = 5 + 3 * 2; // * has higher precedence, so result is 11
Q17. Explain the precedence rules for arithmetic and logical operators in C.
Answer: Precedence (highest to lowest for common operators):
1. ()
2. ++ , --
3. * , / , %
4. + , -
5. < , <= , > , >=
6. == , !=
7. &&
8. ||
Q18. What is type conversion? Explain implicit and explicit type conversions with examples.
Answer:
• Implicit Conversion: Automatic type conversion by the compiler. Example:
int a = 5; float b = a;
• Explicit Conversion (Type Casting): Manual conversion. Example: float b = (float) a / 2;