C

Summary Of Data Types In C – The Most Detail

Data types define the semantics and storage characteristics of data elements in the C programming language. They are expressed as declarations for memory locations or variables in the language syntax. The types of operations or methods of processing data elements are also determined by data types.
The C language supports basic arithmetic types such as integers and real numbers, as well as syntax for creating arrays and compound types. Headers for the C standard library, which are to be used via include directives, contain definitions of support types, which have additional properties such as providing storage with an exact size regardless of the language implementation on specific hardware platforms. The following article will summarize the data types in C.

4 Data types that everyone must know

Integer Types

In C, the integer data type is used to store whole numbers without decimal values. In C, octal, hexadecimal, and decimal values can be stored in the int data type. The sizeof operator in C can be used to determine the size of the int data type. Unsigned int data type in C is used to store data values ranging from zero to positive numbers, but it cannot store negative values like signed int. Unsigned int is larger than signed int and uses “%u” as a format specifier in the C programming language. The C implementation of the int data type is shown below.

  • Range: -2,147,483,648 to 2,147,483,647
  • Size: 2 bytes or 4 bytes
  • Format Specifier:%d

Note: The size of an integer data type is compiler-dependent; when processors are 16-bit systems, the output of int is 2 bytes. When using 32-bit processors, it displays both 2 and 4 bytes.

// 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 Traits

A character data type variable can only store one character. The character has a storage capacity of one. It is the most fundamental data type in C. In almost all compilers, it stores a single character and requires a single byte of memory.
  • (-128 to 127) range or (0 to 255)
  • 1 byte in size
  • %c Format Specifier
// 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

Types 2 and 3

In C, a Double data type is used to store decimal numbers with double precision (numbers with floating point values). It is used to define numeric values in C that contain numbers with decimal values. The double data type is essentially a precision data type that can hold 64 bits of decimal numbers or floating points. Because a double has more precision than a float, it is obvious that it takes up twice as much memory as the floating-point type. It can easily fit 16 to 17 digits after or before a decimal point.

  • 1.7E-308 to 1.7E+308 range
  • 8 bytes in size
  • %lf Format Specifier
// 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

Boolean type

C99 added the boolean (true/false) type _Bool. Furthermore, the stdbool.h> header defines bool as a convenient alias for this type, as well as true and false macros. With one exception, any assignments to a _Bool that are not 0 (false) are stored as 1. (true). This behavior is present to prevent integer overflows during implicit narrowing conversions. For instance, consider the following code:

unsigned char b = 256;

if (b) {
	/* do something */
}

If an unsigned char has a size of 8 bits, variable b evaluates to false. Because the value 256 does not fit in the data type, the lower 8 bits of it are used, resulting in a zero value. Changing the type, however, causes the previous code to behave normally:

_Bool b = 256;

if (b) {
	/* do something */
}

The type _Bool also ensures that true values always compare equally:

_Bool a = 1, b = 2;

if (a == b) {
	/* this code will run */
}

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button