Selasa, 18 Desember 2018

STRUCTURE

Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book −
  • Title
  • Author
  • Subject
  • Book ID

Defining a Structure

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows −
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;  

Accessing Structure Members

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.
Ex : student.number, book1.title

FILE PROCESSING

Opening Files

You can use the fopen( ) function to create a new file or to open an existing file. This call will initialize an object of the type FILE, which contains all the information necessary to control the stream.
Ex : FILE *f = fopen("data.txt","r");
#NB : r = read file
         w = write file
          a = append file

Closing a File

You can use the fclose() function to close an existing file
Ex : fclose(read), fclose(f)

FUNCTION AND RECURSION

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
Ex : void check(){
      .....;
}

int check(int a,int b){
        ......;
        return ...;
}


Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
Ex : 

void recursion(){
      recursion();
}

int recursion(int a, int b){
         recursion(a,b);   
         return ...;
}

ARRAY AND POINTER


pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Ex : int *a, char *ch

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Ex : char str[100], double balance[20]

LOOPING / REPETITION



Repetisi dalam bahasa inggris sering disebut loops, biasanya digunakan untuk mengulang kode yang sama berkali-kali. Jumlah repetisinya itu beragam sesuai yang diinginkan, biasanya berisi ekspresi true/false.
Ada 3 variasi looping : while, do while, dan for.

While Looping : Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
Ex : while(...){
      ......;
}

Do While Looping : It is more like a while statement, except that it tests the condition at the end of the loop body. 
Ex : do{
     ......;
}while(....);

For Looping : Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Ex : for(...;...;...){
            ........;
}