D A R K - C O D E R

Loading

C Programming – Data Types

Data can be in any type it can be visual, textual, sensationally it can be anything. Everything belongs to a certain block of data. This division of data into a specific group knows as Grouped data

Note:  “Datatype defines the type of data”

In programming, there are several types of groups; in simple terms, we can say that these are  data types.  Echo datatype or group defines which type of data to store or use, so the compiler can perform operations accordingly. Datatype determines how much space a variable occupies.

In C programming language, five data types can classify into the following :

  1. Basic Types 
    • character
    • integer
    • floating-point
    • double.
  2. Enumerated Types
  3. The Type void
  4. Bool Type
  5. Derived Types
    1. Array
    2. structure
    3. union
    4. pointer
    5. function

Basic DataType:

The Integer Types, Floating-points, and String Types(Char) are All in Basic Datatype.

Examples:

  • Integer as Int
  • character as char
  • floating-point as float
  • double as double.

Enumerated DataType:

Enumerated are Special kinds of arithmetic types that define the specific type of integers to value variables. C reserved keyword 

enums use to define the variable with

Example:

enums Website { 
    userlogin = True ,
    Status = 1
}; 

The Void Datatype:

The Void Datatype provides a specific function in c programming languages. And Void specifies that it has no value.

It is mostly used with functions that return no value.

Derived DataTypes:

All Data-Structural Type grouped in Derived Datatype.

and Those are 

  • Pointer types as a pointer.
  • Array types as an array.
  • Structure types as a struct.
  • Union types as a union.
  • Function types function.

Note:   In C programming, whenever a programmer defines a variable it’s necessary to mention what kind of data type should use, so the compiler can understand and store data according to the data type.

For Example:

  • For numbers like 0-9 Integers will use 
  • For Decimal Numbers like 1.0, 0.99, 9.99  Float will use 
  • For Characters like a-z, A-Z and All special characters Char will use 
  • Same as we use Array, Pointers, Structure, union, bool, enums, and functions

All these datatypes Compile Differently and have separate ways to represent data. Moreover, datatypes help to define what type of data will store and serve in any program.

Each datatype acts differently, acquires some part of memory, and has a range of values. And on behalf of the data type, the compiler allows performing some operations.

Primary Data Types in C Programming

The C programming language has Five basic data types, they are:

Character:

Char is a reserved keyword in C programming language in which Single bit character is stored in memory and occupies one-byte memory. It can store alphabets (A-Z,a-z) and alphanumeric (0-9) keywords. Also, special characters can store as a character. Every character considers a string.

Example

char d = 'd'; 
char a = 'a'; 
char r = 'r';
char k = 'k'; 
char dash = '-'; 
char c = 'c'; 
char o = 'o';
char d = 'd';
  
char e = 'e'; 
char r = 'r'; 
/** 
* The following code causes an error because the character serves as a string
* and it does not serve as an integer and float or double
* Below all the
**/
char num = 1;
char special = -;
char dark = 0.1;

Special Note: We must enclose our data in single quotes in the char datatype.

An integer is used to store numeric values in any variable. Int is a reserved keyword that represents Integer value, And all non-fractional numbers consist of positive, negative, and zero values.

Integer numbers range from -2,147,483,648 to 2,147,483,647 and acquire 2-4 bytes in memory depending on the operating system or 86x it 64x.

For example,

intarea = 51; 
int mi6 = -90;
int zeros = 0; 
/* 
* Invalid Integer Type
*/
int d = -0.5; 
// because value is in decimal type
int p = "a" 
// cause error because its datatype is integer and its value is in string type

Special Note: All arithmetic operators can perform on this datatype, like subtraction, addition, division, multiplication, bitwise, and modulus operations, and their values are in integers.

Floating-point: 

The floating point represents All the numerical decimal point values are knows as Floating-points numbers. In C programming,  Float is a reserved keyword that use to store decimal(floating-point) values in the variable. Float occupies 4 bytes in memory and its range of numbers is from 3.4E-38 to 3.4E+38.

For example,

float d = 0.5; 
float a = 10.1005;
float r = -0.1005;
float k = 110;  
/*
it can store non-decimal values as well 
like k = 110
but due to Type-Casting, it automatically converts numeric to decimals like k = 110.0000
*/
// some common mistakes made 
float c = "10"; 
// this is not valid because of double quotes it's value becomes a string. that's not valid to add a string into float datatype.
float o = !; 
// Also, special characters are not allowed because it's not numeric values.

Special Note: All arithmetic operators can perform on this datatype, like subtraction, addition, division, multiplication, bitwise, and modulus operations, and the values are in a floating point.

Double: 

It is an advanced type of float, Which is used to store decimal numbers values, but the difference between float and double is their size. The float store 4-byte data in memory but the 

Double use 8 bytes, and its range from 1.7E-308 to 1.7E+308

For example,

 double a = 10.09;
 double b = -67.9;

Special Note: All arithmetic operators can perform on this datatype, like subtraction, addition, division, multiplication, bitwise, and modulus operations, and the values are in a floating point. It also returns more precise results than float. Also, its results are more accurate than float.

Void plays a vital role in c programming. Void means no value or empty. This datatype uses to define functions that have no return value. It occupies 0 bytes of memory. 

We use the void keyword for the void data type.

Example

void function() {
   /** www.dark-coder.com **/
}

Bool: 

The _Bool data type is used to represent Boolean values in C, which can be either true or false. The _Bool data type is a newer addition to the C language and is defined in the stdbool.h header file. It is typically used in conjunction with the bool keyword, which is an alias for _Bool.

Here is an example of using the _Bool data type in C:


#include 
#include 

int main(void) {
  bool flag = false;
  if (flag) {
    printf("flag is true\n");
  } else {
    printf("flag is false\n");
  }
  return 0;
}

In this example, the flag variable is defined as a bool and is initialized to false. The if statement checks the value of the flag variable and prints a message to the screen based on its value.