Welcome to My Blog 👋

Java, Spring Framework, Microservices, Docker, Kubernetes, AWS and Others 🚀
Follow Me

Yapısal Programlama (C) - Ders 4



  February 19, 2015    Labels:,,,,, 

Çanakkale Onsekiz Mart Üniversitesi Bilgisayar Mühendisliği Bölümü yapısal programlama(C) ders notlarım.

#include <stdio.h>
#include <stdlib.h>

int toplama(int sayi){
    if(sayi==0)
    return 0;
    else
    return sayi + toplama(sayi-1);
}

int main()
{
    int sayi,sonuc;
    printf("Lutfen bir sayi giriniz:");
    scanf("%d",&sayi);
    sonuc=toplama(sayi);
    printf("sonuc= %d",sonuc);
    return 0;
}





------------------------------------------------------------------------------------------


#include <stdio.h>
#include <stdlib.h>

void useLocal();
void useStaticLocal();
void useGlobal();
void yazdir(int);
int x=1; //globakl degisken

int main()
{
    int x=5; //local degisken
    printf("\nmain icinde x=%d\n",x);

    //useLocal();
    //useStaticLocal();
    //useStaticLocal();
    useGlobal();
    useGlobal();
    printf("\nmain icinde x=%d\n",x);
/*
    {
        int x=7;
        printf("main icinde ic scope'ta x=%d\n",x);
        x++;
        printf("main icinde ic scope'ta x=%d\n",x);
    }
    printf("main icinde x=%d",x);
*/
    return 0;
}
void yazdir(int x){
    if(x==0) return;
    else{
    printf("******\n");
    yazdir(x-1);
    }
}

void useLocal(){
    int x=25;
    printf("\nuseLocal fonksiyonuna giriste x=%d",x);
    x++;
    printf("\nuseLocal fonksiyonuna cikista x=%d",x);
}
void useStaticLocal(){
        static int x=50;
        printf("\nuseStaticLocal fonksiyonuna giriste x=%d",x);
        x++;
        printf("\nuseStaticLocal fonksiyonuna cikista x=%d",x);
}
void useGlobal(){
        printf("\nuseGlobal fonksiyonuna giriste x=%d",x);
        x*=10;
        printf("\nuseGlobal fonksiyonuna cikista x=%d",x);
}


--------------------------------------------------------------------------------




#include <stdio.h>
#include <stdlib.h>

//void useLocal();
//void useStaticLocal();
//void useGlobal();
//void yazdir(int);
int fibonacci(int);
int x=1; //globakl degisken

int main()
{
    int n;
    printf("\nbir sayi giriniz:");
    scanf("%d",&n);
    printf("\n %d. fibonacci sayisi=%d",n,fibonacci(n));

    //int x=5; //local degisken
    //printf("\nmain icinde x=%d\n",x);

    //useLocal();
    //useStaticLocal();
    //useStaticLocal();
    //useGlobal();
    //useGlobal();
    //printf("\nmain icinde x=%d\n",x);
/*
    {
        int x=7;
        printf("main icinde ic scope'ta x=%d\n",x);
        x++;
        printf("main icinde ic scope'ta x=%d\n",x);
    }
    printf("main icinde x=%d",x);
*/
    return 0;
}
int fibonacci(int n){
    if(n==0){
        return 0;
    }
    else if(n==1){
        return 1;
    }
    else {
        return fibonacci(n-1)+fibonacci(n-2);
    }
}

void yazdir(int x){
    if(x==0) return;
    else{
    printf("******\n");
    yazdir(x-1);
    }
}

void useLocal(){
    int x=25;
    printf("\nuseLocal fonksiyonuna giriste x=%d",x);
    x++;
    printf("\nuseLocal fonksiyonuna cikista x=%d",x);
}
void useStaticLocal(){
        static int x=50;
        printf("\nuseStaticLocal fonksiyonuna giriste x=%d",x);
        x++;
        printf("\nuseStaticLocal fonksiyonuna cikista x=%d",x);
}
void useGlobal(){
        printf("\nuseGlobal fonksiyonuna giriste x=%d",x);
        x*=10;
        printf("\nuseGlobal fonksiyonuna cikista x=%d",x);
}



----------------------------------------------------------------------------------------



1    /* Fig. 5.12: fig05_12.c
2       A scoping example */
3    #include <stdio.h>
4   
5    void useLocal( void );       /* function prototype */
6    void useStaticLocal( void ); /* function prototype */
7    void useGlobal( void );      /* function prototype */
8   
9    int x = 1; /* global variable */
10 
11   /* function main begins program execution */
12   int main()
13   {
14      int x = 5; /* local variable to main */
15 
16      printf("local x in outer scope of main is %d\n", x );
17 
18      { /* start new scope */                                   
19         int x = 7; /* local variable to new scope */             
20                                                                   
21         printf( "local x in inner scope of main is %d\n", x );   
22      } /* end new scope */                                         
23 
24      printf( "local x in outer scope of main is %d\n", x );
25 
26      useLocal();       /* useLocal has automatic local x */
27      useStaticLocal(); /* useStaticLocal has static local x */
28      useGlobal();      /* useGlobal uses global x */
29      useLocal();       /* useLocal reinitializes automatic local x */
30      useStaticLocal(); /* static local x retains its prior value */
31      useGlobal();      /* global x also retains its value */
32 
33      printf( "local x in main is %d\n", x );
34 
35      return 0; /* indicates successful termination */
36 
37   } /* end main */
38 
39   /* useLocal reinitializes local variable x during each call */
40   void useLocal( void )
41   {
42      int x = 25;  /* initialized each time useLocal is called */
43 
44      printf( "\nlocal x in a is %d after entering a\n", x );
45      x++;
46      printf( "local x in a is %d before exiting a\n", x );
47   } /* end function useLocal */
48 
49   /* useStaticLocal initializes static local variable x only the first time
50      the function is called; value of x is saved between calls to this
51      function */
52   void useStaticLocal( void )
53   {
54      /* initialized only first time useStaticLocal is called */
55      static int x = 50;                                           
56 
57      printf( "\nlocal static x is %d on entering b\n", x );
58      x++;
59      printf( "local static x is %d on exiting b\n", x );
60   } /* end function useStaticLocal */
61 
62   /* function useGlobal modifies global variable x during each call */
63   void useGlobal( void )
64   {
65      printf( "\nglobal x is %d on entering c\n", x );
66      x *= 10;
67      printf( "global x is %d on exiting c\n", x );
68   } /* end function useGlobal */


-------SONUC------
local x in outer scope of main is 5
local x in inner scope of main is 7
local x in outer scope of main is 5

local x in a is 25 after entering a
local x in a is 26 before exiting a

local static x is 50 on entering b
local static x is 51 on exiting b

global x is 1 on entering c
global x is 10 on exiting c

local x in a is 25 after entering a
local x in a is 26 before exiting a

local static x is 51 on entering b
local static x is 52 on exiting b

global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5


No comments:

Post a Comment