Está en la página 1de 20

DATA STRUCTURES

REMEMBERING

DATA STRUCTURES : LET US REVISE C


Constants: values that cannot be changed.
Integer Constants:
Any number without decimal.
The value can be negative or positive.
Apart from the signs (+ and -) no other special
characters are allowed to form an integer.
The biggest number to be stored will be 2n-1-1
whereas the smallest number will be -2n-1; where
n is the size of the word. Thus in a 16-bit
computer the allowable range of integers is from
+32767 (216-1 -1) to -32768 (-216-1).

DATA STRUCTURES : LET US REVISE C


Real Constants:
The real constants are the numbers with decimal; signed or
unsigned.
Examples: 1.023
+45.6789
-975.431 0.0000001
These are fraction form of representation of real constants.
** The exponential form or floating point form or
scientific notation.
The mantissa and the exponent, separated by 'e'.
The mantissa follows the rules of real constant in fractional
form and the exponent follows the rule of an integer.
Value = Mantissa * 10^ exponent
Examples: 2.6e02
-5.2e05 6.4e-06 etc. These are equal
to
2.6x100, -5.2x100000 & so on.
The range of the real constant is from +3.4e-38 to
+3.4e+38.

DATA STRUCTURES : LET US


REVISE C

Character Constants:
A Character constant is either
a single alphabet,
b D
a single digit or
9 4
a single special symbol
/ #
enclosed within single inverted commas.
Example: 'A' '+' 'v' '4' etc.

DATA STRUCTURES : LET US


REVISE C

Identifiers / Variables:
Each and every Byte of memory is
sequentially assigned an address, starting
with zero from the first Byte.
To take away the tremendous load on
programmer, Language compilers give a
facility by which these addresses can be
named.
To refer to this memory location this name
is used.
These are called Identifiers or Variables.

DATA STRUCTURES : LET US


REVISE C

Identifiers / Variables(Contd.):
Basic rules for naming variables:

Constructed with a combination of alphabets,


numerals and / or underscores. Other special
character and space is not allowed.
Most compilers allow 8 character long identifier
name.
The first character of an identifier should be
alphabet.
Variable names are case sensitive.
There are primarily three types of identifiers
depending on the type of data stored in the
identifier; viz. Integer, Real and Character
variables.

DATA STRUCTURES : LET US


REVISE C

Commands of C
There are some fixed words, which form
such commands in the C languages. These
fixed words are also called as C Key Words
or C reserved words.
There are 32 such Key words in C.
No identifier or variable name should
normally replicate a Key word. Though there
are some compilers which allow to give a
variable name resembling a Key word, but it
is a good programming practice to
avoid such naming.

DATA STRUCTURES : LET US


REVISE C

Key Words of C.
auto
double int
long
switch
Case
enum register
return Union const
unsigned for signed
Default goto
sizeof
do
if

struct

break else

typedef char extern


float
short
Void continue
volatile
static while

DATA STRUCTURES : LET US


REVISE C

C is a function-oriented language.
A C program is a collection of function(s).
A function is a sub-program that contains
instructions or statements to perform a
specific computation on its variables.
When its instructions have been executed, the
function returns control to the calling program.
Functions may receive or return some variable
values, as arguments, from the caller or to
the caller(only one).
The type of a function is the type of the

value being returned.

DATA STRUCTURES : LET US


REVISE C

Decision Control Statements These make


a program a program. These make the decision
by the program e.g. if-then-else, switch etc.
If (condition) {action} else {action}
If (condition) {action} else if (condition)
{action} else if
Nested if is possible.
Relational operators - ==, !=, <, >, <=, >= etc.
Logical operators - && (logical and), || (logical or)
Hierarchy of operators Unary(- +), Logical not(!),
( / * % ),( + -), relational( < > <= >=), relational
(== !=), logical (&& ||), assignment (=) etc.

DATA STRUCTURES : LET US


REVISE C

Loop Control Statements


e.g. for loop, while loop etc.

# While loop while (condition) statement


block;
Use of break, continue and exit()
The statements may not be executed even once.

Nesting is possible.

# Dowhile loop do {statement block}


while(condition)
At least once the statements are executed.
For loop for ( intialiser; test; increment)
{statement block}
All expressions are optional

*DATA STRUCTURES : LET US


REVISE C

Increment and Decrement Operators:


x + 1 can be written as ++x or x++. Same way
x 1 can be written as x or x .
++x is having preincrement operator and x+
+ the postincrement operator
Same way there is predecrement (- -x)
operator and postdecrement (x- -) operator.
Extensively used in while and for loops for
counter increment / decrement.

*DATA STRUCTURES : LET US


REVISE C

Increment and Decrement Operators: ++x is using


preincrement operator and x++ is using the postincrement
operator
Consider the following program,
#include <stdio.h>
main() {
int count = 0, loop;
loop = ++count; /* same as count = count + 1; loop = count;
*/
printf("loop = %d, count = %d\n", loop, count);
loop = count++; /* same as loop = count; count = count + 1;
*/
printf("loop = %d, count = %d\n", loop, count);
}
Sample Program Output
loop = 1, count = 1
loop = 1; count = 2
N.B. loop = ++count; means increment count first, then assign
the new value of count to loop. Same is with --count and count-- ,
only decrement in stead of increment.

*DATA STRUCTURES : LET US


REVISE C

Compound Assignment Operator:


A statement like
sum = sum + a;
can be written as sum += a;
Compound statement can be used with
following operators:
+, -, *, /, %,
<<(left shift), >>(right shift),
bitwise operators like &(AND), |(OR),
^(XOR).

*DATA STRUCTURES : LET US


REVISE
C
Pointers and Addresses
Pointer is a variable containing address of another
variable.
Say a variable k contains a value 19 stored at a
memory location 7351
Then address of k is 7351 and the value 19.
Address can be fetched by & operator --> &k.
This address can be stored in a variable that is called
a pointer declared as say int *pk
Declaration int *pk interpreted by compiler as below
* tells that this is a pointer
int tells the pointer is pointing to an integer
Example int k = 19, pk, j; pk = &k; j = *pk; (*pk
gives the value stored in that address)

*DATA STRUCTURES : LET US


REVISE C

Pointer arithmetic

Pointers can be incremented or decremented.


Two pointers cannot be added, multiplied or
divided.
Unary operator & and *(pointer) binds more tightly
than arithmetic operators
So, *pk +1 will add 1 to the value contained
in the address pointed by pk
Whereas, *(pk + 1) will increment the
address contained in pk and give the value
stored there.
While using the increment or decrement operators,
unary operators are evaluated from right to left.

So, with *pk++ the address is


incremented after the value being read.

*DATA STRUCTURES : LET US


REVISE C

So, with *pk++ the address is incremented


after the value being read.
With (*pk)++ the value is first fetched and
then the value is incremented.
With *(pk++) the address is incremented
before the value is being read.
What is *pk += 1; (value contained in pk is
incremented by 1)
Increment of pointer is done logically to the
next address, as per the type of the
pointer (char or int or float) the address is
changed adding 1, 2 or 4 bytes.
To print the address of any variable use %u
as format that is unsigned int.

DATA STRUCTURES : LET US


REVISE C

Algorithm Analysis
Time and space analysis of Algorithms
Order Notations O, ,

DATA STRUCTURES : LET US


REVISE C
ALGORITHM

An algorithm is an effective method expressed


as a finite list of well-defined instructions for
calculating a function.
Starting from an initial state and initial input
(perhaps empty), the instructions describe a
computation that,
when executed, will
proceed through a finite number
of welldefined successive states, eventually producing
"output" and terminating at a final ending state.

DATA STRUCTURES : LET US


REVISE C

END of this part

También podría gustarte