C-and-C-Plus-Plus/C2/Tokens-in-C-and-C-Plus-Plus/English

From Script | Spoken-Tutorial
Revision as of 13:45, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 Data Types & Variables
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.

What is a token?

Token is a generic word for Data types, Variables, Constants and Identifiers

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

Let us switch to the file variable.c


Let us start with the program

I have already typed the program on the editor

So i will open it

Let me explain the code now
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.

Now, let us swtich back to our slides.

The syntax for initilization is,

datatype varname = value;

Datatype defines the type of the variable.

varname is the name of the variable.

We can assign it a value here.

Slide 4 Keywords have fixed meanings that cannot be changed

Keywords cannot be used as variable names

There are 32 keywords available in C

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

Come back to our program.
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
Highlight

float c

Here, float is a data type of c
Highlight

float c = 1.5;

We have assigned it a value, 1.5

Data type is a finite set of values along with a set of rules

Highlight

char d = 'A';

Here, d is a variable

Char and single quotes suggest that we are dealing with a character

As a result, d is a character variable

storing the value 'A'

Highlight


a and c

It is easy to see that int, double float and char are datatypes.

a, c and d are variables

Let us now switch back to the slides and know more about constants, datatypes and variables

Slide 5 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

Slide 6 Let us begin with integer data type

It is declared as int

If we want to print an integer, we will use %d as the format specifier

Similarly, we will use float and %f for floating point numbers

For character data type, we use char and %c

For double data type, we use double and %lf.

Slide 7 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)

The value stored in the variable must not be greater or less than the range

Integer value has a

range of -32,768 to 32,767

Floating point has a range of 3.4E +/-38

Character has a range of -128 to 127

Double has a range of 1.7E +/-308

Slide 8 Variable is a data name

It may be used to store a data value

The values can change when a program runs

It must be declared before using a variable

We should try to give meaningful names to variables

example john, marks, sum etc.

On the editor Now come back to our program.
Highlight

printf("The Value of a is %d\n", a);


Here, printf is the identifier name for this function

These cannot be used as keywords.

Let us go back to our slides to know more about our identifiers.
Slide 9 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.

Come back to our program

Click on Save to save the program

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

Let us open the terminal by pressing Ctrl, Alt and T keys simultaneously.

To compile the program
Type

gcc variable.c -o var

Type gcc tokens.c -o tok

and press Enter

Type

./var

To execute this file, let us type

./tok

The output is displayed.

We can see that here we have six values after the decimal point.

And here we have only two values after the decimal point.

Now let us find out how this happened.
Highlight

printf("The Value of c is %.2f", c);

This is because we have %.2f here.

It denotes that we can print only two values after the decimal point.



Type


Suppose I want an output with three decimal places.



Let us replace %.2f with %.3f
Click on Save

Come back to the terminal

On the terminal Now let us compile using the same command we used before

execute the program

Come back to the terminal

Highlight


1.500

We see the output as 1.500

We can see here three values after the decimal point.

Let us go back to the program.

NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++

Come back to our program

Let me change a few things here
Type

<iostream>

First we will change the header file

<iostream>

Type

using namespace std;

We will include the using statement here
Type

cout<<

Now replace the printf statement with cout statement

Since we use cout<< function to print a line in C++

 %d We don't need the format specifier here

So let us delete it

Now type two opening angle brackets here <<a

let us type \n here so again type two opening brackets

Within the double quotes type \n

Now delete this bracket.

Same way here also.

Now go to File

click on Save as

Save the file with .cpp extension

Let us compile

Come back to the terminal

Type

g++ variable.cpp -o var

press Enter

To execute

Type

./var

Here the output is displayed
Errors


Type

%d


Now we will see the common errors which we can come across

Suppose here we type the %d in the place of %f

I will retain rest of the code as it is

Click on save save the program
On the terminal Let us go to the terminal

Let us compile the program

Highlight

format specifier ‘%d’ expects type ‘int’, but argument 2 has type ‘double’

we see a warning

format specifier ‘%d’ expects type ‘int’, but argument 2 has type ‘double’

Error 2

Highlight

a

Let us see another common error which we can come across.

Here we have declared a as integer

Type

8

I will assign a new value to a.
Click on save save the program.
On the terminal let us compile it

Switch to the terminal

Highlight

a = 8;

We see that the output is displayed as the value of a is 8.

The new value is printed.

On the editor Let us switch back to the program
b = 9; Now suppose here i will change the value of b
Click on save click on save switch back to the terminal
On the terminal Let us compile
Highlight


error

We see that there is an error

As b is an integer constant variable

It is a read only variable

We cannot change the value of b.

Let us go back to our program
Delete

' '

Suppose here we forget the single quotes, let us see what happens
On the terminal Switch to the terminal Let us compile and run
Highlight

Z is undeclared

we see an error that Z is undeclared

As anything within single quotes is considered as a character value

Slide 10


Assignment

As an assignment

Write a C program to calculate the simple interest.

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

Slide 11


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 12

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 13


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, Chandrika