Data Types in C https://www.skillvertex.com/blog Fri, 10 May 2024 06:51:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Data Types in C https://www.skillvertex.com/blog 32 32 Data Types in C https://www.skillvertex.com/blog/data-types-in-c/ https://www.skillvertex.com/blog/data-types-in-c/#respond Fri, 10 May 2024 06:51:04 +0000 https://www.skillvertex.com/blog/?p=1842 Read more]]>

Table of Contents

Data Types in C

Data types are like the different kinds of building blocks in C programming. They determine what kind of information a variable can hold, what you can do with it, and how much space it takes up. Whether you’re new to programming or you’ve been doing it for a while, understanding these data types in C is really important. In this explanation, we’ll look at the different types and see when to use each one. So, let’s dive into the world of C data types!

What is the Data type in C

In C, every variable is linked to a specific data type. This data type defines what kind of information the variable can hold, such as integers, characters, floating-point numbers, or doubles. Each data type uses varying amounts of memory and supports specific operations. Think of a data type as a set of data with predefined values and characteristics.

The data types can be classified as:

Types Description
Primitive Data Types
Primitive data types in programming are the simplest and most fundamental types used to represent basic values like integers, floating-point numbers, characters, and so on. They serve as the foundational building blocks for working with data in a program.
User Defined Data TypesUser-defined data types in programming are created and defined by the programmer or user themselves. These data types are not part of the language’s built-in types but are designed and customized by the user to suit specific requirements or to structure data in a particular way that fits the needs of their program.
Derived Types
Derived data types in programming are types that are created by combining or deriving from the primitive or built-in data types. These types are constructed to meet specific programming needs and are often built upon the foundation of existing data types. They can include arrays, pointers, structures, and more, all customized based on the requirements of the program.

Derived data types in programming are types that are created by combining or deriving from the primitive or built-in data types. These types are constructed to meet specific programming needs and are often built upon the foundation of existing data types. They can include arrays, pointers, structures, and more, all customized based on the requirements of the program.

Data Type 
 
Size (bytes) 
 
Range
 
Format Specifier 
 
short int 
 

 
-32,768 to 32,767 
 
%hd 
 
unsigned short int 
 

 
0 to 65,535 
 
%hu 
 
unsigned int 
 

 
0 to 4,294,967,295 
 
%u 
 
int 
 

 
-2,147,483,648 to 2,147,483,647 
 
%d 
 
long int 
 

 
-2,147,483,648 to 2,147,483,647 
 
%ld 
 
unsigned long int 
 

 
0 to 4,294,967,295 
 
%lu 
 
long long int 
 

 
-(2^63) to (2^63)-1 
 
%lld 
 
unsigned long long int 
 

 
0 to 18,446,744,073,709,551,615 
 
%llu 
 
signed char 
 

 
-128 to 127 
 
%c 
 
unsigned char 
 

 
0 to 255 
 
%c 
 
float 
 

 
1.2E-38 to 3.4E+38%f 
 
double 
 

 
1.7E-308 to 1.7E+308%lf 
 
long double 
 
16 
 
3.4E-4932 to 1.1E+4932%Lf 
 

Integer Data Type

In C, the integer data type is employed to store whole numbers without decimal points. It can accommodate octal values, hexadecimal values, and standard decimal values.

Key characteristics of the int data type in C:

  • Range: It can hold values ranging from -2,147,483,648 to 2,147,483,647.
  • Size: Typically, it occupies 4 bytes in memory.
  • Format Specifier: When displaying or reading int values, you use the format specifier %d.

Syntax of Integer

We use int keyword to declare the integer variable:

int var_name;

The integer data type can also be used as

Here are brief explanations of different integer data types in C:

  1. unsigned int: The unsigned int data type in C is used to store values ranging from zero to positive numbers. It cannot store negative values like the signed int.
  2. short int: The short int data type is smaller in size than the regular int, typically using 2 bytes. This means it can store values from -32,768 to 32,767.
  3. long int: The long int data type is a larger version of the regular int and can store values greater than what an int can hold.
  4. unsigned short int: Similar to the relationship between unsigned int and int, unsigned short int can only store non-negative values and is smaller in size than short int.

Example of int

// C program to print Integer data types.
#include <stdio.h>
 
int main()
{
    // Integer value with positive data.
    int a = 9;
 
    // integer value with negative data.
    int b = -9;
 
    // U or u is Used for Unsigned int in C.
    int c = 89U;
 
    // L or l is used for long int in C.
    long int d = 99998L;
 
    printf("Integer value with positive data: %d\n", a);
    printf("Integer value with negative data: %d\n", b);
    printf("Integer value with an unsigned int data: %u\n",
           c);
    printf("Integer value with an long int data: %ld", d);
 
    return 0;
}

Output

Integer value with positive data: 9
Integer value with negative data: -9
Integer value with an unsigned int data: 89
Integer value with an long int data: 99998

Character datatype

In C, the character data type is designed to store a single character. It occupies 1 byte of memory and is the most fundamental data type in the language.

Here are some key characteristics of the char data type in C:

  • Range: It can represent values from (-128 to 127) or (0 to 255), depending on whether it’s signed or unsigned.
  • Size: It typically uses 1 byte of memory in most compilers.
  • Format Specifier: When working with char values in input/output operations, you use the format specifier %c.

Syntax of char

The char keyword is used to declare the variable of character type:

char var_name;

Examples of char

// C program to print Integer data types.
#include <stdio.h>
 
int main()
{
    char a = 'a';
    char c;
 
    printf("Value of a: %c\n", a);
 
    a++;
    printf("Value of a after increment is: %c\n", a);
 
    // c is assigned ASCII values
    // which corresponds to the
    // character 'c'
    // a-->97 b-->98 c-->99
    // here c will be printed
    c = 99;
 
    printf("Value of c: %c", c);
 
    return 0;
}

Output

Value of a: a
Value of a after increment is: b
Value of c: c

Float Data Type

In C programming, the float data type is employed to store floating-point values, which include both decimal and exponential numbers. It is used for representing decimal numbers with single-precision accuracy.

Here are some key characteristics of the float data type in C:

  • Range: It can store values within the range of 1.2E-38 to 3.4E+38.
  • Size: Typically, it uses 4 bytes of memory.
  • Format Specifier: When displaying float values in input/output operations, you use the format specifier %f.

Syntax of float

The float keyword is used to declare the variable as a floating point:

float var_name;

Example of Float

// C Program to demonstrate use
// of Floating types
#include <stdio.h>
 
int main()
{
    float a = 9.0f;
    float b = 2.5f;
 
    // 2x10^-4
    float c = 2E-4f;
    printf("%f\n", a);
    printf("%f\n", b);
    printf("%f", c);
 
    return 0;
}

Output

9.000000
2.500000
0.000200

Double Data Type

In C programming, the double data type is used to store decimal numbers with double precision. It allows you to define numeric values that hold decimal numbers with high precision.

Here are some key characteristics of the double data type in C:

  • Range: It can store values within the extensive range of 1.7E-308 to 1.7E+308.
  • Size: Typically, it occupies 8 bytes of memory, which is double the size of the float data type.
  • Format Specifier: When displaying double values in input/output operations, you use the format specifier %lf.

Syntax Of Double

The variable can be declared as double precision floating point using the double keyword:

double var_name;

Example of Double

// C Program to demonstrate
// use of double data type
#include <stdio.h>
 
int main()
{
    double a = 123123123.00;
    double b = 12.293123;
    double c = 2312312312.123123;
 
    printf("%lf\n", a);
 
    printf("%lf\n", b);
 
    printf("%lf", c);
 
    return 0;
}

Output

123123123.000000
12.293123
2312312312.123123

Void Data Type

In C, “void” means nothing specific. It’s used when a function doesn’t return anything, doesn’t take special input, or when we want a pointer to work with different types of data. It’s a way of saying “nothing specific here.”

Syntax:

// function return type void

void exit(int check);

// Function without any parameter can accept void.

int print(void);

// memory allocation function which
// returns a pointer to void.
void *malloc (size_t size);

Examples of void

// C program to demonstrate
// use of void pointers
#include <stdio.h>
 
int main()
{
    int val = 30;
    void* ptr = &val;
    printf("%d", *(int*)ptr);
    return 0;
}

Output

30

Size of Data Types in C

In C, the size of data types depends on the architecture of the system, so there’s no universal size for them. To find out the size of data types, you can use the sizeof() operator.

Example

// C Program to print size of
// different data type in C
#include <stdio.h>
 
int main()
{
    int size_of_int = sizeof(int);
    int size_of_char = sizeof(char);
    int size_of_float = sizeof(float);
    int size_of_double = sizeof(double);
 
    printf("The size of int data type : %d\n", size_of_int);
    printf("The size of char data type : %d\n",
           size_of_char);
    printf("The size of float data type : %d\n",
           size_of_float);
    printf("The size of double data type : %d",
           size_of_double);
 
    return 0;
}

Output

The size of int data type : 4
The size of char data type : 1
The size of float data type : 4
The size of double data type : 8

FAQ- Data Types in C

Q1. What is data type and its types?

Ans. In software programming, a data type specifies the kind of value that a variable can hold and determines which mathematical, relational, or logical operations can be performed on that value without resulting in an error.

Q2. What is void data type in C?

Ans. The void data type in programming always signifies an empty set of values. You can only use the void type specifier when declaring a pointer. You cannot create a variable with the void type, but you can explicitly convert any expression into the void type.

Q3. How many data types are?

Ans. In modern computer programming languages, there are typically five fundamental categories of data types: Integral, Floating Point, Character, Character String, and Composite types. Each of these categories may include specific subtypes that serve various purposes within their respective broad category.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/data-types-in-c/feed/ 0