Data types, input and output

The Brave Warriors of C Program | Part 4

Data types, input and output

In this part, we will write some small programs. All programs must be run on the computer and try to compile and run with minor changes. 

Our first program will be a program to add two numbers. Now talking about, the numbers have to be kept in the computer memory, how to do that complicated task? Don't worry! All programming languages have a thing called a variable which is used to hold a specific value. A name is given to the variable, then variable = a value is entered into the variable. It has nothing to do with mathematical equations. Let's write and run the program, then we can explain.

#include <stdio.h>
int main()
{
int a;
int b;
int sum;
a = 57;
b = 63;
sum = a + b;
printf("Sum is %d", sum);
return 0;
}

Run the program, and you will see on the screen that: Sum is 120.

Here three variables a, b, and sum hold different numbers. First, we need to say that there are three variables: a, b, and sum. And what kind of data will be in them should also be told. int a; We are telling the compiler that there is a variable called a in the program that will be used to hold the value of an integer. This task is called a variable declaration. And int is the data type, which C compiler will understand that it will contain integer type data. There are several other data types, which we will see later. If we wanted, we could write variables of the same type together with commas instead of on separate lines when declaring them, for example:- int a, b, sum;.   And note that a semicolon must be used at the end of the variable declaration.

Then I wrote two statements:

a = 57;
b = 63;

Here the value of a is 5 and the value of b is 63 (assigned), until we change it, the compiler will take the value of a as 57 and the value of b as 63.

The next statement is: sum = a + b;. This means, that the value of the sum will be equal to a + b, that is, the number that will be the sum of a and b, we left (or assigned) the variable named sum.

Now we need to display the sum on the monitor, so we will use the printf function.                printf("Sum is %d", sum);

Here are the two parts inside the printf function. The first part means "Sum is %d" to print to the screen Sum is followed by the value of an integer variable that will take the place of %d. Then we write sum with a comma to indicate that we want to print the value of sum in %d. As we used %d for integers, we need to write something else for other types of variables, which we will learn how to use. It would be great if I wrote a chart right now of what data types are in the C language, what to write them with, and what to do to print them, and you could memorize that chart. But there is no need for memorization alone, memorization tends to reduce thinking power, and thinking ability is very important for programmers. 

We could also write the above program like this:

#include <stdio.h>
int main()
{
int a, b, sum;
a = 57;
b = 63;
sum = a + b;
printf("Sum is %d", sum);
return 0;
}

Again variables can also be assigned while declaring:

#include <stdio.h>
int main()
{
int a = 57, b = 63, sum;
sum = a + b;
printf("Sum is %d", sum);
return 0;
}

Now a question for you. What will be the output of the following program?

#include <stdio.h>
int main()
{
int x, y;
x = 1;
y = x;
x = 2;
printf("%d", y);
return 0;
}

What do you think? Is the output 1 or 2? The output will be 1 because first, the compiler sees, that x is assigned the value 1 (x = 1;).Then the value of x is again assigned to y (y = x;). Now the value of y is 1. Then x is again assigned the value 2. But it will not change the value of y. In programming y = x; In fact, it is not an equation that will always be true. A specific value is placed in a variable with the '=' sign.

Now look at the program below:

#include <stdio.h>
int main()
{
int a = 57, b = 63, sum;
sum = a + b;
printf("%d + %d = %d", a, b, sum);
return 0;
}

What does the program print on the monitor? keep watching printf("%d + %d = %d", a, b, sum); without writing. printf("%d + %d = %d", b, a, sum); Write and run the program again. Now think about it.

While studying, various questions come to our mind on various topics, the answers of which we look for in books, ask the teacher, search on the internet or think and find out the answer by reasoning. It's unfortunate that most kids don't do the latter, because thinking on their own takes time and effort, and they don't want to put in that time and effort. And our parents, teachers, and education system do not reward thinking, but rather memorization.

However, whenever a question about programming comes to mind, immediately write a program. See what your compiler says. Let's say, if we use the decimal number (Real number) in the int type variable, what would happen?

#include <stdio.h>
int main()
{
int a = 50.45, b = 60, sum;
sum = a + b;
printf("%d + %d = %d", a, b, sum);
return 0;
}

Here I used the value of a=50.45. Now run the program, and see what happens. If the question comes to mind again, return 0 in the last line of the main function; What happens if you don't write? then without return 0; Run the program and see what happens.

The output will be: 50 + 60 = 110.

The C compiler assumes the value of a is 50, even though we assigned 50.45. This is called type cast. C has a data type called double for holding real numbers. By casting the type double to int, this is automatic. Again the compiler can be told: int a = (int) 50.45.

int a = 50.99; Here the value of a will be 50. int a = -50.9; The value of a is written as -50. In short, the casting type from double to int will leave out the part after the decimal point.

Another thing. The variable whose type is being cast, but its value is not changed. The type cast value can be placed in a variable. You can understand only by running the following program on the computer.

#include <stdio.h>
int main()
{
int n;
double x;
x = 10.5;
n = (int)x;
printf("Value of n is %d\n", n);
printf("Value of x is %lf\n", x);
return 0;
}

Look at the output of the program. But the value of x has not changed. And you can understand that the double type variable used in C to hold real numbers, %lf (l here is English lower case L) should be used while printing.

Int data type can hold only whole numbers. But is that any integer? Write a program to answer:

#include <stdio.h>
int main()
{
int a;
a = 1000;
printf("Value of a is %d", a);
a = -21000;
printf("Value of a is %d", a);
a = 10000000;
printf("Value of a is %d", a);
a = -10000000;
printf("Value of a is %d", a);
a = 100020004000503;
printf("Value of a is %d", a);
a = -4325987632;
printf("Value of a is %d", a);
return 0;
}

Here we assign different numbers to a. Are all the values coming right? didn't come Before explaining why they didn't come, let me say one thing. So many printf in a row must be a bit uncomfortable to look at on your computer screen. You can write each printf like this: printf("Value of a is %d\n", a);. Now I won't tell you what happens if you write \n inside "" in the printf function. You will understand by running the program.

Not all values ​​of a are displayed correctly, only values ​​from -2147483648 to 2147483647 will print correctly because numbers outside this range cannot be stored in variables of type int. This is the range of numbers of type int. In C, four bytes of memory are used for int type data. Four bytes means thirty-two bits (1 byte = 8 bit). Each bit can hold two things, 0 and 1. Two bits can hold four numbers (00, 01, 10, 11). Then 32 bits can hold 2^32 numbers ie 4294967296 numbers. Now if we keep half positive and half negative, then from -2147483648 to -1 there are a total of 2147483648 numbers and from 0 to 2147483647 there are a total of 2147483648 numbers, a total of 4294967296 numbers. Hope you understand the calculation.

Now we will write an addition program that can add all real numbers. Let me remind you, integers are, ... -3, -2, -1, 0, 1, 2, 3 ... etc. And the real numbers are -5, -3, -2.43, 0, 0.49, 2.92, etc. (all the numbers on the number line are real numbers).

#include <stdio.h>
int main()
{
double a, b, sum;
a = 9.5;
b = 8.743;
sum = a + b;
printf("Sum is: %lf\n", sum);
printf("Sum is: %0.2lf\n", sum);
return 0;
}

Compile and run the program. The output will be the following two lines:

Sum is: 18.243000
Sum is: 18.24

Using %lf printed up to six decimal places on the first line. Again in the second line, up to two cells are printed after the decimal, because I wrote %0.2lf (if I wanted to print up to three cells, I would have written %0.3lf, and if I didn't want to print the decimal part, I would have written %0.0lf). 64 bits are used for double data type and can hold data from 1.7E-308 (1.7 x 10-308) to 1.7E+308 (1.7 x 10308). Understanding the detailed calculations requires some more computer science knowledge, so I won't go into that now.

Now we want to have a system in our program so that we don't write in the code which two numbers to add, we know it as input from the user. To take input from the user (meaning the program using it) we will use the scanf function (there are more functions in C for this task). So without further ado, let's write the program:

#include <stdio.h>
int main()
{
int a, b, sum;
scanf("%d", &a);
scanf("%d", &b);
sum = a + b;
printf("Sum is: %d\n", sum);
return 0;
}

If you run the program, you will see a blank screen. Then you write a number, then write another number with space or enter. Then press enter again to see the sum.

You must have learned to use the scanf function. scanf("%d", &a); Here %d inside the double quotes tells scanf to read the value of an integer or int type variable (user input from the keyboard). And notice that the ampersand (&) sign is used before a, &a means that the input number will be assigned to the a variable. When you learn C a little better, you will understand the true meaning of &a, for now, we will focus on usage. The values ​​of a and b could also be taken with a scanf function like this: scanf("%d %d", &a, &b);. What is the problem if the & sign is not given before the variable? Try to run the following program, and you will get some errors. I'm not explaining the error right now, because the explanation is a bit complicated and if you try to explain it now, you may misunderstand it and insult me ​​later.

#include <stdio.h>
int main()
{
int a, b, sum;
scanf("%d", &a);
scanf("%d", b);
sum = a + b;
printf("Sum is: %d\n", sum);
return 0;
}

Now, what if we wanted to take data of type double instead of taking integer as input? Using %lf instead of %d in scanf would work. Write the program and see if it runs properly. Then start reading the rest.

It won't actually run properly, because the data type has to be changed as well. It means double should be written instead of int. Fix the program and run it again.

Whenever I ask to write a program in the series, no matter how easy or difficult it seems, it has to be written on the computer, compiled, and run. You can't move forward without doing this. Remember, there is no substitute for learning how to drive a car, just like there is no substitute for swimming for learning to swim, there is no substitute for programming for learning programming, you cannot become a programmer by reading books.

This time we will see another type of data type, that is char (character) type. So this type of character requires only one byte of space to store in memory. This type of data is usually used to hold any character or symbol. But that character must be an English alphabet character, other language characters cannot be put in the char type variable. Write the following program on the computer and run it:

#include <stdio.h>
int main()
{
char ch;
printf("Enter the first letter of your name: ");
scanf("%c", &ch);
printf("The first letter of your name is: %c\n", ch);
return 0;
}

You can understand by looking at the code, %c must be used inside printf and scanf functions for char type. There is another function getchar, which can also be used to read data of char type. Check out the program below:

#include <stdio.h>
int main()
{
char ch;
printf("Enter the first letter of your name: ");
ch = getchar();
printf("The first letter of your name is: %c\n", ch);
return 0;
}

Run it too. Will do the same as the previous program. The getchar function reads a character and assigns it to the ch variable. And if you want to put something directly in a char type variable, you will put single quotation marks on both sides of the character or symbol. Ex: char c = 'A';

Now look at the program below:

#include <stdio.h>
int main()
{
int num1, num2;
printf("Please enter a number: ");
scanf("%d", &num1);
printf("Please enter another number: ");
scanf("%d", &num2);
printf("%d + %d = %d\n", num1, num2, num1+num2);
printf("%d - %d = %d\n", num1, num2, num1-num2);
printf("%d * %d = %d\n", num1, num2, num1*num2);
printf("%d / %d = %d\n", num1, num2, num1/num2);
return 0;
}

t Compile and run. By looking at this, you must understand how to do subtraction, multiplication, and division. This time you have four tasks. One, add, subtract, multiply, divide between num1 and num2 instead of inside the printf function, and put the value in another variable. Write a program for this. The second task is to make the program use variables of type double. The third task is to see what happens when num2 is set to 0. The fourth task is to put the +, -, *, / symbols inside the double quotation marks into a char type variable instead of using them directly inside the printf function and then using it. After doing all four tasks correctly, look at the following program:

#include <stdio.h>
int main()
{
int num1, num2, value;
char sign;
printf("Please enter a number: ");
scanf("%d", &num1);
printf("Please enter another number: ");
scanf("%d", &num2);
value = num1 + num2;
sign = '+';
printf("%d %c %d = %d\n", num1, sign, num2, value);
value = num1 - num2;
sign = '-';
printf("%d %c %d = %d\n", num1, sign, num2, value);
value = num1 * num2;
sign = '*';
printf("%d %c %d = %d\n", num1, sign, num2, value);
value = num1 / num2;
sign = '/';
printf("%d %c %d = %d\n", num1, sign, num2, value);
return 0;
}

Just watch the program to understand what works. However, it is not enough to understand only by seeing. Compile and run the code.

There are several other data types in the C language. There are also various methods for input and output, which you will learn slowly (maybe not all in this series, read other C books). You can write many programs with what you have learned so far.

Now let's say something interesting and useful. You can also use your own language inside the program code. This is called commenting. The compiler will not treat comments as part of the program. If it is a single-line comment, you can start the comment with // symbol. And if there are multiple lines start with /* and end with */. But the following program will compile and run fine.

#include <stdio.h>
int main()
{
// test program - comment 1
printf("Hello ");
/* We have printed Hello,
now we shall print World.
Note that this is a multi-line comment */
printf("World"); // printed world
return 0;
}

This time a question, what are the rules for naming the variables? Variable names can be one or more characters, the characters can be a to z, A to Z, 0 to 9, and _ (underscore). But in the case of multiple letters, the first letter cannot be a digit. You program int 7d; Write down what the compiler says. And variable names should be meaningful. For example, the variable to hold the sum should be named sum, although the program runs if it is named y. Meaningful names make it easier to understand what the variable is used for.

Post a Comment

Previous Post Next Post