SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
Chapter 1
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
 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
 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
 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
 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
 Function definition
 type function_name(parameter list ) {declerations
statements}
 The parameter list is a comma-seperated list of declarations.
Functions
6
Example
7
Scope of Variables
8
 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
Example
10
 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
 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
 #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
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
 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
 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
 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
 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
 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
 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
 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
 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
#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
 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
Examples
25
 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
 .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
 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
 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
Tower of Hanoi
 Write c code that
solves this problem.
You have to use
recursive function.
30

Contenu connexe

Tendances

OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)
Kai-Feng Chou
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
Little Tukta Lita
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
yiditushe
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
Kai-Feng Chou
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
Olve Maudal
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directives
Malik Tauqir Hasan
 

Tendances (20)

OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Function pointer
Function pointerFunction pointer
Function pointer
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directives
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 

Similaire à Data structure week 1

Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 

Similaire à Data structure week 1 (20)

C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Basic construction of c
Basic construction of cBasic construction of c
Basic construction of c
 
Embedded C.pptx
Embedded C.pptxEmbedded C.pptx
Embedded C.pptx
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Cpp
CppCpp
Cpp
 
Structure of the C Program.docx
Structure of the C Program.docxStructure of the C Program.docx
Structure of the C Program.docx
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Storage class
Storage classStorage class
Storage class
 
Book management system
Book management systemBook management system
Book management system
 
Memory management in c++
Memory management in c++Memory management in c++
Memory management in c++
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 

Plus de karmuhtam

Plus de karmuhtam (20)

Devre analizi deney malzeme listesi
Devre analizi deney malzeme listesiDevre analizi deney malzeme listesi
Devre analizi deney malzeme listesi
 
Deney 6
Deney 6Deney 6
Deney 6
 
Deney 5
Deney 5Deney 5
Deney 5
 
Deney 3 ve 4
Deney 3 ve 4Deney 3 ve 4
Deney 3 ve 4
 
Deney 1 ve 2
Deney 1 ve 2Deney 1 ve 2
Deney 1 ve 2
 
Data structure week y 5 1
Data structure week y 5 1Data structure week y 5 1
Data structure week y 5 1
 
Data structure week y 5
Data structure week y 5Data structure week y 5
Data structure week y 5
 
Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2
 
13. sınıfları başlık dosyaları
13.  sınıfları başlık dosyaları13.  sınıfları başlık dosyaları
13. sınıfları başlık dosyaları
 
11. stl kütüphanesi
11. stl kütüphanesi11. stl kütüphanesi
11. stl kütüphanesi
 
10. istisna isleme
10. istisna isleme10. istisna isleme
10. istisna isleme
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlar
 
8. çok biçimlilik
8. çok biçimlilik8. çok biçimlilik
8. çok biçimlilik
 
7. kalıtım
7. kalıtım7. kalıtım
7. kalıtım
 
6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık
 
5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları
 
4. yapılar
4. yapılar4. yapılar
4. yapılar
 
4. nesneler ve sınıflar
4. nesneler ve sınıflar4. nesneler ve sınıflar
4. nesneler ve sınıflar
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 

Data structure week 1

  • 1. Chapter 1 Data Structures Assoc. Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 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