Constants

advertisement
BASICS CONCEPTS OF
‘C’
C Character Set
 Tokens in C
 Constants
 Variables

 Global Variables
 Local Variables

Like any other languages. C has own character set. A character denotes any
letter or alphabet.
digit or special symbol used to represent information
LETTERS
Upper Case
Lower Case
A……………………….Z
a………………………..z
DIGITS
All decimal digits
0………………………..9
SPECIAL SYMBOLS + - * / . < > : “ ‘
, ? { } [ ] ( ) & %
! @ # $ ^
BACK

Keywords
◦ C Contains 32 Keywords that have a standard ,predefined meanings.
These Keywords can be used only for their intended purposes. They can
not be redefined by the programmers. These are reserved words of the C
language. For example int, float, if, else, for, while , break, goto, for,
continue, double, auto, void, union , short, return, extern, enum etc.

Identifiers
◦ An Identifier is a sequence of letters and digits, but must start with a
letter. Underscore ( _ ) is treated as a letter. Identifiers are case sensitive.
Identifiers are used to name variables, functions etc.
◦ Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If
◦ Invalid: 324, short, price$, My Name

String Literals
◦ A sequence of characters enclosed in double quotes as “…”. For
example “13” is a string literal and not number 13. ‘a’ and “a” are
different.

Operators
◦ Arithmetic operators like +, -, *, / ,% etc.
◦ Logical operators like ||, &&, ! etc. and so on.

White Spaces
◦ Spaces, new lines, tabs, comments ( A sequence of characters enclosed
in /* and */ ) etc. These are used to separate the adjacent identifiers,
kewords and constants.

Constants
◦ Constant is a quantity that remains unchanged during the execution of a
program Constants like 13, ‘a’, 1.3e-5 etc.
TYPES OF CONSTANTS
 NUMERIC CONSTANTS
1. Integer Constant
2. Real Constant
 CHARACTER CONSTANT
1. Single Character Constant
2. String Constant
BACK

Integer Constants
An Integer Constant is sequence of digits without decimal point.
Rules for Constructing Integer Constant
◦ An integer Constant Must have at least one digit.
◦ Commas and Blank Spaces can not be included with in the no.
◦ It contains neither a decimal point nor an exponent.
◦ Sign(+ or -) must proceed the no.
◦ Default Sign is +ve.
◦ The allowable range for integer constant is -32768 to +32767
◦ These are valid constants:
25 -11
+40
5678 etc.
◦ These are not valid constants:
25.0
7.1e 4
40 20 5- etc.

Real Constants
An Real Constant is number that contains either a decimal point or an
exponent
Rules for Constructing Fractional Form Real Constant
◦ An Real Constant Must have at least one digit.
◦ Commas and Blank Spaces can not be included with in the no.
◦ It must contains decimal point.
◦ Sign(+ or -) must proceed the no.
◦ Default Sign is +ve.
◦ These are valid constants:
25.6
-11.8
0.0345 567.889 etc.
◦ These are not valid constants:
25.0
7.1e 4
40 20 5- etc.
Rules for Constructing Exponential Form Real Constant
◦ The mantissa is either a real no expressed in decimal notation or an
integer.
◦ Commas and Blank Spaces can not be included with in the no.
◦ It must contains decimal point.
◦ The exponent is always an integer no with an optional (+ or -) sign.
◦ Default Sign is +ve.
◦ The allowable range for real constant expressed in exponential form
is -3.4e-38 to 3.4e38
◦ These are valid constants:
0.5e3
-1.8e-7 -1.4e+8 12e3 etc.
◦ These are not valid constants:
25.0
7.1e4.6 40 12 e 8 etc.

Character and string constants
◦ ‘c’ , a single character in single quotes are stored as char.
Some special character are represented as two characters in single
quotes.
‘\n’ = newline, ‘\t’= tab, ‘\\’ = backlash, ‘\”’ = double quotes.
Char constants also can be written in terms of their ASCII code.
‘\060’ = ‘0’ (Decimal code is 48).
◦ A sequence of characters enclosed in double quotes is called a string
constant or string literal. For example
“Charu”
“A”
“3/9”
“x = 5”
BACK
A Variable is a data name that may be used to store a data value. A
variable may take different values at different times during the execution of
a program. Variable must defined before they are used in a program.

Naming a Variable
◦
◦
◦
◦
◦
A variable may consist of letters, digits and undersore.
Must not be a keyword
Variables are identified by only first 32 characters.
Special symbols and blanks are not allowed.
Both Uppercase and Lowercase letters are allowed and are conidered to be
distinguishable.
◦ Not valid Variables are:
ram Kumar char 8th price$ etc
◦ Valid Variables are:
area sum avg total etc

Declaring a Variable
◦ Each variable used must be declared.
◦ A form of a declaration statement is
data-type var1, var2,…;
◦ 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;

Global Variables
◦ These variables are declared
outside all functions.
◦ Life time of a global variable
is the entire execution period
of the program.
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
float pi = 3.14159; /* Global */
main() {
float rad;
/* Local */
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
float area = pi * rad * rad;
float peri = 2 * pi * rad;
◦ Can be accessed by any
function defined below the
declaration, in a file.
printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri );
}
else
printf( “Negative radius\n”);
printf( “Area = %f\n” , area );
}
BACK

Local Variables
◦ These variables are declared
inside some functions.
◦ Life time of a local variable is
the entire execution period of
the function in which it is
defined.
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
float pi = 3.14159; /* Global */
main() {
float rad;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
float area = pi * rad * rad;
float peri = 2 * pi * rad;
◦ Cannot be accessed by any
other function.
◦ In general variables declared
inside a block are accessible
only in that block.
/* Local */
printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri );
}
else
printf( “Negative radius\n”);
printf( “Area = %f\n” , area );
}
BACK
BACK
Download