Header Ads

C programming - data type and variable:C Programming Language - Part 1.3

C programming - data type and variable



In this chapter, more than a practical program has been discussed, with the topic of C programming languages. The next section will be able to understand the code.

Variable:
In the previous program, we showed only one output of one line. That was enough just to start programming. In reality, we have to write some complicated programs. For which our variables are needed.

Variable is a name, which stores data in the computer's memory. This data may be numeric (any number) or a character (a, b, c..z). What kind of data will be kept in this variable, we are the data type.

Data type:
There are many types of data types in C programming. The main four is in the middle

Int data type
Char data type
Float data type
Double data type

Another data type is void, which is called Values ​​data type.

Int data type:

Int data type means integer quantity (uninterrupted numbers such as 1, 2, 3 etc.). Its size is 2 bytes or 16 bits (1 bytes = 8 bits) and ranges from -37676 to +3,2767. Some compilers give 4 bytes of memory for int data. That means the maximum 4-bit data can be kept for an int data type. Whose range is -2,147,483,648 to 2,147,483,647 This range means that if we set an integer data type variable, then the compiler will not give the correct value. Size and range may vary according to the compiler.
As an example of int data type, we can see a program. If we know the length and breadth of a veritable land, we can find out the area. Is not it? I think the length is 5 units and the width is 8 units. We will area of ​​5 * 8 = 40 units. How can we find out in the program? See the program below.

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Int main ()
{
 Int volume;
 Int length = 5;
 Int width = 8;
 Volume = length * width;
 Printf ("% d", volume);
 Return 0;
}
If we run the program we will see the output 40

It's a lot complicated to see from our first program. And a bit bigger. But of course it is a lot easier. Shortly before, we learned about variables. It is used to store or save something in computer memory.
Shortly, we have come to know about a data type named int. By which the computer can be stored on Integer / integer computer. In the above program we used three integer variables. For the first variable length or length, the second is for width or width. The third has taken volume and width to keep volume out and then to volume.


And all of this is written in a line, such as int volume; Which is called variable declaration. That is to declare it before using a variable. At the end of the declaration we gave a semicolon. To complete a variable declaration, use a semicolon to finish it. That is to declare a variable as follows:

Data_type variable_name;
We have to tell the computer that the length and breadth of our land, is not it? For this, we have taken two variables, namely length and width. These two again int volume; From a little different. We have set a value with the declaration of these two variables. To say that value is to be signed. The rules for declaration of variables with values ​​are:

Data_type variable_name = value;
After that we are writing the volume = length * width; This means that the value of the length variable and the value of the width variable are multiplied by volume.

We are a bit familiar with the next line. Printf ("% d", volume); Which is the printf () function.

Printf function:
The function of the printf () function is to print something. In order to print a text, printf () is written in double quotes within it, we print it out, we have already known it.

But now we will print an integer value and from a variable, We have to follow some rules for this. To print an integer, printf () is to be written with double quote% d, it is called placeholder. There are many different placeholders for each type of data type. % D is the display integer. After double quotes, we gave a comma. After this we write our integer variables. Which has printed the area of ​​our land.
It would have been better if the length of the program could be taken after the run of the program. Then we could find out the area of ​​any land. We will learn something in the next section. In this chapter we will know about some more fundamental concepts.

Placeholder / placeholder:

Shortly before, we heard a word called placeholder. One has to use one placeholder to work with one type of data type. Above all, we just learned about integer data type. Find out about other data types in front of you. Below are different types of data types and their placeholders

 Data type placeholder
 Int% d
 Char% c
 Float% f
 Double% lf

Character data type:

Char data type means single character (a character such as a, b, z, a, n etc.) Its size is 1 byte or 8 bits. Bits (1 bytes = 8 bits) and ranges from 128 to +127. Every character on our keyboard is one character. The rules for declaring the character variable are:

Char variable_name;

Character can be saved only by a character / letter / color in the variable. See the program below

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Char ch = 'A';
Printf ("% c", ch);
Return 0;
}
Here we took a curacter variables named ch. Then we print it. In order to sign a curator in the variable, it is to be placed in single quotes. Thus 'A'.

We have to use% c placeholders for printing the character data.


Float data type:

Integer data type can only be saved in full numbers. Assign a decimal value such as 8.9 or Wish to an integer variable. After that print out the variable. What did you see? Not a part of the decimal, is not it? You can run the following program:

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
 Int n = 8.9;
 Printf ("% f", n);
 Return 0;
}

It just shows us 8. Although we have kept the variable in the 8.9. The reason is that int can save only whole numbers in computer memory. To keep the decimal value on the computer, we need another data type, which is named float.

Float data type means floating point number (decimal number such as 10.5, 1.8, 5.6 etc.). Floating point Data type can save a number accurately up to 6 cells after the decimal. Please change the data type of the variable n to run the float with the above program

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
 Float n = 8.9;
 Printf ("% d", n);
 Return 0;
}

This will give the right value now. While printing, placeholder% type of our floating point data type will be used.

Let's see another example. We know the area of ​​the circle needs the radius value. The radius of the circle is the same as the radius of a circle, the circle radius 7.6 units. We write a program to find out its area

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main ()
{
 Float radius = 7.6;
 Float area = (radius * radius * 3.1416);
 Printf ("% f", area);
 Return 0;
}
We have taken a floating point variable named radius. In it, the radius of the circle is kept. We've also taken another variable named area, in which we have outlined the field.

The area of ​​the circle is the radius * radius * the value of the pie. So I've written and printed the value.

Earlier in the example of integer used as a placeholder in the printf function% d, the placeholder for floating point is% f The rest is not easy?

We know that floating point can give up to 6 cells right after decimal. To get the perfect value for more than one house, we have to use another data type, which is called double.
Double data type

Double data type is called double precision floating point number. It is similar to float data type but size is huge. Its size is 8 bytes or 64 bits. And it can save up to 15 cells accurately after decimal.

We know the ratio of the circle's circumference to the diameter is expressed by Pi [Π]. This is an irrational number. It is not possible to fully publish it in decimal form. There are infinite numbers after the decimal, so for this. If we try to find out the value of the float, we can get up to 6 cells after the decimal. But if we find out its value with double, then the value of 15 to the right after pub decimal We have to use our lf as a placeholder for double quality print or output. See the following program

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Double pi = 3.14159265358979323846;
Printf ("% lf", pi);
Return 0;
}
Now even after compiling it will show 6 cells after decimal. But we're saying that it will give up to 15 rooms. That's right. Now the compiler has to say how many cells will print after the decimal. See the following program

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Double pi = 3.14159265358979323846;
Printf ("% .9lf", pi);
Return 0;
}
Here we have written in the placeholder. 9lf The number of prints that will be printed by the decimal place in the placeholder, it will print up to the room. The above program will print up to 9 cells after the decimal value of the pie.


Does not play naughty intellect on the head? If you place .50lf instead of .9lf, you will print 50 cells after the decimal, so why not? Yes, exactly when writing .50lf, it will print up to 50 cells after the decimal. But we can not find the result that we want. Get something wrong See the following program

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Double pi = 3.14159265358979323846;
Printf ("% .20f", pi);
Return 0;
}
Here we have given up to 20 cells after the decimal value of the pie. Then we want to get value to 20 rooms. So in the placeholder we have given 20f so that we print up to 20 cells after the decimal. But after the decimal, 15 cells are printed exactly. Then fill them with zero. Because the dobule Data type can remember exactly the values ​​up to 15 cells after the decimal type.

Modified data type:

The above four types of data types can be modified by creating many more types of data types. Modifiers are used to modify the modifiers

Signed
Unsigned
Long
Short
Other versions of int data type are:

Signed int
Unsigned int
Long int
Short int
Long long int etc


Data type and their ranges:
At the beginning we read something about ranges, maybe not quite right. But now we will get the details. We said int data type size is 2 bytes. We know that one byte is equal to 8 bits. So the two bytes are equal to 16 bits. And this 16 bits means that the computer can save accurately (216 -1) = (65536 -1) = 65535. That means we can safely save any number up to 0 to 65535 in an integer. Now if we save a value greater than this, such as the 65538 integer data type, the compiler will not be able to output our values ​​exactly. Ultra Plata will give a value.

We have said that some compilers allocate 4 bytes of memory for integers. 4 bytes means 4 * 8 bits. = 32 bit. And 32 bit means we can save the exact value of (232 -1) = (4294967296 -1) = 4294967295 as the integer data type. If you want to save as an integer greater than this, you will not get the right output. That's great. Why are you saying great Before that, we can print any number greater than 4294967295 as an integer variable. Such as 6294967295

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
 Int n = 6294967295;
 Printf ("% d", n);
 Return 0;
}
What is the output? I'm getting 1999999999 ... I did not get it. Because we have tried to save the value of the integer dataType from the greater value, so

4294967295 Next Number 4294967296, print it and see

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
 Int n = 4294967296;
 Printf ("% d", n);
 Return 0;
}
Output is empty. 0.
4294967297 Print and see 1.


1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Int n = 4294967297;
Printf ("% d", n);
Return 0;
}
If I print 4294967298, then get 2. What does that mean? After 4294967295 trying to print the number after that, it starts printing again 0. There is a good reason. What is the reason? Think out


So long we've talked about Unsigned Integers. Unsigned means that the number is not whether the number is positive or negative. But sometimes we have to work with Singed Integer. This means that when we say in the variable declaration that the number is positive or negative, then the range will be reduced again. Then there will be an integer range ranging from -2,147,483,648 to +2,147,483,647. That means we can save the integer value up to 2,147,483,648 and 2,147,483,647 in an integer variable.
If the corpus does not print the + sign in the compiler output But if you are a debtor, it will show

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Int n = -899;
Printf ("% d", n);
Return 0;
}
Above is a funniest number we can say the Singed Integer variables have been created. And then it has been printed.


Below are different types of data and their size is given

 Data type size
 Int 2 bytes
 Char 1 byte
 Float 4 bytes
 Double 8 bytes
It can be different from the compiler.


Modified data type and their size and ranges.


Type bit size range
Char 8-127 to 127
Unsigned chart 8 0 to 255
Signed char 8 -27 to 127
Int 16 or 32 -32776 to 32767
Unsigned int 16 or 32 0 to 65,535
Signed int 16 or 32 -32776 to 32,767
Short int 16 -32,767 to 32,767
Unsigned short int 16 to 65,535
Signed short int 16 -32,767 to 32,767
Long int 32 -2,147,483,647 to 2,147,483,647
Long long int 64 - (263 - 1) to (263 - 1)
Signed long int 32 -2,147,483,647 to 2,147,483,647
Unsigned long int 32 0 to 4,294,967,295
Unsigned long long int 64 to 0 (264 - 1)
Float 32 1.2E-38 to 3.4E + 38
Double 64 2.3E-308 to 1.7E + 308
Long double 80 3.4E-4932 to 1.1E + 4932
Constant or constant:

There are four types of constants or constants in C programming. Integer constant, floating-point constant, character constant & string constant


We've already used these. Integer constant is any integer number. Any number such as 12, 5, 102 etc.


In mathematics, we use commas for the convenience of reading a large number, we can not do it in C programming. As we can not write ten thousand in such a way: 10,000


Floating Point Constant is any number of digits. Such as 5.2, 9.666, 0.145 etc.


Character Constant is a Single Character For example 'A', 'X', 'g' etc.


Let us type in the character constants, in single quotes. And there is only one character. If you type one or more characters in double quotes, we call it C programming at string constants. Such as "Hello", "Bangladesh is a land of stories", "5.5698" etc. If you put a number in double quotes, it does not have a number, it becomes a string.

 Image result for C programming - data type and variable

Escape Sequence:

We want to print a paragraph. Paragraphs contain a new line of sentences sometimes in a few sentences. Previously, we printed a text in printf, printed it. What to do if you want to print your name, father's name? How do you look like if both are on the same line? Write a program that will print your name and your father's name. If you could give a new line in the name of two people, would not it be great?

We have to use \ nto provide new lines or life feed in C programming. With backslash and n one. Here's a change in the function of n because of using a backslash before n. There are many different sequence sequences. They are called Escape Sequence Below is a program written in New Line

1
2
3
4
5
6
7
8
#include <stdio.h>
Main ()
{
Printf ("The creator of C programming language is Danich Ritchie. \ N Danich Ritchie also co developer of Unix");
 Return 0;
}
\ N Like there are many more escaped sequences. Below is a list of them:


Skype sequence work.
\ A Alert (Beep, Bell) (added in C89)
\ B Backspace
\ F Formfeed
\ N New Line (Line Feed); See notes below
\ R Carriage Return
\ T Horizontal Tab
\ V Vertical Tab
\\ Backslash
\ 'Single quotation mark
\ "Double quotation mark


For example, \ a is used to give the word alert. You can run the following program. It does not output anything. But the computer will sound an alert or sound beep.

1
2
3
4
5
6
7
8
#include <stdio.h>
Main ()
{

 Printf ("\ a");
 Return 0;

No comments

Theme images by sebastian-julian. Powered by Blogger.