Only Change is Constant

22.04.2016 |

Episode #3 of the course Getting started with C programming by Sudeshna Das

 

Hey there! It’s good to see you again.

Today, we’ll be tackling variables and constants, which will form a very important part of every program that you write.

A constant, as the name suggests, is a value that does not change. Remember the program we worked on yesterday?

Here you go:

 

#include<stdio.h>
void main()
{
printf(“%i and %i and %i and %i”, 4+2, 4-2, 4*2, 4/2);
}

http://codepad.org/QqD7tW66

 

The values 4 and 2 are called constants, simply because they remain the same.

Now what happens if we want to change the value from 4 to 6? You would need to go through the whole program and change each 4 to 6 one by one. But wouldn’t it be nice if we had a lazy way out? Well, we do! Say hello to variables.

So what is a variable? Err, anything that varies from time to time? Well, yes, but for computers, a variable is more like a container. Just like you can store any number of candies in your hidden-from-everyone-else candy jar, you can store any value in a variable. If you can recall elementary algebra, we’re talking about the same thing here.

So let’s change this program to include two variables:  a and b.

 

#include<stdio.h>
void main()
{
int a=4, b=2;
printf(“%i and %i and %i and %i”, a+b, a-b, a*b, a/b);
}

http://codepad.org/ZCs4OKS5

 

Now all we need to do is change the value of a from 4 to 6 in the first line and we are done!

The word int tells your computer that the type of variable we are interested in is an integer. There are other types of variables as well. We will work only with int, float (real numbers with a decimal point), and char (characters) in this course.

Think of the various types of containers you have in your kitchen: a jug for orange juice, a dish for butter, a jar for sugar. Storing orange juice in a butter dish would be pretty inefficient. Likewise, we need to store values in containers that are specially made for holding them. And since computers are pretty dumb unless you tell them what to do, you have to tell your computer what type of container (variable) you want to use.

Let’s see different types of variables in action.

 

#include<stdio.h>
void main()
{
int a=4, b=2;
char x=’z’;
float m=6.0, n=3.0;
printf(“%i \n”,a+b);
printf(“%c \n”,x);
printf(“%f”, m/n);
}

http://codepad.org/lurWJKYq

 

See how we are using %c to print character variables and %f to print float variables? It is also common to use %d to print integers, instead of using %i.

The \n is to specify that we want the next output on a new line. Make sure you’ve got the backslash (\) and not the commonly used front slash (/). The front slash is for division. Also keep in mind how we are using a separate line to declare each kind of variable—a separate line for the integers and a separate line for the floats.

You might be wondering what’s with the a,b,x,m, and n. You can use almost any name for your variable, as long as it is not words like float or int or main. Feel free to get creative with the names.

 

#include<stdio.h>
void main()
{
char first_initial = ‘S’, second_initial = ‘D’;
printf(“My name is %c.%c.”, first_initial, second_initial);
}

http://codepad.org/JZJPdE6N

 

Recommended book

“Programming in C” by Stephen G. Kochan

 

Share with friends