You are on page 1of 15

C middle level Language

It is developed by Dennies Ritchie in 1972 at At & T lab


it was developed on PDP-11 unix based

standards
27 keywords - used in language - ANSI - +5 keyword added
C89 - 32 keywords
C91
C95
C99 - + 5 keywords = 37 keyword
C11

C is procedure oriented language

for laptop
start --> run -->\\sunbeama\precatassign
username:precat
password:precat

password for wifi device precatl-lab4 is precat@sunbeam

for desktop
smb://172.0.1.210/precatassign

for desktop for laptop


/home/precat/P83 d:\smita\p83

to set font of editor


Window -->Preferences --->General-->appreance ->color and fonts

.c source code-->preprocessor--->.i---->compiler--->.asm / .s ----->


assembler--->.obj + library function .obj will be linked by -->linker
---->execuatable file -->.exe / .out or without extension --->loader loads it in
main memory RAM

header fiel includes


Declaration of functions/data types
(intimation to compiler)
definition of symbolic constant/macro
typedefs

Definition
function definition actual set of instructions collected together to
complete task--sub program

types Errors:
1. Complie-time errors
2. runtime time
3. logical errors
4. linker error

Program- set of instructions


a program can be constructed using
1. sequence
2. decision control -- if ..else / switch
3. iteration -- while , do..while , for

formatted function
printf,scanf,fprintf,fscanf
unformatted function
getchar,gets,puts,getc etc.

Format specifiers
%c converts data into character
%d converts data into signed int
%f converts data into float(6 precisions)
%s converts data into string
%ld converts data into long int
%u converts data into unsigned format
%x converts data into hexadecimal format
%o converts data into octal format
%e converts data into exponetial format

primitive data types


char 1
int 4
float 4
double 8
user defined data types
struct,union,enum
derived
array,pointer

Rules of Identifier
1. should start with alphabet or with _ (underscore)
2. can include alphabets,_ (underscore),digits

cvar_1 //valid
1_cvar //not valid
_cvar1 //allowed
cvar-1 //not allowed

Variable lenth function - count of arguments can vary


printf("HelloWorld");
printf("\n a=%d b=%d",a,b);
printf("\n a=%d b=%d c=%d",a,b,c);

Type Modifiers:
1. signed --char -128 to 127
2. unsigned ---char 0 to 255
3. short
4. long

Escape sequence characters


\n adds new line
\r helps to return carraige to the beginning of line
\t adds horizontal tabspace
\b moves cursor 1 character back
\f formfeed
\a alert
\v adds vertical tabspace
\\ prints \
\" prints "

three streams
1. stdin - standard input stream
2. stdout - standard output stream
3. stderr

for project related setting


project-->properties-->C/C++ build --->settings-->gcc linker-->libraries--> click
on + to add flag m ->apply and close

Arithmetical Operators + - / * %
Relational Operators > < >= <= != ==
Logical Operators && || !
Bitwise Operators & | ^ ~ << >>
Assignment Operator =
Unary Operators + - ++ -- * & . ->
Ternnary/conditional Operators ? :
shorthand operators += -= /= *= %=
&= |= ^= ~= <<= >>=
special operators sizeof , (comma)

Decision Control:

if(<expression>)
{
<true block statements>
}

if(<expression>)
{
<true block statements>
}
else
{
<false block statements>
}

if(<expression>)
{
if(<expression>)
{
<true block statements>
}
}
else
{
<false block statements>
}

if(<expression>)
{
<true block statements>
}
else if(<expression>)
{
<true block statements>
}
else if(<expression>)
{
<true block statements>
}
else
{
<true block statements>
}

Conditional Operator
? :

<expression> ? <true statement> : <false statement> ;

if(num1>num2 && num1>num3)


{
max=num1;
}
if(num1>num2)
{
if(num1>num3)
max=num1;
else
max=num3;
}
else
{
if(num2>num3)
max=num2;
else
max=num3;
}

switch(<expression>)
{
case <integer constant>:
<statements>
break;
case <integer constant>:
<statements>
break;
case <integer constant>:
<statements>
break;
default:
<statements>
break;
}
1. It is necessary that case has to be followed by integer constant.
2. every case last statement has to be break statement.
break: is a jump statement which can be used in switch / loop.
break statements helps to move execution control forcefully outof switch / loop
if break statement is not mentioned execution control is given to next cases even
though they are not satisfied. Next cases will be executed till last statment or
till next break statement.

3. duplicate case is not allowed


4. use of default case is optional
5. sequence of cases do not matter

return is jump statement can be used inside function. which helps to move execution
control forcefully back to calling area

enum - helps to define user type.

enum <tagname> {[<enumaraed field as contant,...>]}; //use of tag is optional


enum is collection of enumarated fields. Where each field represents integer
contant.
Use of enum helps to improve readability of source code

If no field is assigned very first member/field of enum recevies default value zero
and next sequential members will receive step 1 value ahead of previous one

we can assign enum field with int constant.

typedef is used to give another name to existing data type. Helps to improve
readabilty of source code.

typedef <existing data type> <another name>;

typedef int MARKS,BOOL;

int val;
MARKS m1;
BOOL status;

Loop / Itereation : is a programming construct which helps to call block of


statements repeatedly.

Category:
1. on entry check loop ----- while , for
2. on exit check loop ----- do while

Conventionally it is said that if no. of iterations are fixed in advance then use
counter loop i.e. for loop

Conventionally it is said that if no. of iterations are not known in advance then
use while or do while loop.

while(<expression>) //on entry chek


{
<statements>
}

While developing loop think about 3 things


1. initial state - before we start loop
2. expression check- if it is true respective iteration is invoked
3. modification statement - is a statement which helps to result expression
check to false state.

jump statements:
break - can be used inside switch or loop which helps to move execution control
forcefully outof loop/switch
return - can be used inside function helps to move execution control forcefully
back to calling function

continue: can be used only inside loop. Helps to move execution control forcefully
to next iteration. Whenever continue is encountered all c statements below continue
are skipped from evaluation in current iteration from.

goto: can be used to mark statements as label and jump to specific label definition

with goto we can jump to label definition which is in scope of current function.

<LABEL>:
<statements>

goto <LABEL>;

if label definition comes in sequence of execution control even though it is not


invoked execution control is given to label definition.

do
{
<statements>
}while(<expression>); //on exit check loop

//no matter what is initial state 1 execution of do..while is fixed

i=5; i=5;
while(i>=10) do
{ {
printf("\n Inside"); printf("\n Inside");
} }while(i>=10);

for loop:

for(<initialisation> ; <expression> ; <modification statement> ) //on entry


{
<statements>
}

for(step1 ; step2 ;step4 )


{
step3
}

User Defined Functions:


Function is set of instructions collected together to complete specific
task. Which is also called as sub program.
We can write sub program using function or procedure.

A function which may or may not return value


A Procedure which never return value.

Writting function helps to


1. improve readability of source code
2. helps to reuse code
3. reduces complexity

Types of functions:
1. Library Functions
2. user defined functions

A function which may or may not return value


A function which may or may not take argument

We can pass arguments to function using


1. pass by value
2. pass by address

Calling Conventions:
_CDecl
_stdCall
pascal
_fastcall

a++ + ++b

a=11 b=3
printf("\n %d %d %d",++a,b++,a=10);
11 2 10

a=2;
printf("\n %d %d %d",++a,a++,++a);

When user defined function is to be written 3 things to be rembembered


1. declaration
int printf(const char *s,...);
<return type> <identifier> ([<arg type>...]);
2. definition
int printf(const char *s,...)
{

}
<return type> <identifier> ([<arg type> <identifier>...])
{
}

3. call
int x= printf("Hello World");
<location> = <identifier>(<arg value/address>);

printf("%d",10); //10 here is passed by value


scanf("%d",&x); //&x is arg which is passed by address

Storage Classes:
scope life memory from default value
auto block block stack garbage

register block block cpu register garbage

extern program program data section 0


(applicable to global resource)

static block program data section 0

scope: to whom resource is known .. who can access resource

life : time span how long memory is retained in alive/valid state.

register : when register storage class is used we try to request register to


identify with some name. There is no gaurantee our request will be entertained.

As no. of registers availability is very less our request may be rejected and it
will be automatically converted in auto type. Needs more time and slows down
performance

if request is entertained then programmer can enjoy speed/performance of


application

we can not apply address of operator on registers.

We can not request resiters other than local scope.

static:
1. can be declared within function
2. it is neceesary to initialise static variables at the time of
declaration else it voilets the rule of static
3. static variable is to be initialised with constant value only.

static variables helps to retain state of particular variable through multiple


calls of same function. static variables are initialised only once on first
invocation of particular function in which is declared
Recursion: is a algorithm in which the one functionality which is called at last
will complete its job first (LIFO)

It is necessary to mention terminating condition else causes runtime error - stack


overflow

Recursion helps to call block of statement repeatedly

In case of recursion function is called within it's own definition

recursion loop
uses more memory less memory utilisation

follows LIFO FIFO

more time consumption less time consumption

if no terminating condition is if no terminating condition is


mentioned causes to runtime error mentioned causes to infinite loop
stack overflow state

Pointer: is a derived data type. Using pointer concept we can create a variable to
store address. We hould always store a valid/in alive state address in pointer.

In case of pointer we can use operators


1. & address of oeprator
2. * value at operator / indirection operator / dereferencing operator

* operator is always applied on address as operand

Address through system is always given in unsigned int format. Hence size of any
pointer will be equals to unsigned int of respective compiler.

int *ptr; //ptr is pointer to integer

we can declare n indirection level of pointers

void * is a pointer which is designed to hold address of any location. It is a


generic pointer.

char *cptr=100 //assumes address is of character whose scale factor is 1byte


*cptr dereferes 1 byte from given base address 100

int *ptr=100; //assumes address is of integer whose scale factor is 4 bytes


*ptr dereferes 4 bytes from given base address 100

void pointer is not aware of scale factor i.e. how many bytes to be dereferred from
given base address. Hence if we want to receive value at void pointer it is
necessary always typecast prior its to use.

void *vptr;
vptr=&x;
*vptr ?????????? // error unknown abot bytes to be derefferred

Array:is a collection of similar type elements.

In case array a common name is shared amongst all members where as every member is
uniquely identified with index value.

Very first element of array receives by default index as 0 and last (nth position)
element receives index as n-1

Memory is always given in sequence.

We can implement array statically or dynamically.

If it is static implementation of array memory once given can't be shrinked or


grown at runtime.

In case of static implementation base address of array is always locked by


compiler.

If prerequisite of how many elements to processed is known in advance use static


implementation

If array is initialsied partially at the time of declaration then elements which


are not initialised are set with default value 0(ZERO).

We can declare n dimentions of array.

It is necessary to specify last dimention in case of all dimention of array only in


case of 1D array we can skip last dimention providing members of array are
initialsied at the time of declaration.

Array bounds is job of programmer.

When we request array a location is identified with array name stores always first
element address of first element because of which array interchangability with
pointer is possible

You might also like