0% found this document useful (0 votes)
3 views

3.basics of c

Uploaded by

Tarakeswara Rao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

3.basics of c

Uploaded by

Tarakeswara Rao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

History of ‘C’ language

The features of ‘C’ are derived from many of its ancestor languages as shown in the
following hierarchy.

Language developed Developed by

ALGOL 60 (ALGOrithmic Language) International Committee, 1960

CPL (Combined Programming Language ) Cambridge University, 1963

BCPL ( Basic CPL) Martin Richards, 1967

B (Basic language) Ken Thomson, 1970

C ( Traditional ‘C’ ) Dennis Ritchie, 1972

K&R C B.W.Kennighan, D.M.Ritchie, 1976

ANSI C (American National Std Institution ) International Committee, 1989

ISO ( International Standards Organization ) International Committee, 1990

Characteristics of C language:

1. C language is simple to learn and simple to use.

2. C programs are efficient in terms of the execution speed.

3. C language contain many built-in functions.

4. C programs are portable from one environment to another environment.

5. C language supports modular programming

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.

7. C language is case sensitive.

8. C language is widely used for developing system software.

1
Structure of C program:

Documentation section //optional


 Preprocessor directive section //optional
 Global declaration section //optional
 Main function section //mandatory
main( )
{
Declaration Part
Executable Part
}
User defined function section //optional
Function-1
Function-2
Function-3
:
:

A ‘C’ program contains several sections as described above.


The documentation section contains the information about the program such as
1. Purpose of the program
2. Name of the program
3. Author of the program
4. When the program is developed
5. Where the program is developed etc.
In a ‘C’ program, the above information will be placed in the form of comments. Comments
are written by using /*----*/ or //(single line comments)
The preprocessor directive section contains the statements which start with ‘#’. These
preprocessor directive statements are processed by the preprocessor and makes the ‘c’
program ready for execution.
The Global declaration section contains declarations of variables, declarations of
functions and definitions of functions. All these declarations and definitions must be placed

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.

The character set of C language:

It contains the following four types of characters.

1. Alphabets: A-Z, a-z

2. Digits: 0-9

3. Special symbols: #,+,-,*,/,&,%,!,?,:,; >,<,= …. etc.,

Symbol Meaning Symbol Meaning Symbol Meaning


{ Opening ‘ Apostrophe ^ Caret or
curly brace exclusive
OR
} Closing curly “ Double & Ampersand
brace quotation
mark
( Opening ~ Negation or * Asterisk
parenthesis tilde
) Closing ! Exclamation + Plus
parenthesis
[ Opening # Pound or - Minus or
square number or hyphen
bracket hash
] Closing % Mod / Forward
square slash
bracket
. Dot or ; Semi-colon \ Backward
period slash
? Question : Colon > Greater
Mark than
| Pipe , Comma < Lesser than
_ Underscore = Assigns to

4. White spaces: blank space, tab, new line character, carriage return, form feed

Data types in C language:

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;

Example: int x; x=15;

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;

Example: char x; x=’a’;

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;

Datatype Size(in bytes) range


int 2 bytes or 4 bytes (varies from one compiler to -32768 to +32767
another).
float 4 bytes or one word. -3.4 e 38 to 3.4 e 38 with 6
digits of precision.
double 8 bytes or two words. -1.7 e 308 to 1.7 e 308 with 10
digits of precision
char 1 byte. -128 to +127
void 0 bytes. Valueless

Type qualifiers or Type modifiers:


There are 4 kinds of type qualifiers in ‘C’ language.
1. Signed
2. Unsigned
3. Short
4. Long
Signed:
The type qualifier “ signed” tells the compiler that a particular data type can store both
positive and negative values.
Syntax of signed qualifier is

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

unsigned datatype variablename;

Example: unsigned int x;


The unsigned type qualifier tells the compiler that only positive values will be stored in a
particular datatype i.e., the most significant bit (MSB) is also used for the storing value.

MSB-0 1 2 3 4 5 6 7 8 9 10 11 1 13 14 LSB-15
2

All the 16 bits are used for storing value.


Short:
The type qualifier “short” is used to decrease both size and range of a particular data type.
The syntax of short qualifier is

short datatype variablename;


Example: short int x;
Long:
The type qualifier “long” is used to increase both size and range of a particular data type.
The syntax of long qualifier is

long datatype variablename;


Example: long int x;
We can combine the type qualifiers and can apply on basic data types. Various combinations
of type qualifiers that can be applied on a particular datatype is given in the following table.

Datatype Size(in bytes) range

char / signed char 1 -127 to +127


unsigned char 1 0 to 255
int / signed int / short int/ signed 2 -32768 to +32767
short int

6
unsigned int / unsigned short int 2 0 to 65535

long int / signed long int 4 -2,147,483,648 to +2,147,483,647

unsigned long int 4 0 to 4,294,967,295


float 4 -3.4 e 38 to +3.4 e 38 with 6 digits of
precision.
double 8 -1.7 e 308 to +1.7 e 308 with 10 digits
of precision.
long double 10 -1.7 e 4932 to +1.7 e 4932 with 10
digits of precision.

Derived data types:


There are 4 kinds of derived data types in ‘C’ language.
1. Arrays
2. Pointers
3. Structures
4. Unions
Arrays :
Arrays are the derived data types in C language which can store multiple data items at a time.
All the data items are stored in physically continuous memory locations.

A 9 4 10 20 12

1000 1001 1002 1003 1004


Pointers :
Pointers are derived data types which can store the address of general variables. Pointer can
also be used to access the value of a variable indirectly.
Example: int *p,a=5;
p=&a;
printf(“%d %d”,a, *p);
here, the value of ‘a’ is 5 and also the value of ‘*p’ is 5.
Structures , Unions:

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;

User defined data types:


There are 2 kinds of user defined data types in ‘C’ language.
1. typedef
2. enum
typedef: typedef is used to define a new data type for the existing data types.
Example: long int x,y;
typedef long int li;
li x,y;
enum: enum is used to treat strings as numeric data.
enum type_name{ value1, value2,...,valueN };

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.

auto break case char const continue


default do double else enum extern
Float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while

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:

1. The first character in the name should be either an alphabet or an underscore.

2. The other characters in the name can be alphabets, digits and underscores.

3. Names should not contain blank spaces and hyphens.

4. Keywords should not be used as names.

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.

Examples: 25 - decimal integer , 016 - octal integer , 0x196 - hexadecimal integer

34.87 - floating point constant 348.7 e -1 - floating point constant

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

()- used to represent functions

[]-used to represent arrays

{}-used to represent blocks

5. Operators:

Operator is a symbol which is used to perform a particular operation on data items(operands).


The data items on which the operations are performed are called operands.

A valid combination of operators and operands is called an expression.

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.

C language provides the following types of operators.

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:

C language supports or contains the following five types of arithmetic operations.

Operator Name Purpose

+ 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:

Unary plus Binary plus


x=+10 x=y+z

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:

Unary minus Binary minus


x= -10 x=y-z

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:

Binary modulo division


x=y%z
if y=10, z=3
then x=1

Assignment operator:

Assignment operator is a binary operator. The purpose of assignment operator is to store


the value of right hand side of assignment operator into the operand on left hand side.

Example:

Given values Return value


x=10 x=10
y=5 y=5
a=x a=10
b=x+y b=15

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.

Short hand assignment operators:

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:

Given values Return value


x+=5 x=x+5
x-=5 x=x-5
x*=5 x=x*5
x/=5 x=x/5

Relational operators:

There are 6 types of relational operators in C language.

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

Any relational expression can return either true or false (1 or 0).

Example:

Given values Return value


If a=10, b=20
a<b True(1)
a>b False(0)
a<=b True(1)
a>=b False(0)
a= =b False(0)
a!=b True(1)

Logical operators:

Logical operators are mainly used to combine relational expressions that forms a compound
relational expression.

There are 3 types of logical operators in C language.

Operator Name
&& logical AND
|| logical OR
! logical NOT

The logical AND is a binary operator which behaves according to the following truth table.

rel1 rel2 rel 1&&rel2


T T T
T F F
F T F
F F F
Example:

Given values Return value


If a=10, b=20, x=20, y=15
then
(a<b)&&(b<x) False(0)

14
(a<x)&&(b>y) True(1)

The logical OR is a binary operator which behaves according to the following truth table.

rel1 rel2 rel1| |rel2


T T T
T F T
F T T
F F F
Example:

Given values Return value


If a=10, b=20, x=20, y=15
then
(a>b)||(x>y) True(1)
(a>x)||(b<y) False(0)

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:

Given values Return value


If a=10, b=20, x=20, y=15
then
!(a<b) False(0)
!(x<y) True(1)

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.

bit1 bit2 bit1&bit2


1 1 1
1 0 0
0 1 0
0 0 0
Example:

Given values Binary form Return value


If a=6 00000110
b=5 00000101
then a&b 00000100 4

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.

bit1 bit2 bit1|bit2


1 1 1
1 0 1
0 1 1
0 0 0
Example:

Given values Binary form Return value


If a=6 00000110
b=5 00000101
then a|b 00000111 7

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.

bit1 bit2 bit1^bit2


1 1 0
1 0 1
0 1 1
0 0 0
Example:

Given values Binary form Return value


If a=6 00000110
b=5 00000101
then a&b 00000011 3

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:

Given values Binary form Return value


If a=6 00000110
Then ~a 11111001 249

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:

Given values Binary form Return value


If a=18 00010010
Then a<<2 010010 //remove 2bits on left side
01001000 //append 2 “zeroes” on right side 72

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:

Given values Binary form Return value


If a=18 00010010
Then a<<2 000100 //remove 2bits on right side
00000100 //append 2 “zeroes” on right side 4

Increment / Decrement operators:

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:

Pre increment Return value Post increment Return value


If a=10, b=++a If a=10, b=a++
Then Then
1. a=a+1; a=11 1. b=a; b=10
2. b=a; b=11 2. a=a+1; a=11

The decrement operator (--) is used to subtract a quantity ‘1’ to the operand. Decrement
operators are divided into two types as given below.

Example:

Pre decrement Post decrement


If - -x If x- -
Then x=x-1 Then x=x-1

When decrement operators are combined with an assignment operator, then the behavior of
predecrement will differ from post decrement.

Example:

Pre decrement Return value Post decrement Return value


If a=10, b=--a If a=10, b=a--
Then Then
1. a=a-1; a=9 1. b=a; b=10
2. b=a; b=9 2. a=a-1; a=9

18
Conditional operator:

Conditional operator (?:) is a ternary operator which operates on three operands. The general
syntax of conditional operator is given below.

(operand1) ? (operand2) : (operand3);

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.

The main purpose of a conditional operator is to evaluate simple conditions.

Example:

Given values Return value


If x=10, y=20 and
C=(x<y)?(return x): (return y)
then
C 10
Special operators:

There are 2 types of special operators in C language.

1. Comma operator

2. sizeof() operator

The comma operator is used to combine several statements to form a compound statement.

Example:

Given values Using comma operator


int a; int a, b, c=20, x=5;
int b;
int c=20;
int x=5;

When more than one expression is combined using comma operator then the result of last
expression will be returned by the comma operator.

Example:

Given values Return value


If c=(a=10, b=20, a+b)
Then c 30

If c=(a=10, b=20, a-b, a+b)


Then c 30

19
The sizeof() operator is used to find the number of bytes required for a data item or variable.

compiler int float Char

16-bit 2 4 1

32-bit 4 4 1

Operator precedence and Associativity:-

Precedence is used to determine the order in which different operators in a compound


expression are evaluated.

Associativity is used to determine the order in which operators with same precedence in a
compound expression are evaluated.

Example: consider the compound expression 2+3*4


Here multiplication operator (*) has the highest precedence and hence will be evaluated
first.
Therefore the above expression is equal to 2+(3*4)=14.
Associativity can be either Left to Right Associativity (or) Right to left
Associativity.
Left to Right Associativity:
Expressions are evaluated by starting at left side and moving towards the right.
Example: consider the expression 3*8/4
Here the expression contains 2 operators ‘*’ and ‘/’ which have the same precedence level.
The associativity of these operators is from left to right.
Therefore the above expression is equal to (3*8)/4=6.
Right to Left Associativity:
Expressions are evaluated by starting at right side and moving towards the left. Several
operators have right to left associativity as given in the precedence table.
Example: consider the expression a+=b*=c
Here the expression contains 2 short hand assignment operators ‘+=’ and ‘*=’ which have the
same precedence level. The associativity of these operators is from right to left.
Therefore the above expression is equal to a+=(b*=c).

20
Precedence Category Operator Operation Associativity
1 Highest () Functional call
precedence [] Array subscript
 C indirect component selector LR
:: C scope resolution
. C direct component selector
2 Unary ! Logical NOT
~ Bitwise (One’s) complement
+ Unary plus
- Unary minus
++ Increment
-- Decrement RL
& Address of
* Indirection
sizeof( ) Sizeof
3 * Multiplication
/ Division LR
% Modulus
4 Binary + Addition LR
- Subtraction
5 Shift << Left shift LR
>> Right shift
6 Relational < Less than
> Greater than
<= Less than or equal to LR
>= Greater than or equal to
7 Equality == Equal to LR
!= Not equal to
8 Bitwise & Bitwise AND LR
AND
9 Bitwise ^ Bitwise XOR LR
XOR
10 Bitwise OR | Bitwise OR LR
11 Logical && Logical AND LR
AND
12 Logical OR || Logical OR LR
13 Conditional ?: Conditional RL
14 Assignment = Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference RL
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
>>= Assign left shift
<<= Assign right shift
15 Comma , Evaluation LR

21
22

You might also like