int Human -> 4 bytes of memory.
memory bisa terdapat di 2 tempat : data segment / stack.
jika dideklarasikan diluar function ( global var ), masuk ke data segment.
Selama program dijalankan , memory tetap akan digunakan.
stack -> berisi variabel yang terdapat didalam function.
sifatnya temporary.
setelah selesai digunakan, memory dibebaskan dan dapat digunakan oleh program / function lain.
======
#include <stdio.h>
int number_of_worker = 10;
void addWorker(){
int stackWorker = 3; -> memory berada di stack.
number_of_worker += stackWorker;
}
main(){
printf ("there is %i number of worker", number_of_worker);
addWorker();
printf ("there is %i number of worker", number_of_worker);
addWorker();
printf ("there is %i number of worker", number_of_worker);
}
// result -> 10 13 16
value seperti integer, float, single char tidak perlu manajemen memori yang special. mereka akan otomatis langsung di "free" kan ketika function berakhir / program selesai.
=========
** array
int anArray[10] // declaring 5 integer of array. (0-4).
10 integer = 10*4 bytes
= 40 bytes
c tidak bisa secara otomatis membesarkan ukuran array. harus dilakukan secara manual / menggunakan library 3rd party.
=========
** looping array, random number generator example
#include <stdio.h>
#include <time.h>
#define COUNT 10
main(){
srand (time(NULL));
int anArray[COUNT];
int x;
for (x=0; x<COUNT; x++){
anArray[COUNT] = rand(); // generate rng, input ke anArray
}
for (x=0; x<COUNT; x++){
printf("value an array: %i\n %", anArray[x]);
}
}
=======
string di c adalah kumpulan array character.
char aString[10]="toro"
======
null terminator ->> \0. end of array string. biar bisa di proses secara sempurna ketika di printf %s.
======
src code library loca: /usr/include/string.h
No comments:
Post a Comment