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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 131: Line 131:
 
|-
 
|-
 
| 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 6
| 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.
+
| 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.
 
They do not change during the execution of a program.

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