File

advertisement
Interface
between computer &
human being.
ALPHABETS
WORDS
SENTENCES
PARAGRAPH
Alphabets, digits
special symbol
Constants,
Variables,
Keywords
Instructions
Program
 ALPHABETS
- A,B…………Z
a,b………….z
 DIGITS – 0,1,2,3,4,5,6,7,8,9
 SPECIAL SYMBOLS - ? ! @ # $ % & *
()+ - \ =
_ | : ;etc..
 WHITE SPACES-Blank space,Tab,New
line
‫ﲤ‬
TRIGRAPH CHARACTERS
Many non-English keyboards do not support all the characters.
For that C introduces the concept of “trigraph” sequences to provide a
way to enter certain characters that are not available on some
keyboards.
Each trigraph sequence consists of three characters(two questionmark
followed by another character). Ex.keyword not support square
brackets,
Ex. Trigraph Sequence
Translation
??=, ??/
#,\
??( , ??)
[, ]
TOKENS
VARIABLES
CONSTANTS
STRING
KEYWORDS
SPECIAL
SYMBOL
OPERATORS
KEYWORDS AND IDENTIFIERS
All keyword have fixed meanings and meanings cannot be changed.
Ex, break,else
Identifiers refer to the names of variable, functions and arrays.
These are user defined names and consist of sequence of letters and
digits
Rules:First char must be alphabet.
Contain letters, digits, underscore not white space, keyword.
CONSTANT is a quantity that doesn’t change.
 VARIABLE is a quantity that value can be changed
respectively.

漠For example :
3X + Y = 20
‫ﲤ‬
Since 3 and 20 can not change, they are called
constants, where as the quantities X & Y can vary
or change hence are called variables.
C Constants
Primary Constants
Secondary Constants
Integer Constants
Real Constants
Character Constants
String constants
Escape sequence
Array, Pointer ,
Structure , Union
Enum etc.
INTEGER CONSTANTS
- sequence of digits
Three types
Decimal: set of digits,0 through 9
123, -87, 0, +56 <- valid
$1000, 15 750, (20,000) <-Invalid
Octal: combination of 0 to 7 with leading zero. i.e. 075, 051 are octal
Hexadecimal:sequence of digits 0-9,A..F/a..f ….. Preceded by 0x or
0X
like 0x2, 0x3A, 0Xe5,etc are hexadecimal numbers.
REAL CONSTANTS(FLOATING POINT)
- to represent varying entity like prices. it contain fractional parts.
in decimal notation 34.32, -0.56, .4555 , 0.00054 etc.
In exponential notation(or scientific)
2.1565e2 also valid
More 0.65e4 ,12e-2, 3.18E3.
-> Should the following be valid ?
1.5E2.5 (YES / NO ) ?
215.65 may be written as
SINGLE CHARACTER CONSTANTS
-a single character enclosed within a pair of single quote marks I.e ‘y’
, ‘4’, ‘;’ , ‘ ‘, etc.
- character constants have integer values known as ASCII (American
Standard Code for Information Interchange) values.
i.e. printf(“%d”,’a’); will print no. 97, the ascii value of the letter a.
While printf(“%c”,97); will print --- a
*error in book statement instead of ’97’ it should be only 97
STRING CONSTANTS
-sequence of characters enclosed in double quotes like
“hello!” , “hi 18”, “well done”, “a”
Remember that “x” and ‘x’ is different because ‘x’ has equivalent
integer value in ASCII.
String constants are normally used like displaying menu/messages.
BACKSLASH CHARACTER CONSTANTS
- used in output functions…
- ‘\n’ stands for new line character
- ‘\b’ for back space
- ‘\t’ for making horizontal tab
all above represents one character, although they consist of two
chars.
These char combination are known as “escape sequences”
VARIABLES
A specific place where specific data is stored
variable name : to access/use the data stored at the specific place.
A variable is a data name that may be used to store a data value
i.e. you want to store two numbers 423 and 54.
No1
No2
423
54
RULES FOR DEFINING A VARIABLE
1. They must begin with a letter & no special symbols are used
without underscore.( _ )
i.e. total, mark1, ave_mark (Valid)
3mark, 345 (Invalid)
2. The length should not be normally more than 8 characters,
since only the first 8 characters are treated as significant by the
compiler.
i.e. average_weight & average_height
will be treated same.
RULES FOR DEFINING A VARIABLE
3. Uppercase and lowercase are significant.
i.e. the variable Total is not the same as total or TOTAL.
4. The variable name should not be a keyword.
i.e. a person name should not be a vocabulary spelling …
5. White space is not allowed.
i.e. variable name ‘my total’ is invalid.
DATA TYPES
Classes of data types :
Primary data types
User defined data types
Derived data types
Basically C supports four Primary data types :integer (int) , character (char), floating point (float) , double
precision floating point (double).
PRIMARY DATA TYPES
TYPE
SIZE
RANGE
Char
Int
Float
Double
Long int
Long double
1 byte
2 byte
4 byte
8 byte
4 byte
10 byte
-128 to 127
-32,768 to 32,767
3.4e-38 to 3.4e+38
1.7e-308 to 1.7e+308
-232 to 232
+/- 3.4*104932
QUALIFIER
Signed :
Indicates that the data can be positive or negative
Unsigned :
Indicates that data to store is positive only…
Short :
Data to store will require less memory
Long :
Data to store will require more memory…

–32768 to 32767.
( from -215 to 215)
1
SIGN BIT

So, here first bit is used for sign. Means if it is 0 then
+ve no and 1 then –ve no. so, only other 15 bits are
used for magnitude. And so that range is –32768 to
32767.
TYPE
SHORT FORM SYMBOL
character
char
%c
Signed integer
int
%d
Unsigned int
Same
%u
real
float
%f
double
double
%lf
Long double
Same
%Lf
Signed long int
Same
%ld
Unsigned long int Same
%lu
DECLARATION OF VARIABLES
data_type v1,v2,….,vn;
Where v1,v2, … vn are variable names.
i.e. int maximum;
will have two bytes memory allocated and the name ‘maximum’
associated with it.
(by default the int is considered as signed, so range -32768 to
32767)
i.e. unsigned int age;
then 2 bytes allocated but the range will be 0 to 65535.
ASSIGNING VALUES TO VARIABLE
assignment operator
int num1,num2,sum;
sum = num1 + num2;
No1=no2=no3; also possible…
int no1=10; also possible …. Initialization at the time of declaration.
char choice;
choice = ‘y’;
or scanf(“%c”,&choice);
USER DEFINED TYPE DECLARATION
Type definition that allows programmers to define an
identifier/name that would represent an existing data type….
general form: typedef type identifier;
i.e. typedef int marks_inc
So, now onward when we require to store marks of c subject we will
create variable of type marks_inc like,
marks_inc m_stu1;
typedef just increase readability via creating meaningful data type
names, it can’t create a new datatype.
ENUMERATED DATA TYPE
Suppose u want to store week days like
Sunday, Monday,…,
So, u know the possible values in advance …. Can be done using
enumerated data type
enum identifier {value1,value2,..};
i.e enum day {Mon,Tue,Wed,Thur,Fri,Sat,Sun};
enum day week_st;
So, here week_st can have any possible value from the list
CONTINUING ENUM
Here by default
Enumeration constant Mon is assigned 0,
Tue assigned 1, Wed is assigned 2, and so on.
However automatic assignments can be overridden by …..
enum day {Mon=10,Tue,Wed,…..};
Now As Mon is having 10, Tue will have one more 11, Wed will have
12, and so on.
Q. int x; x = Wed – Mon; x = ?
VARIABLE’S STORAGE CLASS
Provides information about the location and visibility/scope of a
variable.
There are four storage class specifiers
auto,register,static,extern.
auto variables
- defined within a function …. gets memory when the function is
executing ……destroys when the function is over …..local to that
function so can’t be used in outside function…. Gets garbage
initialized..
…GLOABAL
the variable declare outside any function before main
known as global variable.
It can be used in all the functions after the declaration
of it.
Note ::
Global is External variable…
External and static variable are initialized to zero by
default… .while auto variable contains garbage .. Until
they are initialized.
DECLARING A IDENTIFIER AS
CONSTANT
Suppose u want to make some identifier whose value need not to be
changed during the execution …
const int class_size = 40;
So, you can use const qualifier.
This ensures that
x= class_size;
valid
class_size = 10;
invalid because as class_size is constant
and can’t change it’s value.
VARIABLE AS VOLATILE
a variable’s value may be changed at any time by some external
sources (from outside the program).
i.e. volatile int date;
So, the value of date may be altered by some external factors … the
compiler will examine the value of the variable each time it is
encountered to see whether it is changed by other.
OVERFLOW OF DATA
- when the value of a variable is either too big or too small for the
data type to hold…
- ‘C compiler does not provide any warning or indication of integer
overflow.
- Programmer must take care for the type and range of input
possible
DEFINING SYMBOLIC CONSTANTS
- if some constant value is used at many places in the program…
Like value of pi 3.142
So, if we write the actual constant 3.142 at each places then the
problem can be
- modification to do at each place if require
- understanding the program like STRENGTH can be 50
and MAX_MARKS can also 50….
#DEFINE
#define is used to define symbolic constants in our program.
We face two problems if we are using some numbers in the
program at many places.
1. Problem in modification of the program.
2. Problem in understanding of program.
By using the #define macro we can overcome these two
problems.
THE GENERAL FORMAT OF #DEFINE
A constant can be defined as follows:
#define symbolic-name value of constant
Valid examples are:
#define PI 3.14159
#define MAX 100
RULES APPLY TO #DEFINE
1.Symbolic name have the same form as variable names. But usually
we are using CAPITALS for symbolic names. But that is not a rule.
#define MAX 200
2.No blank space between the pound sign’#’ and the word define is
permitted.
# define MAX 200 <- Not permitted
3.’#’ must be the first character in the line.
4.A blank space is required between #define and symbolic name and
between the symbolic name and value.
CONTINUE…
5.#define statements must not end with a semicolon.
#define PI 3.1415; <- Not valid
6.After definition, the symbolic name should not be assigned any
other value within the program using an assignment statement.
i.e. #define STRENGTH 100
main()
{
STRENGTH = 200;
}
Is illegal.
CONTINUE…
7.Symbolic names are NOT declared for data types. Its data type
depends on the type of constant.
8.#define statements may appear anywhere in the program but
before it is referenced in the program.
Download