Lesson 4: Data Type

Every programming language has some basic data types. The C Programming Language has 5 basic data types. They are: character, integer number, floating-point number, double-precision floating-point number and valueless (no type) type.

In C, we work with some variables, these variables maybe a character or a number or a floating-point number. So we need to declare what kind of variables we are using. The way of declaring a variable is:

type variable-name;

What will we write on the place of type? We will write some keywords regarding the data types.

Data Type
Keyword
Character
char
Integer Number
int
Floating-point number
float
Double-precision Floating-point number
double
Valueless ( No-type)
void

Let’s consider a variable name x and its data type is integer. So the declaration will be:

int x;

Whenever we declare a variable it takes some memory from RAM. See the table below to see which data type takes how many memories.

Data Type
Size
int
16 or 32 bit
char
8 bit
float
32 bit
double
64 bit
void
0 bit

Assigning Values
We have declared a integer type variable x. Now we need to assign a value into that variable. To assign something into a variable we use an operator called assignment operator. = (single equal sign) is called the assignment operator.

x = 15;

The above line means we have assigned an integer 15 to the variable x. Now we will print the value of x on the screen so look at the following example:

#include <stdio.h>

int main()
{
    int x;
    x = 15;
    printf("The value of x is: %d",x);
    return 0;
}

Now compile and run the program. What is the output? The output should be:

The value of x is: 15

Here you may want to ask why I am using %d. Here %d is a format specifier. When we want to print some values of variables we need to put format specifiers instead of the variable name. After the end of the double quotation we will put a comma and then write down the name of the variable. We used %d for integer type data. What about others? See the table below:

Data Type
Format Specifier
int
%d
char
%c
float
%f
double
%lf

Now we want to some arithmetic operations with this C Programming Language. We want to do a summation program.  See the program:

#include <stdio.h>

int main()
{
    int x, y, sum;
    x = 15;
    y = 30;
    sum = x + y;
    printf("The sum is: %d",sum);
    return 0;
}

Compile and run the program. Is the output correct?

It must be correct.  What we did actually first declared three integer type variables x, y and sum. We did it in one line because we can declare same type of variables at a time. Then we assigned values to x and y. And we assigned (when we assign we actually store) the summation of x and y into sum. Finally we printed the output.
What should we do if we want to print more than one variables at a time? Well to clear this to you I am writing the above program this way:

#include <stdio.h>

int main()
{
    int x, y, sum;
    x = 15;
    y = 30;
    sum = x + y;
    printf("The sum of %d and %d is: %d", x, y, sum);
    return 0;
}

I think you have understood what happened. If not then ask me in the comments.
We can assign the values of the variables at the time of declaration. So, we can rewrite the above program like this:

#include <stdio.h>

int main()
{
    int x = 15, y = 30, sum;
    sum = x + y;
    printf("The sum of %d and %d is: %d", x, y, sum);
    return 0;
}

Now write a program that can subtract, multiply or divide two or more variables.
In this lesson I just discussed about int type data. In the next lesson you will learn more about char, float and double type data.

0 comments :

Post a Comment

Spam comments will be deleted. :)

 
Loading...
TOP