(C Programming Tutorials)
C Data Types
In this tutorial, you will learn about basic data types such as int, float, char etc. in C programming.
In C programming, data types are declarations for variables. This determines the type and size of data associated with variables. For example,
int myVar;
Here, myVar is a variable of int
(integer) type. The size of int
is 4 bytes.
Basic types
Here’s a table containing commonly used types in C programming for quick access.
Type | Size (bytes) | Format Specifier |
---|---|---|
int |
at least 2, usually 4 | %d , %i |
char |
1 | %c |
float |
4 | %f |
double |
8 | %lf |
short int |
2 usually | %hd |
unsigned int |
at least 2, usually 4 | %u |
long int |
at least 4, usually 8 | %ld , %li |
long long int |
at least 8 | %lld , %lli |
unsigned long int |
at least 4 | %lu |
unsigned long long int |
at least 8 | %llu |
signed char |
1 | %c |
unsigned char |
1 | %c |
long double |
at least 10, usually 12 or 16 | %Lf |
int
Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0
, -5
, 10
We can use int
for declaring an integer variable.
int id;
Here, id is a variable of type integer.
You can declare multiple variables at once in C programming. For example,
int id, age;
The size of int
is usually 4 bytes (32 bits). And, it can take 232
distinct states from -2147483648
to 2147483647
.
float and double
float
and double
are used to hold real numbers.
float salary;
double price;
In C, floating-point numbers can also be represented in exponential. For example,
float normalizationFactor = 22.442e2;
What’s the difference between float
and double
?
The size of float
(single precision float data type) is 4 bytes. And the size of double
(double precision float data type) is 8 bytes.
char
Keyword char
is used for declaring character type variables. For example,
char test = 'h';
The size of the character variable is 1 byte.
void
void
is an incomplete type. It means “nothing” or “no type”. You can think of void as absent.
For example, if a function is not returning anything, its return type should be void
.
Note that, you cannot create variables of void
type.
short and long
If you need to use a large number, you can use a type specifier long
. Here’s how:
long a;
long long b;
long double c;
Here variables a and b can store integer values. And, c can store a floating-point number.
If you are sure, only a small integer ([−32,767, +32,767]
range) will be used, you can use short
.
short d;
You can always check the size of a variable using the sizeof()
operator.
#include <stdio.h>
int main(){
short a;
long b;
long long c;
long double d;
printf("size of short = %d bytesn", sizeof(a));
printf("size of long = %d bytesn", sizeof(b));
printf("size of long long = %d bytesn", sizeof(c));
printf("size of long double= %d bytesn", sizeof(d));
return 0;
}
signed and unsigned
In C, signed
and unsigned
are type modifiers. You can alter the data storage of a data type by using them. For example,
unsigned int x;
int y;
Here, the variable x can hold only zero and positive values because we have used the unsigned
modifier.
Considering the size of int
is 4 bytes, variable y can hold values from -231
to 231-1
, whereas variable x can hold values from 0
to 232-1
.
Other data types defined in C programming are:
- bool Type
- Enumerated type
- Complex types
Derived Data Types
Data types that are derived from fundamental data types are derived types. For example: arrays, pointers, function types, structures, etc.
We will learn about these derived data types in later tutorials.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
C Example for Beginners: C Program to Find the Size of int, float, double and char
C++ for Beginners: C++ Program to Find Size of int, float, double and char in Your System
Learn Java by Example: Java Program to convert int type variables to char