Data Types in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Data types in C (or any programming language) are essential to learn and understand before you start to write a program. They are needed to store different data types like integers, characters, decimals, strings, and even user-defined data. In this article, we will understand C data types and how many data types are in c.

Classification of C Data Types

Basic data types in C are attributes of data that tell the C compiler which type of data a variable holds.

It can be of type integer, float( decimal), character, boolean( true/false ) etc.

We use data types to specify the data type our variables hold.

Before delving into the specifics, a common question arises: "How many data types in C?"

c data types

Broadly, there are two types of C data types: - Primary Data types or Basic Data Types in c - Secondary Data Types

a. Primary Data Types or Basic Data Types: These are the most basic data types, and all the other data types are derived or made from them only. It contains integers, floating points, and chars.

Four main types of primary/basic data types are:

  1. Integer
  2. Float
  3. Char
  4. Void

data type size based on a 32 bit and 64 bit

b. Secondary Data Types: Secondary data types are formed by combining two or more primary data types in C.

They are mainly of two types:

  1. USER-DEFINED DATA TYPES The user defines these data types as per their convenience. If a user needs to have a data type that is not predefined in the C library, they make their own. User-defined c data types

  2. DERIVED DATA TYPE: Derived data types are data types that are formed by combining one or more primitive data types or basic data types in C. For example, some cases might be where primitive data types are insufficient for us. If we want to store a phone number, we can use an integer, but what if we want to store the phone numbers of all the students in a college? There are better ways to make variables for each of them.

To deal with such situations optimally, C has some derived data types, which we can use at our convenience.

  1. ARRAY
  2. STRING
  3. POINTER
  4. FUNCTIONS

We will discuss Data types in c with examples in the next paragraphs.

Integer Data Types in C

Integer datatypes in C are used to represent whole numbers. They are classified based on their storage size and whether they are signed or unsigned.

  • Range refers to the set of values a data type can represent. For example, a 16-bit signed integer ranges from -32,768 to 32,767.
  • Size: It indicates the amount of memory allocated to store a value of a particular data type. For instance, an int typically occupies 4 bytes of memory.
  • Format Specifier: It's used in input/output functions like print and scan to specify the data type of the value being printed or read. For example, %d is the format specifier for printing integers.

The integer data type can also be used as:

  1. unsigned int:

    • Represents non-negative integers (zero and positive integers).
    • Example: unsigned int count = 10;
  2. short int:

    • Represents integers with a smaller range compared to int.
    • Example: short int temperature = -5;
  3. long int:

    • Represents integers with a larger range compared to int.
    • Example: long int population = 1000000L;
  4. unsigned short int:

    • Represents non-negative integers with a smaller range compared to unsigned int.
    • Example: unsigned short int quantity = 100;

Character Data Type in C

The char datatype in C represents single characters, such as letters, digits, and symbols. It typically occupies 1 byte of memory, allowing it to store ASCII characters encoded using 7 bits. Characters are enclosed in single quotes when assigned to char variables, distinguishing them from strings.

  • Range: The range typically spans from -128 to 127 for signed' char'. However, if treated as unsigned, the range extends from 0 to 255.
  • Size: The char data type typically occupies 1 byte of memory.
  • Format Specifier: The format specifier for char values is %c when used with input/output functions like printf and scanf.

Syntax:

Example:

In this example, letter is a char variable initialized with the character 'A'. The character is enclosed in single quotes to differentiate it from a string. You can assign any single character to a char variable using single quotes.

Float Data Type in C

The float data type in C represents single-precision floating-point numbers or decimals, which are numbers with a fractional component.

  • Range: Typically, a float data type range is approximately from 1.2E-38 to 3.4E+38. It can represent numbers with a precision of about six decimal places.
  • Size: The float data type typically occupies 4 bytes (32 bits) of memory.
  • Format Specifiers: The format specifier for float values is %f when used with input/output functions like printf and scanf.

Syntax:

Example:

In this example, temperature is a float variable initialized with the value 98.6. Floats are used when a higher range or precision than int is needed, especially for decimal numbers.

Double Data Type in C

The double data type in C represents the double-precision floating-point numbers, which provide higher precision than float.

  • Range: Typically, the range of a double data type is approximately from 1.7E-308 to 1.7E+308. It can represent numbers with a precision of about 15 decimal places.
  • Size: The double data type typically occupies 8 bytes (64 bits) of memory.
  • Format Specifiers: The format specifier for double values is %lf when used with input/output functions like printf and scanf.

Syntax:

Example:

In this example, pi is a double variable initialized with the value of pi to a high precision. Doubles are used when even higher precision than float is needed, especially for scientific calculations.

Void Data Type in C

The void data type in C is used to tell the absence of a specific type. It is often used as a return type for functions that do not return a value.

  • Range: The void data type does not represent a range of values since it indicates nothingness or absence of a value.
  • Size: The void data type does not occupy any memory, as it does not hold any value.
  • Format Specifiers: There are no format specifiers for void data type as it does not hold any value that can be printed or scanned.

Syntax:

Example:

In this example, greet() is a function with a return type of void, indicating that it does not return any value. It simply prints "Hello, world!" to the console.

Size of The Data Types in C

The size of data types in C may vary depending on the compiler and the system's architecture. However, the C standard guarantees minimum sizes for each data type. Here are the typical sizes of data types on most systems:

  1. char: Typically 1 byte
  2. short int (or simply short): Typically 2 bytes
  3. int: Typically 4 bytes
  4. long int (or simply long): Typically 4 or 8 bytes
  5. long long int (or simply long long): Typically 8 bytes
  6. float: Typically 4 bytes
  7. double: Typically 8 bytes
  8. long double: Typically 10 or 12 bytes

Remember that these sizes are based on common implementations and may vary on different systems. To determine the size of a data type on a particular system, you can use the sizeof operator in C. For example:

This will print the size of the int data type in bytes on the current system.

Conclusion

  • Understanding data types in C is fundamental for effective programming.
  • C data types are essential for storing various kinds of data, such as integers, characters, decimals, and more.
  • Different data types in C classifications include integer, character, float, double, and void data types.
  • Common C datatypes have specific sizes, which are crucial for memory management and portability across different systems.