Skip to main content

Data Types in C

C is a statically-typed language, which means that the data type of a variable is known at compile time. The language provides a variety of data types to store different types of data. In this section, we will discuss the different data types available in C.

Primary Data Types

The primary data types in C are the basic data types that are built into the language. These data types are:

Integer Types

Integer types are used to store whole numbers. The following are the integer types available in C:

  • int: This is the most commonly used integer type. It is a 32-bit signed integer type that can store values ranging from -2147483648 to 2147483647.
  • short: This is a 16-bit signed integer type that can store values ranging from -32768 to 32767.
  • long: This is a 32-bit signed integer type that can store values ranging from -2147483648 to 2147483647.
  • long long: This is a 64-bit signed integer type that can store values ranging from -9223372036854775808 to 9223372036854775807.

Floating-Point Types

Floating-point types are used to store decimal numbers. The following are the floating-point types available in C:

  • float: This is a 32-bit floating-point type that can store values ranging from 1.17549435e-38 to 3.40282347e+38.
  • double: This is a 64-bit floating-point type that can store values ranging from 2.2250738585072014e-308 to 1.7976931348623157e+308.
  • long double: This is an 80-bit or 128-bit floating-point type that can store values ranging from 3.36210314311209350626e-4932 to 1.18973149535723176502e+4932.

Character Types

Character types are used to store single characters. The following are the character types available in C:

  • char: This is an 8-bit signed integer type that can store values ranging from -128 to 127.
  • unsigned char: This is an 8-bit unsigned integer type that can store values ranging from 0 to 255.
  • signed char: This is an 8-bit signed integer type that can store values ranging from -128 to 127.

Derived Data Types

Derived data types are data types that are derived from the primary data types. The following are the derived data types available in C:

Array Types

Array types are used to store collections of values of the same type. An array is declared by specifying the type of the elements and the number of elements.


int scores[5]; // declares an array of 5 integers

Pointer Types

Pointer types are used to store memory addresses. A pointer is declared by specifying the type of the value that the pointer points to.


int *ptr; // declares a pointer to an integer

Structure Types

Structure types are used to store collections of values of different types. A structure is declared by specifying the type of each member and the name of the structure.


struct Person {
  int age;
  char name[20];
}; // declares a structure with two members

Union Types

Union types are used to store values of different types in the same memory location. A union is declared by specifying the type of each member and the name of the union.


union Data {
  int i;
  float f;
}; // declares a union with two members

Enum Types

Enum types are used to store values that have a specific set of named values. An enum is declared by specifying the name of the enum and the values that it can take.


enum Color {
  RED,
  GREEN,
  BLUE
}; // declares an enum with three values

Void Type

The void type is used to specify that a function does not return a value. It is also used to declare a pointer that can point to any type of value.


void printHello(); // declares a function that does not return a value
void *ptr; // declares a pointer that can point to any type of value

Type Qualifiers

Type qualifiers are used to modify the behavior of a type. The following are the type qualifiers available in C:

Const Qualifier

The const qualifier is used to specify that a variable cannot be modified.


const int x = 5; // declares a constant integer

Volatile Qualifier

The volatile qualifier is used to specify that a variable can be modified by external factors.


volatile int x; // declares a volatile integer

Restrict Qualifier

The restrict qualifier is used to specify that a pointer is the only pointer that points to a particular object.


int *restrict ptr; // declares a restricted pointer

Static Qualifier

The static qualifier is used to specify that a variable is initialized only once.


static int x = 5; // declares a static integer

Auto Qualifier

The auto qualifier is used to specify that a variable is automatically allocated and deallocated.


auto int x; // declares an automatic integer

Register Qualifier

The register qualifier is used to specify that a variable should be stored in a register.


register int x; // declares a register integer

Type Conversions

Type conversions are used to convert a value of one type to another type. The following are the type conversions available in C:

Implicit Conversions

Implicit conversions are conversions that are performed automatically by the compiler.


int x = 5;
double y = x; // implicitly converts x to a double

Explicit Conversions

Explicit conversions are conversions that are performed using a cast operator.


int x = 5;
double y = (double)x; // explicitly converts x to a double

Conclusion

In conclusion, C provides a variety of data types to store different types of data. The primary data types include integer, floating-point, and character types. The derived data types include array, pointer, structure, union, and enum types. Type qualifiers are used to modify the behavior of a type, and type conversions are used to convert a value of one type to another type.

Comments

Popular posts from this blog

How to Fix Accelerometer in Mobile Phone

The accelerometer is a crucial sensor in a mobile phone that measures the device's orientation, movement, and acceleration. If the accelerometer is not working properly, it can cause issues with the phone's screen rotation, gaming, and other features that rely on motion sensing. In this article, we will explore the steps to fix a faulty accelerometer in a mobile phone. Causes of Accelerometer Failure Before we dive into the steps to fix the accelerometer, let's first understand the common causes of accelerometer failure: Physical damage: Dropping the phone or exposing it to physical stress can damage the accelerometer. Water damage: Water exposure can damage the accelerometer and other internal components. Software issues: Software glitches or bugs can cause the accelerometer to malfunction. Hardware failure: The accelerometer can fail due to a manufacturing defect or wear and tear over time. Symptoms of a Faulty Accelerometer If the accelerometer i...

Unlocking Interoperability: The Concept of Cross-Chain Bridges

As the world of blockchain technology continues to evolve, the need for seamless interaction between different blockchain networks has become increasingly important. This is where cross-chain bridges come into play, enabling interoperability between disparate blockchain ecosystems. In this article, we'll delve into the concept of cross-chain bridges, exploring their significance, benefits, and the role they play in fostering a more interconnected blockchain landscape. What are Cross-Chain Bridges? Cross-chain bridges, also known as blockchain bridges or interoperability bridges, are decentralized systems that enable the transfer of assets, data, or information between two or more blockchain networks. These bridges facilitate communication and interaction between different blockchain ecosystems, allowing users to leverage the unique features and benefits of each network. How Do Cross-Chain Bridges Work? The process of using a cross-chain bridge typically involves the follo...

Customizing the Appearance of a Bar Chart in Matplotlib

Matplotlib is a powerful data visualization library in Python that provides a wide range of tools for creating high-quality 2D and 3D plots. One of the most commonly used types of plots in matplotlib is the bar chart. In this article, we will explore how to customize the appearance of a bar chart in matplotlib. Basic Bar Chart Before we dive into customizing the appearance of a bar chart, let's first create a basic bar chart using matplotlib. Here's an example code snippet: import matplotlib.pyplot as plt # Data for the bar chart labels = ['A', 'B', 'C', 'D', 'E'] values = [10, 15, 7, 12, 20] # Create the bar chart plt.bar(labels, values) # Show the plot plt.show() This code will create a simple bar chart with the labels on the x-axis and the values on the y-axis. Customizing the Appearance of the Bar Chart Now that we have a basic bar chart, let's customize its appearance. Here are some ways to do it: Changing the...