KEMBAR78
Tokens - Variables, Datatypes & Constants | PDF | Data Type | Integer (Computer Science)
0% found this document useful (0 votes)
18 views23 pages

Tokens - Variables, Datatypes & Constants

The document provides an overview of tokens in the C programming language, including keywords, identifiers, constants, and data types. It explains the rules for creating identifiers, the importance of variable declaration, and categorizes data types into built-in, derived, and user-defined. Additionally, it covers the types of constants and their representations in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Tokens - Variables, Datatypes & Constants

The document provides an overview of tokens in the C programming language, including keywords, identifiers, constants, and data types. It explains the rules for creating identifiers, the importance of variable declaration, and categorizes data types into built-in, derived, and user-defined. Additionally, it covers the types of constants and their representations in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Course

Problem Solving & Programming in C


Topic
Tokens

Dr. B. Srinivasan
Assistant Professor, School of Computing
SASTRA Deemed To Be University
C Token
 Smallest Individual Unit in a C Program is known as Token

 Means that a program is constructed using a combination of these tokens

Tokens in C

Keywords Constants Operators

Identifiers Strings Special Symbols

2
C Token Keywords : include

Keywords : int

Identifier : a, b, c
(Variable Names)

Identifier :
printf
Scanf
scanf String : Message represented
main in double quote
(Function Names)

Special Symbols : , ; # { } ( ) /
Operator : +, =,
= &
Keywords : return

3
Constant : 0
Keywords
 Keywords are reserved words in C that have special meaning to the compiler.

 We cannot use them as identifiers (like variable or function names).

 There are 32 Keywords in C language

4
Identifiers
 Name given for a variable and functions by the programmer, is usually known as
Identifiers in C program

 Identifiers are names used to refer to program elements like variables and
functions

 An identifier is a programmer-defined name for program elements.

 Example: In the above program, the variables names “a”, “b” and “c”, the functions
names “main”, “printf”, “scanf” are termed as Identifiers

 Here, printf and scanf are built-in available in C language. Similar to this, as a
programmer, we can define our own functions for performing tasks. In this case,
the name for the functions should be given by the programmer.

 For giving names for the variables and functions, the programmer must follow the
following RULES.
5
Identifiers - Rules
 Identifier can contain following characters:

o Uppercase (A-Z) and lowercase (a-z) alphabets.

o Numeric digits (0-9).

o Underscore (_).

 The first character of an identifier must be a alphabets or an underscore.

 Identifiers cannot be keywords in C (such as int, return, if, while etc.).

 White spaces are not allowed.

 Identifiers are case-sensitive.

 In most of the cases, the identifiers are unique in a program

Clean Coding Practice: To maintain the coding standards, it is advised to use meaningful and
descriptive variable names and function name. Example: employeeId, registerNo, studentName etc.,
6
Identifiers - Rules

 Since, identifiers are case-sensitive, mark1 and Mark1 are considered as


two different identifiers
 Within a scope (usually a block), the variable names are unique, so we
7 cannot declare a variables like: int m1; float m1;
Variables, Declaration & Datatypes
 While solving a problem by writing a C program, we are in
need of memory spaces (storage) for storing input values, This statement is called as
output values and some temporary values during program variable declaration statement
execution.
 For example, in “addition of two numbers” problem, the
input are two numbers, and the output is one number. In
this case we need memory space for storing these three
values (2 input and 1 output).
 VARIABLE is nothing but a container or a storage space
for storing values during program execution. A variable
usually have a name to be given by the programmer.
 VariableDECLARATION is the process of allocating
memory space for the variables.
 During this, the system will allocate memory required for
your program

8
Some Valid Declaration of Variables

 Here, the words, “float”, “int”, “double” and “char” are keywords, known as datatypes
in C language

 Need to follow the rules of Identifiers strictly, while giving the name for the variables.

 The declaration statements must be at the beginning of main() block in C language

9
Variables, Declaration & Datatypes
 For declaring variables, we need to specify the type of the data to be stored in the memory.

 In computer language, each and every data is categorized in different types. It is some what differed
from how we are categorizing data in our human language.

Some Examples:
 The data 10 is known as whole number in our
language. The same data is known as “integer”
type of data in C language.
 The data 10.5 is a decimal number in our
language. The same data 10.5 is know as
“double” type of data in C language.
 The data A is known as alphabet in our
language. It is known as “character“ type in C
language.

 So, it is mandatory to know the categorization of data in C language, for the programmer.

 DATATYPE – type of the data to be stored in a variable in C language


10
Datatypes
 DATATYPE – type of the data to be stored in a variable in C language

 Datatypes in C language is categorized in three major categories.

Datatypes in C

Basic or Built-in Derived User-Defined


Datatypes Datatypes Datatypes
Built-in available in Datatype derived from Programmer can define
C language the existing datatype their own datatypes in
following ways
int int *  Pointer
float typedef
int []  Array enum
char
11 void struct
union
Basic or Built-in Datatypes
To Store Number
without decimal Basic Datatypes Used to indicate the
absence of value

Integer Float Character Void

int float char void

short int double


To Store a
character Variables
long int long double of void
To Store Number data type
long long int with decimal are not
12 allowed
Datatypes – Built-in Datatypes

13
Datatypes – Built-in Datatypes
 The format specifier is used in input/output statements (scanf & printf) to represent
the corresponding type values.
 The basic datatypes are available in optional unsigned type.
 The default specification of datatype like “int” is signed datatype.
 It can also specified explicitly with “signed” keyword as “signed int”.
 Signed : Can store both POSITIVE and NEGATIVE numbers
 Unsigned : Allowed to store ONLY POSITIVE numbers

Point to Note
Most used datatypes in solving
simple computational problems are
(“int”) and (“float” or “double”)
14
Constants or Literals
 If we specified any number directly usually referred as Constants
 Constant in C: A fixed value that cannot be changed and remains the same
throughout the program
 Variable in C: A named storage that can store and change data

Constant
Type: Integer

Variable
Type: Float
Constant
Type: Double
Variable
Type: Float

 Like variables, the constants are also categorized into different types in C Language
 To obtain the exact result, we must know the different types of constants also.
15
 Constants can also referred as “Literals”
Constants - Types
Constants in C Language Sequence of characters represented
within double quote

Number with
decimal values.

Integer Character String Float


Float
Number without Any single character
decimal values. represented within single quote
Double
Integer Decimal Double

Long Octal Unsigned

Long Long Hexadecimal


16
Types of Integer Constants in C
Decimal Constants Unsigned Constants
 Base 10 Number  Base 10 or Base 16 or Base 8 Numbers
 Allowed digits are 0 to 9  Allowed Special Symbol is only ‘+’
 Allowed Special Symbols ‘+’ and ‘-’  Must Ends with ‘U’ or ‘u’ to indicate the
 Default integer constant constant is unsigned type
Long and Long Long Constants
Octal Constants
 Base 8 Number  Ends with ‘L’ or ‘l’ to indicate the
 Allowed digits are 0 to 7 constant is Long Integer
 Allowed Special Symbols ‘+’ and ‘-’  Ends with ‘LL’ or ‘ll’ to indicate the
 Begins with the digit ‘0’ (zero) constant is Long Long Integer

Hexadecimal Constants Unsigned Long / Long Long Constants


 Base 16 Number  Ends with ‘UL’ or ‘LU’ to indicate the constant
 Allowed digits are 0 to 9 is unsigned Long Integer
 Allowed alphabets ‘a’ to ‘f’ and ‘A’ to ‘F’  Ends with ‘LLU or ‘ULL’ to indicate the constant
is unsigned Long Long Integer
 Allowed Special Symbols ‘+’ and ‘-’
 Can also use Lower case letters ‘u’ and ‘l’
17  Begins with the digit ‘0’ (zero)
Integer Constants - Examples

18
Types of Floating Point Constants in C
Float Constants
 Allowed digits are 0 to 9
 Allowed a decimal Point
 Allowed Special Symbols ‘+’ and ‘-’
 Must ENDS with ‘F’ or ‘f’ to indicate the
constant is FLOAT.
 Allowed ‘e’ of ‘E’ to represent a number
in exponential format.
Double Constants
 No suffix defaults to double type
Long Double Constants
 A double constant ENDS with ‘L’ or ‘l’ is
considered as LONG DOUBLE constant.
Exponential Representation
 Allowed ‘e’ of ‘E’ to represent a number
19 in exponential format.
Character Constants in C
Definition List of Escape Sequences
 A character constant represents a single
character enclosed in single quotes (‘ ‘)
Examples

Stored As
 Internally stored as an integer value
(ASCII code)
 ‘A’ is stored as 65
Escape Sequences
 Special characters are represented using
backslash (\) escape sequences.
20  Example ‘\n is newline character
String Constants in C
Definition
 A string constant (or string literal) is a sequence of characters enclosed in double quotes (“ “)
 Represented as array of character type: char[].
 Can include escape sequences to represent special characters.
 Strings cannot span multiple lines directly

Examples
• “Thanjavur”
• “SASTRA University”
• “1283”
• “s”
Stored As
 Always ENDS with an invisible null character ‘\0’ to mark the end of the string
 The string constant “Hi How are you” is stored in memory as:

21
Summary

 Token – Individual Unit in C program


 Identifier – Names of Variables and Functions, given by the programmer
 Keyword – Fixed words in C language cannot be used as Identifiers
 Variable – Named Storage (or Container) for storing data of different type
 Datatypes – Type of a data in C language like int, float and double etc.,
 Constants – Data directly specified in a C program

22
…Thank You…

23

You might also like