C FAQ's
C Faq's
Declaration of a variable in C hints the compiler about the type and size of the variable in compile time. Similarly, declaration of a function hints about type and size of function parameters. No space is reserved in memory for any variable in case of declaration.
Main memory and CPU registers are the two memory locations where auto variables are stored. Auto variables are defined under automatic storage class. They are stored in main memory. Memory is allocated to an automatic variable when the block which contains it is called and it is de-allocated at the completion of its block execution. Auto variables: Storage : main memory. Default value : garbage value. Scope : local to the block in which the variable is defined. Lifetime : till the control remains within the block in which the variable is defined.
Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global.Example: Global variables: are variables which are declared above the main( ) function. These variables are accessible
Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000, which is equal to 8 in decimal. Explanation: ANDing operation : 10101101 original bit pattern 00001000 AND mask --------- 00001000 resulting bit pattern --------- The resulting value we get in this case is 8, i.e. the value of the second operand. The result turned out to be a 8 since the third bit of operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and complete bit pattern would have been 00000000. Thus depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF
Left shifting of an unsigned integer is equivalent to multiplying an unsigned int by 2. Eg1: 14<<1; Consider a number 14-----00001110 (8+4+2)is its binary equivalent left shift it by 1--------------00011100(16+8+4) which is 28. Eg2: 1<<1; consider the number as 1---00000001(0+0+1). left shift that by 1------------00000010(0+2+0) which is 2. left shift by 1 bit of a number=2*number left shift by 1 bit of 2*number=2*2*number left shift by n bits of number=(2^n)*number
Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure. Example: consider light_status as a data type. It can have two possible values - on or off. enum light_status { on, off }; enum light_status bulb1, bulb2; /* bulb1, bulb2 are the variables */
In C, we can supply arguments to 'main' function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program. The main ( ) function can take arguments as: main(int argc, char *argv[]) { } The first argument argc is known as 'argument counter'. It represents the number of arguments in the command line. The second argument argv is known as 'argument vector'. It is an array of char type pointers that points to the command line arguments. Size of this array will be equal to the value of argc. Example: at the command prompt if we give: C:\> fruit.exe apple mango then argc would contain value 3 argv [0] would contain base address of string " fruit.exe" which is the command name that invokes the program. argv [1] would contain base address of string "apple" argv [2] would contain base address of string "mango" here apple and mango are the arguments passed to the program fruit.exe
Argument: An argument is an expression which is passed to a function by its caller (or macro by its invoker) in order for the function(or macro) to perform its task. It is an expression in the comma-separated list bound by the parentheses in a function call expression. Actual arguments: The arguments that are passed in a function call are called actual arguments. These arguments are defined in the calling function. Formal arguments: The formal arguments are the parameters/arguments in a function declaration. The scope of formal arguments is local to the function definition in which they are used. Formal arguments belong to the called function. Formal arguments are a copy of the actual arguments. A change in formal arguments would not be reflected in the actual arguments.
Out of functions fgets( ) and gets( ), fgets( ) is safer to use. gets( ) receives a string from the keyboard and it is terminated only when the enter key is hit. There is no limit for the input string. The string can be too long and may lead to buffer overflow. Example: gets(s) /* s is the input string */ Whereas fgets( ) reads string with a specified limit, from a file and displays it on screen.The function fgets( ) takes three arguments. First argument : address where the string is stored. Second argument : maximum length of the string. Third argument : pointer to a FILE. Material from Interview Mantra. Subscribe to free updates via email. Example: fgets(s,20,fp); /* s: address of the string, 20: maximum length of string, fp: pointer to a file */ The second argument limits the length of string to be read. Thereby it avoids overflow of input buffer. Thus fgets( ) is preferable to gets( ).
strcpy function: copies a source string to a destination defined by user. In strcpy function both source and destination strings are passed as arguments. User should make sure that destination has enough space to accommodate the string to be copied. 'strcpy' sounds like short form of "string copy". Syntax: strcpy(char *destination, const char *source); Source string is the string to be copied and destination string is string into which source string is copied. If successful, strcpy subroutine return strdup function: duplicates a string to a location that will be decided by the function itself. Function will copy the