3.basics of c
3.basics of c
The features of ‘C’ are derived from many of its ancestor languages as shown in the
following hierarchy.
Characteristics of C language:
6. C language is also called as middle level language because it can be used to create
high level programs and also low level programs such as assembly programs.
1
Structure of C program:
2
before the main function section. The variables which are declared in global declaration
section are called global variables.
The main function section contains only one function called as main(). The execution of
a ‘c’ program starts with the main() function. Main() function may contain several statements
that achieves a particular task. The variables which are declared inside the main() function
are called local variables.
The user defined function section contains definitions of several functions. These user
defined functions can be placed either before the main function or after the main function.
Note : Any ‘C’ program can contain atmost one main() function.
2. Digits: 0-9
4. White spaces: blank space, tab, new line character, carriage return, form feed
3
The data types in ‘c’ language can be broadly classified into the following categories.
1. Primary data types
2. Derived data types
3. User defined data types
4. Empty data type
Primary data types/ primitive data types/ fundamental data types/ basic data types/
scalar data types/ pre defined data types :
There are four kinds of primary data types in ‘c’ language.
1. int
2. float
3. char
4. double
int:
The “int” data type is used to store the integer values. The size of integer is either 2
bytes or 4 bytes based on the compiler. If the size of integer is 2 bytes, then its range will be
- 215 to 215-1. If the size of integer is 4 bytes, then its range will be -231 to 231-1.
Integer variables are declared by using the syntax
int variablename;
float:
The “float” data type is used to store the real values. The size of integer is 4 bytes. The
range of float data type will be -3.4*1038 to 3.4*1038. The number of precision digits
supported by float data type is 6.
Float variables are declared by using the syntax
float variablename;
Example:float x; x=23.6;
char:
The “char” data type is used to store a single character at a time. All the characters
in ‘c’ language are represented using ASCII code. Therefore, the characters in ‘c’ language
are also treated as integers. The size of char is 1 byte. The range of char data type will be
-27 to 27-1.
4
Character variables are declared by using the syntax
char variablename;
double:
The “double” data type is used to store the real values. The size of integer is 8 bytes.
The range of float data type will be -1.7*1038 to 1.7*1038. It is an extension of float data type.
The number of precision digits supported by double data type is 10.
double variables are declared by using the syntax
double variablename;
Example:double x; x=13452.976;
5
signed datatype variablename;
The default type qualifier which is applicable to all the data types in ‘c’ language is
“Signed.”
Example: signed int x; // same as int x;
Unsigned:
The type qualifier “unsigned” is used to increase the range of a particular data type.
The syntax of unsigned qualifier is
MSB-0 1 2 3 4 5 6 7 8 9 10 11 1 13 14 LSB-15
2
6
unsigned int / unsigned short int 2 0 to 65535
A 9 4 10 20 12
7
These are the derived data types which can store multiple data items of different data types.
All the data items in a structure are stored in physically continuous memory locations.
Example: struct employee
{
char name[10];
int id;
float salary;
Here, type_name is the name of enumerated data type or tag. And value1, value2 ,...., valueN
are values of type type_name. By default, value1 will be equal to 0, value2 will be 1 and so
on but, the programmer can change the default value as below:
enum day{Sunday=1, Monday,……, saturday}
Example program:
#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
main()
{
enum week today;
today=wednesday;
printf("%d day",today+1);
}
Output: 4 day
Empty data type:
There is only one kind of empty data type i.e., void. Void is used to declare functions that
does not return any value.
8
Example: void functionname()
{
:
}
Variables:
Variable is the basic storage area which can store different values at different times.
There are 4 types of variables in ‘c’ language as given below.
1. Integer variables
2. Character variables
3. Float variables
4. Double variables
C tokens: A ‘C’ token is the smallest individual unit in a C program. C tokens are divided into the
following categories:
1. Keywords
2. identifiers
3. constants
4. special symbols
5. operators
1. Keywords:
Keywords are the reserved words which are predefined by the developers of C language.
Every keyword will have a special meaning and purpose that cannot be changed by the
programs. There are 32 keywords in C language as given below.
9
2. Identifiers:
Identifiers are the names given to data variables, functions and other types of objects in a
language. While choosing a name, the following rules must be considered:
2. The other characters in the name can be alphabets, digits and underscores.
Example:
Valid Invalid
Xyz my name
x12 1xy
_x52 Continue
my_name 2yz
3. Constants:
Constant is a quantity which does not change its value throughout its lifetime. Constants in C
language can be classified into the following categories.
Integer constants and floating point constants can be represented using any of the number
systems such as decimal, octal, and hexadecimal number systems. Octal numbers starts with
0 as the first digit. Hexadecimal numbers starts with 0x or 0X followed by the number.
Character constants are enclosed in a pair of single quotes and are equal to an integer
constant. Ex: ‘a’ , ‘x’ etc..
String constants are enclosed in a pair of double quotes. Ex: “hi” , “hello” etc..
10
4. Special symbols:
There are several special symbols used in C language. Some symbols are given below
5. Operators:
Types of operators:
Unary operator: An operator which applies on only one operand is called an unary operator.
Binary operator: An operator which operates on two operands is called a binary operator.
Ternary operator:An operator which operates on three operands is called a ternary operator.
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bitwise operators
6. Increment/Decrement operators
7. Conditional operator
8. Special operators
11
Arithmetic operators:
+ plus Addition
- minus Subtraction
* Asterisk Multiplication
/ slash Division
% percentile Modulo division
The operator + can be used as both unary and binary operator. When it is used as
unary operator, it represents the sign of the operand. When it is used as binary operator, it
represents the addition operation.
Example:
The operator - can be used as both unary and binary operator. When it is used as unary
operator, it represents the sign of the operand. When it is used as binary operator, it
represents the subtraction operation.
Example:
The operator * can be used as both unary and binary operator. When it is used as unary
operator, it represents pointer operation. When it is used as binary operator, it represents the
multiplication operation.
Example:
Unary * Binary *
x=*p x=y*z
The operator / can be used as binary operator only. It represents division operation and
returns the quotient of the division operation.
Example:
Binary division
x=y/z
if y=10, z=3
then x=3
12
The operator % can be used as binary operator only. It represents modulo division
operation and returns the remainder of the division operation. It can’t be applied on floating
point numbers.
Example:
Assignment operator:
Example:
The left hand side of an assignment operator must always be a single operand. The right
hand side of an assignment operator can be a value, an operand or an expression.
If the operand on L.H.S of an assignment operator is same as the operand on R.H.S, then
short hand assignment operators can be used to represent that expression. The general form of
short hand assignment operator is given below.
Example:
Relational operators:
13
Operator Name Purpose
< less than
> greater than
<= less than or equal to To Compare any
>= greater than or equal to two quantities
== is equal to
!= is not equal to
Example:
Logical operators:
Logical operators are mainly used to combine relational expressions that forms a compound
relational expression.
Operator Name
&& logical AND
|| logical OR
! logical NOT
The logical AND is a binary operator which behaves according to the following truth table.
14
(a<x)&&(b>y) True(1)
The logical OR is a binary operator which behaves according to the following truth table.
The logical NOT is an unary operator which is used to negate the truth value of a relational
expression. The behavior of logical NOT operator is given in the following truth table.
rel1 !rel1
T T
F T
Example:
Bitwise operators:
Bitwise operators are used to perform operations on data items at bit level. C language
provides the following types of bitwise operators given in the table.
Operator Name
& bitwise AND
| bitwise OR
^ Exclusive OR
~ 1’s complement/ negation
<< left shift
>> right shift
15
The bitwise AND is a binary operator which operates on data items at bit level.The behavior
of bitwise AND operator is given in the following truth table.
The bitwise OR is a binary operator which operates on data items at bit level. The behavior of
bitwise OR operator is given in the following truth table.
The exclusive OR is a binary operator which operates on data items at bit level. The behavior
of exclusive OR operator is given in the following truth table.
The negation is a unary operator which operates on data items at bit level. The behavior of
1’s complement operator is given in the following truth table.
16
bit1 ~bit1
1 0
0 1
Example:
The left shift operator is a binary operator which is used to shift the specified number of bits
towards left. The general syntax of left shift operator is operand<<n where ‘n’ is the number
of bits to be shifted towards left.
Example:
The right shift operator is a binary operator which is used to shift the specified number of
bits towards right. The general syntax of right shift operator is operand>>n where ‘n’ is the
number of bits to be shifted towards right.
Example:
The increment operator (++) is used to add a quantity ‘1’ to the operand. Increment operators
are divided into two types as given below.
Example:
17
Pre increment Post increment
If ++x If x++
Then x=x+1 Then x=x+1
When increment operators are combined with an assignment operator, then the behavior of
preincrement will differ from post increment.
Example:
The decrement operator (--) is used to subtract a quantity ‘1’ to the operand. Decrement
operators are divided into two types as given below.
Example:
When decrement operators are combined with an assignment operator, then the behavior of
predecrement will differ from post decrement.
Example:
18
Conditional operator:
Conditional operator (?:) is a ternary operator which operates on three operands. The general
syntax of conditional operator is given below.
Here operand1 is generally a relational expression which returns either true or false. If
operand1 is true, then operand2 will be executed. If operand1 is false, then operand3 will be
executed.
Example:
1. Comma operator
2. sizeof() operator
The comma operator is used to combine several statements to form a compound statement.
Example:
When more than one expression is combined using comma operator then the result of last
expression will be returned by the comma operator.
Example:
19
The sizeof() operator is used to find the number of bytes required for a data item or variable.
16-bit 2 4 1
32-bit 4 4 1
Associativity is used to determine the order in which operators with same precedence in a
compound expression are evaluated.
20
Precedence Category Operator Operation Associativity
1 Highest () Functional call
precedence [] Array subscript
C indirect component selector LR
:: C scope resolution
. C direct component selector
2 Unary ! Logical NOT
~ Bitwise (One’s) complement
+ Unary plus
- Unary minus
++ Increment
-- Decrement RL
& Address of
* Indirection
sizeof( ) Sizeof
3 * Multiplication
/ Division LR
% Modulus
4 Binary + Addition LR
- Subtraction
5 Shift << Left shift LR
>> Right shift
6 Relational < Less than
> Greater than
<= Less than or equal to LR
>= Greater than or equal to
7 Equality == Equal to LR
!= Not equal to
8 Bitwise & Bitwise AND LR
AND
9 Bitwise ^ Bitwise XOR LR
XOR
10 Bitwise OR | Bitwise OR LR
11 Logical && Logical AND LR
AND
12 Logical OR || Logical OR LR
13 Conditional ?: Conditional RL
14 Assignment = Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference RL
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
>>= Assign left shift
<<= Assign right shift
15 Comma , Evaluation LR
21
22