Difference between revisions of "C-and-C++/C2/Increment-And-Decrement-Operators/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | || | + | || Time |
− | || | + | || Narration |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:01 |
| Welcome to the spoken tutorial on '''Increment and Decrement Operators in C and C++'''. | | Welcome to the spoken tutorial on '''Increment and Decrement Operators in C and C++'''. | ||
|- | |- | ||
− | | 00 | + | | 00:08 |
| In this tutorial, we will learn about: | | In this tutorial, we will learn about: | ||
|- | |- | ||
− | | 00 | + | | 00:10 |
|Increment and decrement operators | |Increment and decrement operators | ||
|- | |- | ||
− | | 00 | + | | 00:12 |
| ++ eg. a++ which is postfix increment operator. | | ++ eg. a++ which is postfix increment operator. | ||
|- | |- | ||
− | | 00 | + | | 00:18 |
| ++a which is prefix increment operator. | | ++a which is prefix increment operator. | ||
|- | |- | ||
− | | 00 | + | | 00:22 |
| - - . a- - is a postfix decrement operator. | | - - . a- - is a postfix decrement operator. | ||
|- | |- | ||
− | | 00 | + | | 00:27 |
| - -a is a prefix decrement operator. | | - -a is a prefix decrement operator. | ||
|- | |- | ||
− | | 00 | + | | 00:31 |
|We will also learn about Type casting. | |We will also learn about Type casting. | ||
|- | |- | ||
− | | 00 | + | | 00:35 |
| To record this tutorial, I am using: '''Ubuntu 11.10''' as the operating system | | To record this tutorial, I am using: '''Ubuntu 11.10''' as the operating system | ||
|- | |- | ||
− | | 00 | + | | 00:40 |
|'''gcc''' and '''g++ Compiler''' version''' 4.6.1 '''in''' Ubuntu'''. | |'''gcc''' and '''g++ Compiler''' version''' 4.6.1 '''in''' Ubuntu'''. | ||
|- | |- | ||
− | | 00 | + | | 00:48 |
| The '''++''' operator increases the existing value of the operand by one. | | The '''++''' operator increases the existing value of the operand by one. | ||
|- | |- | ||
− | | 00 | + | | 00:54 |
|'''a++''' and '''++a''' are equivalent to ''a = a + 1''. | |'''a++''' and '''++a''' are equivalent to ''a = a + 1''. | ||
|- | |- | ||
− | | 01 | + | | 01:00 |
|The '''--''' operator decreases the existing value of the operand by one. | |The '''--''' operator decreases the existing value of the operand by one. | ||
|- | |- | ||
− | | 01 | + | | 01:06 |
|'''a--''' and '''--a''' are equivalent to '''a = a - 1'''. | |'''a--''' and '''--a''' are equivalent to '''a = a - 1'''. | ||
|- | |- | ||
− | | 01 | + | | 01:13 |
| I will now demonstrate the use of increment and decrement operators with the help of a C program. | | I will now demonstrate the use of increment and decrement operators with the help of a C program. | ||
|- | |- | ||
− | | 01 | + | | 01:19 |
|I have already made the program, so I'll explain the code. | |I have already made the program, so I'll explain the code. | ||
|- | |- | ||
− | | 01 | + | | 01:25 |
|Here, we have the code for increment and decrement operators in '''C.''' | |Here, we have the code for increment and decrement operators in '''C.''' | ||
|- | |- | ||
− | | 01 | + | | 01:30 |
| Here, I have taken an integer variable '''a''' that holds the value '''1'''. | | Here, I have taken an integer variable '''a''' that holds the value '''1'''. | ||
|- | |- | ||
− | | 01 | + | | 01:35 |
|This way we will be able to observe the changes in the value of '''a'''. | |This way we will be able to observe the changes in the value of '''a'''. | ||
|- | |- | ||
− | | 01 | + | | 01:39 |
|It will thus give us a better idea about the working of the operators. | |It will thus give us a better idea about the working of the operators. | ||
|- | |- | ||
− | | 01 | + | | 01:47 |
| Let's see how the '''postfix''' increment operator works. | | Let's see how the '''postfix''' increment operator works. | ||
|- | |- | ||
− | | 01 | + | | 01:51 |
|The output of this printf statement is 1. | |The output of this printf statement is 1. | ||
|- | |- | ||
− | | 01 | + | | 01:55 |
|The value wont change. | |The value wont change. | ||
|- | |- | ||
− | | 01 | + | | 01:57 |
|This is because the postfix operation occurs after the operand is evaluated. | |This is because the postfix operation occurs after the operand is evaluated. | ||
|- | |- | ||
− | | 02 | + | | 02:04 |
|If an operation is performed on '''a++''', it is performed on the current value of '''a'''. | |If an operation is performed on '''a++''', it is performed on the current value of '''a'''. | ||
|- | |- | ||
− | | 02 | + | | 02:10 |
|After that the value of '''a''' is incremented. | |After that the value of '''a''' is incremented. | ||
|- | |- | ||
− | | 02 | + | | 02:17 |
| Now if we see the value of''' a '''here, it has been incremented by 1. | | Now if we see the value of''' a '''here, it has been incremented by 1. | ||
|- | |- | ||
− | | 02 | + | | 02:27 |
| We again initialize a to 1 so as to reflect on the changes. | | We again initialize a to 1 so as to reflect on the changes. | ||
|- | |- | ||
− | | 02 | + | | 02:35 |
| We now come to the prefix increment operators | | We now come to the prefix increment operators | ||
|- | |- | ||
− | | 02 | + | | 02:38 |
| This printf statement prints 2 on the screen. | | This printf statement prints 2 on the screen. | ||
|- | |- | ||
− | | 02 | + | | 02:42 |
| This is because a prefix operation occurs before the operand is evaluated. | | This is because a prefix operation occurs before the operand is evaluated. | ||
|- | |- | ||
− | | 02 | + | | 02:49 |
| So the value of a is first incremented by 1 and then it is printed. | | So the value of a is first incremented by 1 and then it is printed. | ||
|- | |- | ||
− | | 02 | + | | 02:58 |
| We again print a's value to see that there are no further changes. | | We again print a's value to see that there are no further changes. | ||
|- | |- | ||
− | | 03 | + | | 03:03 |
| Now lets check by executing this code. | | Now lets check by executing this code. | ||
|- | |- | ||
− | | 03 | + | | 03:07 |
| I will comment out the following lines. Type /*, */ | | I will comment out the following lines. Type /*, */ | ||
|- | |- | ||
− | | 03 | + | | 03:19 |
| Click on '''Save'''. | | Click on '''Save'''. | ||
|- | |- | ||
− | | 03 | + | | 03:22 |
| I have saved my file as '''incrdecr.c'''. | | I have saved my file as '''incrdecr.c'''. | ||
|- | |- | ||
− | | 03 | + | | 03:29 |
| Open the terminal window by pressing '''Ctrl, Alt and T '''keys''' '''simultaneously. | | Open the terminal window by pressing '''Ctrl, Alt and T '''keys''' '''simultaneously. | ||
|- | |- | ||
− | | 03 | + | | 03:35 |
| To compile, type the following on the terminal '''gcc space incrdecr dot c space minus o space incr'''. Press enter. | | To compile, type the following on the terminal '''gcc space incrdecr dot c space minus o space incr'''. Press enter. | ||
|- | |- | ||
− | | 03 | + | | 03:51 |
| To execute the code, type ''./incr''. Press enter. | | To execute the code, type ''./incr''. Press enter. | ||
|- | |- | ||
− | | 03 | + | | 03:59 |
| The output is displayed on the screen, | | The output is displayed on the screen, | ||
|- | |- | ||
− | | 04 | + | | 04:01 |
| This is the output when you print a++ | | This is the output when you print a++ | ||
|- | |- | ||
− | | 04 | + | | 04:06 |
| This is the output when you print ++a. | | This is the output when you print ++a. | ||
|- | |- | ||
− | | 04 | + | | 04:09 |
| We can see that the result is as discussed before. | | We can see that the result is as discussed before. | ||
|- | |- | ||
− | | 04 | + | | 04:13 |
| Now Coming back to the rest of the program. | | Now Coming back to the rest of the program. | ||
|- | |- | ||
− | | 04 | + | | 04:16 |
| I will now explain the postfix and prefix decrement operators. | | I will now explain the postfix and prefix decrement operators. | ||
|- | |- | ||
− | | 04 | + | | 04:21 |
| Remove the multiline comments from here and here . | | Remove the multiline comments from here and here . | ||
|- | |- | ||
− | | 04 | + | | 04:29 |
| We now again assign the value of ''1'' to ''a''. | | We now again assign the value of ''1'' to ''a''. | ||
|- | |- | ||
− | | 04 | + | | 04:35 |
| This '''printf''' statement outputs the value of 1 as explained previously. | | This '''printf''' statement outputs the value of 1 as explained previously. | ||
|- | |- | ||
− | | 04 | + | | 04:40 |
| A's value will be decremented after a-- is evaluated as its a postfix expression. | | A's value will be decremented after a-- is evaluated as its a postfix expression. | ||
|- | |- | ||
− | | 04 | + | | 04:47 |
| The next statement prints a's value as o. | | The next statement prints a's value as o. | ||
|- | |- | ||
− | | 04 | + | | 04:51 |
| A's value has now being decremeted by 1. | | A's value has now being decremeted by 1. | ||
|- | |- | ||
− | | 04 | + | | 04:54 |
| We now have the prefix decrement operator. | | We now have the prefix decrement operator. | ||
|- | |- | ||
− | | 04 | + | | 04:58 |
| Output of this printf statement would be 0. | | Output of this printf statement would be 0. | ||
|- | |- | ||
− | | 05 | + | | 05:00 |
| As it is a prefix operation. | | As it is a prefix operation. | ||
|- | |- | ||
− | | 05 | + | | 05:05 |
| The prefix operation occurs before the operand is evaluated. | | The prefix operation occurs before the operand is evaluated. | ||
|- | |- | ||
− | | 05 | + | | 05:09 |
| This printf statements output is 0. | | This printf statements output is 0. | ||
|- | |- | ||
− | | 05 | + | | 05:11 |
| No further changes have being made to a's value. | | No further changes have being made to a's value. | ||
|- | |- | ||
− | | 05 | + | | 05:15 |
| Type return 0; And close the ending curly bracket | | Type return 0; And close the ending curly bracket | ||
|- | |- | ||
− | | 05 | + | | 05:21 |
| Click on '''Save.''' | | Click on '''Save.''' | ||
|- | |- | ||
− | | 05 | + | | 05:24 |
| Switch back to the terminal. | | Switch back to the terminal. | ||
|- | |- | ||
− | | 05 | + | | 05:27 |
| To compile type the following on the terminal; gcc space incrdecr dot c space minus o space incr. Press Enter. | | To compile type the following on the terminal; gcc space incrdecr dot c space minus o space incr. Press Enter. | ||
|- | |- | ||
− | | 05 | + | | 05:42 |
| To execute type, ./incr.Press Enter. | | To execute type, ./incr.Press Enter. | ||
|- | |- | ||
− | | 05 | + | | 05:52 |
| This is the output when you print a-- | | This is the output when you print a-- | ||
|- | |- | ||
− | | 05 | + | | 05:56 |
| This is the output when you print --a | | This is the output when you print --a | ||
|- | |- | ||
− | | 05 | + | | 05:59 |
| So, now we see how the increment and decrement operator works. | | So, now we see how the increment and decrement operator works. | ||
|- | |- | ||
− | | 06 | + | | 06:05 |
| If we want to write the same program in C++. | | If we want to write the same program in C++. | ||
|- | |- | ||
− | | 06 | + | | 06:07 |
| I can make a few changes to the above C code. | | I can make a few changes to the above C code. | ||
|- | |- | ||
− | | 06 | + | | 06:10 |
| Let me go back to the editor. | | Let me go back to the editor. | ||
|- | |- | ||
− | | 06 | + | | 06:13 |
| Here is the '''C++ '''file with the necessary code. | | Here is the '''C++ '''file with the necessary code. | ||
|- | |- | ||
− | | 06 | + | | 06:16 |
| Notice that the header is different from the C file header. | | Notice that the header is different from the C file header. | ||
|- | |- | ||
− | | 06 | + | | 06:20 |
| We have the '''using namespace '''statement also. | | We have the '''using namespace '''statement also. | ||
|- | |- | ||
− | | 06 | + | | 06:24 |
| Also, note that the output statement in C++ is ''cout''. | | Also, note that the output statement in C++ is ''cout''. | ||
|- | |- | ||
− | | 06 | + | | 06:28 |
| So, apart from these differences, the two codes are very similar. | | So, apart from these differences, the two codes are very similar. | ||
|- | |- | ||
− | | 06 | + | | 06:33 |
| Save the file. The file is saved with an extension ''.cpp'' | | Save the file. The file is saved with an extension ''.cpp'' | ||
|- | |- | ||
− | | 06 | + | | 06:40 |
| Let's compile the code. | | Let's compile the code. | ||
|- | |- | ||
− | | 06 | + | | 06:42 |
| Open the terminal and type '''g++ space incrdecr dot cpp space minus o space incr'''. Press Enter. | | Open the terminal and type '''g++ space incrdecr dot cpp space minus o space incr'''. Press Enter. | ||
|- | |- | ||
− | | 07 | + | | 07:00 |
| To execute Type '''./ incr'''.Press Enter. | | To execute Type '''./ incr'''.Press Enter. | ||
|- | |- | ||
− | | 07 | + | | 07:07 |
| The output is displayed on the screen: | | The output is displayed on the screen: | ||
Line 332: | Line 331: | ||
|- | |- | ||
− | | 07 | + | | 07:10 |
| So, we see the output is identical to the C program. | | So, we see the output is identical to the C program. | ||
|- | |- | ||
− | | 07 | + | | 07:15 |
| We now have the concept of typecasting. | | We now have the concept of typecasting. | ||
|- | |- | ||
− | | 07 | + | | 07:17 |
| It is implemented the same way in both '''C '''and '''C++'''. | | It is implemented the same way in both '''C '''and '''C++'''. | ||
|- | |- | ||
− | | 07 | + | | 07:22 |
| Typecasting is a used to make a variable of one type, act like another type. | | Typecasting is a used to make a variable of one type, act like another type. | ||
|- | |- | ||
− | | 07 | + | | 07:27 |
| Typecasting is done by enclosing the data type you want within parenthesis. | | Typecasting is done by enclosing the data type you want within parenthesis. | ||
|- | |- | ||
− | | 07 | + | | 07:33 |
| This cast is put in front of the variable you want to cast. | | This cast is put in front of the variable you want to cast. | ||
|- | |- | ||
− | | 07 | + | | 07:38 |
| This '''typecast''' is valid for one single operation only. | | This '''typecast''' is valid for one single operation only. | ||
|- | |- | ||
− | | 07 | + | | 07:42 |
| Now '''a''' will behave as a '''float''' variable for a single operation. | | Now '''a''' will behave as a '''float''' variable for a single operation. | ||
|- | |- | ||
− | | 07 | + | | 07:47 |
| Here is an example that I have already created. | | Here is an example that I have already created. | ||
|- | |- | ||
− | | 07 | + | | 07:50 |
| I shall now explain the code. | | I shall now explain the code. | ||
|- | |- | ||
− | | 07 | + | | 07:54 |
| We first declare variables '''a''' and '''b''' as integer and '''c''' as float. | | We first declare variables '''a''' and '''b''' as integer and '''c''' as float. | ||
|- | |- | ||
− | | 08 | + | | 08:00 |
| '''a''' is assigned the value of 5. '''b''' is assigned the value of 2. | | '''a''' is assigned the value of 5. '''b''' is assigned the value of 2. | ||
|- | |- | ||
− | | 08 | + | | 08:06 |
| We will perform operations on '''a''' and '''b'''. | | We will perform operations on '''a''' and '''b'''. | ||
|- | |- | ||
− | | 08 | + | | 08:10 |
| We divide '''a''' by '''b'''. The result of division is stored in '''c'''. | | We divide '''a''' by '''b'''. The result of division is stored in '''c'''. | ||
|- | |- | ||
− | | 08 | + | | 08:14 |
| We have used '''%.2f''' to denote a precision of 2 decimal places. | | We have used '''%.2f''' to denote a precision of 2 decimal places. | ||
|- | |- | ||
− | | 08 | + | | 08:20 |
| The result displayed will be 2.00 against the expected result of 2.50. | | The result displayed will be 2.00 against the expected result of 2.50. | ||
|- | |- | ||
− | | 08 | + | | 08:25 |
| The fractional part has been truncated as both the operands '''a''' and '''b''' are integers. | | The fractional part has been truncated as both the operands '''a''' and '''b''' are integers. | ||
|- | |- | ||
− | | 08 | + | | 08:31 |
| To perform real division one of the operands will have to be typecast to float. | | To perform real division one of the operands will have to be typecast to float. | ||
|- | |- | ||
− | | 08 | + | | 08:35 |
| Here we are typecasting '''a '''to float. '''c''' now holds the value of real division. | | Here we are typecasting '''a '''to float. '''c''' now holds the value of real division. | ||
|- | |- | ||
− | | 08 | + | | 08:41 |
| Now the result of real division is displayed. The answer is 2.50 as expected. | | Now the result of real division is displayed. The answer is 2.50 as expected. | ||
|- | |- | ||
− | | 08 | + | | 08:47 |
| Type''' return 0;''' and close the ending curly bracket. | | Type''' return 0;''' and close the ending curly bracket. | ||
|- | |- | ||
− | | 08 | + | | 08:51 |
| Click on '''Save'''. Save the file with '''.c''' extension. | | Click on '''Save'''. Save the file with '''.c''' extension. | ||
|- | |- | ||
− | | 08 | + | | 08:55 |
| I have saved my file as '''typecast.c'''. | | I have saved my file as '''typecast.c'''. | ||
|- | |- | ||
− | | 08 | + | | 08:59 |
| Open the terminal. | | Open the terminal. | ||
|- | |- | ||
− | | 09 | + | | 09:01 |
| To compile, type '''gcc space typecast dot c space minus o space type'''.Press Enter. | | To compile, type '''gcc space typecast dot c space minus o space type'''.Press Enter. | ||
|- | |- | ||
− | | 09 | + | | 09:17 |
| To execute, type '''./type'''.Press Enter. | | To execute, type '''./type'''.Press Enter. | ||
|- | |- | ||
− | | 09 | + | | 09:25 |
| The output is displayed on the screen. | | The output is displayed on the screen. | ||
|- | |- | ||
− | | 09 | + | | 09:27 |
| Looking at the two values we see the effects of typecasting. | | Looking at the two values we see the effects of typecasting. | ||
|- | |- | ||
− | | 09 | + | | 09:32 |
| We will summarize the tutorial now. | | We will summarize the tutorial now. | ||
|- | |- | ||
− | | 09 | + | | 09:34 |
| In this tutorial we learnt, | | In this tutorial we learnt, | ||
|- | |- | ||
− | | 09 | + | | 09:36 |
| How to use the increment and decrement operators. | | How to use the increment and decrement operators. | ||
|- | |- | ||
− | | 09 | + | | 09:40 |
| We learn't about the forms, Postfix and Prefix | | We learn't about the forms, Postfix and Prefix | ||
|- | |- | ||
− | | 09 | + | | 09:44 |
| Also we learnt about typecasting and how it is used. | | Also we learnt about typecasting and how it is used. | ||
|- | |- | ||
− | | 09 | + | | 09:47 |
| As an assignment: | | As an assignment: | ||
|- | |- | ||
− | | 09 | + | | 09:49 |
| Write a program to solve the following expression, a divided by b plus c divided by d | | Write a program to solve the following expression, a divided by b plus c divided by d | ||
|- | |- | ||
− | | 09 | + | | 09:56 |
| The values of '''a, b, c''' and '''d''' are taken as input from the user. | | The values of '''a, b, c''' and '''d''' are taken as input from the user. | ||
|- | |- | ||
− | | 10 | + | | 10:01 |
| Use typecasting to perform real division. | | Use typecasting to perform real division. | ||
|- | |- | ||
− | | 10 | + | | 10:05 |
| Watch the video available at the following link | | Watch the video available at the following link | ||
|- | |- | ||
− | | 10 | + | | 10:08 |
| It summarises the Spoken Tutorial project | | It summarises the Spoken Tutorial project | ||
|- | |- | ||
− | | 10 | + | | 10:10 |
| If you do not have good bandwidth, you can download and watch it | | If you do not have good bandwidth, you can download and watch it | ||
|- | |- | ||
− | | 10 | + | | 10:15 |
| The Spoken Tutorial Project Team | | The Spoken Tutorial Project Team | ||
|- | |- | ||
− | | 10 | + | | 10:17 |
| Conducts workshops using spoken tutorials | | Conducts workshops using spoken tutorials | ||
|- | |- | ||
− | | 10 | + | | 10:20 |
| Gives certificates for those who pass an online test | | Gives certificates for those who pass an online test | ||
|- | |- | ||
− | | 10 | + | | 10:24 |
| For more details, please write to contact at spoken hyphen tutorial dot org | | For more details, please write to contact at spoken hyphen tutorial dot org | ||
|- | |- | ||
− | | 10 | + | | 10:33 |
|Spoken Tutorial Project is a part of the Talk to a Teacher project | |Spoken Tutorial Project is a part of the Talk to a Teacher project | ||
|- | |- | ||
− | | 10 | + | | 10:37 |
| It is supported by the National Mission on Education through ICT, MHRD, Government of India | | It is supported by the National Mission on Education through ICT, MHRD, Government of India | ||
|- | |- | ||
− | | 10 | + | | 10:44 |
| More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro | | More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro | ||
|- | |- | ||
− | | 10 | + | | 10:55 |
| This is Ritwik Joshi from IIT Bombay. | | This is Ritwik Joshi from IIT Bombay. | ||
Revision as of 12:11, 23 June 2014
Time | Narration |
00:01 | Welcome to the spoken tutorial on Increment and Decrement Operators in C and C++. |
00:08 | In this tutorial, we will learn about: |
00:10 | Increment and decrement operators |
00:12 | ++ eg. a++ which is postfix increment operator. |
00:18 | ++a which is prefix increment operator.
|
00:22 | - - . a- - is a postfix decrement operator.
|
00:27 | - -a is a prefix decrement operator. |
00:31 | We will also learn about Type casting. |
00:35 | To record this tutorial, I am using: Ubuntu 11.10 as the operating system |
00:40 | gcc and g++ Compiler version 4.6.1 in Ubuntu. |
00:48 | The ++ operator increases the existing value of the operand by one.
|
00:54 | a++ and ++a are equivalent to a = a + 1. |
01:00 | The -- operator decreases the existing value of the operand by one. |
01:06 | a-- and --a are equivalent to a = a - 1. |
01:13 | I will now demonstrate the use of increment and decrement operators with the help of a C program. |
01:19 | I have already made the program, so I'll explain the code. |
01:25 | Here, we have the code for increment and decrement operators in C. |
01:30 | Here, I have taken an integer variable a that holds the value 1. |
01:35 | This way we will be able to observe the changes in the value of a. |
01:39 | It will thus give us a better idea about the working of the operators. |
01:47 | Let's see how the postfix increment operator works. |
01:51 | The output of this printf statement is 1. |
01:55 | The value wont change. |
01:57 | This is because the postfix operation occurs after the operand is evaluated. |
02:04 | If an operation is performed on a++, it is performed on the current value of a. |
02:10 | After that the value of a is incremented. |
02:17 | Now if we see the value of a here, it has been incremented by 1. |
02:27 | We again initialize a to 1 so as to reflect on the changes. |
02:35 | We now come to the prefix increment operators |
02:38 | This printf statement prints 2 on the screen. |
02:42 | This is because a prefix operation occurs before the operand is evaluated. |
02:49 | So the value of a is first incremented by 1 and then it is printed. |
02:58 | We again print a's value to see that there are no further changes. |
03:03 | Now lets check by executing this code. |
03:07 | I will comment out the following lines. Type /*, */ |
03:19 | Click on Save. |
03:22 | I have saved my file as incrdecr.c. |
03:29 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously. |
03:35 | To compile, type the following on the terminal gcc space incrdecr dot c space minus o space incr. Press enter. |
03:51 | To execute the code, type ./incr. Press enter. |
03:59 | The output is displayed on the screen, |
04:01 | This is the output when you print a++ |
04:06 | This is the output when you print ++a. |
04:09 | We can see that the result is as discussed before.
|
04:13 | Now Coming back to the rest of the program. |
04:16 | I will now explain the postfix and prefix decrement operators. |
04:21 | Remove the multiline comments from here and here .
|
04:29 | We now again assign the value of 1 to a. |
04:35 | This printf statement outputs the value of 1 as explained previously. |
04:40 | A's value will be decremented after a-- is evaluated as its a postfix expression.
|
04:47 | The next statement prints a's value as o. |
04:51 | A's value has now being decremeted by 1. |
04:54 | We now have the prefix decrement operator. |
04:58 | Output of this printf statement would be 0. |
05:00 | As it is a prefix operation. |
05:05 | The prefix operation occurs before the operand is evaluated. |
05:09 | This printf statements output is 0. |
05:11 | No further changes have being made to a's value. |
05:15 | Type return 0; And close the ending curly bracket |
05:21 | Click on Save. |
05:24 | Switch back to the terminal. |
05:27 | To compile type the following on the terminal; gcc space incrdecr dot c space minus o space incr. Press Enter. |
05:42 | To execute type, ./incr.Press Enter. |
05:52 | This is the output when you print a--
|
05:56 | This is the output when you print --a
|
05:59 | So, now we see how the increment and decrement operator works. |
06:05 | If we want to write the same program in C++. |
06:07 | I can make a few changes to the above C code. |
06:10 | Let me go back to the editor. |
06:13 | Here is the C++ file with the necessary code.
|
06:16 | Notice that the header is different from the C file header. |
06:20 | We have the using namespace statement also. |
06:24 | Also, note that the output statement in C++ is cout. |
06:28 | So, apart from these differences, the two codes are very similar. |
06:33 | Save the file. The file is saved with an extension .cpp |
06:40 | Let's compile the code. |
06:42 | Open the terminal and type g++ space incrdecr dot cpp space minus o space incr. Press Enter. |
07:00 | To execute Type ./ incr.Press Enter. |
07:07 | The output is displayed on the screen:
|
07:10 | So, we see the output is identical to the C program. |
07:15 | We now have the concept of typecasting. |
07:17 | It is implemented the same way in both C and C++. |
07:22 | Typecasting is a used to make a variable of one type, act like another type. |
07:27 | Typecasting is done by enclosing the data type you want within parenthesis. |
07:33 | This cast is put in front of the variable you want to cast. |
07:38 | This typecast is valid for one single operation only. |
07:42 | Now a will behave as a float variable for a single operation. |
07:47 | Here is an example that I have already created. |
07:50 | I shall now explain the code. |
07:54 | We first declare variables a and b as integer and c as float. |
08:00 | a is assigned the value of 5. b is assigned the value of 2. |
08:06 | We will perform operations on a and b. |
08:10 | We divide a by b. The result of division is stored in c. |
08:14 | We have used %.2f to denote a precision of 2 decimal places. |
08:20 | The result displayed will be 2.00 against the expected result of 2.50. |
08:25 | The fractional part has been truncated as both the operands a and b are integers. |
08:31 | To perform real division one of the operands will have to be typecast to float. |
08:35 | Here we are typecasting a to float. c now holds the value of real division. |
08:41 | Now the result of real division is displayed. The answer is 2.50 as expected. |
08:47 | Type return 0; and close the ending curly bracket. |
08:51 | Click on Save. Save the file with .c extension. |
08:55 | I have saved my file as typecast.c. |
08:59 | Open the terminal. |
09:01 | To compile, type gcc space typecast dot c space minus o space type.Press Enter. |
09:17 | To execute, type ./type.Press Enter. |
09:25 | The output is displayed on the screen.
|
09:27 | Looking at the two values we see the effects of typecasting. |
09:32 | We will summarize the tutorial now. |
09:34 | In this tutorial we learnt, |
09:36 | How to use the increment and decrement operators. |
09:40 | We learn't about the forms, Postfix and Prefix
|
09:44 | Also we learnt about typecasting and how it is used. |
09:47 | As an assignment: |
09:49 | Write a program to solve the following expression, a divided by b plus c divided by d |
09:56 | The values of a, b, c and d are taken as input from the user. |
10:01 | Use typecasting to perform real division. |
10:05 | Watch the video available at the following link |
10:08 | It summarises the Spoken Tutorial project |
10:10 | If you do not have good bandwidth, you can download and watch it
|
10:15 | The Spoken Tutorial Project Team |
10:17 | Conducts workshops using spoken tutorials |
10:20 | Gives certificates for those who pass an online test |
10:24 | For more details, please write to contact at spoken hyphen tutorial dot org |
10:33 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
10:37 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
10:44 | More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro |
10:55 | This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |
Contributors and Content Editors
Ashwini, Devisenan, Jyotisolanki, Krupali, PoojaMoolya, Pratik kamble, Sandhya.np14