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
Hello!
I hope you’ve gotten the hang of using the different types of variables now.
Now what happens if we need to store ten integer values? The usual way would be to name all ten variables one by one, like so:
int a, b, c, d, e, f, g, h, i, j;
Reminds me of kindergarten, but doable. Now what if we had to store 100 integer values? Okay, now that's a problem.
Arrays to the rescue!
An array is a group of variables of the same type. So, if we have an integer array of size 10, we could store 10 integers together without having to name each of them separately. We just need to name our array.
Let's take an example:
#include<stdio.h>
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
printf("%d %d %d %d", a[1], a[2], a[4], a[7]);
}
http://codepad.org/pTBSKRCu
This program creates an integer array called a, which is of size 10. So, it has the capacity to store 10 integers. The values that we want to store are given inside curly braces, with commas separating them.
Did you notice something weird about the output? a[1] should ideally be displaying the first value in the array, but it is displaying the second value. Well, that is because in C, the arrays start from the index 0. So the first value—that is, 1—gets stored ina[0] and not a[1].
We can do everything with array elements that we would usually do with variables.
#include<stdio.h>
void main()
{
int a[5] = {1,4,3,2,2};
printf("%d \n", a[0]+a[1]);
printf("%d", a[4]+a[3]);
}
http://codepad.org/x24bmHWg
Here's a simple program to add all the integers in an array and display the result:
#include<stdio.h>
void main()
{
int a[3] = {1001, 2002, 3003};
printf("%d", a[0]+a[1]+a[2]);
}
http://codepad.org/4RMO1yPX
Just like we have used an integer array, we can use float arrays and character arrays too.
#include<stdio.h>
void main()
{
char first_name[4] = {'A','r','y','a'};
char last_name[5] = {'S','t','a','r','k'};
printf("Initials: %c.%c. \n", first_name[0], last_name[0]);
printf("First Name: %s \n", first_name);
}
http://codepad.org/5iipaqEn
See the second printf? We have used %s to print a whole character array. This is what we call a string—as in, a string of characters.
We can do lots of fun stuff with strings, which is something that we will cover later in this course.
Tomorrow, we will learn about the most commonly used operators in C. Till then, keep coding!