1. Gyanastra
  2. Programming Languages
  3. Elementary Data Types
  4. Properties of Types and Objects
Free lesson, no account needed 10 min read 9 practice questions after it
Asked in past papers3 times
  • GATE CSE2003
  • ISRO2018
  • UGC NET2012

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 struct types with different tags are different types even if their member lists are identical. A typedef does 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 int to double. Narrowing conversion may lose information, such as double to int.
  • In C, int / int performs 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 by 1 unless 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

  1. The programmer writes a declaration such as int x;, binding the name x to the type int. The compiler now knows the allowed operations and the amount of storage needed for the object.
  2. The language’s type system checks every expression. For a + b, it first finds the types of a and b, applies any implicit promotions or coercions, and then determines the result type.
  3. 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.
  4. 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, int with double gives double.
  5. For user-defined types, the compiler checks type equivalence. In C, a tag such as struct S1 creates a distinct type; typedef int A; only makes A another spelling of int.
  6. For enumerations, the compiler turns enumerator names into integer constants and enforces the language’s rules on assigning values to enum variables.
  7. 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.
Figure. Notice the split: static typing checks before execution and leads to compile-time errors, while dynamic typing checks during execution and leads to run-time errors.

Formulas

  • For an unsigned integer of nn bits: 0≤v≤2n−10 \le v \le 2^n-1
  • For a signed two’s-complement integer of nn bits: −2n−1≤v≤2n−1−1-2^{n-1} \le v \le 2^{n-1}-1
  • For an enumeration with no explicit initialisers: v(E0)=0,v(Ei)=v(Ei−1)+1v(E_0)=0,\qquad v(E_i)=v(E_{i-1})+1
  • For the geometric sum used in float-recursion traces: ∑k=1N(23)k=2(1−(23)N)→2 as N→∞\sum_{k=1}^{N}\left(\frac{2}{3}\right)^k =2\left(1-\left(\frac{2}{3}\right)^N\right)\to 2 \text{ as }N\to\infty
  • Here kk is the recursion step and NN 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;
}
  1. x / 2 has operands int and int, so it is integer division: 5/2=25/2=2.
  2. the value 2 is converted to float and stored in y as 2.0.
    Output: 2.0. Answer: the stored value is 2.0, not 2.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;
}
  1. A and B are aliases for int, so B y = x; is just int y = int x; and is allowed.
  2. struct S1 and struct S2 have different tags, so they are incompatible C types even though both contain one int.
    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;
}
  1. RED gets 0.
  2. GREEN is explicitly 5.
  3. BLUE follows GREEN, so BLUE = 6.
    Output:
    0 5 6
    6
    Answer: the enumerator values are 0, 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;
}
  1. int i = d; converts 3.9 to int; the fractional part is discarded, so i = 3.
  2. a / b is 7 / 2 with both operands int, so it gives 3; then 3 is converted to double, giving 3.0.
  3. (double) a / b converts a to double, so division is floating: 7.0/2=3.57.0/2=3.5.
    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-IList-II
A. Static typingi. Types checked at run time
B. Dynamic typingii. Types checked before execution
C. Coercioniii. Programmer-written conversion
D. Explicit conversioniv. 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

PointStatic typingDynamic typing
Check timeBefore executionDuring execution
Typical errorCompile-time type errorRun-time type error
ExamplesC, C++, JavaPython, JavaScript
FlexibilityLess at run timeMore at run time
PointCoercionExplicit conversion
Who writes itCompiler inserts itProgrammer writes a cast
VisibilityHiddenVisible in source
Exampledouble d = 5;double d = (double) 5 / 2;
Main riskUnexpected automatic conversionUnsafe narrowing if used carelessly
PointName equivalenceStructural equivalence
RuleSame declared type nameSame internal structure
C structDifferent tags mean different typesIdentical members do not make them same
typedefAlias only, not new typeAlias keeps underlying type
Java classesNominal, by class nameNot used for ordinary class types
PointDeclarationDefinition
PurposeIntroduces name and typeAllocates storage or gives implementation
C exampleextern int a;int a = 10;
Type checkingEnough for compiler to know typeAlso 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 typedef creates an alias, not a new type.
  • Different C struct tags mean different types even with identical members.
  • int / int in C truncates toward zero before assignment conversion.
  • double to int truncates the fractional part for in-range values.
  • Coercion is implicit; cast is explicit.
  • First C enumerator is 0; later ones increase by 1 unless 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 struct tags versus typedef aliases; 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 double to int truncation: 3.9 becomes 3, and -3.9 becomes -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:

  1. Define a data type using value set and operations.
  2. Distinguish static typing, dynamic typing, strong typing, and weak typing with examples.
  3. Trace C expressions involving integer division, float promotion, and assignment conversion.
  4. Explain why double to int truncates and state the result for positive and negative values.
  5. Decide whether two C struct types with different tags are compatible.
  6. Explain why typedef int A; typedef int B; does not create two distinct types.
  7. Compute enumerator values when some enumerators are explicitly initialised.
  8. Distinguish coercion from explicit conversion in code.
  9. Analyse a simple macro expansion and identify precedence or repeated-evaluation traps.
  10. Answer match-the-list and assertion–reason questions on type checking and conversion.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2