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 Use Logging in Nest.js

Logging is an essential part of any application, as it allows developers to track and debug issues that may arise during runtime. In Nest.js, logging is handled by the built-in `Logger` class, which provides a simple and flexible way to log messages at different levels. In this article, we'll explore how to use logging in Nest.js and provide some best practices for implementing logging in your applications. Enabling Logging in Nest.js By default, Nest.js has logging enabled, and you can start logging messages right away. However, you can customize the logging behavior by passing a `Logger` instance to the `NestFactory.create()` method when creating the Nest.js application. import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule, { logger: true, }); await app.listen(3000); } bootstrap(); Logging Levels Nest.js supports four logging levels:...

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...

Debugging a Nest.js Application: A Comprehensive Guide

Debugging is an essential part of the software development process. It allows developers to identify and fix errors, ensuring that their application works as expected. In this article, we will explore the various methods and tools available for debugging a Nest.js application. Understanding the Debugging Process Debugging involves identifying the source of an error, understanding the root cause, and implementing a fix. The process typically involves the following steps: Reproducing the error: This involves recreating the conditions that led to the error. Identifying the source: This involves using various tools and techniques to pinpoint the location of the error. Understanding the root cause: This involves analyzing the code and identifying the underlying issue that led to the error. Implementing a fix: This involves making changes to the code to resolve the error. Using the Built-in Debugger Nest.js provides a built-in debugger that can be used to step throug...