Introduction
Program: limited or finite steps use data to achieve a specific goal.
Data: any data numbers or alpha.
Information: specific data.
Knowledge: describe characteristics of something like width,
height of triangle.
…In C we use data information and knowledge
Algorithm: way to put some instructions in memory.
Data structure: way to put data in memory.
Levels of SW languages:
Assembly low level language. (work with the architecture of
processor )
Java,Pascal,C/C++ high level language
PLAN (for active network)very high level language.
Executable files deal with OS not machine language
(exe): depend on OS platform and HW platform
Executable files are OS dependent...
and OS’s are H/W dependent... and OS is machine language..
MATLAB is not a general purpose programming language...
actually it has been written using a general purpose programming
language (i.e. C) same as HTML... scripting language
Improvements of SW languages
1) Linear Programming Technique :sequentially (more redundancy)
2) GO TO: very dangerous.
3) Sub program: call sub program then return again.
4) Structure Programming Technique: some data and functions collected
together as a class to be general differentiate in its usage with different
values.
5) Object-Oriented Technique (more general)
OOP = Global Data + Specific Data + Functions .
These functions are implemented using structure programming languages.
6) Agent-Oriented Technique:like AI
Agent: do specific instructions by learning…
Like: aglets and prologo genetic Algorithmvery high level language
How Do Programming Languages Work? How to be executed
1) Interpreter
How Does It Work?
Take source code as input C/C++
Moves line by line
Checks the source code for error and executes it at each line
If it finds an error, it restarts.
Example: Basic
2) Compiler
First, it checks it at all: If it finds an error
Second, it transforms it to an executable file.
Last, it executes the program.
Example: C
3) Interpreter & Compiler
Compiler
Check and return
Intermediate language: Byte Code
Interpreter
Take it and execute it.
To be machine independent: like Java not exe(depend on OS and HW)
C-Programming Language
I. Source file (.c)
Compiler
II. Objective file (.obj)
Linker
III. Executable file (.exe)
(.obj)
Source file(.C) Compiler Linker (.exe)
Compiler
It places some flags (library name) indicating the properness of the inputs of
library functions. It checks the other parts of the source code.
Linker
It removes the flags.
Replaces the library files usage requests with the actual library
files (.exe) It transforms the other parts.
Notes:
Static Link Library
All libraries get loaded into the exe file.
Dynamic Link Library (.dll)
Only the required functions get loaded into the exe file at runtime and just the link for
The library not library itself like in static link library.
Lecture One
I. Preprocessor Part
1. Include always be first of all
2. Define & Macro always be first of all
3. Structures and Unions Description
4. User-Define Functions’ Prototypes
5. Global variables
II. Main Function
III. User defined function
Includes:
C is case sensitive language: small differ from caps
#include <stdio.h> one include for one library
#include “d:\\My dir\\MyLib.h” include from another directory
#include “ MyLib.h” (In the current directory)
Note:
anything inside coots use \\ not \
Includes come as the first thing, we can't put anything before it.
Define:
#define ONE 1
#define TWO ONE+ONE
#define FOUR TWO*TWO
X=Four ; print(x) - x=1+1*1+1=3 print(3)
Print(four)print(1+1*1+1)
Note: #define PI 3.14 ; error
Define replace all at execution time
Macro:
#define intake(x,y) x+y
X=intake(5,7); x=5+7;
User define functions
Headers
Good for modularity
Data: local variables stack
Global variablesheap
Note:
OS can call main function and pass parameters to it, and main can return
parameters to OS.
int main( --, --) I can't say int main(empty) error : unexpected result
But say int main(void)
All small letters at C
All variables at the first of main or functions , can't put them between functions
Any variables between {} be local variables even if inside main
Use printf() to print on screen
Scanf("%d",&x); address
Lecture Two
Operators:
1. Unary + , - , ( )casting , !(false) , ~ (not: invert 0's to 1's and 1's to 0's)
2. Arithmetic + addition, - subtraction, / , *, %
3. Comparison > < <= >= == !=
4. Bitwise (logical) & , |
5. Short-circuit && , ||
6. Shift >> , << , >>>
7. Ternary ?:
8. Assignment “op=”
3+4 2 operators
Unary operators have signs positive and negative (+)(-)
Example of casting:
Int x ;
Float f ;
f = 3.4 ;
x=f; will give error that way
x = (int) f ; o/p will be x = 3
this is called explicit casting because I must do it with myself
Char. int. long float double
Auto conversion
Explicit conversion
Auto conversion if smaller in larger one
f=x;
Explicit conversion if larger in smaller one
x=(int)f;
Note:
if mapping from larger to smaller without casting will losing data,
take the lower and distract the upper, there is no error found
casting doesn’t change in the type of variable but take copy from
it and change the copy then put it to left hand side
int x;
x=5/3; correct but x=1 because x is int
if x=5/3.0; not error but may discard data
Signed : last byte used for + or –
Unsigned : use all bytes of variable
Remainder:
1. 5 % 3 = 2
2. -5 % 3 =-2
3. 5 % -3 =2
4. -5 % -3=-2 mynf3shy atyr esharat m3a b3d 3lshan el computer mosh by3ml kda
-- right hand side neglect its sign
-- left hand side take its sign
number 2
-1
3 -5
(+)-3 result -2
++ or-- increase the value or decrease it by 1 UNIT
Example on prefix and postfix in the incrimination
int x=5 ;
int y ;
y = ++x ; x = x+1 then y = x
y = x ++; y = x then x = x+1
-----
X ++ ; postfix
++ x ; prefix
Shift left has a meaning of multiplying by two <<
Shift right has a meaning of dividing by two >>
>>> unsigned shift right
For unsigned shift right the default number to insert is zero
For signed shift right the number to insert from the left should be a copy of
the bit in the ‘sign’ bit
Sign
1 0010
If shift right by one 1 1010
Ternary : simplified way of if else
X>y ? z=0 : z=10;
if x>y then z=0 else z=10
^ (XOR)Bitwise Exclusive OR
Control system
Branching: if (under condition), switch(not under condition)
Looping : for , while , do while
If:
For every if there is only one else
if( ); wrong that will terminate the if condition
{code} logical error and will execute the
code even if it violate the if condition
Switch:
used with (int) or any auto converted to int like (char)
int x;
switch(x)
{
Case 10;
break;
case 12;
break;
default:
}
If I haven’t inserted break the code will proceed through the cases
Switch test the value of the first case only if there is no break , it will execute
the other cases without testing.
For statement :
1st 2nd 3rd
for (initial statement (s) ; condition(s) ; loop statement (s))
{
…..
}
initial is made only once, conditions is made n+1 times and is checked after the loop
starts, the loop is checked before the loop starts
Example on loop
A program that gets the sum of 10
counts int sum=0 , count;
for (count=0 ; count<10 ; count ++)
{
sum += count +1 ;
}
Note: I can discard the first cond and the 3rd one but not the 2nd
one
while (condition(s))
{
} if the condition is not satisfied it will not enter the loop
do
{
}while (condition(s)) ; the command will be checked at least once
Common mistake :
for (-- ; -- ; -- ) ; inserting ; will result in an infinite loop (logical error)
{
}
For more than one statement .. use a ,
Lecture Three
Array Characteristics:
-Homogeneous: all elements same to each other
-Fixed Size: have start and end
-Finite
-Ordered
-Indexed: reached to any element by index
-Contiguous: reserve block in memory
-Direct Access: mosh lazem amshy mn el 2wl 3lshan 2wsl
int var[10]; size mustn't be variable , should be known at a
declaration
Notes:
The time to reach any element in the array is fixed
Any program see logical addresses in memory, CPU only see
physical addresses
No negative addresses in index of array
Any compiler uses a ‘dope vector’ when dealing with any data type
including the arrays
after the compiler creates the dope vector it create ‘access equation’
at C access out of range doesn’t give error
access time is the time taken to access this equation:
Access Equation =Base address + index*element size
Contiguous disadvantage:
Lecture 4
(Array and strings)
String terminator: ‘\0’
That indicate the end of string, if I have arr[20] and store ahmed in it
This print this array that give you ahmed+ garbage..
Scanf(“%d”,id);
Scanf put terminator automatically.
Name[0]=’a’;
Name[1]=’b’;
Name[2]=’\0’;
Scanf understand the Space and Enter as terminator..
gets handle this bag and take the space as space…
gets() is like scanf() that is both receive input from keyboard, the only
difference is that scanf() has some limitations while receiving strings of
characters, as the foll example illustrates
main()
{
char name[50];
printf("\nEnter name ");
scanf("%s",name);
printf("%s",name);
the output..
Enter name Rahul Kothari
Rahul
"Kothari" never got stored in the array name[], because the moment the
blank was typed after "Rahul" scanf() assumed that the name being
entered has ended. The solution is to use gets(). Thus spaces and tabs are
perfectly acceptable as part of the input string in gets().
The limitation of gets(name) is that it can be used to read only 1 string at
a time.. unlike scanf("%d %c", &age, &initial).. u get the point.
Printf
Puts() doesn’t make formatting
Strlen() get the length of string
Strcpy() copy string from sourc to distination
Strcat() concatenate 2 strings
Strcmp() compare 2 strings:
0 : both are equal
+ : larger
- : smaller
Extended kyes:
Up,down,left,right arrows – end – home –page up and down
Ch=getch(); if null extended
Ch=getch(); to get the value from buffer
getch at the end of the program to wait for user to enter any key..
Lecture 5
Note on Initialization:
int arr[4] = {3,5,7,6}
int arr2d[3][2] = {q,2,3,4,5,6}; 5 is the element (2,0) in the array
another way to do the initialization is int arr2d[3][2] =
{ {1,2},{3,4},{5,6}};
Structure:
1. Definition: outside the main
Struct student
{
Int id;
Int age;
Char dept[15];
}
2. Declare variable: at the first of main
Struct student s1;
Note:
- I can do s2=s1; all field of s1 be in all fields of s2
- Structure at C don’t include functions
3. Deal with it
Printf(“%s”, [Link]);
I can’t print all structure in one time , print it field by filed
4. Initialization
Struct student s1={12, 30 , ‘a’};
Struct student s1={12, , ‘b’};
But I can’t do struct student s1={ ,30,’a’};
I can’t leave the first filed empty
Array of structure:
struct structure sarr[4];
sarr[3].ID=7;
sarr[2].dept=’x’;
Anonymous structure:
ex .
Struct
{
Int ID;
..
}anonymous name;
This is a structure which we don’t declare variable from it it used one time only
Union structure:
Same variable used as different types, char,int ,…
So overwrite in the same address.
union U {
struct {
int a;
double b;
} A;
struct {
char* c;
unsigned d;
} B;
};
Union U u;
U.A.a = 1;
Lecture 6
Procedure : sub program don’t return data
Function: sub program return data
Main can return for OS:
0 normal rermination
1 abnormal termination
2 abnormal termination due to I/O
3abnormal form memory management error
Local variables identified inside functions, and its life time inside
memory=life time of its functions
Local variables stored inside stach
Global, static, dynamic variables inside heap
Scope operator ::
Local x=s;
Global :: x=s;
Recursion: 6!= 6*5!+5*4!+4*3!+3*2!+2*1!
1. The problem should divided into equal sub problems
2. Termination condition
3. Initial value
Int fact(int n)
{
If(n==0|| n==1)
Return 1;
Else return n*fact(n-1)
}