70 likes | 82 Views
C Miscellaneous Programs Prabhat Kumar Padhy. 1. C Variable Arguments. Variable length argument is a feature that allows a function to receive any number of arguments. Variable number of arguments are represented by three dotes (…)
E N D
C Variable Arguments • Variable length argument is a feature that allows a function to receive any number of arguments. • Variable number of arguments are represented by three dotes (…) • Macros are used to implement the functionality of variable arguments. • Use va_list type variable in the function definition • Use int parameter and va_start macro to initialize the va_list variable to an argument list. The macro va_start is defined in stdarg.h header file. • Use va_arg macro and va_list variable to access each item in argument list. • macro va_end to clean up the memory assigned to va_list variable. inta_function(int x, ...) { va_lista_list; va_start( a_list, x ); }
C Volatile keyword • Volatile tells the compiler not to optimize anything that has to do with the volatile variable. • Let's say we have a little piece of hardware that is mapped into RAM somewhere and that has two addresses: a command port and a data port: typedefstruct { int command; int data; intisbusy; } myhw; Let us send some commands void SendCommand(volatile myhw* egadget, int command, int data) { // wait while the gadget is busy: while (gadget->isbusy) { // do nothing here. } // set data first: gadget->data = data; // writing the command starts the action: gadget->command = command; }
Volatile Key word in C Another use for volatile is signal handlers. If you have code like this: quit = 0; while (!quit) compiler can optimize to while(true) { /* very small loop which is completely visible to the compiler */ } The compiler is allowed to notice the loop body does not touch the quit variable and convert the loop to a while (true) loop. Even if the quit variable is set on the signal handler for SIGINT and SIGTERM; the compiler has no way to know that. • So better way is to declare the quit variable as volatile
Unions in C • Like Structures, union is a user defined data type. • In union, all members share the same memory location. • For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y. // Declaration of union is same as structures union test { int x, y; }; int main() { // A union variable t union test t; t.x = 2; // t.y also gets value 2 printf ("After making x = 2:\n x = %d, y = %d\n\n", t.x, t.y); t.y = 10; // t.x is also updated to 10 printf ("After making Y = 'A':\n x = %d, y = %d\n\n", t.x, t.y); return 0; }
Applications of union What are applications of union? Unions can be useful in many situations where we want to use the same memory for two or more members. For example, suppose we want to implement a binary tree data structure where each leaf node has a double data value, while each internal node has pointers to two children, but no data. If we declare this as: struct NODE { struct NODE *left; struct NODE *right; double data; }; • every node requires 16 bytes struct NODE { bool is_leaf; union { struct { struct NODE *left; struct NODE *right; } internal; double data; } info; };