Wikipedia

Hasil penelusuran

Sabtu, 05 Desember 2015

Summary.

1. When is the right time to use :

a. Struct
b. Pointer
c. Function
d. Array

2.  Write an Example That uses Combination of the above!


 Answer:

a. A structure is another user defined data type available in C that allows to combine data items of different kinds. Structures are used to represent a record. whenever we want to create a new data type, struct is the correct way to use.

b.A 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. whenever we want to change the value of a variable using the address, 
pointer is the correct way to use.

c.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.whenever we want to create a certain instruction separated from the main() , function is the correct way to use.

d. An Array is 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. whenever we want to create a storage of variable, 
array is the correct way to use.

2.


and one with pointer :




 source : www.tutorialspoint.com







Minggu, 22 November 2015

stdarg.h in C

  • stdarg.h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg.

  • The contents of stdarg.h are typically used in variadic functions, though they may be used in other functions (for example, vprintf) called by variadic functions.
  •  stdarg.h header defines a variable type va_list and three macros which can be used to get the arguments in a function when the number of arguments are not known i.e. variable number of arguments. 

Library Variables

  •  va_list
This is a type suitable for holding information needed by the three macros va_start(), va_arg() and va_end().



Library Macros


  • void va_start(va_list ap, last_arg)

This macro initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis.


  • type va_arg(va_list ap, type)
This macro retrieves the next argument in the parameter list of the function with type type.

  • void va_end(va_list ap)
This macro allows a function with variable arguments which used the va_start macro to return. If va_end is not called before returning from the function, the result is undefined.

 

 Example :



more at : 
https://en.wikipedia.org/wiki/Stdarg.h

 http://www.tutorialspoint.com/c_standard_library/stdarg_h.htm




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!