C-and-C++/C2/Increment-And-Decrement-Operators/English
Title of script: Increment and Decrement Operators in C and C++
Author: Ritwik Joshi
Keywords: Increment and Decrement Operators, Type casting, Video Tutorial
|
|
Slide1 | Welcome to the spoken tutorial on Increment and Decrement Operators in C and C++. |
Slide 2
|
In this tutorial, we will learn about:
Increment and decrement operators ++ eg. a++ which is a postfix increment operator. ++a which is a prefix increment operator. - - eg. a- - is a postfix decrement operator. - -a is a prefix decrement operator. We will also learn about Type casting. |
Slide 3
|
To record this tutorial, I am using:
Ubuntu 11.10 as the operating system and gcc and g++ Compiler version 4.6.1 in Ubuntu. |
Slide 4 | The ++ operator increases the existing value of the operand by one.
a++ and ++a are equivalent to a = a + 1. The -- operator decreases the existing value of the operand by one. a-- and --a are equivalent to a = a - 1. |
Switch to the file incrdecr.c in gedit | I will now demonstrate the use of increment and decrement operators with the help of a C program.
I have already made the program, so I'll explain the code. Here, we have the code for increment and decrement operators in C. |
Highlight
|
Here, I have taken an integer variable a that holds the value 1.
This way we will be able to observe the changes in the value of a. It will thus give us a better idea about the working of the operators. |
Highlight
a++);
|
Let's see how the postfix increment operator works.
The output of this printf statement is 1. The value won't change. This is because the postfix operation occurs after the operand is evaluated. If an operation is performed on a++, it is performed on the current value of a. After that the value of a is incremented. |
printf("a's value is %d\n",
a); |
Now if we see the value of a here, it has been incremented by 1. |
Highlight
a = 1; |
We again initialize a to 1 so as to reflect on the changes. |
Highlight
printf(“a's value is now %d\n”, ++a); |
We now come to the prefix increment operators
This printf statement prints 2 on the screen. This is because a prefix operation occurs before the operand is evaluated. So the value of a is first incremented by 1 and then it is printed. We again print a's value to see that there are no further changes. |
/*
*/ |
Now lets check by executing this code.
I will comment out the following lines. Type /* */ |
Click on Save | Click on Save.
I have saved my file as incrdecr.c |
press Ctrl, Alt and T keys simultaneously. | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously. |
Type
Type ./incr |
To compile, type the following on the terminal
gcc space incrdecr.c space -o space incr Press Enter To execute the code, type ./incr Press Enter |
Highlight
Output a's value is now 1 a's value is now 2
a's value is now 2
|
The output is displayed on the screen,
This is the output when you print a++ This is the output when you print ++a. We can see that the result is as discussed before. |
y/* | Now Coming back to the rest of the program.
I will now explain the postfix and prefix decrement operators. Remove the multilinecomments from here and here.
|
Highlight
a=1 |
We now again assign the value of 1 to a. |
Highlight
printf("a's value is %d\n”, a--); |
This printf statement outputs the value of 1 as explained previously.
A's value will be decremented after a-- is evaluated as its a postfix expression. |
Highlight
printf("a's value is now %d\n”, a); |
The next statement prints a's value as o.
A's value is now decemented by 1. |
Highlight
printf("a's value is now %d\n”, --a); |
We now have the prefix decrement operator.
Output of this printf statement will be 0. As it is a prefix operation. The prefix operation occurs before the operand is evaluated. |
This printf statements output is 0.
No further changes are being made to a's value. | |
Type return 0;
And close the ending curly bracket | |
Click on Save.
Switch back to the terminal. To compile type the following on the terminal; gcc space incrdecr.c space -o space incr Press Enter To execute type, ./incr Press Enter | |
Highlight
a's value is now 0 a's value is now 0 a's value is now 0
|
This is the output when you print a-- This is the output when you print --a So, now we see how the increment and decrement operators work. |
If we want to write the same program in C++.
I can make a few changes to the above C code. | |
Let me go back to the editor.
| |
//Increment and Decrement Operators
#include <iostream> using namespace std; int main() { int a=1; cout <<"a's value is " <<a++ <<"\n"; cout <<"a's value is now " <<a <<"\n"; a=1; cout <<"a's value is now " <<++a <<"\n"; cout <<"a's value is now " <<a <<"\n"; a=1; cout <<"a's value is now " <<a-- <<"\n"; cout <<"a's value is now " <<a <<"\n"; a=1; return 0; }
|
Here is a C++ file with the necessary code.
|
Highlight
#include<iostream>
|
Notice that the header is different from the C file header.
We have the using namespace statement also. Also, note that the output statement in C++ is cout. So, apart from these differences, the two codes are very similar. |
Save the file.
The file is saved with an extension .cpp | |
Type
|
Let's compile the code.
Open the terminal and type g++ space incrdecr.cpp space -o incr Press Enter To execute Type ./incr Press Enter |
Output:
a's value is 1 a's value is now 2 a's value is now 2 a's value is now 2 a's value is now 1 a's value is now 0 a's value is now 0 a's value is now 0
|
The output is displayed on the screen:
So, we see the output is identical to the C program. |
We now have the concept of typecasting.
It is implemented the same way in both C and C++. Typecasting is a used to make a variable of one type, act like another type. | |
Slide 5
Highlight (float)
(int) a |
Typecasting is done by enclosing the data type you want within parenthesis.
This cast is put in front of the variable you want to cast. This typecast is valid for one single operation only. Now a will behave as a float variable for a single operation. |
Open incrdecr.c | Here is an example I have already created.
I shall now explain the code. |
Highlight
int a,b; float c; |
We first declare the variables a and b as integer and c as float. |
Highlight
a=5; b=2;
|
a is assigned the value of 5.
b is assigned the value of 2. We will perform operations on a and b. |
Highlight
c=a/b; |
We divide a by b.
The result of division is stored in c. |
Highlight
printf(“Value of c is %.2f\n”, c); |
We have used %.2f to denote a precision of 2 decimal places.
The result displayed will be 2.00 against the expected result of 2.50. The fractional part has been truncated as both the operands a and b are integers. To perform real division one of the operands will have to be type cast to float. |
Highlight
c=(float)a/b; |
Here we are typecasting a to float.
c now holds the value of real division. |
Highlight
printf(“Value of c is %.2f\n”, c); |
Now the result of real division is displayed.
The answer is 2.50 as expected. |
Highlight
return 0; } |
Type return 0;
and the close the ending curly bracket. |
Click on Save.
Save the file with .c extension. I have saved my file as typecast.c. | |
Open the terminal.
./type |
Open the terminal.
To compile, type gcc space typecast.c space -o space type Press Enter to execute, type ./type Press Enter |
Output | The output is displayed on the screen.
Value of c is 2.00 Value of c is 2.50 looking at the two values we see the effects of typecasting. |
Summary | We will summarize the tutorial now.
In this tutorial we learnt, How to use the increment and decrement operators. We learnt about the forms Postfix and Prefix Also we learnt about typecasting and how it is used. |
Assignment: | As an assignment:
Write a program to solve the following expression, (a\b) + (c\d) The values of a, b, c and d are taken as input from the user. Use typecasting to perform real division.
|
Slide 6
About the Spoken Tutorial Project
|
* Watch the video available at the following link
|
Slide 7
Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team
|
Slide 8
Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Remain on previous slide
No slide for this part |
This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |