Difference between revisions of "C-and-C++/C2/Tokens/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
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;"
| style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
+
! <center>Visual Cue</center>
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</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
+
| 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
  
gcc and g++ Compiler version 4.6.1 on Ubuntu
+
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'''
+
 
+
  
 +
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 program on the editor
+
I have already typed the code on the editor
  
So i will open it
+
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;"| Now, let us swtich back to our slides.
+
| 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.
  
The syntax for initilization is,
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
datatype varname = value;
+
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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Datatype defines the type of the variable.
+
  
varname is the name of the variable.
+
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
  
We can assign it a value 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;"| 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 4
+
| 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;"| Come back to our program.
+
| 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.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
double const '''b '''<nowiki>= </nowiki>'''4;'''
+
There are two types of''' constants.'''
| 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.
+
'''Numeric constants.'''
  
|-
+
'''Character constants.'''
| 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;"| 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.'''
  
Let us now switch back to the slides and know more about '''constants, datatypes''' and '''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 5
+
| 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 to the slides to know more about constants
+
 
+
'''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:none;padding:0.097cm;"| Slide 6
+
| 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 7
+
| 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'''
  
(We '''CANNOT''' store a value in the '''variable''' above or below the range of its '''data type''')
+
'''Integer''' value has a .
  
The value stored in the '''variable''' must not be greater or less than the range
+
range of this.
  
'''Integer''' value has a  
+
'''Floating point''' has a range of this.
  
range of '''-32,768 to 32,767'''
+
'''Character''' has a range of this.
  
'''Floating point''' has a range of '''3.4E +/-38 '''
+
'''Double''' has a range of this.  
  
'''Character''' has a range of '''-128 to 127'''
+
The values stored in the '''variable''' must not be greater or less than the range.
  
'''Double''' has a range of '''1.7E +/-308'''
+
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 8
+
| 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.
  
It must be declared before using a '''variable'''  
+
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 come back to our program.
+
| 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:1pt solid #000000;padding:0.097cm;"| Here,''' printf''' is the '''identifier''' name for this function
 
 
These cannot be used as keywords.
 
  
 
|-
 
|-
| 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;"| Let us go 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: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 9
+
| 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;"| Come back to our program
+
| 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.
  
Click on '''Save '''to save the program
+
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
  
Let us open the terminal by pressing '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously.
+
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;"|
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program
+
  
 
|-
 
|-
 
| 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 variable.c -o var'''
+
'''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;"| Type '''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;"| To compile type,
  
and press''' Enter'''
+
'''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 this file, let us type
+
| 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 after the decimal point.
+
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;"| Suppose 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;"|
+
 
| 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;"| 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;"| 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;"| Now let us compile using the same command we used before
+
| 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 the program
+
execute as before
 
+
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;"| 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 output as '''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 here three values after the decimal point.
  
We can 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++
  
Let us go back to the program.
+
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;"| NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
+
| 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.
  
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: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;"| Let me change a few things here
+
| 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;"| First we will change the header file  
+
| 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;"| We will include the '''using '''statement here
+
| 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''' here
+
| 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 '''
  
So let us delete it
+
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 type two opening angle brackets here '''<nowiki><<a</nowiki>'''
+
| 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
  
let us type \n here so again type two opening brackets
+
Type two opening angle brackets.
  
Within the double quotes type \n
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
  
Now delete this bracket.
 
  
Same way here also.
+
'''delete ('''
  
|-
 
| 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 go to '''File'''
 
  
click on '''Save as'''
+
'''<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'''
  
Save the file''' '''with''' .cpp '''extension
+
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 compile
+
| 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  
  
'''g++ variable.cpp -o var'''
+
To compile type:
  
press''' Enter'''
+
'''gcc tokens.c -o tok1'''
  
To execute
+
Here we have '''tok1 '''
  
Type
+
Because we don't want to overwrite the output parameter''' tok '''for the file''' tokens.c'''
  
'''./var'''
+
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;"| Here 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;"| 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 we will see the common errors which we can come across
+
| 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.
  
Suppose here we type the %d in the place of %f
+
Come back to our program.
  
I will retain rest of the code as it is
+
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;"| save the program
+
| 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;"| Let us go to the terminal
+
| 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.
  
Let us compile the program
+
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
  
'''format specifier ‘%d’ expects type ‘int’, but argument 2 has type ‘double’'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| we see a warning
 
  
'''format specifier''' '''‘%d’''' expects type '''‘int’''', but argument 2 has type '''‘double’'''
+
'''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'''.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Error 2
+
  
Highlight
+
Come back to our program
  
'''a'''
+
This is because '''b''' is a constant.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see another common error which we can come across.
+
  
Here we have declared '''a''' as '''integer'''
+
Constants have fixed values.
  
|-
+
They do not change during the execution of program.  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
  
'''8'''
+
Hence it is giving an error.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will assign a new value to '''a'''.
+
  
 
|-
 
|-
| 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;"| save 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 fix the error.
  
|-
+
Delete this.
| 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 compile it
+
  
Switch to the terminal
+
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;"| Highlight
+
| 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.
  
'''a = 8;'''
+
Execute as before.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see that the output is displayed as the value of '''a''' is '''8.'''
+
  
The new value is printed.
+
Yes it is working.
  
 
|-
 
|-
| 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;"|  
| 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 the program
+
| 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;"| '''b = 9;'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now suppose here i will change the value of b
+
  
 
|-
 
|-
| 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;"|  
| 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 switch back to the terminal
+
| 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;"| Let us compile
+
| 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:
  
  
'''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 that there is an error
 
  
As b is an integer constant variable
+
| 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.
  
It is a read only variable
+
'''A''' was not declared in the scope.
  
We cannot change the value of b.
+
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 go back to our 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 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;"| Delete
+
| 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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here we forget the single quotes, let us see what happens
+
  
|-
+
Compile as before
| 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;"| Switch to the terminal Let us compile and run
+
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;"| Highlight
+
| 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
  
'''Z is undeclared'''
+
In this tutorial we learnt,
| 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 that Z is undeclared
+
  
As anything within single quotes is considered as a '''character''' value
+
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 10
+
| 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 11
+
| 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 12
+
| 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 13
+
| 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


Visual Cue
Narration
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


a and c

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


delete (


Type << “\n”

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


gcc tokens.c -o tok1


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


Type

%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


error

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


Assignment

As an assignment

Write a C program to calculate the simple interest.

Hint: Simple Interest = principal * rate * time / 100

Slide 12


About the Spoken Tutorial Project

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


Acknowledgement

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

Contributors and Content Editors

Ashwini, PoojaMoolya