[go: up one dir, main page]

0% found this document useful (0 votes)
16 views27 pages

(Lect. 4) Token Variable Constant

Uploaded by

bv6226780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views27 pages

(Lect. 4) Token Variable Constant

Uploaded by

bv6226780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

C Language

UNIT-II
C Tokens
The smallest element in the C language is the token.
It may be a single character or a sequence of characters.

C Tokens

Identifiers Keywords Constants Strings operators spI


symbol
Eg:main, Eg: int, Eg:17, Eg: “ab” Eg: + Eg: #
avg for 15.5 - $%
Identifiers
Identifiers are names given to various program elements such
as variables, functions and arrays etc,.
Eg: #define N 10
#define PI 3.14

int xval;
const int service_tax = 0.12;
const float pi = 3.14;
void sum_of_num( );
The rules for forming identifier
First character must be alphabetic or
underscore.
Must consist only of alphabetic characters,
digits, or underscores.
Only the first 31 characters of an identifier are
significant and are recognized by the compiler.
Cannot use a keywords or reserved word (e.g.
main, include, printf & scanf etc.).
No space are allowed between the identifiers
etc,.
C is case sensitive, e.g. My_name 
my_name.
Variables
• A variable is a container (storage area) to hold data.
• Each variable should be given a unique name (identifier).
• Variable names are just the symbolic representation of a
memory location.
• A variable name can be chosen by you.
• The C language allows storing different types of data into the
variables & proper declaration for the variable is made before
it is used in the program.
• Variables can be used to store floating-point numbers,
characters, and even pointers to memory locations.
Variables
• Is a memory location
• Have a name given by the programmer
• It can store/hold a value
• Its value can be changed/altered/modified
• Every variable belongs to a data type

• Need of a variable: To store a value while progrme is


executing.
• Every variable must be declared before using it.
Variable declarations

Declaring a Variable
 Each variable used must be declared.
 A form of a declaration statement is
data-type var1, var2,…;
int sum;
 Declaration announces the data type of a variable and
allocates appropriate memory location. No initial value
(like 0 for integers) should be assumed.
 It is possible to assign an initial value to a variable in
the declaration itself.
data-type var = expression;
 Examples
int sum = 0;
char newLine = ‘\n’;
float epsilon = 1.0e-6;
Variable declarations

If we have not assign any value
to a variable then it contains
garbage.

Using such variables will give
unpredictable results.

Giving value to a variable is
known as assignment.

When assignment is done first
time after variable declaration
it is called initialization.

Never use a variable which is

Global and Local Variables
/* Compute Area and Perimeter of a

Global Variables circle */
 These variables are #include <stdio.h>
declared outside all float pi = 3.14159; /* Global */
functions. main() {
 Life time of a global float rad, area, peri; /* Local
variable is the entire */
execution period of the
program. printf( “Enter the radius “ );
 Can be accessed by any scanf(“%f” , &rad);
function defined below the
declaration, in a file. if ( rad > 0.0 ) {
area = pi * rad * rad;
peri = 2 * pi * rad;

printf( “Area = %f\n” , area );


printf( “Peri = %f\n” , peri );
}
else{
printf( “Negative radius\n”);
}
}

Global and Local Variables
/* Compute Area and Perimeter of a

Local Variables circle */
 These variables are #include <stdio.h>
declared inside some float pi = 3.14159; /* Global */
functions.
 main() {
Life time of a local float rad; /* Local */
variable is the entire
execution period of the printf( “Enter the radius “ );
function in which it is scanf(“%f” , &rad);
defined.
 Cannot be accessed by any if ( rad > 0.0 ) {
other function. float area = pi * rad * rad;
 float peri = 2 * pi * rad;
In general variables
declared inside a block are printf( “Area = %f\n” , area );
accessible only in that printf( “Peri = %f\n” , peri );
block. }
else
printf( “Negative radius\n”);

printf( “Area = %f\n” , area );


}
Suggestions on choosing variable
name
• Pick names that reflect the intended use of the
variable (i.e. type of value or purpose). The reasons
are obvious.
• Just as with the comment statement, meaningful
variable names can dramatically increase the
readability of a program and pay off in the debug and
documentation phases.
• In fact, the documentation task is greatly reduced
because the program is more self-explanatory.
List of valid List of not valid variable names
variable names with reason
Sum sum$value
sum $ is not a valid character.
pieceFlag piece flag
i Embedded spaces are not
J5x7 permitted.
Number_of_moves 3Spencer
_sysflag Variable cannot start with a
number
int
int is a reserved word/name.
Data Type

Every variable belongs to a data type.

In C language, Data types can be classified as: Primitive and User
defined.

Need of a Data type: Data type tells-

How much space a variable is going to occupy in memory.

What is the range of value a variable can store.

What functions can be performed on a particular variable.
Variable Storage Sizes and Ranges
• The amount of storage that is allocated to store
a particular type of data.
• It is not defined in the language. It is machine-
dependent.
• You should never write programs that make
any assumptions about the size of your data
types.
• Guaranteed that a minimum amount of storage
will be set aside for each basic data type.
Data types
Data type Size(bytes) Range Format string

char 1 -128 to 127 %c

int 2 -32,768 to 32,767 %d %i

float 4 3.4 e-38 to 3.4 e+38 %f

double 8 1.7 e-308 to 1.7 e+308 %lf


Data type
Entire
Size(bytes)
Data
Range
types in c
Format string

Char 1 -128 to 127 %c

Unsigned char 1 0 to 255 %c

Short or int 2 -32,768 to 32,767 %i or %d

Unsigned int 2 0 to 65535 %u

Long 4 -2147483648 to 2147483647 %ld

Unsigned long 4 0 to 4294967295 %lu

Float 4 3.4 e-38 to 3.4 e+38 %f or %g

Double 8 1.7 e-308 to 1.7 e+308 %lf

Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf


Using the Basic Data Types
#include <stdio.h>
int main (void)
{
int integerVar = 100;
float floatingVar = 331.79;
char charVar = 'W';

printf ("integerVar = %i\n", integerVar);


printf ("floatingVar = %f\n", floatingVar);
printf ("charVar = %c\n", charVar);
return 0;
}
Output
integerVar = 100
floatingVar = 331.790000
charVar = W
Specifier Meaning
%c – Print a character
%d – Print a Integer
%i – Print a Integer
%e – Print float value in exponential form.
%f – Print float value
%g – Print using %e or %f whichever is smaller
%o – Print actual value
%s – Print a string
%x – Print hexadecimal integer (Unsigned) lower case a – F
%X – Print hexadecimal integer (Unsigned) upper case A – F
%a – Print a unsigned integer.
%p – Print a pointer value
%hx – hex short
%lo – octal long
%ld – long unsigned integer.
Keywords
auto register continue
double typedef for
int char signed
struct extern void
break return default
else union goto
long const sizeof
switch float do
case short if
enum unsigned
static while
Constants

Constants refer to fixed values that the
program may not alter during its execution.

These fixed values are also called literals.

Constants can be of any of the basic data types
like an integer constant, a floating constant, a
character constant, or a string literal.
Constants

Constants

Numeric Constants Character Constants

Integer Real Single String


Constant Constant Character Constant
Constant
Numeric constants
Integer constants
It is formed using a sequence of digits.
Decimal - 0 to 9,
Octal - 0 to 7.
Hexa - 0 to 9, A to F

Real constants
It is formed using a sequence of digits but it contain decimal
point.
length, height, price distance measured in real number
Eg: 2.5, 5.11, etc.
Character constants
Single character constant
A character constant is a single character they also
represented with single digit or a single special
symbol which is enclosed in single quotes.
 Eg: ‘a’, ‘8’,’_’etc.
String constants
String constant are sequence of characters enclosed with in
double quote.
Eg: “Hello” ,”444”,”a” etc,.
main() {
int x;
x=5.5;
printf(“%d”, x);
x=6;
printf(“%d”, x);

}
ASCII Code

ASCII stands for American Standard Code for Information
Interchange.

In C programming, a character variable holds ASCII value
rather than that character itself. This integer value is the ASCII
code of the character.

For example, the ASCII value of 'A' is 65.


char ch=’A’;

What this means is that, if you assign 'A' to a character
variable, 65 is stored in the variable rather than 'A' itself.
Program to Print ASCII Value

#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);

// %d displays the integer value of a character


// %c displays the actual character

printf("ASCII value of %c = %d", c, c);

return 0;

You might also like