Data Types in C Programming

data types in c programming

In everyday life, we need to store some data. Sometimes we store the data in the written form and sometimes in our own memory.

Let, we have to store data in written form, for this purpose we need a marker and a paper to write. If one has these two things (marker, paper) he/she can store data in written form.

The same concept is in the programming.

In all programming languages, we need to store some data in our program. Sometimes we get the data from the user while the program is running and store in some variable and sometimes we hardcode the data in our program.

Once the data is stored in some variable, we can change it later as needed.

So the conclusion is we can store data in our program.

Now to store the data, we need some variables of the particular data type. For example, if we need to store the whole number, the variable is of different data type and if we want to store floating point value the variable will be of a different data type.

In C programming language, there are many data types. But usually we don't use all, we only use 3 to 4 data types which are int, float, char, double.

Int:

To store the whole number or integer value we use data type int. Its format specifier is %d. If we use %d in print statement its means we want to print integer value. Just like
int x = 9;
printf("Value of x: %d",x);

Output:
Value of x: 9

Char:

It is used to store a single character in c programming. Its format specifier is %c. If we use %c in print statement its means we want to print a single character. Just like
char myChar = 'C';
printf("Value of myChar: %c",myChar);

Output:
Value of myChar: C

Float:

If we want to store floating point value with single precision then we use float. Its format specifier is %f. We can use it just like
float y = 3.9;
printf("Value of y: %f",y);

Output:
Value of y: 3.9

Double:

Whenever we have to store floating point with double precision then we use double. Its format specifier is %lf. We can use it just like
double z = 3.99;
printf("Value of z: %lf",z);

Output:
Value of z: 3.99

Getting data from the user:

I have said that we can get data from the user while our program is running. To do this we use scanf statement.

If I want to get an integer value from the user then, I will declare a variable of int and get an integer value from the user in i. Just like
int x;
printf("Enter a number in x: ");
scanf("%d",&x);
printf("Value of x: %d",x);

Output:
Enter a number in x: 21
Value of x: 21

Scanf Statement:
It is used to get values from the user. I have used %d in scanf statement, it means I want to get an integer value as this is the format specifier of int and I have use &x it means, I want to store user's value in variable x. & operator is known as address operator.

When I write &x its means the value will store at the address of x, which means in variable x.

We can get any value of any data type from the user by just changing the format specifier in scanf statement and variable type in which we want to store data. Because we can't store a character in the integer variable.

More Data Types in C Programming:


Data Type Memory Format Specifier
short int 2 %hd
unsigned short int 2 %hu
unsigned int 4 %u
int 4 %d
long int 4 %ld
unsigned long int 4 %lu
long long int 8 %lld
unsigned long long int 8 %llu
signed char 1 %c
unsigned char 1 %c
float 4 %f
double 8 %lf
long double 12 %Lf

Comments

Popular posts from this blog

c/c++ Programming questions || Practice Problems for Beginners || C Programs List

Change your Application background in Scene Builder || JavaFX FXML Tutorial

Implementation of Graph in C || Directed and Undirected Graph