Difference between revisions of "C-and-C++/C2/Tokens/English"
PoojaMoolya (Talk | contribs) |
|||
Line 4: | Line 4: | ||
'''Keywords: Tokens, Data types, Variables, Identifiers, Constant, Video Tutorial ''' | '''Keywords: Tokens, Data types, Variables, Identifiers, Constant, Video Tutorial ''' | ||
+ | |||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | + | ! <center>Visual Cue</center> | |
− | + | ! <center>Narration</center> | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1 | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Tokens''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Tokens in C and C++.''' |
|- | |- | ||
Line 26: | Line 27: | ||
We will also see some common errors and their solutions. | We will also see some common errors and their solutions. | ||
− | |||
|- | |- | ||
Line 32: | Line 32: | ||
− | |||
− | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | |
− | + | Ubuntu Operating system version 11.10. | |
+ | gcc and g++ Compiler version 4.6.1 on Ubuntu. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4 |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction. | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction. |
− | + | ||
− | + | ||
− | + | ||
+ | Token is a generic word for '''Data types''', '''Variables''', '''Constants''' and '''Identifiers.''' | ||
|- | |- | ||
Line 54: | Line 52: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the program | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the program | ||
− | I have already typed the | + | I have already typed the code on the editor |
− | + | Let me open it | |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point the cursor on '''tokens.c''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''tokens.c''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this program we will initialize the variables and print their values. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki>#include<stdio.h></nowiki>''' | ||
+ | |||
+ | '''int main()''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our header file. | ||
+ | |||
+ | This is our main function. | ||
|- | |- | ||
Line 86: | Line 100: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If a value is not assigned to a variable then it is called as declaration of the variable. |
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | + | double '''const '''b = 4 | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, b is a '''constant.''' | ||
+ | |||
+ | We have initialized b, by assigning a value of 4 to it. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | ||
− | + | double '''const '''b = 4; | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''const''' '''keyword''' is used to create read only variable | ||
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to our slides to know more about keywords and constant | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5 |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Keywords''' have fixed meanings that cannot be changed | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Keywords''' have fixed meanings that cannot be changed | ||
Line 111: | Line 131: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6 |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Constants''' are fixed values. |
− | + | They do not change during the execution of a program. | |
− | + | ||
− | + | There are two types of''' constants.''' | |
− | + | ||
− | + | '''Numeric constants.''' | |
− | + | '''Character constants.''' | |
− | + | ||
− | + | |- | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our program. | ||
|- | |- | ||
Line 132: | Line 150: | ||
'''float c''' | '''float c''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, '''float''' is a '''data type''' of '''c ''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, '''float''' is a '''data type''' of '''c. ''' |
|- | |- | ||
Line 138: | Line 156: | ||
float c =''' 1.5; ''' | float c =''' 1.5; ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have assigned it a value, '''1.5''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have assigned it a value, '''1.5.''' |
− | '''Data type '''is a finite set of values along with a set of rules | + | '''Data type '''is a finite set of values along with a set of rules. |
|- | |- | ||
Line 159: | Line 177: | ||
'''a''' and '''c''' | '''a''' and '''c''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It is easy to see that '''int, double float''' and char are datatypes. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It is easy to see that '''int, double, float''' and '''char''' are datatypes. |
− | '''a,''' '''c and d''' are '''variables''' | + | '''a,''' '''c and d''' are '''variables.''' |
− | + | Now come back to our slides. | |
− | + | We will know more about datatypes and variables'''.''' | |
− | + | ||
− | + | ||
− | + | ||
− | ''' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7 |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us begin with '''integer data type''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us begin with '''integer data type''' | ||
Line 194: | Line 200: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8 |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the range of '''data types''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the range of '''data types''' | ||
− | + | '''Integer''' value has a . | |
− | + | range of this. | |
− | ''' | + | '''Floating point''' has a range of this. |
− | + | '''Character''' has a range of this. | |
− | ''' | + | '''Double''' has a range of this. |
− | ''' | + | The values stored in the '''variable''' must not be greater or less than the range. |
− | ''' | + | Now we will move on to''' variables'''. |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9 |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Variable''' is a data name | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Variable''' is a data name. |
− | It may be used to store a data value | + | It may be used to store a data value . |
− | The values can change when a program runs | + | The values can change when a program runs. |
− | + | Before using a '''variable '''it must be declared'''.''' | |
We should try to give meaningful names to '''variables''' | We should try to give meaningful names to '''variables''' | ||
Line 227: | Line 233: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will move back to our program. |
|- | |- | ||
Line 233: | Line 239: | ||
'''printf'''("The Value of a is %d\n", a); | '''printf'''("The Value of a is %d\n", a); | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here,''' printf''' is the '''identifier''' name for this function | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the slides |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our slides to know more about our identifiers. |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10 |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Identifiers''' are user defined names | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Identifiers''' are user defined names | ||
Line 252: | Line 256: | ||
Both uppercase and lowercase letters are permitted | Both uppercase and lowercase letters are permitted | ||
− | First character must be an alphabet. | + | First character must be an alphabet or underscore. |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have initialized the variables and constants. |
− | + | Here we print them. | |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is our return statement. | ||
|- | |- | ||
Line 264: | Line 276: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program | ||
− | + | Please open the terminal by pressing '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously on your keyboard. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | ||
− | '''gcc | + | '''gcc tokens.c -o tok''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type, |
− | + | '''gcc tokens.c -o tok''' | |
+ | |||
+ | press''' Enter''' | ||
|- | |- | ||
Line 282: | Line 292: | ||
'''./var''' | '''./var''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute type, |
'''./tok''' | '''./tok''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
+ | |||
+ | '''Output''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed. | ||
We can see that here we have six values after the decimal point. | We can see that here we have six values after the decimal point. | ||
− | And here we have only two values | + | And here we have only two values. |
|- | |- | ||
Line 305: | Line 317: | ||
It denotes that we can print only two values after the decimal point. | It denotes that we can print only two values after the decimal point. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here I want an output with three decimal places. | ||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Replace | ||
− | + | '''%.2f with %.3f''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us replace %.2f with %.3f | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us replace %.2f with %.3f | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save''' |
Come back to the terminal | Come back to the terminal | ||
Line 331: | Line 336: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Compile as before |
− | execute | + | execute as before |
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | |||
'''1.500''' | '''1.500''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see here three values after the decimal point. |
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++ | ||
− | + | Come back to our program. | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will change a few things here. |
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First press '''shift+ctrl+s''' keys simultaneously on your keyboard. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now save the file with an extension '''.cpp '''and click on''' save.''' |
|- | |- | ||
Line 362: | Line 368: | ||
'''<nowiki><iostream></nowiki>''' | '''<nowiki><iostream></nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us change the header file as |
'''<nowiki><iostream></nowiki>''' | '''<nowiki><iostream></nowiki>''' | ||
Line 370: | Line 376: | ||
'''using namespace std;''' | '''using namespace std;''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now include the '''using '''statement |
+ | |||
+ | And click on '''Save.''' | ||
|- | |- | ||
Line 378: | Line 386: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement | ||
− | Since we use '''<nowiki>cout<< function</nowiki>''' to print a line in C++ | + | Since we use '''<nowiki>cout<< function</nowiki>''' to print a line in '''C++''' |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on S'''earch for and replace text option''' | ||
+ | |||
+ | '''printf(''' | ||
+ | |||
+ | '''<nowiki>cout <<</nowiki>''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on S'''earch for and replace text option''' | ||
+ | |||
+ | '''Type here printf opening bracket “(”''' | ||
+ | |||
+ | '''And here in this column type,''' | ||
+ | |||
+ | '''<nowiki>cout and two opening angle brackets “<<”.</nowiki>''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| click on '''Replace All '''option and '''Close.''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Replace All '''and click on '''Close.''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| %d | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| %d | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We don't need the '''format specifier''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We don't need the '''format specifier''' and '''\n ''' |
− | + | Let us delete them. | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now delete the comma and |
− | + | Type two opening angle brackets. | |
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' | ||
− | |||
− | + | '''delete (''' | |
− | |||
− | |||
− | |||
− | + | '''<nowiki>Type << “\n”</nowiki>''' | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save''' | ||
− | + | Now delete the closing brackets. | |
+ | |||
+ | Type two opening brackets again. | ||
+ | |||
+ | And within the double quotes type '''\n''' | ||
+ | |||
+ | Now Click on '''Save''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program. |
− | Come back to the terminal | + | Come back to the terminal. |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type |
+ | |||
+ | |||
+ | '''gcc tokens.c -o tok1''' | ||
+ | |||
+ | |||
+ | '''Type''' | ||
+ | |||
+ | '''./tok1''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type | ||
− | + | To compile type: | |
− | + | '''gcc tokens.c -o tok1''' | |
− | + | Here we have '''tok1 ''' | |
− | + | Because we don't want to overwrite the output parameter''' tok '''for the file''' tokens.c''' | |
− | ''' | + | Now press''' Enter''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute |
+ | |||
+ | Type | ||
+ | |||
+ | '''./tok1''' | ||
+ | |||
+ | press''' Enter ''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | '''Output''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed. | ||
|- | |- | ||
Line 440: | Line 490: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us move on to the common errors which we can come across. |
− | + | Come back to our program. | |
− | I will | + | Suppose here I will reassign a new value to '''b as 8.''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save.''' |
+ | |||
+ | Let us see what happens. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our terminal. |
+ | |||
+ | Let me clear the prompt. | ||
− | + | Now compile as before. | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | |||
− | |||
− | ''' | + | '''error''' |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see an error at line no.7 in our '''tokens.cpp''' file. | ||
− | + | Assignment of read only variable '''b'''. | |
− | + | ||
− | + | Come back to our program | |
− | ''' | + | This is because '''b''' is a constant. |
− | + | ||
− | + | Constants have fixed values. | |
− | + | They do not change during the execution of program. | |
− | + | ||
− | + | Hence it is giving an error. | |
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error. |
− | + | Delete this. | |
− | + | ||
− | + | ||
− | + | Click on '''Save''' | |
+ | |||
+ | Let us execute again | ||
+ | |||
+ | Come back to our terminal | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Compile and Execute |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Compile as before. | ||
− | + | Execute as before. | |
− | + | ||
− | + | Yes it is working. | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see another common error. |
− | + | Switch back to our program. | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here I will miss the single quotes. |
+ | |||
+ | Click on '''Save''' | ||
+ | |||
+ | let us execute. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our terminal. |
+ | |||
+ | Compile as before. | ||
|- | |- | ||
Line 518: | Line 573: | ||
− | |||
− | |||
− | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| we see an error at line no.9 in our '''tokens.cpp''' file. | |
− | + | '''A''' was not declared in the scope. | |
− | + | Come back to our program. | |
+ | |||
+ | This is because anything within the single quotes is considered as a character value. | ||
+ | |||
+ | And here we have declared '''d''' as a character variable. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error. |
+ | |||
+ | Type single quotes at line no.9 here. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save''' | ||
− | + | Come back to our terminal | |
− | + | ||
− | + | Compile as before | |
− | + | ||
− | + | Execute as before. | |
+ | |||
+ | Yes it is working. | ||
+ | |||
+ | Let us switch back to our slides. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize | ||
− | + | In this tutorial we learnt, | |
− | + | ||
− | + | Data types | |
+ | |||
+ | eg. int, double, float etc. | ||
+ | |||
+ | Variables | ||
+ | |||
+ | eg. int a=2; | ||
+ | |||
+ | Identifiers | ||
+ | |||
+ | eg. printf() | ||
+ | |||
+ | Constatnt | ||
+ | |||
+ | eg. double const b=4; | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 11 |
Line 561: | Line 638: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 12 |
Line 574: | Line 651: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 13 |
Spoken Tutorial Workshops | Spoken Tutorial Workshops | ||
Line 588: | Line 665: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 14 |
Revision as of 18:04, 29 January 2014
Title of script: Tokens in C
Author: Ashwini R. Patil
Keywords: Tokens, Data types, Variables, Identifiers, Constant, Video Tutorial
|
|
---|---|
Slide 1 | Welcome to the spoken tutorial on Tokens in C and C++. |
Slide 2
|
In this tutorial we will learn how,
To define and use tokens. We will do this with the help of an example. We will also see some common errors and their solutions. |
Slide 3
|
To record this tutorial, I am using
Ubuntu Operating system version 11.10. gcc and g++ Compiler version 4.6.1 on Ubuntu. |
Slide 4 | Let us start with an introduction.
Token is a generic word for Data types, Variables, Constants and Identifiers. |
Let us switch to the file variable.c
|
Let us start with the program
I have already typed the code on the editor Let me open it |
Point the cursor on tokens.c | Note that our filename is tokens.c |
In this program we will initialize the variables and print their values. | |
Let me explain the code now | |
#include<stdio.h>
int main() |
This is our header file.
This is our main function. |
Highlight
int a = 2; |
Here, int is a keyword
The compiler knows the meaning of keywords. |
Highlight
int a |
a is an integer variable |
Highlight
int a = 2; |
We have assigned a value of 2 to it.
This is called as initialization. |
If a value is not assigned to a variable then it is called as declaration of the variable. | |
Highlight
double const b = 4 |
Here, b is a constant.
We have initialized b, by assigning a value of 4 to it. |
Highlight
double const b = 4; |
const keyword is used to create read only variable |
Let us switch back to our slides to know more about keywords and constant | |
Slide 5 | Keywords have fixed meanings that cannot be changed
Keywords cannot be used as variable names There are 32 keywords available in C To name some, auto, break, case, char, const, default, enum extern, etc. |
Slide 6 | Constants are fixed values.
They do not change during the execution of a program. There are two types of constants. Numeric constants. Character constants. |
Come back to our program. | |
Highlight
float c |
Here, float is a data type of c. |
Highlight
float c = 1.5; |
We have assigned it a value, 1.5.
Data type is a finite set of values along with a set of rules. |
Highlight
char d = 'A'; |
Here, d is a variable
Char and single quotes suggest that we are dealing with a character As a result, d is a character variable storing the value 'A' |
Highlight
|
It is easy to see that int, double, float and char are datatypes.
a, c and d are variables. Now come back to our slides. We will know more about datatypes and variables. |
Slide 7 | Let us begin with integer data type
It is declared as int If we want to print an integer, we will use %d as the format specifier Similarly, we will use float and %f for floating point numbers For character data type, we use char and %c For double data type, we use double and %lf. |
Slide 8 | Now we will see the range of data types
Integer value has a . range of this. Floating point has a range of this. Character has a range of this. Double has a range of this. The values stored in the variable must not be greater or less than the range. Now we will move on to variables. |
Slide 9 | Variable is a data name.
It may be used to store a data value . The values can change when a program runs. Before using a variable it must be declared. We should try to give meaningful names to variables example john, marks, sum etc. |
On the editor | Now we will move back to our program. |
Highlight
printf("The Value of a is %d\n", a); |
Here, printf is the identifier name for this function
|
On the slides | Come back to our slides to know more about our identifiers. |
Slide 10 | Identifiers are user defined names
An identifier consists of letters and digits Both uppercase and lowercase letters are permitted First character must be an alphabet or underscore. |
On the editor | Now come back to our program. |
Here we have initialized the variables and constants.
Here we print them. | |
And this is our return statement. | |
Ctrl, Alt and T keys simultaneously | Let us execute the program
Please open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
Type
gcc tokens.c -o tok |
To compile type,
gcc tokens.c -o tok press Enter |
Type
./var |
To execute type,
./tok |
Highlight
Output |
The output is displayed.
We can see that here we have six values after the decimal point. And here we have only two values. |
Now let us find out how this happened. | |
Highlight
printf("The Value of c is %.2f", c); |
This is because we have %.2f here.
It denotes that we can print only two values after the decimal point. |
Type | Suppose here I want an output with three decimal places. |
Replace
%.2f with %.3f |
Let us replace %.2f with %.3f |
Click on Save | Now click on Save
Come back to the terminal |
On the terminal | Compile as before
execute as before |
Highlight
1.500 |
We see here three values after the decimal point. |
NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
Come back to our program. | |
I will change a few things here. | |
First press shift+ctrl+s keys simultaneously on your keyboard. | |
Now save the file with an extension .cpp and click on save. | |
Type
<iostream> |
Let us change the header file as
<iostream> |
Type
using namespace std; |
Now include the using statement
And click on Save. |
Type
cout<< |
Now replace the printf statement with cout statement
Since we use cout<< function to print a line in C++ |
Click on Search for and replace text option
printf( cout << |
Click on Search for and replace text option
Type here printf opening bracket “(” And here in this column type, cout and two opening angle brackets “<<”. |
click on Replace All option and Close. | Now click on Replace All and click on Close. |
%d | We don't need the format specifier and \n
Let us delete them. |
Now delete the comma and
Type two opening angle brackets. | |
Click on Save
|
Click on Save
Now delete the closing brackets. Type two opening brackets again. And within the double quotes type \n Now Click on Save |
On the terminal | Let us execute the program.
Come back to the terminal. |
Type
./tok1 |
Type
To compile type: gcc tokens.c -o tok1 Here we have tok1 Because we don't want to overwrite the output parameter tok for the file tokens.c Now press Enter |
To execute
Type ./tok1 press Enter | |
Highlight
Output |
The output is displayed. |
Errors
%d
|
Now let us move on to the common errors which we can come across.
Come back to our program. Suppose here I will reassign a new value to b as 8. |
Click on save | Click on Save.
Let us see what happens. |
On the terminal | Come back to our terminal.
Let me clear the prompt. Now compile as before. |
Highlight
|
We see an error at line no.7 in our tokens.cpp file.
Assignment of read only variable b. Come back to our program This is because b is a constant. Constants have fixed values. They do not change during the execution of program. Hence it is giving an error. |
Click on Save | Let us fix the error.
Delete this. Click on Save Let us execute again Come back to our terminal |
Compile and Execute | Compile as before.
Execute as before. Yes it is working. |
Now we will see another common error.
Switch back to our program. | |
Suppose here I will miss the single quotes.
Click on Save let us execute. | |
On the terminal | Come back to our terminal.
Compile as before. |
Highlight
|
we see an error at line no.9 in our tokens.cpp file.
A was not declared in the scope. Come back to our program. This is because anything within the single quotes is considered as a character value. And here we have declared d as a character variable. |
Let us fix the error.
Type single quotes at line no.9 here. | |
Click on Save
Come back to our terminal Compile as before Execute as before. Yes it is working. Let us switch back to our slides. | |
Let us summarize
In this tutorial we learnt, Data types eg. int, double, float etc. Variables eg. int a=2; Identifiers eg. printf() Constatnt eg. double const b=4; | |
Slide 11
|
As an assignment
Write a C program to calculate the simple interest. Hint: Simple Interest = principal * rate * time / 100 |
Slide 12
|
Watch the video available at
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it. |
Slide 13
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, contact us at the following id contact@spoken-tutorial.org |
Slide 14
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
Ashwini Patil from IIT Bombay
Thank You for joining |