bscodinglab

Data Types in C

In computer programming, a data type is an attribute of a variable that specifies the type of data that the variable can hold. Data types define the size, format, and range of values that can be stored in a variable.

there are two types of data types in C
  1. Primitive Data Type :- Primitive data types are the basic building blocks of data manipulation in Java. They are eight in number :-
    1. bool :- A boolean value can be either true or false.
    2. char :- A char value can store a single character.
    3. Short data type :- Short data type is used to save space in memory, especially when the values being stored are small.
    4. int :- A whole number, such as 125 or -455.
    5. float :- A float value can store a floating-point number with 6 to 7 decimal digits of precision.
    6. double :- A double value can store a floating-point number with 15 to 16 decimal digits of precision.
    7. Long and Long Duble :- These are mostly used with decimal numbers. By using these prefixes, we can increase the range of values represented in float.
    8. void :- A type that does not store any data.
  1. Non-rimitive :- Non-primitive data types are more complex data types that are built on top of the primitive data types. They include:
    1. Arrays :- An array is a collection of elements of the same data type.
    2. String :- String is an array of characters. The difference between a normal character array and a string is that string contains .
    3. Pointer :- A pointer in C is a variable that stores the address of another variable.
    4. Structures :- A structure is a collection of related data items of different data types.
    5. Unions :- A union is a collection of related data items of the same data type.
    6. Functions :- A function is a block of code that performs a specific task.
    7. Enumerations :- An enumeration is a list of named constants.
    8. Typedef :- It is a keyword present in C, which is used to give a new name to any data type.

  In addition to these built-in data types, C also allows programmers to define their own   data types using structures and enumerations.

 Understanding data types is important in programming because it allows the programmer   to write code that is efficient and safe, and to avoid common programming errors such as overflow, underflow, and type mismatch.

Here is a table of the most commonly used data types in C programming language, along with their sizes and range of values:

Data Type Description Format Specifier Size (bytes) Range
char
Character
%c or %s
1
-128 to 127 or 0 to 255
Int
Integer
%d or %i
2 or 4
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
short
Short integer
%hd or %hi
2
-32,768 to 32,767
Long
Long integer
%ld or %li
4 or 8
-2,147,483,648 to 2,147,483,647 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
Single-precision floating-point
%f
4
+/- 3.4E +/- 38 (~7 digits)
double
Double-precision floating-point
%lf or %e
8
+/- 1.7E +/- 308 (~15 digits)
long double
Extended-precision floating-point
%Lf or %Le
10 or 12
+/- 3.4E +/- 4932 (~19 digits)
Bool
Boolean value
%d or %i
1
0 (false) or 1 (true)

Note:- that the size and range of values of each data type may vary depending on the compiler and the platform. Also, C allows programmers to define their own data types using structures and enumerations.