2. Referencess Books
A book on C All KELLEY, İra POHL
Data Structures and Program Design in C++ By Robert L.
Kruse and Alexander J Ryba
Referencess Book
2
3. You can not do anything without data on world . İf you want to
proceed add operator, you should have at least two number.
Computer sciences deal with storing, organizing and retrieving
effectively of data.
Computer programmer should get data from user or another
sources and use them.
for this purpose you have to use data structure for every software
program or system.
Why data structures is important for
computer engineering?
3
4. WE will use eclipse IDE for developing program in this course.
Steps to install Eclipse and run
Download eclipse Neon from
http://www.eclipse.org/downloads/
Choose Eclipse IDE for C/C++ Developers and install
You should download and install MinGW GCC
http://www.mingw.org/
Eclipse
4
5. Functions break large computing tasks into smaller
ones.
Taking a problem and breaking into small,
manageable pieces is ritical to writing large programs.
So important definition for function,
Functions return values to where is invoked.
C programming
5
6. Function definition
type function_name(parameter list ) {declerations
statements}
The parameter list is a comma-seperated list of declarations.
Functions
6
9. return; // return ++a; // return (a*b)
When a return statement is encountered, execution of the
function is terminated and control is passed back to calling
environment. İf the return statement contains an expression,
then the value of the expressions is passed to the calling
environment as well.
Return Statement
9
11. Every variable and function in C has two attributes.
Type and storage class. Four storage classes are
automatics, external, register and static with
corresponding
auto extern register static
Storage Classes
11
12. Storage class auto:
Variables declared within function are automatic by default.
These variables can be used in scope of the function.
Storage class extern:
One methods of transmitting information across blocks and
functions is to use external variables. When a variable is
declared outside a function, storage is permanently assigned
to it, and its storage class is extern.
Storage Classes
12
13. #include <stdio.h>
extern int a = 1, b = 2;
c = 3;
int f(void);
int main(void) {
printf("%3dn", f());
printf("%3d%3d%3dn", a, b, c);
return 0;
}
int f(void) {
auto int b, c;
a = b = c = 4;
return (a + b + c);
}
Example
13
14. file2.c
int f(void) {
extern a;
int b, c;
b = c = a;
return (a + b +
c);
}
Extern keyword
This use of extern is used to tell the
compiler to ‘’look for it elsewhere’’ either
in this file or in some other file.
Örnek1.c
#include <stdio.h>
int a = 1, b = 2;
c = 3;
int f(void);
int main(void) {
printf("%3dn", f());
printf("%3d%3d%3dn", a, b, c);
return 0;
} 14
15. Storage class register tells the compiler that the association
variables should be stored in high-speed memory registers.
#include <stdio.h>
#include <time.h>
int a =1;
#define N 10000
int main(void) {
clock_t start, end;
double cpu_time_used;
register double i;
start = clock();
for(i=0;i<N;i=i+0.0001);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Running time is %f",cpu_time_used);
return 0;
}
Storage Class Register
Running
time is
0,163
second
with
register
variable.
Running
time is
0,419
second
without
register
variable.
15
16. Static declarations have two important and distinct
uses. One of them is to allow a local variable to retain its
previous value when the block is reentered.
Storage class static
16
17. The second and more subtle use of static is in connection with
external declarations. İt is use to restriction of the scope of
the variable.
Storage class static
17
18. A typical memory
representation of C program
consists of following sections.
1. Text segment
2. Initialized data segment
3. Uninitialized data segment
4. Stack
5. Heap
18
Memory Layout of C Programs
19. 1. Text Segment:
A text segment , also known as a code segment or simply as
text, is one of the sections of a program in an object file or in
memory, which contains executable instructions.
Usually, the text segment is sharable so that only a single
copy needs to be in memory for frequently executed
programs, such as text editors, the C compiler, the shells,
and so on. Also, the text segment is often read-only, to
prevent a program from accidentally modifying its
instructions. 19
Memory Layout of C Programs
20. 2. Initialized Data Segment: A data segment is a portion of
virtual address space of a program, which contains the global
variables and static variables that are initialized by the
programmer.
3. Uninitialized Data Segment:Data in this segment is
initialized by the kernel to arithmetic 0 before the program
starts executing uninitialized data starts at the end of the
data segment and contains all global variables and static
variables that are initialized to zero or do not have explicit
initialization in source code.
20
Memory Layout of C Programs
21. 4. Stack:
Stack, where automatic variables are stored, along with
information that is saved each time a function is called. Each
time a function is called, the address of where to return to
and certain information about the caller’s environment, such
as some of the machine registers, are saved on the stack. The
newly called function then allocates room on the stack for its
automatic and temporary variables.
This is how recursive functions in C can work. Each time a
recursive function calls itself, a new stack frame is used, so
one set of variables doesn’t interfere with the variables from
another instance of the function.
21
Memory Layout of C Programs
22. 5. Heap:
Heap is the segment where dynamic memory allocation
usually takes place.
Heap area is managed by malloc, realloc, and free.
22
Memory Layout of C Programs
23. #include <stdio.h>
int global; /* Uninitialized variable stored in bss*/
int main(void)
{
int *ptr_one;
ptr_one = (int *)malloc(sizeof(int));
int c;//stack
static int i = 100; /* Initialized static variable stored in DS*/
return 0;
}
23
Example
24. A function is said to be recursive if it class itself
directly or indirectly. in C, all functions can be used
recursively, including main function.
Recursion
24
26. Typically, a large program is written in a separate
directory as a collection of .h and .c file, with each .c
file contains one or more functions definition
When preprocessor encounters #include "pgm.h«
directive search this file in the same directory or
system-dependent places. İf it cannot be found,
preprocessor issues an error message and
compilations stops.
Developing Large Program
26
27. .h files contain #includes, #defines, templates of enumaration
types, templates of structure and union types, and list of
function prototype at the bottom.
pgm.h file:
#include <stdio.h>
#include <stdlib.h>
#define N 3
void fct1(int k);
void fct2(void);
void wrt_info(char *);
Developing Large Program
27
28. fct.c file
#include "pgm.h"
void fct1(int n)
{
int i;
printf("Hello from fct1()n");
for(i=0;i<n;++i)
fct2();
}
void fct2(void){
printf("Hello from fct2()n");
}
Example
28
29. wrt.c file:
#include "pgm.h"
void wrt_info(char
*pgm_name){
printf("Usage:
%snn",pgm_name);
printf("%sn",
"Hello from wrt_info-1n"
"Hello from wrt_info-2n"
"Hello from wrt_info-3n");
}
Example
main.c file:
#include "pgm.h"
int main(void) {
char ans;
int i, n = N;
printf("%s", "This program does not do very much.n
"
"Do you want to more
information?");
fflush(stdout);
scanf("%c", &ans);
if (ans == 'y' || ans == 'Y')
wrt_info("pgm");
for (i = 0; i < n; ++i)
fct1(i);
printf("Bye!n");
return 0;
}
}
29
30. Tower of Hanoi
Write c code that
solves this problem.
You have to use
recursive function.
30