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

In this chapter we will study Operators in C Programming Language.

Quick review, in the last chapter 2 we will study variables, constants and their different types

In the last chapter, we have written a simple program to add two integer numbers.

```
main()
{
int a=10;
int b=20;
int c;
c=a+b;
printf("Addition Result : %d",c);
}
```

`Addition Result : 30`

Now we write a new example which will do the division work.

Let’s write a program. We will have main, now declare variable a, b, and c variable to store the result.

```
main()
{
int a, b, c;
}
```

Now we write printf so that user will understand to enter any number,

```
main()
{
int a, b, c;
printf("\n Welcome to Division Program");
printf("\n Enter value for a");
}
```

then scanf statements to accept user input.

```
main()
{
int a, b, c;
printf("\n Welcome to Division Program");
printf("\n Enter value for a");
scanf("%d",&a);
}
```

Write a similar statement for variable b.

```
main()
{
int a, b, c;
printf("\n Welcome to Division Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c = a/b;
printf("\n Division Result : %d",c);
}
```

Let’s run the program. Now here we are entering value 10 as the first value and the second value is 4.

```
Welcome to Division Program
Enter value for a : 10
Enter value for b : 4
```

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

The output says 2.

```
Welcome to Division Program
Enter value for a : 10
Enter value for b : 4
Division Result : 2
```

We divide 10/4 its 2.5. So our answer is wrong.

There is a reason for this and we did purposely. Because this will explain the difference between integer and float. Since we declare the variable as integer and it can’t hold the decimal.

To get the correct output we will have to change the integer to float.

```
main()
{
float a, b, c;
printf("\n Welcome to Division Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a/b;
printf("\n Division Result : %d",c);
}
```

We are updating %d with %f, %f is the placeholder of float.

```
main()
{
float a, b, c;
printf("Welcome to Division Program");
printf("\n Enter value for a : ");
scanf("%f",&a);
printf("\n Enter value for b : ");
scanf("%f",&b);
c=a/b;
printf("\n Division Result : %f",c);
}
```

Run the program again,

and we now give the input 10 and 4.

```
Welcome to Division Program
Enter value for a : 10
Enter value for b : 4
```

The output is 2.5. That’s right.

```
Welcome to Division Program
Enter value for a : 10
Enter value for b : 4
Division Result : 2.5
```

Now we will write a few more examples to get a better hold of C Programming.

Let’s write a program which calculates simple interest.

For calculating simple interest formula is (Principal Amount* Rate of Interest Per Annum * Duration)/100. I hope you have studied this in schooling.

So here, we need 3 Variables, and another variable to store the interest.

Let’s write the program and execute it. First, let’s declare a variable. We will declare a float variable for the principal amount, and rate of interest.

```
main()
{
float principal, rate;
}
```

We assume that duration is only in years and hence we are declaring it as an integer.

```
main()
{
float principal, rate;
int year;
}
```

Let’s do some cosmetics so that our program will look good. Put a printf statement. Saying please enter the principal amount.

```
main()
{
float principal, rate;
int year;
printf("Enter the principal Amount");
}
```

And after that, we are adding our scanf statement to accept principal amount from user.

```
main()
{
float principal, rate;
int year;
printf("\nEnter the principal Amount");
scanf("%f",&principal)
}
```

Next, we say please enter the rate of Interest per Year.

```
main()
{
float principal, rate;
int year;
printf("\nEnter the principal Amount");
scanf("%f",&principal);
printf("\nEnter the Rate Of Interest");
}
```

again followed by a scanf statement.

```
main()
{
float principal, rate;
int year;
printf("\nEnter the principal Amount");
scanf("%f",&principal);
printf("\nEnter the Rate Of Interest");
scanf("%f",&rate);
}
```

We do similar for the duration in years. Just remember we have assumed it an integer.

```
main()
{
float principal, rate;
int year;
printf("\nEnter the principal Amount");
scanf("%f",&principal);
printf("\nEnter the Rate Of Interest");
scanf("%f",&rate);
printf("\nEnter duration in Years");
scanf("%d",&year);
}
```

Now let’s put our expression. In general, people call it formula.

```
main()
{
float principal, rate;
int year;
printf("\nEnter the principal Amount");
scanf("%f",&principal);
printf("\nEnter the Rate Of Interest");
scanf("%f",&rate);
printf("\nEnter duration in Years");
scanf("%d",&year);
float SI= ( principal * rate * year ) / 100;
}
```

Let’s display the result. In this result display, we display a complete statement. Here we will write Interest on Principal amount of %f with the rate of interest %f for the %d year is %d. And then we will add the variables in the same order after the comma in printf.

```
main()
{
float principal, rate;
int year;
printf("\n Enter the principal Amount : ");
scanf("%f",&principal);
printf("\n Enter the Rate Of Interest : ");
scanf("%f",&rate);
printf("\n Enter duration in Years : ");
scanf("%d",&year);
float SI=(principal * rate * year) / 100;
printf("\principal amount : %f, \nRate Of Interest : %f , \nYears : %d ", principal, rate, year);
printf("\nSimple interest : %f", SI);
}
```

Let’s execute it.

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

Enter the values, 1000 is the principal amount. 10 is the rate of interest. And the duration is 2 Year.

```
Enter the principal Amount : 1000
Enter the Rate Of Interest : 10
Enter duration in Years : 2
```

Press enter here is your answer.

```
Interest on principal amount : 1000,
Rate Of Interest : 10 ,
Years : 2
Calculating simple interest : 200
```

Here we have used the same concept that we studied in the first two chapters.

Please make a not that, we have used this variables name as princinpal_amount , interest_rate and duration. Previously we have used a, b, and c. Well by industry standard we should have the name of variables so that they will represent their purpose. But there are certain rules you should follow to construct the variable names.

- A variable name can have alphabets, digits, and underscores.
- The first character in the variable should be an alphabet
- As a special symbol, you can have only underscores. You can’t have spaces or any other special symbol like dollar, @, #, etc.
- Length of character could be 1 to 31, but some latest version supports up to 247 characters. That huge never try that because it causes a lot of issues while writing code.
- Don’t use special keywords like printf, scanf in the variable name. You will study many more keywords in the coming future.

So, in short, you need to understand that you should always a meaningful variable name. That variable name should be in this rule otherwise your program will give you an error.

Let’s execute one more program so that we will have a better understanding of C Programming.

Well before writing any C program the logic is very important. To computer you need to give a logic via programming, computers will solve all your problems using that same logic.

Let’s create a program which will swap the values.

```
main()
{
int a, b, c;
a=100;
b=200;
printf("Before Swap - \na = %d \nb = %d",a,b);
c=a;
a=b;
b=c;
printf("After Swap - \na = %d \nb = %d",a,b);
}
```

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

```
Before Swap -
a = 100
b = 200
After Swap -
a = 200
b = 100
```

I mean the value of variable a will got to b and vice versa.

In most of the cases or when you guys become a developer you will have to design many complicated logic, here I will explain to you how it’s done.

Forget about the programming. Let’s assume you have two containers. Container A and Container B. Now you want the content of a Container of A to go to B and content of B should go to A. How you will do this do. In real life, we will need a third container to call it container number C.

Let’s put the content of container A in container C. Now the content of container B in A. And again content of container C in B. Now throw away container C. This is done. So this was the real-life example.

In the field of computer programming, we need to first understand the real life logic and then put it computer language. The computer can perform tasks faster, they are very good for repetitive tasks but we need to define the logic.

Let’s implement the swapping logic in C language.

First, we declare three variables, a, b, and c. Take the user inputs for variable a and using scanf statement.

```
main()
{
int a, b, c;
scanf("%d",&a);
scanf("%d",&b);
}
```

To make it more friendly we will add a few statements using printf .

```
main()
{
int a, b, c;
printf("\n Swapping Program");
printf("\nEnter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
}
```

Now write logic.

We will assign the value of a into c.

```
main()
{
int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
}
```

Then the value of b into a.

```
main()
{
int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
a=b;
}
```

Then the value of c into b.

```
main()
{
int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
a=b;
b=c;
}
```

Let’s put a printf statement with respective placeholders and execute the programs.

```
main()
{
int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("\n After Swapping ");
printf("\n a = %d",a);
printf("\n b = %d",b);
}
```

I am giving it an input of 10 and then 20. Now press enter. See the result values are swap.

```
Swapping Program
Enter value for a :10
Enter value for b : 20
After Swapping
a = 20
b= 10
```

we wrote this example to demonstrate the use of logic in the C programming language.

When we speak of C Language or in fact any programming language we need to understand a few terms like Editor, Compiling, and Execution. When we run this program The editor is where we write our program. In our case its code blocks.

Computers don’t understand our language they only understand 0 and 1 or we can say machine level language. But we can’t understand machine level language. So whatever we write is converted into machine level language, this process is called compiling and when we run the program it’s called execution. You will get more aware of such concepts as we process to further. For now, just assume before execution compiling is a must. You don’t have to worry much about this since Code Blocks i.e your editor takes care of it.

Well we have used Multiplication, division in our program, this are called as operators. Like multiplication operator, division operator etc. It’s you simple math.

Operators in C Programming Language supports many type. C Operators supports following operator.

**Multiplication, Division**and**Modular Division****Addition**and**Subtractions****Assignment**

They have their priorities. Multiplication, division and Modular Division are a 1st priority. Addition and Subtraction have second priority. And assignment is the last priority. Let’s understand priority

Suppose you write

x = 5* 3+12, so what will be the output. It can give two possible output. 5 multiplied by 3 is fifteen and plus 12 is 27, so output could be 27. Or 3 + 12 will be 15 and multiply by 5 it will be 75.

Lets execute this program. We will write the main, declare an integer variable , then we will write our expression. I.e x = 5 * 3 + 12. Then we will print the value using printf.

```
main()
{
int x;
x = 5 * 3 + 12;
printf("5 * 3 + 12 = %d",x);
}
```

Lets build and run. We got the output as 27.

`27`

Its because of priority. Since multiplication is high priority is executed first and then the addition. You can execute different examples expression and dive deeper into priority.

Well to summarize the things we can say that we have 3 types of variables i.e integer, float and char.

We have two statement printf for input and scanf for output.

We have different Operators in C Programming Language like multiplication, division, and Modular Division. Then we have Addition, subtraction and Finally Assignment.

[adrotate group=”2″]

Facebook Comments

(Visited 1 times, 1 visits today)