Wikipedia

Hasil penelusuran

Sabtu, 21 November 2015

File in C

A File represents a sequence of bytes, regardless of it being a text file or a binary file. C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.

How to Create a File

        FILE *fileptr // File pointer called fileptr
         fileptr = fopen()

Opening a File

 use the fopen( ) function to create a new file or to open an existing file.

    FILE *fopen( const char * filename, const char * mode );
 
 
Here, filename is a string literal, which you will use to name your file, and access mode can have one of the following values −



r/rb :Opens an existing text file for reading purpose.
w/wb :Opens a text file for writing. If it does not exist, then a new file is created. Here your  program will start writing content from the beginning of the file.
a/ab ;Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content.
r+/r+b ;Opens a text file for both reading and writing.
w+/w+b ;Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
a+/a+b; Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.
If you are going to handle binary files, then you will use the ones with 'b' in the above.




Closing a File

To close a file, use the fclose( ) function. The prototype of this function is −

     int fclose( FILE *fp );
 
The fclose(-) function returns zero on success, or EOF if there is an error in closing the file.

Reading a File

   int fgetc( FILE * fp );
 
The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOF. The following function allows to read a string from a stream −

   char *fgets( char *buf, int n, FILE *fp );
 
The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.

 Example :




 Since line 6 - 9 already create the file., if we delete line 6-9, it will still compile the same result, because the file is already saved

You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character.
example :






more at :
 http://www.tutorialspoint.com/cprogramming /c_file_io.htm









Kamis, 19 November 2015

Structures in C

Structure is another user defined data type available in C that allows to combine data items of different kinds.

 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 :

struct [structure tag] {     //*structure tag is optional

   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  //*one or more structure variables also optional

 And an Example of how to use Structure :







Structures as Function Arguments


You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
and it can be simplified like this : 




more at : www.tutorialspoint.com





Pointer & Reference in C

What is a Pointer ? 

A Pointer is a variable whose value is the address of another variable, or direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. 
 
Format :   type *var-name;
  • type is the pointer's base type; it must be a valid C data type 
  • var-name is the name of the pointer variable. 
  • The asterisk (*) used to declare a pointer
     
     Example of a Valid format : 
     
    - int    *ip;    /* pointer to an integer */
    - double *dp;    /* pointer to a double */
    - float  *fp;    /* pointer to a float */
    - char   *ch     /* pointer to a character */ 
     
     
    Example How To Use a Pointer:
     
     
     
     
     
    Relation Between Pointer and Array 
     
    •  An array name is a constant pointer to the first element of the array.
       Example :
       

    •  Once we have the address in p,
             *p will give us the value available at the address stored in p
     
     
     
    Relation Between Pointer and Function  
     
    • C programming allows passing a pointer to a function. 
      We just need to declare the function parameter as a pointer type.
       
      here is an example where we pass a pointer to a function 
      and change the value inside the function which reflects back in the 
      calling function 
     
     
    source : www.tutorialspoint.com
     
    
    
    Reference
    
    
    Theres also one more kind of passing, called Passing By Reference
    Basically, A reference is an alias for another variable
    the syntax/ Format for this Reference is :
    'type &variable' and it only works on c++ compiler
    Heres what i learned For Example :
    
    
    
    
     
     
     
     
     
    
    
    
    
    
    
    
    
    
    
    
    

Minggu, 18 Oktober 2015

Arrays In C

  • Array is a way to store variables in contingous space, that can be looped later
  • Use Array when the number of inputs to be stored are too many 
 
Since Example is The Best Teacher for me, lets look at this example :

 Look at the pic : 






we can also write it this way :











To know the size of your Array, you can use function "sizeof" and divide it by one element of the array.

example :



Hope This Will Help
Peace !










Sabtu, 17 Oktober 2015

Recursion / Recursive Function in C

  • Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows.
 here is a basic example about how recursion works




This program ends when we've counted to twenty, or more precisely, when count is no longer less than twenty. This is a good base case because it means that if we have an input greater than twenty, we'll stop immediately. If we'd chosen to stop when count equaled twenty, then if the function were called with the input 21, it would run out of memory before stopping.

By the way, i wrote the function name as count_to_ten ., actually it should be count_to_twenty . but its just a name given by us, so i guess its okay. i forgot to modify that one

source  :
Tutorialspoint   : http://www.tutorialspoint.com/cprogramming/c_recursion.htm 

Cprogramming : http://www.cprogramming.com/tutorial/c/lesson16.html 


Hope this basic recursion example helps.
Peace!
  

Jumat, 16 Oktober 2015

Function , Arguments/Parameter in C

  • Function can be described as a group of Code to do specified things. 
  • Argument = Parameter of the function 

Defining a Function

return_type function_name( parameter list )
{
   body of the function
}
 

  •  Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

  • Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. 
     
    • Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. 
      • Function Body: The function body contains a collection of statements that define what the function does.
       


     To make it clear, Lets Jump to Code::Blocks


    Function is not always placed before int main ().
     Every function usually have the return value,
    i have explained it on the pic . 


    Here's an example without using return value,
     usually we call it as a procedure. 


    source : http://www.tutorialspoint.com/cprogramming/c_functions.htm

    Hope This Basic Functions will Help.
    Peace!













    Kamis, 01 Oktober 2015

    Looping/ Iteration in C Programming

    Sup' guys, today im going to share about Looping And Iteration In C Programming.
    Loop is actually Looping, it allows you to iterate something until specified times, and stop at specified point.
    to help you understand, see the Loop / Iteration Flowchart below:

     

    in Looping, Basically i use :
    -While
    -For
    -Do-While

    i'll explain all of it to you , starting with 

    While .
      for me, Example is the Best teacher. so ill give the example directly to you , jump to Code::Blocks !

    *this example is about finding the average glass of water drunk per day in a week.







     For.
    *same example





    Do-While.
    *same example


     Basically, Do While is a bit similar to While
     the difference is in While, you state the loop condition first then give instruction. but in Do-Whle , you do the instruction first , Then state the loop condition
    Example:


      Hope this Helps 
    Peace !