UNIT-2 History of C' Language
UNIT-2 History of C' Language
UNIT-2 History of C' Language
Main() Function:
Every ‘C’ program must have one main() function, which specify the starting of
‘C’ program. It contains the following two parts.
Declaration part:
This part is used to declare all the variables that are used in the executable part
of the program and these are called local variables.
Executable part:
It contains atleast one valid ‘C’ statement.
The execution of a program begins with opening brace ‘{‘and ends with closing
brace ‘}’.
The closing brace of the main function is the logical end of the program.
All the statements in the program ends with a semicolon(;) except conditional and
control statements.
PROGRAMMING RULES:
All statements in ‘C’ program should be written in lower case letters.
Blank spaces may be inserted between the words. It is not used while declaring a variable, keyword,
constant and function.
The program statement can write anywhere between the two braces following the declaration
part.
The user can also write one or more statements in one line separating them with a
semicolon(;).
C TOKENS:
The tokens are usually referred as individual text and punctuation in a passage of text.
The ‘C’ language program can contain the individual units called the C tokens and
has the following types.
1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Operators
6. Special symbols
1. IDENTIFIERS
Identifiers are names given to various program elements, such as variables, functions
and arrays etc.
Rules:
Identifiers consist of letters and digits in any order.
The first character must be a letter/character or may begin with underscore( _ ).
Both upper/lower cases are permitted although uppercase character is not
equivalent to corresponding lowercase character.
The underscore ‘ _’ can also be used and is considered as a letter.
An identifier can be of any length while most of the ‘C’ compiler recognizes only the
first 31 characters.
No space and special symbols are allowed between the identifier.
The identifier cannot be a keyword.
Valid identifiers:
STDNAME, SUB, Y2K.
Invalid identifiers:
STD NAME, Return, $stay, 7rno.
2. KEYWORDS:
There are certain reserved words called keywords that have standard and predefined
meaning in ‘C’ language which cannot be changed and they are the basic building
blocks for program statements.
Keywords are must written in lower case.
The ‘C’ keywords are listed below.
1. auto 9. double 17. int 25. struct
2. break 10. else 18. long 26. switch
3. case 11. enum 19. register 27. typedef
4. char 12. extern 20. return 28. union
5. const 13. float 21. short 29. unsigned
6. continue 14. for 22. signed 30. void
7. default 15. goto 23. sizeof 31. volatile
8. do 16. if 24. static 32. while
Data types:
Data type is the type of the data, that are going to access within the program.
Each data type may have predefined memory requirement and storage
representation.
Primary User defined Derived Empty
char structures Arrays
int union pointer void
float typedef
double
SCOPE OF VARIABLES:
Local variables:
The variables which are defined inside a main function block or inside a
function are called local variables.
It is declared within blocks.
Examp
le: void
main()
{
int i,j;
/* body of the function */
}
Global variables:
The variables that are declared before the function main() are called the global
variables.
Exampl
e: int
a=5,b=2
; void
main()
{
fun();
}
void fun()
{
int
sum;
sum =
a+b;
}
STORAGE CLASSES:
1. Auto:
They are called as the automatic because their memory space is automatically
allocated as the variable is declared.
It is optional keyword.
Syntax: Storage_class_type data_type var1, var2……var n;
Example: auto int a,b;
2. Static:
The static variables are the variables for which the contents of the variables
will be retained throughout the program.
These are permanent within the function in which they are declared.
Syntax: Storage_class_type data_type var1, var2,…….varn;
Example: static int a,b;
3. Extern:
The external variables are declared out of the main() function the availability of these
variables are throughout the program and that is both in main program and inside the
user
defined functions.
Syntax: Storage_class_type data_type var1, var2,……var n;
Example: extern int a, b;
4. Register:
Registers are special storage areas within a computer’s central processing unit.
The actual arithmetic and logical operation that comprise a program are carried
out within these registers.
Syntax: Storage_class_type data_type var1, var2, ………..var n;
Example: register int a,g;
CONSTANTS:
The item whose values cannot be changed during the execution of program are
called constants.
a) Numeric constants:
i) Integer constants:
An integer constant formed with the sequence of digits.
There are three types of integer constants which forms different number
system. Syntax: const datatype identifier=value;
Example: const int M = 95;
Rules:
It must have atleast one value.
Decimal point is not allowed.
It can be either positive or negative.
ii) Real constants:
A real constant is made up of a sequence of numeric digits with presence of a
decimal point.
Real constants serve as represent quantities such as distance, heights,
temperatures, etc. Syntax: const datatype identifier=value;
Example: const float distance =126.5;
Rules:
A real constant must have one digit.
A real constant must have decimal point.
No commas or blank spaces are allowed.
b) Character constants:
i) Single character constants:
The character constant contains a single character enclosed within pair of single
inverted commas(‘) both pointing to the left.
Syntax: const datatype identifier= value;
Example: const char
c=’s’,gender=’F’; ii) String
constants:
A string constant is a sequence of characters enclosed in double quotes the
characters may be letters, numbers, special characters and blank spaces etc.
Syntax: const datatype identifier= value;
Example: const char c[10]=”senthil”;
Delimiters:
These are the symbols, which has some syntactic meaning and has got significance.
Symbol Name Meaning
# Hash Pre-processor directive
, Comma separate list of variables
: Colon Label
; Semi colon Statement end
() Parenthesis Function and Expression
{} Braces Creating blocks
[] Square bracket Arrays
Statements:
Statements can be defined as set of declaration or sequence of action. Statement
causes the computer to perform some action.
1. Assignment statements:
Assignment operator used for assigning values to the variables.
Example: basic =3890;
2. Null statements:
A statement without any characters and it has only semicolon is called null statement
Example: ; (null statement)
3. Block of statements:
Block contains several statements that are enclosed within a pair of braces {}.
These can be any of expression, assignments and keywords etc.
Example:
{
int
a=890;
float b=
89.9;
printf(“%d%f”, a, b);
}
4. Expression statements:
These consist of expressions and can be arithmetic, relational or logical.
Exampl
e: a=
29
B=a
+77;
fun(
a,b);
OPERATORS
An operator is as symbol that specifies an operation to be performed on the operands.
The data items are called operands.
Example: a+b
‘+’ operator and a, b are the operands.
Types of operators:
1) Arithmetic Operator (+, -,*,/,%,)
2) Relational Operator (<,>,<=,>=,!=,==)
3) Logical Operators (&&.||,!)
4) Assignment Operator ( = )
5) Increment & decrement Operator (++,--)
6) Conditional (or) Ternary Operator (? , : )
7) Bitwise Operator (&,!,|,^)
8) Special Operator (, sizeof,&&*, .&->)
1) Arithmetic operators:
Basic arithmetic operation like addition, subtraction, multiplication, and division.
Operator Meaning Example A=9, B=5
+ addition C=a+b C=14
- subtraction C=a-b C=4
* multiplication C=a*b C=45
/ division C=a/b C=1
% Modulo C=a%b C=4
2) Relational operators:
Relational operators are used to compare two or more operands.
Operands may be variables, constants or expression.
The value of relational expression is either one or zero.
Relational operators are used in decision making process.
Operator Meaning Example A=9, B=5
< Less than C=a<b C=0
> Greater than C=a>b C=1
<= Less than or Equal to C=a<=b C=0
>= Greater than or equal to C=a>=b C=1
== Equal to C=a==b C=0
!= Not equal to C=a!=b C=1
3) Logical operators:
Logical operators are used to combine the results of two or more conditions.
The logical operators are &&, ||,!
Syntax: (exp1)&&(exp2)
Operator Meaning Example A=9, B=5, C=2
&& Logical AND d=(a>b)&&(a>c) d=1
|| Logical OR d=(a>b)||(a>c) d=1
! Logical NOT d=!(a>=b) d=0
4) Assignment operators:
Assignment operators are used to assign a value or an expression or a value of a
variable to another variable.
Example: x=5;
i) Compound assignment:
Compound assignment operators to assign a value to a variable in order to assign a
new value to a variable after performing a specified operation.
Example: x+ = y
ii) Nested assignments:
‘C’ language has got distinct features in assignment called nested assignment.
Syntax:
var1=var2=…………..var-n= single variable or expression or value;
Example: i=j=k=l;
x=y=z=(i+j
+k);
6) Conditional operator:
It checks the condition and executes the statement depending on the condition.
It is like as if…else statement.
Syntax:
Condition?exp1:exp2;
Example:
int a=5,b=3,big;
big=a>b?a:b;
printf(“%d is big”,big);
7) Bitwise operators:
It is used to manipulate the data at bit level.
It operates on integer only.
For all the above operators in all possible combinations of bits as shown below.
A B A|B A&B A^B ~A
0 0 0 0 0 1
0 1 1 0 1 1
1 0 1 0 1 0
1 1 1 1 0 0
3. Pointer operators:
&---This symbol specifies the address of the variable.
* --- This symbol specifies the value of the variable.
4. Member selection operators:
. and -- > : These symbols used to access the elements from a structure.
EXPRESSIONS:
An expression represents data item such as variables, constants and are
interconnected with operators as per the syntax of the language.
An expression is evaluated using assignment operator.
Syntax: variable = expression;
Example: x=a*b-c;
Type conversion:
It refers to the process of changing an entity of one data
type into another. i) Implicit conversion:
It is an automatic type conversion. In a mixed type expression data of one
or more subtypes can be converted to a super type as needed at runtime so
that the program will run correctly.
E
x
a
m
p
l
e
:
i
n
t
c
;
f
l
o
a
t
f
=
3
.
5
;
c
=
f
;
p
r
i
n
t
f
(
“
%
d
”
,
c
)
;
OUTPUT:
3
ii) Explicit conversion:
The explicit conversion can be made possible to convert one data
type to another by forcefully and it is different from the implicit
conversion.
Syntax: var1=(datatype)var2;
Ex:
int a=10;
float(a);
Where a=10;
float (a) will contain 10.000000