Upload
cody-quinn
View
216
Download
0
Embed Size (px)
Citation preview
BASICS CONCEPTS OF ‘C’
C Character Set Tokens in C Constants Variables
Global Variables Local Variables
CONTENTS
C Character Set Like any other languages. C has own character set. A character denotes any
letter or alphabet.
digit or special symbol used to represent information
LETTERSUpper Case A……………………….Z
Lower Case a………………………..z
DIGITSAll decimal digits 0………………………..9
SPECIAL SYMBOLS + - * / . < > : “ ‘
, ? { } [ ] ( ) & %
! @ # $ ^BACK
Tokens in C 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
Cont… 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.
Cont… 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
Constants 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.
Cont…. 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.
Cont….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.
Cont… 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
Variables 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
Declaration of Variable 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;
◦ Examplesint sum = 0;
Global Variables
Global Variables◦ These variables are declared
outside all functions.
◦ Life time of a global variable is the entire execution period of the program.
◦ Can be accessed by any function defined below the declaration, in a file.
/* 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;
printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”);
printf( “Area = %f\n” , area );}
/* 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;
printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”);
printf( “Area = %f\n” , area );}
BACK
Local Variables 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.
◦ Cannot be accessed by any other function.
◦ In general variables declared inside a block are accessible only in that block.
/* 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;
printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”);
printf( “Area = %f\n” , area );}
/* 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;
printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”);
printf( “Area = %f\n” , area );}
BACK
THANKS
BACK