EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Data Types in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Data Types in C

Data Types in C

Introduction to Data Types in C

C is a compact, general-purpose computer programming language that was developed by Dennis Ritchie for Unix operating system at bell laboratories. C is a structured programming language that is machine-independent. C has been used by many organizations for developing operating systems, interpreters, device drivers, also database oracle is written in C and in the modern era, the embedded system designs and IoT development also use C language. C is a compiled language in which the compiler takes responsibility to convert the source code into machine-readable object code. There are various compilers available like – TurboC, Clang, etc.

Types of Data Types in C

  1. Whenever a variable is defined in C, it has to be associated with a certain data type.
  2. This gives an indication about the amount of memory to be allocated to that variable and each variable will hold its own unique memory location, except for some cases where the variables point to same memory location only
  3. C has categorized the data types into:
  • a. Primary data types
  • b. Derived data types

a. The primary data types are also called as primitive data types and they include the following :

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Int
  • Float
  • Char
  • Void

b. The derived data types can be among the following :

  • Array
  • Structure
  • Union
  • Pointer

Lets now Describe all of them with examples

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 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 type Size( in bytes) Range allowed
int or signed int 2 -32,768 to 32767
unsigned int 2 0 to 65535
short int or signed short int 1 -128 to 127
unsigned short int 1 0 to 255
long int or signed long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 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:

output1

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.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)
Float Type Size( in bytes) Range of Float
Float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 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 :

output2

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 Type Size( in bytes) Range of char
char or signed char 1 -128 to 127
unsigned char 1 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:

output3

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 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, 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 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:

output4

6. Structures: If there is a requirement, where you need to represent any physical world structure into coding world then this type could come handy, like 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:

output5

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:

Pointer data type in C

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.

Recommended articles

This is a guide to Data type in C. Here we discuss the basic concept, different types of data with respective examples and code implementation. You can also go through our other suggested articles to learn more –

  1. C Programming Interview Questions
  2. Types of Computer Language
  3. Arrays in C Programming
  4. Patterns in C Programming

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you