C Programming for Beginners

Variables and Constants

Introduction Variables and Constants

Introduction Variables and Constants

Video content of Introduction to C Programming Season 1, Chapter 2 Can we found at Amazon, YouTube.

In this chapter we will study variable, constant and their different types.

Quick review, in the last chapter 1 we used printf to display some text and the result of an expression like 9 + 5, 3+4, etc

Let’s get started

Understanding constant and variable is pretty simple. Constants mean values never change and a variable means values can change.

In C language we will study 3 types of constants i.e. integer, float and char.

what is an integer, float and char?

First let’s try and understand integers. In c language, an integer is a value which doesn’t have a decimal. So we can say 4 is an integer but 4.00 is not an integer.  One more thing, there is a limit in which an integer can hold  a value.

OK, now we can say that an integer constant  is something whose value never changes throughout the program and does not contain any decimals. And of course it’s within the range.

Lets quickly execute a program.

main()

{

const int a=100;

printf(“%d”,a);

}

Integer Constant Program

Lets understand our program. In the program we are declaring a constant with the initial value of 100.

main()

{

const int a=100;

printf(“%d”,a);

}

The int is a keyword. Keyword in a sense that it has a special meaning in C language and you can’t use otherwise.

You can only use int to  declare different types of integers. Before integer we have used another keyword const, again it can only be used to declare a constant.

What is %d ?

%d is a placeholder only for integers. Whenever we want to display an integer value we will use this placeholder

When ever we use a placeholder, we place the expression, or constant or variable after “ ” and a comma ,

I hope you understand this, after executing few examples it will be like A.B.C for the C Language.

C Language understand placeholder i.e %d in a different way. If you put lot of characters around it, then it also it will handle %d properly.

Let’s take an example. In the printf statement we will write some characters. We will execute it. You see the output . printf statement displays as it is, except the placeholder. For placeholder %d it’s displayed a value.

main()

{

const int a=100;

printf(“Value of a : %d”,a);

}

Output : 

Value of a : 100

You can view the source code from Newtum github repository  link over here.

Float Constant

Similarly like constant integer, we have float. Float can have positive and negative values. But they can also hold decimals. Placeholder for float is %f. And the keyword for declaration is float.

Let’s start with execution again. We are writing a program starting with main, followed by curve brackets.

Here we are declaring a constant float.

main()
{

const float a;

}

Here we are declaring a constant float. 

main()
{

const float a=99.99;

}

Assigning it a value of 99.99 And printing the same. execute the program.

main()
{

const float a=99.99;
printf(“%f”,a);

}

Output : 

99.990000

You can view the source code from Newtum github repository  link over here.

Now let’s try another way, we will try to print 99.990000 with declaring a constant as an integer and using %d as a placeholder. Now execute this program.

main()
{

const int a=99.99;
printf(“%d”,a);

}

Output : 

99

You can view the source code from Newtum github repository  link over here.

You see the output is 99 ,instead of 99.990000. Its because the integer can’t accept decimal values. Even if assign a value of the decimal to integer it will reject it.

Talking about range , the maximum value of a float that can be stored is very high, its 3.4 E38 (3.4 exponential 38). It’s a lot  we will worry about this later on.

So now we have studied two types of constants, one is an integer and one a float. Let’s try to compare the two. 

Integer and float both can hold + and – values. Integers can’t hold decimal values but floats can hold decimal values.

The range of an integer is this and the range of a float is this, which is pretty high.

The placeholder of an integer is %d, the placeholder of float is %f.

Character Constant

Now we move to another chapter of constant that is character constant. Character constant is pretty simple it can hold any character. Like A, B, C, D or small a,b,c,d and its placeholder is %c. You need to remember one more thing about character , that it is always enclosed in single quotes.

To declare a constant we use char keyword.

Lets execute a small example, we will write our main program, followed by curve brackets and curly brackets.

main()
{

const char b=’s’;
printf(“%c”,b);

}

Now we will declare character constant. You see the keyword we are using is char, before that we have written const so that the value won’t change. As told earlier the character has to be enclosed in single quotes.  There is a big reason behind this, you will understand this in later chapters.

Now execute the code. Your output is a plain S.

Output : 

S

You can view the source code from Newtum github repository  link over here.

You can add any character over here. We will study more about characters when we study strings. For now let’s keep it simple.

Introduction Variable

So we have now studied 3 types of constants, now let’s go for variable. 

A variable is pretty simple , the value can be changed any time during the execution of code.

Let’s start with a simple example. Create a program, main, and in curly brackets we will write the business logic. Here we are declaring a variable and printing them. Please observe here we have not used the constant keyword. Let’s execute it.  Here we are declaring a variable and printing them.

main()
{

int a=100;
printf(“%d”,a);

}

Please observe here we have not used the constant keyword. Let’s execute it. Output is pretty simple.

Output : 

100

Similarly we can have a float variable. Let me modify the program. Here I am using a float variable and printing it. It’s pretty simple only the constant keyword is different.

main()
{

float a=99.9;
printf(“%f”,a);

}

Output : 

99.900000

Variable and  Program

Lets understand the importance of const keyword.

Now I am again writing the same program with constant keywords.

main()
{
const float a=99.99;
printf(“%f”,a);
}

But in the next line putting a constant a = 9,

main()
{

const float a=99.99;
a=9;
printf(“%f”,a);

}

and I will execute the program.

Output : 

Error message screen

Here “a” is constant but we have tried to change the value. Now when we execute, it gives an error. Logic is simple. 

Now we will remove the constant keyword and will try to execute the program again.

main()
{

Float a=99.99;
a=9
printf(“%f”,a);

}

See It didn’t give any error and displayed correct value as 9.

Output : 

99.900000

I think now you understand the difference between constant and variable. So much with the constants and variables.

Addition of two numbers

Let’s move forward and create our first program that will do some work for us. Here, to start we will create a program  that will add two numbers. Here we will create 3 integers using the keyword int variables a, b, c.

main()
{

int a;
int b;
int c;

}

we want to tell you that we can create as many variables in a single line while using int keywords only one time.

main()
{

int a, b, c;

}

Now we put value 15 in a

main()
{

int a=15;
int b;
int c;

}

and value 10 in b.

main()
{

int a=15;
int b=10;
int c;

}

Now we do c= a+b.

main()
{
int a=15;
int b=10;
int c=a+b;
printf(“%d”,c);
}

Please make sure that all the statements are followed by a semicolon. I have explained the reason in the very first chapter.

Now we print the variable c. Execute the program.

Output : 

25

Well you got your answer as 25.

So what we have done? We have created 3 variables. A and b act as input variables , while C stores the result of expression i.e a+b and we print the result.

main()
{

int a=15;
int b=10;
int c=a+b;
printf(“%d”,c);

}

Output : 

25

It’s a pretty simple program. But we can’t have fixed values i.e a=15 and b=10. And every one  doesn’t know programming languages. We need to prepare a program which can be used by any one. We don’t have to write values of a and b via editing the program. 

How to achieve this. Today we will be learning a new function, called as scanf. scanf takes the input from the users and does the processing. Let’s open with the same the example.

main()
{

int a=15;
int b=10;
int c=a+b;
printf(“%d”,c);

}

Only difference is here we will not assign a=15 and b=10, but we will  accept inputs from user and do the addition.

Our program is the same, we are removing the two statements i.e a=10 

main()
{

int a;
int b=10;
int c=a+b;
printf(“%d”, &c);

}

and b=5

main()
{

int a;
int b;
int c=a+b;
printf(“%d”, &c);

}

Now we will write scanf.

main()
{

int a;
int b;
int c;
scanf(“%d”, &a);
scanf(“%d”, &b);
c=a+b;
printf(“%d”, &c);

}

Let’s execute the program. We have executed the program. But we still see the blank black screen. It’s because of scanf. Its waiting for our input. Now since we have specified scanf twice it will wait for two inputs

Lets see. I am typing the value 21. And pressed enter. One input is gone and now the program is waiting for second input.Now enter value 4 and again press enter. Now you see the output C. Only scanf waits for input once input is given the program runs the expression a+b, stores the result in c and then print the value of c.

Before understanding the scanf and this program in detail, lets modify it so that it’s more user friendly.

After declaring variables, we are writing printf statement to give a title , and another line to ask the user to give the first input.

main()
{

int a,b,c;

printf(“Welcome to program of Addition”);
printf(“\nEnter value for a : “);
scanf(“%d”, &a);
printf(“\nEnter value for b : “);
scanf(“%d”, &b);

}

Same for the second input. Now we will print result in a decent way.

main()
{

int a,b,c;

printf(“Welcome to program of Addition”);
printf(“\nEnter value for a : “);
scanf(“%d”, &a);
printf(“\nEnter value for b : “);
scanf(“%d”, &b);
c=a+b;
printf(“Result : %d”,c);

}

Let’s execute the program. As we execute it, the system displays the title first.

You can view the source code from Newtum github repository  link over here.

Output : 

Welcome to program of Addition

We will input the values 12 and 18.

Output : 

Welcome to program of Addition
Enter value for a : 12
Enter value for b : 18

Output : 

Welcome to program of Addition
Enter value for a : 12
Enter value for b : 18
Result : 30

Hope you got some more understanding of the printf statement.

Let’s explore the scanf statement. I am saying again scanf takes the input. It has two parameters, First is placeholders of variables and second is the variable followed by  ampersand (&). Ampersand is the biggest and most difficult part of the C language , we will understand this in a future chapter.

For now just assume that its a syntax , I mean a way for writing a scanf statement.

We have used printf statement to give the output and scanf statement to take input.

In the next chapter we will explore more about Basic Methods and syntax of the C Language.

You can download the code of this chapter from below link  {github.com/newtum.}

Facebook Comments
(Visited 1 times, 1 visits today)