Data Types In C

Data Types In C
Techiio-author
Written by Shuvhojit DebJanuary 29, 2022
11 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, we'll know what kind of data types are in C.

Introduction

Applications require different types of data to store information. For example, the name is an array of characters, but age is better stored as an integer. We can perform many operations (sum, average, concatenation, matching, etc.) if the data is stored in the correct format and with the correct types. That is why we have so many data types in C so that we can differentiate and segregate data in the best possible way.

Types

  • Whenever a variable is defined in C, it has to be associated with a certain data type.
  • This indicates the amount of memory to be allocated to that variable, and each variable will hold its unique memory location, except for some cases where the variables point to the same memory location only
  • C has categorized the data types into:

A) The primary data types are also called primitive data types, and they include the following :

  1. Int
  2. Float
  3. Char
  4. Void

B) The derived data types can be among the following :

  1. Array
  2. Structure
  3. Union
  4. Pointer

Let's discuss the examples of the data types:

1. The integer data type (int):

If you have to store the whole numbers, then int can be used as a data type, it can have a range of numbers based upon the size you choose in memory, and it can have either all positive or from negative to positive range of numbers based upon user choice of code design.

int or signed int ( size - 2 bytes | | range allowed -32,768 to 32767 )

unsigned int ( size - 2 | | range allowed 0 to 65535 )

short int or signed short int ( size - 1 | | ranged allowed -128 to 127 )

unsigned short int (size - 1 | | ranged allowed - 0 to 255 )

long int or signed long int ( size - 4 | | ranged allowed -2,147,483,648 to 2,147,483,647 )

unsigned long int ( size - 4 | | range allowed - 0 to 4,294,967,295 )

For example

Code:

#include <stdio.h>
void main()
{
int a = 1;
printf(" %d is the integer value ",a);
unsigned short int x = -3278989;
printf(" %hu is the integer value ",x);
}

Output:

blogpost

2. Float data type:

Any real number can be stored in the float data type, and here also we can specify the range; based on data type and size selection, a range of numbers is allowed.

Float (size - 4 | | ranged allowed - 3.4E-38 to 3.4E+38 )

double (size - 8 | | ranged allowed - 1.7E-308 to 1.7E+308 )

long double (size - 10 | | ranged allowed - 3.4E-4932 to 1.1E+4932 )

For example

Code:

#include <stdio.h>
#include <limits.h>
#include <float.h>
void main() {
printf("max float value allowed in positive range   :   %g\n", (float) FLT_MAX);
printf("max float value allowed in negative range   :   %g\n", (float) -FLT_MAX);
printf("max double value possible in positive range :   %g\n", (double) DBL_MAX);
printf("max double value possible in negative range :  %g\n", (double) -DBL_MAX);
}

Output :

blogpost

3. Char type:

This represents the character data type, and it can be either signed or unsigned with a constant size of 1 byte for both cases.

char or signed char ( size - 1 | | ranged allowed -128 to 127 )

unsigned char( size - 1 | | ranged allowed - 0 to 255 )

For example

Code:

#include <stdio.h>
void main() {
char c ='a';
char f = 65; // represents ASCII char value, refer to ASCII table
printf("%c %c ", c, f);
}

Output:

blogpost

4. Void type:

If you don’t want to assign any type to a function (i.e. it won’t return anything like you saw the main function prefixed with void type in the above snippets), then you can mark it as void type.

The above snippets can be referred to as examples for the same.

5. Arrays:

When any homogenous set of data has to be stored in contiguous memory locations, then this data type is chosen, the use case is that there may be times when your code would return more than one result, and that has to be returned from functions cumulatively, like if we have to find a list of all months in a year, then they will be 12, hence we can’t place 12 months discretely in a single variable, so we use arrays for the same.

Let’s see a simple snippet to understand the declaration and use of arrays.

For example

Code:

#include <stdio.h>
void main() {
int i;
char arr[] = {'a', 'b', 'c'};
for(i = 0 ; i < 3 ; i++)
{
printf("%c\n",arr[i]);
}
}

Output:

blogpost

6. Structures:

If there is a requirement where you need to represent any physical world structure into the coding world, then this type could come in handy, like the class of students can be defined as a structure, and student marks and student roll number can be used as variables inside it, an array can be introduced which could hold data related to such structure for many students.

For example

Code:

#include <stdio.h&gt
struct class{
int marks;
int rollNo;};
void main() {
struct class c;
c.marks=10;
c.rollNo=1;
printf("%d\n", c.marks);
printf("%d", c.rollNo);
}

Output:

blogpost

7. Pointer:

This is one of the most important data types as we are not into the OOPs world in C language, languages like java do not use it, but functional programming languages always use it. The concept of pointers is to allocate the memory to some variable and then refer to that memory location for reading and write operations, that memory location can be the address of a function, can be the address of a variable, etc. Pointers get necessary for Array and structure handling in C language and also provides dynamic memory management.

For example

Code:

#include <stdio.h>
void main() {
int a, *p;  // variable and pointer declaration
a = 10;
p = &a;
printf("%d", *p);    // print the value of 'a'
printf("%u", &a);    //print the address of 'a'
printf("%u", p);     // print the address of 'a' in different way
// remember & represents address of variable
}

Output:

blogpost

Conclusion

Hence we saw various data types in C and how they work along with C language to handle coding scenarios. You can do embedded programming also with C, as utilities for the same have been developed too. So C is a versatile language, but with real-world scenarios, coding gets complex and more involved.

C Language
Programming
Operator
Data Type
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance