Learning To Program - Being A Better Programmer (Episode - 4)
Working with data assigned to variables:
In the previous episode, we learned how to declare variables. But what is the benefit of keeping data in variables in a program? And how to use the data?
Before answering the question, let's run a program. 😀
Well, what programs can be run? Let's make a program where you can add any number you want.
In the very beginning, I said that developing logic is more important than learning the language.
Then we got a problem.
The problem is to find the sum of two numbers.
The problem-solving process can be as follows:
- First we have to take three variables. Since we will add two numbers for two inputs. Another variable is needed for output.
- Given two inputs from the user.
- to add
- output.
Now let's complete the processes through the C programming language.
#include<stdio.h>
int main()
{
int a, b, c;
printf(“\n Enter first value:”);
scanf(“%d”, &a);
printf(“\n Enter second value:”);
scanf(“%d”, &b);
c=a+b;
print(“\n %d+%d is %d”, a,b,c);
}
Now we will analyze this program.
int a, b, c;
Through this line, three variables of type int named a, b, and c are declared in the program
You must put integer numbers in this type of variable.
printf(“\n Enter first value:”);
Through this line Enter the first value: and the text is displayed on the screen.
scanf(“%d”, &a);
Through this line, the user is asked to input an integer number which the user will press through the keyboard button. The number that the user types will be stored in the variable.
&a means the address of a. That is, this statement tells the compiler that the received number should be placed at the address assigned to a.
printf(“\n Enter second value:”);
Through this line, the text Enter second value: is displayed on the screen.
scanf(“%d”, &b);
This line asks the user to input another integer which the user will press through the keyboard button. The number that the user types will be in the b variable.
c=a+b;
Through this line, the data stored in addresses a and b will be added. The sum obtained by addition is assigned to the variable c.
printf(“\n %d+%d is %d”, a,b,c);
Through this line, the data stored in a,b, and c are shown in the output.
Usage of scanf() function:
The addition of two numbers can also be done without taking input with the scanf() function.
Let's run the following program.
#include<stdio.h>
int main()
{
int a, b, c;
a=3;
b=2;
c=a+b;
printfThe addition(“\n %d+%d is %d”, a,b,c);
}
By writing this program we can add two numbers. But the problem is that I can add only two specific numbers here.
If we want to add a number later, we have to change the source program again, which is quite a difficult task.
So to increase the efficiency of the program, we can add any two numbers instead of adding specific two numbers while taking input from the user.
Again, multiple variables cannot be declared with the same name
What do you do if you want to add any three numbers? Four variables or three? Figure it out for yourself. 😛
And what can be done to multiply or subtract any two numbers? I hope it can be done.
Keywords:
Keywords are special words used in programs. Each keyword has a specific meaning and performs a specific function in the program.
For example, auto, for, double, break, int, void, float, etc keywords. You will get ideas about keywords while programming. But one thing must be remembered the keyword name should be written in one word. That is, there cannot be any gap between them. However, if two keywords are used in a program, there will be a gap in between.
Some common mistakes:
Some common mistakes can be made while writing programs in C. For example, when an instruction is given, if a semicolon is not given, the program will give an error.
Again, giving a value of another type for a variable of one type will give wrong results in the program. But in this case, the compiler will not catch any error, but your result will be reversed. Again, multiple variables cannot be declared with the same name.
int Value1=Value1=9;
Even if you write like this, the compiler will give an error message.
In the next sections, we will learn the basics about operators, statements, or loops.
Happy Programming… 😀
No comments