Wikipedia

Hasil penelusuran

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 :