C Programming Tutorial Part 3 - Variables basics

Up until now, we've discussed the basics of what a C program is, how to compile and execute it, and what are preprocessors. If you have gone through these tutorials, it's time we discuss the next topic, which is variables.

Variables are one of the core elements of C programming as they store values for programmers to use as per their requirement. Let's understand their basics through an example. Following is a basic C program:

#include <stdio.h>

int main (void)
{
int a = 10;
char b = 'z';
float c = 1.5;
printf("\n a=%d, b=%c, c=%f \n", a,b,c);
return 0;
}

In previous C programming tutorials, we have already explained things like what is 'stdio.h,' what does '#include' mean, and what is a function (especially 'main'). So, we'll directly jump onto the variables part.

The line 'int a =10' means there is a variable named 'a' which can hold integer ('int') type values, and the current value it's holding is '10'. Similarly, 'b' can hold characters and 'c' can hold floating point numbers, with their current values being 'z' and '1.5', respectively. 

The example program shown above prints these values in output through the 'Printf' function. Note that %d, %c, and %f are used to tell the 'printf' function that variables 'a', 'b', 'c' should be treated as integer, character, and float, respectively.

Operations on variables

Of course, you can do a lot more than just printing these values in output. For example, the following program calculates factorial of number 5.

#include <stdio.h>

int main (void)
{
int num = 5;
int result = 1;
while (num > 0)
{
result = result * num;
num = num -1;
}

printf("\n Factorial of 5 is %d\n", result);

return 0;
}

For those who aren't aware, the factorial of a number, say 'n', is the result of the following multiplication:

nx(n-1)x(n-2)x.....1

So, if the number is 5, then factorial of 5 (or, 5!) would be 5x4x3x2x1, which equals 120.

Coming back to the program, we have defined two integer variables named 'num' and 'result'. While num contains the number whose factorial is to be calculated (5 in this case), 'result' just holds a dummy value of '1' to begin with. Then comes a 'while' loop.

As the name suggests, loops are used in C functions to repeatedly execute a set of instructions. In case of 'while', the loop begins by checking a condition ('num should be greater than zero' in this case) and then instructions inside the loop are executed repeatedly until the while condition becomes false.

In our case,  value of 'num' is 5 initially. So the execution will enter inside the loop and the first value of result 'would' be '5' (1x5). Thereafter 'num' becomes 'num -1', which means the new value of 'num' is now 4. 

The loop then executes again because 4 is still greater than zero. This time, 'result' would be 5x4, which is 20. And 'num' then becomes (4-1), which is 3. This way, the loop continues to execute until 'num' becomes 0, and by that time, result is '120', which is the factorial of 5.

So here's the output produced by this program:

Factorial of 5 is 120

Now, instead of a specific value (like '5' in this case), what if you want the user of the program to specify the number whose factorial is to be calculated? Well, that can be done in the following way:

#include <stdio.h>

int main (void)
{
int num = 0, temp=0;
printf("\n Enter a positive integer: ");
scanf("%d", &num);
temp = num;
int result = 1;
while (temp > 0)
{
result = result * temp;
temp = temp -1;
}

printf("\n Factorial of %d is %d\n", num, result);

return 0;
}

So here, we have used a new function called 'scanf', which does exactly opposite of 'printf' - it accepts input from user.

Now, every variable has a certain amount of memory associated with it in terms of bytes. The scanf function requires you to pass the starting address of a variable's memory, which you can access by placing '&' before the variable's name.

We have introduced a new variable 'temp' here as we need the actual number passed by the user (stored in 'num' here) in the last 'printf' statement.

Conclusion

In this tutorial, we touched upon the basics of variables. This tutorial should have given you a basic idea on what variables are, and how they can be used in the C programming language. There are several other aspects related to variables that need to be discussed - we'll be doing that in the next tutorial.

Share this page:

Suggested articles

2 Comment(s)

Add comment

Comments

By: slowlearner at: 2019-03-08 14:45:46

Looking forward for

Tutorial Part 4

By: till at: 2019-03-08 15:15:59