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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(7 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
|-
 
|-
 
| 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 26:
  
 
We will also see some common errors and their solutions.
 
We will also see some common errors and their solutions.
 
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3  
 +
  
  
 
| 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
 
| 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
+
Ubuntu Operating system version 11.10.
 
+
gcc and g++ Compiler version 4.6.1 on Ubuntu
+
  
 +
gcc and g++ Compiler version 4.6.1
  
 
|-
 
|-
| 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 51:
 
| 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 99:
 
|-
 
|-
 
| 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
  
 
'''Keywords''' cannot be used as '''variable''' names
 
'''Keywords''' cannot be used as '''variable''' names
  
There are 32''' keywords''' available in C
+
There are 32''' keywords''' in C
  
To name some, '''auto''', '''break''', '''case''', '''char''', '''const''',''' default''', '''enum''' '''extern''', etc.
+
To name some, '''auto''', '''break''', '''case''', '''char''', '''enum''' '''extern''', etc.
  
 
|-
 
|-
| 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
 +
'''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.''' and
  
|-
+
'''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;"|Now 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 variable '''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,of '''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;"|Datatypes Let us begin with '''integer data type'''
  
 
It is declared as '''int'''
 
It is declared as '''int'''
Line 189: Line 195:
 
Similarly, we will use '''float '''and '''%f '''for '''floating point''' numbers
 
Similarly, we will use '''float '''and '''%f '''for '''floating point''' numbers
  
For '''character''' '''data type''', we use '''char''' and '''%c'''
+
For '''character''' '''data type''', we will use '''char''' and '''%c'''
  
For '''double data typ'''e, we use '''double''' and '''%lf'''.
+
And for '''double data type''', we will use '''double''' and '''%lf''' as the format specifier.
  
 
|-
 
|-
| 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''' datatype value has a range of this.
  
The value stored in the '''variable''' must not be greater or less than the range
+
'''Floating point''' has a range of this.
  
'''Integer''' value has a  
+
'''Character''' has a range of this.
  
range of '''-32,768 to 32,767'''
+
And '''Double''' has a range of this.
  
'''Floating point''' has a range of '''3.4E +/-38 '''
+
The values stored in the '''variable''' must not be greater or less than this range.
  
'''Character''' has a range of '''-128 to 127'''  
+
Now we will move on to''' variables'''.
 
+
'''Double''' has a range of '''1.7E +/-308'''
+
  
 
|-
 
|-
| 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 231:
 
|-
 
|-
 
| 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 237:
  
 
'''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.
 +
Let us know about 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 255:
 
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;"|  
+
| 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;"| 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 come back to our program.
 
+
Click on '''Save '''to save the program
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously
+
| 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 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;"| Here we have initialized the variables and constants.
  
Let us open the terminal by pressing '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously.
+
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: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:1pt solid #000000;padding:0.097cm;"| And this is our return statement.
 +
Now click on '''Save'''
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously
 +
| 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 window 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 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 space tokens.c space -o space 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 two 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;"| Now let us find out how this happened.
 
| 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 find out how this happened.
 +
Come back to our program.
  
 
|-
 
|-
Line 305: Line 318:
  
 
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 337:
 
|-
 
|-
 
| 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 WE WILL 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 and 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 369:
  
 
'''<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 377:
  
 
'''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 387:
 
| 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 '''Search 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 '''Search 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 angle 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'''
+
'''g++ space tokens.c space -o space 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 491:
  
  
| 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;"|Now 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 are 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 574:
  
  
'''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;"|Now click on '''Save'''
 +
Let us execute.
  
'''' ''''
+
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
+
  
|-
+
Now 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.
 +
 
 +
Now 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 640:
  
 
|-
 
|-
| 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
  
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below
  
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
Line 574: Line 653:
  
 
|-
 
|-
| 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 583: Line 662:
 
Gives certificates to those who pass an online test  
 
Gives certificates to those who pass an online test  
  
For more details, contact us at the following id
+
For more details, Please write to
 
+
 
contact@spoken-tutorial.org
 
contact@spoken-tutorial.org
  
 
|-
 
|-
| 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
  
  
Line 596: Line 674:
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
  
More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
+
More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro
  
 
|-
 
|-

Latest revision as of 15:34, 30 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

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 in C

To name some, auto, break, case, char, enum extern, etc.

Slide 6 Constants

Constants are fixed values.

They do not change during the execution of a program.

There are two types of constants.

Numeric constants. and

Character constants.

Now Come back to our program.
Highlight

float c

Here, float is a data type of variable c.
Highlight

float c = 1.5;

We have assigned it a value,of 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 Datatypes 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 will use char and %c

And for double data type, we will use double and %lf as the format specifier.

Slide 8 Now we will see the range of data types

Integer datatype value has a range of this.

Floating point has a range of this.

Character has a range of this.

And Double has a range of this.

The values stored in the variable must not be greater or less than this 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.

Let us know about 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.

Now click on Save

Ctrl, Alt and T keys simultaneously Let us execute the program.

Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.

Type

gcc tokens.c -o tok

To compile type,

gcc space tokens.c space -o space 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 two values.

Now let us find out how this happened.

Come back to our program.

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 WE WILL EXECUTE THE SAME PROGRAM IN C++

Come back to our program.

I will change a few things here.
First press shift+ctrl and 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 angle 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:

g++ space tokens.c space -o space 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 Now 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 are 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.

Now click on Save

Let us execute.

Come back to our terminal

Now Compile as before

Execute as before.

Yes it is working.

Now 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 the link shown below

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, Please write to 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 the link shown below: http://spoken-tutorial.org\NMEICT-Intro

Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini, PoojaMoolya