Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

CP-EXAMEN!!!!

Algorithm, given both the problem and the device: precise characterization of a
method of solving the problem, presented in a language comprehensible to the
device.
Properties:
◼ Application of the algorithm to a particular input set or problem description
results in a finite sequence of actions.
◼ The sequence of actions has a unique initial action.
◼ Each action in the sequence has a unique successor.
◼ The sequence terminates with either a solution to the problem, or a statement
that the problem is unsolvable for that set of data.

Pseudocode – uses English-like statements which outline a particular task or


process.
◼ Short version of the actual computer code.
◼ Advantages:
◼ Can be translated into a programming language easily.
◼ Compact
◼ Looks like the final code.
What Is Programming?
Programming =
◼ Process of taking an algorithm and encoding it into a notation, a
programming language, so that it can be executed by a computer.
◼ The important first step = the need to have the solution
◼ No algorithm  no program.
C Strengths
◼ Efficiency: intended for applications where assembly language had
traditionally been used.
◼ Portability: hasn’t splintered into incompatible dialects; small and easily
written
◼ Power: large collection of data types and operators
◼ Flexibility: not only for system but also for embedded system
commercial data processing ◼ Standard library
◼ Integration with UNIX

space ’ ’, tab ’\t’, newline ’\n’


To indicate length and signedness:
◼ L, l: long
◼ U, u: unsigned
◼ UL, ul, LU, lu: unsigned long
◼ Examples: 100, 100L, 100U, 100ul
Constants. Integers
◼ Octal constants
◼ Begin with a zero and contain only octal digits
◼ Are unsigned or unsigned long
◼ Examples: 0144 (=100 decimal), 0176 (=126)
◼ Hexadecimal
◼ Begin with 0x or 0X and contain only hex digits
◼ Are unsigned or unsigned long
◼ Examples: 0xab1 (=2737 decimal)

Data type Size in bytes Range

Char or signed char 1 -128 to

Unsigned char 1 0 to 255

Note: In C, there is no Boolean


data type.
int alpha[10]={0, 1, 2, 3, 4, 5};
int matrix[5][5]= { {1, 2, 3}, {5, 6, 7, 8}, {9, 10, 11}, {7, 7, 7, 7}}
CONVERSIONS IN C
EXPRESSIONS.OPERATORS
Operators are grouped in classes
◼ Arithmetic operators: unary + – and binary + – * / %
◼ Relation operators < <= > >=
◼ Equality operators == !=
◼ Logic operators: negation !, and &&, or ||
◼ Bitwise operators: one’s complement ~, left shift <>, and &, or |,
exclusive or ^
◼ Assignment operators: assignment =; compound assignment: op=
where op is an arithmetic or bitwise operator. The effect is: v op = operand
 v = v op operand
◼ Increment/decrement operators: ++, – –
◼ Type cast operators: (type) operand
◼ Size operator: sizeof(variable), sizeof(type )
◼ Referencing operator: &identifier
◼ Parenthesis operator: () []
◼ Conditional operator: ? : (expression? exp1: exp2)
◼ Comma operator: ,
◼ Dereferencing operators: *, ., ->
◼ C++ specific:
◼ Memory allocation/free: new/delete
◼ Resolution: ::

EXPRESSION EVALUATION
◼Prefix: in evaluation the value after increment/decrement is used,
e.g. x=10; y=++x; /* y=11 and x=11 */
◼ Postfix: the value before increment/decrement is used in evaluation
x=10; z=x++; /* z=10 and x=11 */

OPERATOR PRECEDENCE AND ASSOCIATIVITY


for(i = 0, s = 0; i < n; s += a[i], i++);// Here the for does nothing because it is the
empty statement
THE SWITCH STATEMENT
◼ Format:
switch ( expression )
{ case C1: statements_1;
break;
case C2: statements_2;
break;
...
case Cn: statements_n;
break;
default: statements; /* optional */ }
◼ Effect:
◼ expression is evaluated
◼ if the result matches one of the constants Ci then statements_i are
executed
◼ if the result matches no constant, then the statements following the label
default are executed
◼ If no break statement is present, then control falls down till a break is
met or the switch ends
THE DO-WHILE STATEMENT
◼ Format:
do
statement;
while (expression);
◼ Effect, described in terms of while:
Statement;
While(expression)
Statement;

!! NOTE: THE LOOP BODY IS EXECUTED


AT LEAST ONCE!!!

THE STATEMENTS CONTINUE AND BREAK


continue causes the current iteration to be abandoned and, for
◼ for statement, the re-initialization step is executed, and loop expression
is reevaluated
◼ while, do-while: evaluation of expression controlling the loop ◼ break
terminates the loop and execution continues with the statement
immediately following the loop
◼ break terminates the loop and execution continues with the statement
immediately following the loop
PREPROCESSING IN C
◼ The inclusion directive is specified as:
#include <file_specifier>
or
#include "file_specifier"
CALLING FUNCTIONS
◼Correspondence between formal and effective parameters is positional:
◼If an effective parameter type is different than the formal
parameter type, an automatic conversion occurs
◼Call by value:
◼ Copy of argument passed to function
◼Changes in function do not effect original
◼Use when function does not need to modify argument

◼Call by reference (in C++ only)


◼ Passes original argument
◼ Changes in function affect original
◼Only used with trusted functions
◼When values of array elements or the referred value should
not be changed by a function use pointer to constant construct
for formal parameters : const type *identifier
EXTERN VARIABLES
extern type identifier, identifier;
Can be declared:
◼after a function header. Scope is within function.
◼ at the beginning of the source file: scope is all functions
in that file
CATEGORIES OF VARIABLES:
◼automatic:
allocated on the stack at run time
cease to exist upon function return, or when control leaves
block
example: int a, b, c; double x;
◼static:
allocated by the compiler in a special area
persist during program execution
cannot be declared extern  example: static int x, y, z;
◼registers:
allocated in the registers of the processor
declared using: register type variable_name
AUTOMATIC VARIABLES:
Automatic variables exist only while the function is being executed: they
are destroyed when the function is exited and re-created when it is re-entered
STATIC AUTOMATIC VARIABLES
Automatic variables that are declared static have permanent storage: their
value is preserved across function calls
1) A static int variable remains in memory while the program is running. A
normal or auto variable is destroyed when a function call where the
variable was declared is over.
2) Static variables are allocated memory in data segment, not stack
segment.
3) Static variables (like global variables) are initialized as 0 if not initialized
explicitly. For example in the below program, value of x is printed as 0,
while value of y is something garbage.
STRINGS:
The last character in the array is the ASCII NUL ('\0')
The name of the array is a constant pointer to the first element
Character string array declaration:
char *arr[]={"string0", "string1", "string2", ..., "stringn"}
CHARACTER HANDLING LIBRARY
STRING CONVERSION FUNCTIONS

STRING MANIPULATION FUNCTIONS OF THE STRING HANDLING


LIBRARY

SEARCH FUNCTIONS OF THE STRING HANDLING LIBRARY

COMMON C ERRORS:
double half = 1/2;
◼This sets half to 0 not 0.5!
◼ 1 and 2 are integer constants.
◼ At least one needs to be floating point: double half = 1.0/2;
◼ Or cast one to floating point: int a = 1, b = 2; double half =
((double)1)/2.

You might also like