Professional Documents
Culture Documents
C
C
CHARACTERISTICS
Correctness- The extent to which a program
satisfies its specification and fulfills the
customer’s mission objectives .
Arithmetic operators.
Relational operators and logical operators.
Increment and decrement operators .
Bitwise operators.
Assignment operator
Ternary operator or conditional operator.
Arithmetic operators
expression 1 ;
while ( expression 2)
statement ;
expression 3 ;
Do-While
Automatic Variables
External Variables
Static Variables
Automatic Variables
What is a pointer?
A pointer is a group of memory
locations that stores the address of
another memory location.
The operators used here are the unary
operators “ * ” and “ & ”.
The unary operator “ * ” is the indirection
or dereferencing operator ; when applied
to a pointer , it accesses the object the
pointer points to .
The unary operator “ & ” gets the address
of the memory location where a variable
resides.
e.g. &a gets the memory address of the
variable a .
Declaration of pointers
Pointers are declared using following syntax :
char a[10],ch;
ip y 4000 ip y 4000
4000 5 2 4000 3
2
Difference between pointers
and ordinary variables
For ordinary variables the C compiler
allocates memory on its own at
compilation time.
memory location by assigning the
address of some other variable to the
pointer or by allocating memory when
the program runs.
Allocation of memory is done by using
the C function “ malloc ”. The syntax of
malloc is:
pointer=(datatype *) malloc (size of
memory to be allocated)
e.g. to allocate 10 bytes of memory to
the pointer to integer *ip:
ip=(int *) malloc (10);
e.g. to free the memory allocated to the
pointer to integer *ip:
free(ip);
C Memory model
The C language views a program’s
memory as being divided into three pieces
:
Text Area : The area for the program
code(instructions).
Data Area : For static variables and data.
Dynamic Area : For the automatic
variables ,for dynamic allocation and for
function call bookkeeping.
The following diagram shows a conceptual
model of the memory layout for a C
program.
Text
Data
Dynamic
Besides the instructions which make up
the program , other items may also be
placed into the text area. Constant
items such as strings , jump tables for
switch statements , and floating point
constants are occasionally also allocated
in the text area. The text section is not
modifiable.
Data objects which have a lifetime that
persists for the entire program are placed
in the data area. In many implementations ,
strings , floating point constants , and switch
tables are also placed in this area.
The data area is sometimes divided into
two sections , an area for explicitly initialised
data and an area for “uninitialised” global and
static data.
The dynamic area includes automatic
variables , function housekeeping
information , and storage which is used
when objects are created using the
memory allocation routines.
The dynamic area is often
divided into two sections . The first is the
stack .
The stack is the area where automatic
variables are dynamically allocated for
each function when the function is
invoked.
The stack is also the area where , in
almost all systems , the actual
arguments are pushed for function calls.
Function housekeeping information is also
pushed onto the stack when the called
function is entered. The stack is also the area
where compiler-generated temporaries (used
during complex computations and some
optimizations) are kept.
#include<stdio.h>
#define STACK_SIZE 20
int stack[STACK_SIZE]; /*space for stacking
integers*/
int top=-1; /*top_of_stack is
defined as global
variable for a global stack */
/*Function to check whether the stack is ‘full’ */
int stack_full()
{
if(top==STACK_SIZE)
return(1);
else
return(0);
}
/* Function to check whether the stack is ‘empty’ */
int stack_empty()
{
if(top==-1)
return(1);
else
return(0);
}
/*Function to push or add an element on the stack.*/
void push(int number)
{
stack[++top]=number; /*add element on top of stack
*/
}
/* Function to pop an element from the stack*/
int pop()
{
int number;
number=stack[top]; /*remove top element
from stack */
top--;
return(number);
}
QUEUE
Head