Skip to study material
← Question index

QUESTION 44 · C fundamentals

Explain the basic C data types, their ranges and how to check their sizes.

5 marks

Answer in brief

A data type tells us what kind of value an object can store and which operations can be performed on it.

Basic data types

  1. char: Stores character-sized values. It is commonly used for individual characters and character arrays.
  2. Integer types: short, int, long and long long store whole numbers. Signed forms can represent negative values; unsigned forms represent non-negative values.
  3. Floating types: float, double and long double store approximate values that can include fractions.
  4. Boolean type: In C11, _Bool stores logical values as 0 or 1. The stdbool.h header provides the name bool.
  5. void: Shows the absence of a value, such as a function that returns no result.

Sizes and ranges

Type sizes can differ between implementations. int is not always two bytes, and long double is not always ten bytes. sizeof reports a size in C bytes. sizeof(char) is always 1. CHAR_BIT, from limits.h, tells us how many bits are in a C byte. INT_MIN and INT_MAX give the int limits. Use %zu to print a sizeof result, whose type is size_t.

Enumeration

An enumeration gives names to related integer values. In enum Day { MONDAY, TUESDAY, WEDNESDAY };, the values start at 0 unless other values are specified. Thus TUESDAY has value 1. Names can make the code easier to read than unexplained numbers. Do not assume an enum object always occupies one byte.

Choice of type

Use an integer for a count and a floating type when a fractional result is required. Check the largest allowed value and intermediate calculations. Every fixed-width type has a limited range. A floating type does not store every decimal fraction exactly. Plain char can act as signed or unsigned depending on the implementation; specify the form when that difference matters.

Reference table

↔ Scroll horizontally on a small screen
Common scalar types
Type familyExamplesUse
Characterchar, signed char, unsigned charCharacter-sized values and small integers.
Integershort, int, long, long long and unsigned variantsWhole-number arithmetic.
Floating-pointfloat, double, long doubleApproximate real-number arithmetic.
Boolean_Bool; bool through <stdbool.h> in C11Logical values normalised to 0 or 1.

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
#include <limits.h>
int main(void)
{
    printf("sizeof(char) = %zu C byte\n", sizeof(char));
    printf("CHAR_BIT is at least 8: %d\n", CHAR_BIT >= 8);
    printf("int supports at least -32767..32767: %d\n",
           INT_MIN <= -32767 && INT_MAX >= 32767);
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

sizeof(char) = 1 C byte
CHAR_BIT is at least 8: 1
int supports at least -32767..32767: 1

Key points

  • Use sizeof and limits macros to inspect the implementation.
  • A C byte is the unit measured by sizeof.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • September/October 2021 · Q6(a) · 7 marks

    Define data type. Briefly discuss the fundamental data types supported in C.

    Paper P01 · page 1
  • September 2023 · Q1(c) · 1 marks

    Define the enumeration data type in C.

    Paper P06 · page 1
  • January/February 2024 · Q4(a) · 4 marks

    Explain how to define and use an enumeration data type in C.

    Paper P08 · page 1
View all matching exam references →