Imperative Programming Language: Human
Imperative Programming Language: Human
language
Machine
language
C program
Lets analyze a basic C program. It will take two numbers from
user and add them. Then it will print the result.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
#include <stdio.h>
#include <conio.h>
int main()
{
int a;
int b;
int c;
printf(Enter the value for a: );
scanf(%d, &a);
12.
13.
14.
15.
16.
17.
18. }
int
float
char
void
double
during the program run. a can be in one case 5, in the other case
8. It all depends on user input. It is not fixed so it is a vary-able :D.
When we say int a; we are declaring that we will be using a
variable called a. So it is called variable declaration. If we say int
a=5; then we are giving an initial value to a. So this is called
variable initialization. This can also be done by declaring a in one
line and initializing it in the other, like int a; a = 5;
Variables are actually empty boxes whom we have given names.
They refer to a memory location and so a does not have an
existence of its own. Its just a name we have given to a memory
location in the computer. We may give it another name if we dont
like the first one. But yes, we cannot give same name to two
boxes. It would be confusing which one is referred to.
Just like we can store anything in a box if it fits in it, we can store
any value in a variable if it fits the data type. Thus, data type
describes the capacity of our box.
Another interesting thing. We know that char is smaller than int.
So if we can place a big object in a box, I can obviously place the
smaller one as well. Thus we can place a char value in an int
variable; an int value in a float variable and so on.
But, what if we try to place the bigger value in smaller variable?
Well, C is like a short-tempered strong man, who would just push
the object in the box even if it breaks. So if we try to put a float in
an int, it removes the decimal part forcefully and places it. This is
called implicit type conversion, as data type is being converted
and implicit as it is done without programmer asking the code to
do so.
We can also tell the compiler to do this explicitly if the need
arises. We can simply write the needed data type in brackets
before the variable in assignment statement, for example int a =
(int)b; where b is a float variable. This process is called type
casting. Type casting will not have a space between casting
operator () and variable name.
Notice line 5. We gave a curly bracket there and even in the end
of the code. These two brackets specify what we call body of a
function. All statements are written between these brackets.
These are also the scope of the variable declared in them. Any
variable declared within { } cannot be used outside its own { }.
We have a getch() statement. getch stands for get character.
Because Windows immediately exists after completing execution,
we may not be able to see the output. So we give a getch() call,
which waits until we press any key on the keyboard. So we get
time to see the output. It is not needed in Linux. Its definition is in
conio.h.
We also have a return 0 statement. It returns a value 0 to the call
of main, i.e., the operating system that the execution was
successful. It will be clear when functions is discussed.
We also have the statement c = a + b; It adds the two numbers a
and b and store their result in c. There should be space between
all variables and operators. Operators will be discussed in next
lecture Im tired now. Just remember, always, the value on right
hand side goes to the variable in left hand side of =.
Now the only remaining statements are the printf and scanf
functions. Always remember, anything with () is a function. There
is no space between printf and (). printf is short for printing
function. It is used to print an output or a message to the screen.
Strings are written in . Any variable which has to be printed has
to be specified by giving the short form of its type to identify the
type of value to print. They are %d for int, %f for float, %c for
char.
scanf stands for scanning function. It is used to scan the values
inputted by the user on the console. It only specifies the type of
value to be taken in the string by %d, %f etc. Then it gives a
reference to that variable. Explaining references will take time, so
in next lecture. Remember always, you cannot give a statement
to print in scanf. Never ever do that.
So, it is wrong to write scanf(enter number:%d, &a);