Programming Languages · Elementary Data Types
Properties of Types and Objects
Overview
In programming languages, a type describes a set of values and the operations that may be applied to those values.
The properties of types and objects decide how a language stores values, checks expressions, converts one type to another, and treats two type names as equivalent.
This topic sits under Elementary Data Types and is examined through definitions, small C/Java code traces, type-equivalence rules, static versus dynamic typing, coercion, casting, enumeration, and macro/type traps.
Before studying it, you should know what a variable, constant, expression, operator, and compile-time error are.
Key concepts
- A data type is defined by its value set and its operation set; representation and size are implementation properties.
- A type error occurs when an operation is applied to a value of an unsuitable type. Type checking verifies that operands and assignments are compatible.
- Static typing checks most type errors before execution, usually at compile time. Dynamic typing checks types during execution.
C, C++, Java and C# are normally statically typed; Python, JavaScript and Lisp are normally dynamically typed. - Strong typing means the language prevents operations that violate type rules, or requires explicit conversions. The term is not standardised; in UGC answers, treat it as “type errors are not silently allowed”.
- Weak typing permits many implicit conversions or unsafe operations, so type errors may survive compilation and cause run-time faults.
- Type equivalence decides whether two types are the same.
Name equivalence says two types are equivalent only if they have the same declared type name; structural equivalence says two types are equivalent if their structures are identical. - In C, two
structtypes with different tags are different types even if their member lists are identical. Atypedefdoes not create a new type; it creates an alias for an existing type. - Coercion is an implicit, compiler-inserted conversion. Explicit conversion or casting is written by the programmer.
- Widening conversion converts to a type that can represent all values of the source type, such as
inttodouble. Narrowing conversion may lose information, such asdoubletoint. - In C,
int / intperforms integer division and truncates toward zero before any assignment conversion. Floating division needs at least one floating operand or a cast. - An enumeration is a user-defined type consisting of named integral constants called enumerators. In C, the first enumerator is
0, and each later one increases by1unless explicitly initialised. - A declaration introduces a name and associates it with a type, scope, and possibly storage class. A definition also allocates storage or provides the implementation.
- An object is a run-time entity that has a type, a value, and usually an identity or storage location. The type of an object determines which operations are legal on its value.
- A preprocessor macro performs textual substitution, not typed function calling. It has no independent type checking; the expanded text is type-checked by the compiler.
How it works
- The programmer writes a declaration such as
int x;, binding the namexto the typeint. The compiler now knows the allowed operations and the amount of storage needed for the object. - The language’s type system checks every expression. For
a + b, it first finds the types ofaandb, applies any implicit promotions or coercions, and then determines the result type. - If typing is static, this checking happens before execution. If typing is dynamic, the run-time system attaches type information to values and checks operations during execution.
- When types differ, the language may apply coercion automatically or require an explicit cast. The common order in C is integer promotions, then usual arithmetic conversions: for example,
intwithdoublegivesdouble. - For user-defined types, the compiler checks type equivalence. In C, a tag such as
struct S1creates a distinct type;typedef int A;only makesAanother spelling ofint. - For enumerations, the compiler turns enumerator names into integer constants and enforces the language’s rules on assigning values to enum variables.
- For macros, the preprocessor replaces the macro call by its body before compilation. Parentheses and side effects matter because the substituted expression is evaluated as written.
Drawing the diagram…
Formulas
- For an unsigned integer of bits:
- For a signed two’s-complement integer of bits:
- For an enumeration with no explicit initialisers:
- For the geometric sum used in float-recursion traces:
- Here is the recursion step and is the number of positive-argument terms.
Worked examples
Example 1 — integer division then conversion.
#include <stdio.h>
int main(void) {
int x = 5;
float y = x / 2;
printf("%.1f\n", y);
return 0;
}
x / 2has operandsintandint, so it is integer division: .- the value
2is converted tofloatand stored inyas2.0.
Output:2.0. Answer: the stored value is2.0, not2.5.
Example 2 — typedef versus struct tags.
#include <stdio.h>
typedef int A;
typedef int B;
struct S1 { int a; };
struct S2 { int a; };
int main(void) {
A x = 7;
B y = x; /* allowed: A and B are both int */
struct S1 p = {1};
struct S2 q = {2};
/* q = p; */ /* compile error if uncommented */
printf("%d\n", y);
return 0;
}
AandBare aliases forint, soB y = x;is justint y = int x;and is allowed.struct S1andstruct S2have different tags, so they are incompatible C types even though both contain oneint.
Output:7. Answer: typedef assignment is legal; struct assignment across different tags is not.
Example 3 — enumeration values. Consider:
#include <stdio.h>
enum color { RED, GREEN = 5, BLUE };
int main(void) {
enum color c = BLUE;
printf("%d %d %d\n", RED, GREEN, BLUE);
printf("%d\n", (int)c);
return 0;
}
REDgets0.GREENis explicitly5.BLUEfollowsGREEN, soBLUE = 6.
Output:
0 5 6
6
Answer: the enumerator values are0, 5, 6.
Example 4 — truncation and floating division.
#include <stdio.h>
int main(void) {
double d = 3.9;
int i = d;
int a = 7, b = 2;
double q1 = a / b;
double q2 = (double) a / b;
printf("%d %.1f %.1f\n", i, q1, q2);
return 0;
}
int i = d;converts3.9toint; the fractional part is discarded, soi = 3.a / bis7 / 2with both operandsint, so it gives3; then3is converted todouble, giving3.0.(double) a / bconvertsatodouble, so division is floating: .
Output:3 3.0 3.5. Answer: narrowing truncates toward zero; a cast forces floating division.
Example 5 — match-the-list type concepts. Match:
| List-I | List-II |
|---|---|
| A. Static typing | i. Types checked at run time |
| B. Dynamic typing | ii. Types checked before execution |
| C. Coercion | iii. Programmer-written conversion |
| D. Explicit conversion | iv. Compiler-inserted implicit conversion |
Correct matching: A–ii, B–i, C–iv, D–iii.
Reason: static typing checks before execution; dynamic typing checks during execution; coercion is implicit; an explicit cast is written by the programmer.
Comparison
| Point | Static typing | Dynamic typing |
|---|---|---|
| Check time | Before execution | During execution |
| Typical error | Compile-time type error | Run-time type error |
| Examples | C, C++, Java | Python, JavaScript |
| Flexibility | Less at run time | More at run time |
| Point | Coercion | Explicit conversion |
|---|---|---|
| Who writes it | Compiler inserts it | Programmer writes a cast |
| Visibility | Hidden | Visible in source |
| Example | double d = 5; | double d = (double) 5 / 2; |
| Main risk | Unexpected automatic conversion | Unsafe narrowing if used carelessly |
| Point | Name equivalence | Structural equivalence |
|---|---|---|
| Rule | Same declared type name | Same internal structure |
C struct | Different tags mean different types | Identical members do not make them same |
typedef | Alias only, not new type | Alias keeps underlying type |
| Java classes | Nominal, by class name | Not used for ordinary class types |
| Point | Declaration | Definition |
|---|---|---|
| Purpose | Introduces name and type | Allocates storage or gives implementation |
| C example | extern int a; | int a = 10; |
| Type checking | Enough for compiler to know type | Also provides object |
Revision summary
- A type = values + operations.
- Static typing checks before run time; dynamic typing checks during run time.
- Strong typing means type violations are not silently accepted.
- C
typedefcreates an alias, not a new type. - Different C
structtags mean different types even with identical members. int / intin C truncates toward zero before assignment conversion.doubletointtruncates the fractional part for in-range values.- Coercion is implicit; cast is explicit.
- First C enumerator is
0; later ones increase by1unless initialised. - Macros are text substitution and have no independent type checking.
- Parenthesise macro arguments and the whole macro body.
Previous-year exam insights
- Direct definition questions ask what a data type is, what a declaration does, and what strong/static typing means.
- Code-trace questions commonly test
int / int, float/double promotion, truncation on assignment, and the effect of a cast. - C type-equivalence questions test different
structtags versustypedefaliases; identical members do not make two tagged structs equivalent. - Statement I/II questions compare static typing with dynamic typing and correctly separate compile-time checking from run-time checking.
- Assertion–Reason questions often test
doubletointtruncation:3.9becomes3, and-3.9becomes-3. - Macro questions rely on textual substitution; check operator precedence, repeated evaluation, and whether the substituted expression is type-valid after expansion.
- Enumeration questions ask for enumerator values, the meaning of enumeration, and the difference between named constants and ordinary variables.
- In match-the-list questions, pair static typing with compile-time checking, dynamic typing with run-time checking, coercion with implicit conversion, and cast with explicit conversion.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Define a data type using value set and operations.
- Distinguish static typing, dynamic typing, strong typing, and weak typing with examples.
- Trace C expressions involving integer division, float promotion, and assignment conversion.
- Explain why
doubletointtruncates and state the result for positive and negative values. - Decide whether two C
structtypes with different tags are compatible. - Explain why
typedef int A; typedef int B;does not create two distinct types. - Compute enumerator values when some enumerators are explicitly initialised.
- Distinguish coercion from explicit conversion in code.
- Analyse a simple macro expansion and identify precedence or repeated-evaluation traps.
- Answer match-the-list and assertion–reason questions on type checking and conversion.
Attempt the sub-topic test first, then the subject test.