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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 128: Line 128:
 
There are 32''' keywords''' available in C
 
There are 32''' keywords''' available in C
  
To name some, '''auto''', '''break''', '''case''', '''char''', '''const''',''' default''', '''enum''' '''extern''', etc.
+
To name some, '''auto''', '''break''', '''case''', '''char''', '''enum''' '''extern''', etc.
  
 
|-
 
|-
Line 138: Line 138:
 
There are two types of''' constants.'''
 
There are two types of''' constants.'''
  
'''Numeric constants.'''  
+
'''Numeric constants.''' and
  
 
'''Character constants.'''
 
'''Character constants.'''
Line 144: Line 144:
 
|-
 
|-
 
| 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;"|Now Come back to our program.
  
 
|-
 
|-
Line 150: 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 156: 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 187: Line 187:
 
|-
 
|-
 
| 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 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 195: 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.
  
 
|-
 
|-
Line 203: Line 203:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the range of '''data types'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the range of '''data types'''
  
'''Integer''' value has a .
+
'''Integer''' value has a range of this.
 
+
range of this.
+
  
 
'''Floating point''' has a range of this.
 
'''Floating point''' has a range of this.
Line 211: Line 209:
 
'''Character''' has a range of this.
 
'''Character''' has a range of this.
  
'''Double''' 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 the range.
+
The values stored in the '''variable''' must not be greater or less than this range.
  
 
Now we will move on to''' variables'''.
 
Now we will move on to''' variables'''.
Line 246: Line 244:
 
|-
 
|-
 
| 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: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;"| 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:1pt solid #000000;padding:0.097cm;"| Come back to our slides.
 +
Let us know about identifiers.
  
 
|-
 
|-
Line 271: Line 270:
 
| 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;"| And this is our return statement.
 
| 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: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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program.
  
 
Please open the terminal by pressing '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously on your keyboard.
 
Please open the terminal by pressing '''Ctrl,''' '''Alt''' and '''T keys '''simultaneously on your keyboard.
Line 284: Line 284:
 
| 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,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type,
  
'''gcc tokens.c -o tok'''
+
'''gcc space tokens.c space -o space tok'''
  
 
press''' Enter'''
 
press''' Enter'''
Line 304: Line 304:
 
We can see that here we have six values after the decimal point.
 
We can see that here we have six values after the decimal point.
  
And here we have only two values.
+
And here we have 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 348: Line 349:
 
|-
 
|-
 
| 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;"| NOW WE WILL EXECUTE THE SAME PROGRAM IN C++
  
 
Come back to our program.
 
Come back to our program.
Line 358: Line 359:
 
|-
 
|-
 
| 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;"| 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:1pt solid #000000;padding:0.097cm;"| First press '''shift+ctrl and s''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 389: Line 390:
  
 
|-
 
|-
| 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'''
+
| 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('''
 
'''printf('''
  
 
'''<nowiki>cout <<</nowiki>'''
 
'''<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'''
+
| 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 “(”'''
 
'''Type here printf opening bracket “(”'''
Line 456: Line 457:
 
To compile type:
 
To compile type:
  
'''gcc tokens.c -o tok1'''
+
'''gcc space tokens.c space -o space tok1'''
  
 
Here we have '''tok1 '''
 
Here we have '''tok1 '''
Line 498: Line 499:
 
|-
 
|-
 
| 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;"| 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.'''
  
 
Let us see what happens.
 
Let us see what happens.
Line 523: Line 524:
 
This is because '''b''' is a constant.
 
This is because '''b''' is a constant.
  
Constants have fixed values.
+
Constants are fixed values.
  
 
They do not change during the execution of program.  
 
They do not change during the execution of program.  
Line 592: Line 593:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''
+
| 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 our terminal
 
Come back to our terminal
  
Compile as before
+
Now Compile as before
  
 
Execute as before.
 
Execute as before.
Line 602: Line 603:
 
Yes it is working.
 
Yes it is working.
  
Let us switch back to our slides.
+
Now switch back to our slides.
  
 
|-
 
|-

Revision as of 11:39, 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 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, 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. 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 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 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 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 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

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

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