- 2067 Asoj · 1+4 marks
Data types and operators in C
Short answer
Data type defines value range, memory size and allowed operations. C operators include arithmetic (+, -, *, /, %), relational, logical, assignment (=, +=), increment, bitwise, conditional (?:), special. Precedence: parentheses highest, comma lowest; associativity mostly left‑to‑right, right‑to‑left for unary/assignment.
What is a data type? Explain the operators available in C along with their precedence and associativity.
Data type
A data type specifies the kind of value a variable can hold, the memory it takes and the operations allowed on it. C has basic types (int, float, double, char), derived types (array, pointer, structure, union) and void.
Operators in C
| Type | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | a % b |
| Relational | < <= > >= == != | a > b |
| Logical | && || ! | a>0 && b>0 |
| Assignment | = += -= *= /= %= | a += 5 |
| Increment/decrement | ++ -- | i++ |
| Bitwise | & | ^ ~ << >> | a & b |
| Conditional | ?: | max = a>b ? a : b |
| Special | sizeof, ,, &, *, ., -> | sizeof(int) |
Sample program (output 9 5 14 3 1, then 1 0 0, 10, 10, 2):
#include <stdio.h>
int main(void)
{
int a = 7, b = 2;
printf("%d %d %d %d %d\n", a + b, a - b, a * b, a / b, a % b);
printf("%d %d %d\n", a > b, a == b, (a > 3) && (b > 3));
a += 3;
printf("%d\n", a);
printf("%d\n", a > b ? a : b);
printf("%d\n", a & b);
return 0;
}
Output:
9 5 14 3 1
1 0 0
10
10
2
Precedence and associativity (highest to lowest)
| Level | Operators | Associativity |
|---|---|---|
| 1 | () [] . -> | left to right |
| 2 | ! ~ ++ -- + - (unary) * & sizeof (type) | right to left |
| 3 | * / % | left to right |
| 4 | + - | left to right |
| 5 | << >> | left to right |
| 6 | < <= > >= | left to right |
| 7 | == != | left to right |
| 8 | & then ^ then | | left to right |
| 9 | && then || | left to right |
| 10 | ?: | right to left |
| 11 | = += -= *= /= %= etc. | right to left |
| 12 | , | left to right |
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Precedence and associativity · Computer Programming, chapter 3 · asked 2×
- Global vs local; & and * operators · Computer Programming, chapter 3
- Machine learning, GA, operators, areas · Artificial Intelligence, chapter 6 · asked 2×
- Distance class relational operators · Object Oriented Programming, chapter 5