You are on page 1of 29

Tokens in C

 KIIT 2014
Objectives
After completing this lesson, you should be able to do the following:
• Identify various tokens in C language
• List and describe various data types
• List the uses of variables
• Declare and initialize variables
• Define constants and literals
• Use of storage classes
• Identify and use of operators

 KIIT 2014
Tokens in C
A C program consists of various tokens and a token is either a
keyword, an identifier, a constant, a string literal, or a symbol.
For example, the following C statement consists of five tokens::
• Keyword
• Identifier
• Constant
• String Literals
• Symbols

 KIIT 2014
Token - Example

C statement consists of five tokens:

printf("Hello, World! \n");

The individual tokens are:


printf
( "
Hello, World! \n"
);

 KIIT 2014
Semicolons
In C program, the semicolon is a statement terminator.

Example:
printf("Hello, World! \n");
return 0;

 KIIT 2014
Comments
Comments are like helping text in your C program and they are
ignored by the compiler.

Example:
/* my first program in C */

 KIIT 2014
Identifiers
Identifiers are used for:
• Naming a variable
• Providing a convention for variable names:
– Must start with a letter
– Can include letters or numbers
– Can’t include special characters such as dollar sign,
percentage and pound sign
– Must not be keywords

mohd zara abc move_name a_123


myname50 _temp j a23b9 retVal

 KIIT 2014
Keywords
The following list shows the reserved words in C:
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double

 KIIT 2014
Whitespace in C
A line containing only whitespace, possibly with a
comment, is known as a blank line, and a C compiler
totally ignores it.

int age;

fruit = apples + oranges; // get the total fruit

 KIIT 2014
Data Types in C
Refer to an extensive system used for declaring variables
or functions of different types. The type of a variable
determines how much space it occupies in storage and
how the bit pattern stored is interpreted.
The types in C can be classified as follows:
• Basic Types
• Enumerated Types
• The Type Void
• Derived Types

 KIIT 2014
Integer Types
Standard integer types with its storage sizes and value
ranges:

 KIIT 2014
Floating - Point Types
Standard floating - point types with its storage sizes and
value ranges:

 KIIT 2014
C - Variables
A variable is nothing but a name given to a storage area
that programs can manipulate.

Syntax:
type variable_list;

Example:
int i, j, k;
char c, ch;
float f, salary;
double d;

 KIIT 2014
Variable Initialization
Variables can be initialized (assigned an initial value) in their
declaration. The initializer consists of an equal sign followed
by a constant expression as follows:
Syntax:

type variable_name = value;


Example:

extern int d = 3, f = 5; // declaration of d and


f.
int d = 3, f = 5; // definition and initializing d
and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.

 KIIT 2014
Variable Declaration

A variable declaration provides assurance to the compiler


that there is one variable existing with the given type and
name so that compiler proceed for further compilation
without needing complete detail about the variable.
A variable declaration has its meaning at the time of
compilation only, compiler needs actual variable
declaration at the time of linking of the program.

 KIIT 2014
Variable Declaration - Example
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main ()
{
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}

 KIIT 2014
// function declaration
int func();
int main()
{
// function call
int i = func();
}

// function definition
int func()
{
return 0;
}

 KIIT 2014
Variable Types

Based on the scope of the variable, can


be classified into:
• Local Variable
• Global Variable

 KIIT 2014
Local Variable

• Has local / limited scope


• Is accessible only within a function or block in which
it is declared
• Has higher precedence over global variable

 KIIT 2014
Local Variable
• Variables declared inside outer block are visible or meaningful only
inside outer block
• Similarly variables declared inside inner block are visible or meaningful
only inside inner block
• Variables declared inside inner block are not accessed by outer block

#include<stdio.h>
void main()
{
int var1=10;
{
int var2 = 20;
printf("%d %d",var1,var2);
}
printf("%d %d",var1,var2);
}

 KIIT 2014
Global Variable

• Has global scope and available throughout the


program
• Is also visible inside function, provided that it should
not be re-declared
• Can be accessed from any function

 KIIT 2014
Global Variable
• Inner block variables declared in outer block acts as “Global Variable”

#include<stdio.h>
int var=10;
void message();
void main()
{
int var=20;
{
int var = 30;
printf("%d ",var);
}
printf("%d ",var);
message();
}
void message()
{
printf("%d ",var);
}

 KIIT 2014
Attributes of Variable

Fundamentals attributes of C variable :


• Name of Variable
• Values inside Variable
• Address of Variable
• Size of Variable
• Type of Variable

 KIIT 2014
Constants and Literals

• The constants refer to fixed values that the program


may not alter during its execution. These fixed values
are also called literals.
• The constants are treated just like regular variables
except that their values cannot be modified after
their definition.
– Integer Literals
– Floating point Literals
– Character Constants
– String Literals

 KIIT 2014
Character Literals

Character literals can be


• A plain character
• An escape sequence

 KIIT 2014
Escape Sequence

 KIIT 2014
String Literals
String literals or constants are enclosed in double quotes
""
Example:
"hello, dear"

There are two simple ways in C to define constants:


• Using #define preprocessor.
• Using const keyword

const type variable = value;

 KIIT 2014
Operators

C provide following types of operators:


• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators

 KIIT 2014
Summary
In this lesson, you should have learned how to:
• Identify various tokens in C language
• List and describe various data types
• List the uses of variables
• Declare and initialize variables
• Define constants and literals
• Identify and use of operators

 KIIT 2014

You might also like