2 Data Types And Constants

C Programming ยท Unit 2

Data Types and Constants

Exam-focused notes for Data Types and Constants (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.

Data Types and Constants in short

This chapter explains the kinds of data a C program can store and how to write fixed values. Choosing the right type saves memory and avoids wrong results.

Data Types and Constants: notes

Basic data types and their ranges

A data type tells the compiler how much memory a variable needs. C has these basic types:

  • char: one character, usually 1 byte.
  • int: a whole number, usually 4 bytes on modern systems.
  • float: a decimal number with single precision, 4 bytes.
  • double: a decimal number with double precision, 8 bytes.
  • void: no value, used for functions that return nothing.

Modifiers change the size or sign. They are short, long, signed and unsigned. Typical ranges on a system with a 4-byte int are shown below.

TypeSizeRange
char1 byte-128 to 127
unsigned char1 byte0 to 255
short int2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647.
unsigned int4 bytes0 to 4,294,967,295
long long8 bytesabout -9.2 x 10^18 to 9.2 x 10^18.
float4 bytesabout 6 decimal digits of precision.
double8 bytesabout 15 decimal digits of precision.

In older 16-bit compilers such as Turbo C, int takes 2 bytes. The C standard only fixes minimum sizes, so the exact size depends on the machine.

What this unit covers

  • Basic data types and their ranges
  • Constants and constant declaration
  • Type conversion and casting
  • Sizeof operator
  • Conversion specifiers

Conversion specifiers

2081 ยท 5 marks

What are conversion specifiers? Explain the compilation of C program in brief. [2+3]

--- Conversion specifiers are special format codes used inside printf() and scanf() functions to specify the type and format of data to be printed or read. They always begin with a percent sign (%). Specifier Data Type Example ------------------------------- %d Integer (decimal) printf("%d", 5); ...

Open this answer on its own page โ†’

2080 ยท 5 marks

What are conversion specifiers? Explain the basic structure of a C program. [5]

--- Conversion specifiers are special format codes used inside printf() and scanf() functions to specify the type and format of data to be printed or read. They always begin with a percent sign (%) followed by a character that indicates the data type. Specifier Data Type Example -----------------...

Open this answer on its own page โ†’

Basic data types and their ranges

2081 ยท 5 marks

What is constant? Explain basic data types in C programming along with their range. [1+4]

Show solved answer

Constants and Basic Data Types in C Programming


What is a Constant? [1 Mark]

A constant is a fixed value that does not change during the execution of a program. Once defined, its value remains the same throughout the program.

Example:

const int x = 10;   // x is always 10
#define PI 3.14159  // PI is always 3.14159

Basic Data Types in C Programming [4 Marks]

C provides several fundamental (basic) data types to store different kinds of data. These are:


1. int (Integer Type)

  • Used to store whole numbers (positive, negative, or zero).
  • No decimal point.
TypeSizeRange
int2 or 4 bytes-32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes)
short int2 bytes-32,768 to 32,767
long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (4 bytes)
int age = 20;

2. float (Floating Point Type)

  • Used to store real numbers (numbers with decimal points).
  • Single precision.
TypeSizeRange
float4 bytes3.4 x 10^-38 to 3.4 x 10^+38 (6 decimal digits precision)
float price = 99.99;

3. double (Double Precision Float)

  • Used to store large real numbers with more precision than float.
TypeSizeRange
double8 bytes1.7 x 10^-308 to 1.7 x 10^+308 (15 decimal digits precision)
long double10 bytes3.4 x 10^-4932 to 1.1 x 10^+4932
double pi = 3.14159265358979;

4. char (Character Type)

  • Used to store a single character (letter, digit, or symbol).
  • Stored as its ASCII value internally.
TypeSizeRange
char1 byte-128 to 127 (signed)
unsigned char1 byte0 to 255
char grade = 'A';

Summary Table

Data TypeSizeRange
int2/4 bytes-32,768 to 32,767 / -2,147,483,648 to 2,147,483,647
float4 bytes3.4e-38 to 3.4e+38
double8 bytes1.7e-308 to 1.7e+308
char1 byte-128 to 127

Note: The exact size may vary depending on the compiler and system architecture (32-bit or 64-bit).

Open this answer on its own page โ†’

2079 ยท 5 marks

How keyword is different from Variables? Discuss Basic data types with its ranges. [5]

Aspect Keywords Variables ----------------------------- Definition Reserved words predefined by the programming language Named memory locations used to store data Purpose Have special meaning and perform specific functions in the language Hold values that can change during program execution User-...

Open this answer on its own page โ†’

2078 ยท 10 marks

What different data types available in C along with their respective range?Write a program to check whether a given integer is palindrome or not.[5+5]

Show solved answer

Data Types in C with Their Ranges

(a) Data Types in C

A data type defines the type of data a variable can hold, the amount of memory allocated, and the operations that can be performed on it.

Primary (Fundamental) Data Types in C

Data TypeSizeRange
char1 byte-128 to 127
unsigned char1 byte0 to 255
int2 or 4 bytes-32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned int2 or 4 bytes0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (4 bytes)
short int2 bytes-32,768 to 32,767
long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned long int4 bytes0 to 4,294,967,295
float4 bytes3.4E-38 to 3.4E+38 (6 decimal digits precision)
double8 bytes1.7E-308 to 1.7E+308 (15 decimal digits precision)
long double10 bytes3.4E-4932 to 1.1E+4932
void0 bytesNo value

Classification of Data Types

Data Types in C
โ”œโ”€โ”€ Primary (Basic)
โ”‚   โ”œโ”€โ”€ int
โ”‚   โ”œโ”€โ”€ char
โ”‚   โ”œโ”€โ”€ float
โ”‚   โ”œโ”€โ”€ double
โ”‚   โ””โ”€โ”€ void
โ”œโ”€โ”€ Derived
โ”‚   โ”œโ”€โ”€ Array
โ”‚   โ”œโ”€โ”€ Pointer
โ”‚   โ””โ”€โ”€ Function
โ””โ”€โ”€ User Defined
    โ”œโ”€โ”€ struct
    โ”œโ”€โ”€ union
    โ””โ”€โ”€ enum

(b) Program to Check Palindrome Integer

Concept

A number is called a palindrome if it reads the same forwards and backwards.

  • Example: 121, 1331, 12321 are palindromes
  • Example: 123, 456 are NOT palindromes

Algorithm

  1. Read the integer n
  2. Store original number in original
  3. Reverse the digits of n to get reversed
  4. If original == reversed, it is a palindrome
  5. Otherwise, it is not a palindrome

C Program

#include <stdio.h>

int main() {
    int n, original, reversed = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &n);

    original = n;

    /* Handle negative numbers - negative numbers are not palindromes */
    if (n < 0) {
        printf("%d is NOT a palindrome.\n", n);
        return 0;
    }

    /* Reverse the digits of the number */
    while (n != 0) {
        remainder = n % 10;       /* Extract last digit */
        reversed = reversed * 10 + remainder;  /* Build reversed number */
        n = n / 10;               /* Remove last digit */
    }

    /* Check if original and reversed are equal */
    if (original == reversed) {
        printf("%d is a PALINDROME.\n", original);
    } else {
        printf("%d is NOT a PALINDROME.\n", original);
    }

    return 0;
}

Dry Run / Trace (for n = 121)

Iterationnremainder (n%10)reversed
Start121-0
1st1210*10+1 = 1
2nd121*10+2 = 12
3rd0112*10+1 = 121
  • original = 121, reversed = 121
  • Since 121 == 121, it is a PALINDROME

Sample Output

Enter an integer: 121
121 is a PALINDROME.

Enter an integer: 123
123 is NOT a PALINDROME.
Open this answer on its own page โ†’

Type conversion and casting

2081.2 ยท 5 marks

What is the task of sizeof and comma operator? Discuss about type conversion. [5]

--- The sizeof operator is a compile-time unary operator that returns the size (in bytes) of a variable or data type. Syntax: Tasks of sizeof: - Determines the number of bytes occupied by a data type or variable in memory. - Helps write portable programs (size may vary across platforms). - Common...

Open this answer on its own page โ†’