C FAQ

1. How should I decide which integer type to use?
If you might need large values (above 32,767 or below -32,767), use long. Otherwise, if space is very important (i.e. if there are large arrays or many structures), use short. Otherwise, use int. If well-defined overflow characteristics are important and negative values are not, or if you want to steer clear of sign-extension problems when manipulating bits or bytes, use one of the corresponding unsigned types.

2. Do all declarations for the same static function or variable have to include the storage class static?
The language in the Standard does not quite require this (what's most important is that the first declaration contain static), but the rules are rather intricate, and are slightly different for functions than for data objects. (There has also been a lot of historical variation in this area.) Therefore, it's safest if static appears consistently in the definition and all declarations.

 
3. What does extern mean in a function declaration?
extern is significant only with data declarations. In function declarations, it can be used as a stylistic hint to indicate that the function's definition is probably in another source file, but there is no formal difference between extern int f(); and int f();

 
4. How are structure passing and returning implemented?
When structures are passed as arguments to functions, the entire structure is typically pushed on the stack, using as many words as are required. (Programmers often choose to use pointers to structures instead, precisely to avoid this overhead.) Some compilers merely pass a pointer to the structure, though they may have to make a local copy to preserve pass-by-value semantics.Structures are often returned from functions in a location pointed to by an extra, compiler-supplied ``hidden'' argument to the function. Some older compilers used a special, static location for structure returns, although this made structure-valued functions non-reentrant, which ANSI C disallows.

 
5. What's the difference between a structure and a union, anyway?
A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. (You can also cheat by writing to one field and reading from another, to inspect a type's bit patterns or interpret them differently, but that's obviously pretty machine-dependent.) The size of a union is the maximum of the sizes of its individual members, while the size of a structure is the sum of the sizes of its members. (In both cases, the size may be increased by padding.

 
6. What's the difference between ++i and i++?
++i adds one to the stored value of i and ``returns'' the new, incremented value to the surrounding expression; i++ adds one to i but returns the prior, unincremented value.

 
7. What are pointers really good for, anyway?
They're good for lots of things, such as:
  • dynamically-allocated arrays
  • generic access to several similar variables
  • (simulated) by-reference function parameters 
  • malloc'ed data structures of all kinds, especially trees and linked lists
  • walking over arrays (for example, while parsing strings)
  • efficient, by-reference ``copies'' of arrays and structures, especially as function parameters

 
8. Does *p++ increment p, or what it points to?
The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. Therefore, *p++ is equivalent to *(p++); it increments p, and returns the value which p pointed to before p was incremented. To increment the value pointed to by p, use (*p)++.

 
9. What is meant by the ``equivalence of pointers and arrays'' in C?
Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are ``equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array.

 
10. How much memory does a pointer variable allocate?
When you declare a pointer variable, you (or, more properly, the compiler) have allocated only enough memory to hold the pointer itself; that is, in this case you have allocated sizeof(char *) bytes of memory. But you have not yet allocated any memory for the pointer to point to.

 
11. How can I find out how much memory is available?
Your operating system may provide a routine which returns this information, but it's quite system-dependent. (Also, the number may vary over time.) If you're trying to predict whether you'll be able to allocate a certain amount of memory, just try it--call malloc (requesting that amount) and check the return value.

 
12. When I call malloc to allocate memory for a pointer which is local to a function, do I have to explicitly free it?
Yes. Remember that a pointer is different from what it points to. Local variables are deallocated when the function returns, but in the case of a pointer variable, this means that the pointer is deallocated, not what it points to. Memory allocated with malloc always persists until you explicitly free it. (If the only pointer to a block of malloc'ed memory is a local pointer, and if that pointer disappears, there will be no way to free that block.) In general, for every call to malloc, there should be a corresponding call to free.

 
13. How can I determine the byte offset of a field within a structure?
ANSI C defines the offsetof() macro, which should be used if available. If you don't have it, one possible implementation is

 #define offsetof(type, mem) ((size_t) \ ((char *)&((type *)0)->mem - (char *)(type *)0)).


 
14. What is the difference between char a[] = "string"; and char *p = "string"; ??
The first declares an initialized and modifiable array; the second declares a pointer initialized to a not-necessarily- modifiable constant string.

 
15. How do I get a null pointer in my programs?
A constant 0 in a pointer context is converted into a null pointer at compile time. A "pointer context" is an initialization, assignment, or comparison with one side a variable or expression of pointer type, and (in ANSI standard C) a function argument which has a prototype in scope declaring a certain parameter as being of pointer type. In other contexts (function arguments without prototypes, or in the variable part of variadic function calls) a constant 0 with an appropriate explicit cast is required.

 
16. How do I know how many elements an array can hold?
The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model.

 
17. What is Preprocessor?
The preprocessor is used to modify your program according to the preprocessor directives in your source code. Preprocessor directives (such as #define) give the preprocessor specific instructions on how to modify your source code. The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.

 
18. What is the purpose of realloc( )?
The function realloc(ptr,n) uses two arguments.the first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc( ) may create a new region and all the old data are moved to the new region.

 
19. Does C even have ``pass by reference''?
Not really.Strictly speaking, C always uses pass by value. You can simulate pass by reference yourself, by defining functions which accept pointers and then using the & operator when calling, and the compiler will essentially simulate it for you when you pass an array to a function.

 
20. Can I initialize unions?
In the original ANSI C, an initializer was allowed only for the first-named member of a union. C99 introduces ``designated initializers'' which can be used to initialize any member.

 
21. What is alloca and why is its use discouraged?
alloca allocates memory which is automatically freed when the function which called alloca returns. That is, memory allocated with alloca is local to a particular function's ``stack frame'' or context. alloca cannot be written portably, and is difficult to implement on machines without a conventional stack.

 
22. Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0?
It's your choice. Preprocessor macros like TRUE and FALSE (and, of course, NULL) are used for code readability, not because the underlying values might ever change. It's a matter of style, not correctness, whether to use symbolic names or raw 1/0 values.

 
23. Does the sizeof operator work in preprocessor #if directives?
No. Preprocessing happens during an earlier phase of compilation, before type names have been parsed. Instead of sizeof, consider using the predefined constants in ANSI's <limits.h>, if applicable, or perhaps a ``configure'' script. (Better yet, try to write code which is inherently insensitive to type sizes;)

 
24. can we store values and addresses in the same array? explain?
Yes, We can Store Value and Address in the Same Array using the LinkedList Concept of the Data & File Structure.

 
25. What is that continue statement?
Whenever this statement encountered in a loop , the control will immediately send to the starting of the loop , without continuing the sequence of statements.

 
26. What is a static identifier in C?
In C, a variable declared as static in a function is initialised once, and retains its value between function calls. The default initial value of an uninitialized static variable is zero. If a function or global variable is declared static, it can only be accessed in that file.

 
27. What is C tockens?
In passage of text, individual words and punctuation marks are called tockens. Similarly, in a C program the smallest individual units are known as C tockens. 

 
28. What is mean by User-Defined Type declaration?
C supports a feature known as "type definition" that allows users to to define an identifier that would represent an existing data type. The user-defined data type identifier can later be used to declare variables.It takes the general form:

typedef type identifier;


 
29. What is the difference between entry-controlled loop and exit-controlled loop?
In the entry-controlled loop, the controll conditions are tested before the start of the loop execution. If the conditions are not satisfied, then the body of the loop will not be executed. In the case of an exit-controlled loop, the test is performed at the end of the body of loop and therefore the body is executed unconditionally for the first time.

 
30. What is the difference between static and dynamic array?
An array created at compile time by specifying size in the source code has a fixed size and cannot be modified at run time. The process of allocating memory at compile time is known as static memory allocation and array that recieve static memory allocation is called static array. The process of allocating memory at run time is called dynamic memory allocation and the array that recieve dynamic memory allocation are called dynamic array.

 
31. Why do we need a terminating null character in character array?
The string is a variable-length structure and is stored in a fixed-length array. The array size is not always the size of the string and most often it is much larger than the string stored in it. There fore last element of the array need not represent the end of the string. We need some way to determine the end of the string data and the null character serves as the "end-of-string" marker.

 
32. What are the string-handling functions?
1. strcat()   = concatenates two strings

2. strcmp() = compares two strings

3. strcpy()  = copies one string over another

4. strlen()   =  finds the length of a string


 
33. What is a function?
A function is a self-contained block of code that performs a particular task. Once a function has been designed and packed, it can be treated as a 'black box' that takes some data from the main program and returns a value.

 
34. What is the difference between scope and visibility?
The scope of variable determines over what region of the program a variable is actually available for use. The visibility refers to the accessibility of a variable from the memory.

 
35. What is mean by automatic variable?
Automatic variables are declared inside a function in which they are to be utilized. They created when the function is called and destroyed automatically when the function is exited, hence the name automatic. Automatic variables are therefore private (or local) to the function in which they declared.

 
36. What is the use of fseek() function?
fseek() function is used to move the file position to a desired location within the file. It takes the following form,

fseek(file_ptr, offset, position);


 
37. What is the use of command- line argument?
Many operating systems, such as DOS and UNIX, enable the user to pass parameters to your program when the program starts. These are called command-line arguments.

 
38. What is linked list? what is the main advantage of using linked kist?
A linked list is a dynamic data structure. The primary advantage of linked list is that it can grow or shrink in size during the execution of a program.

 
39. What is a variable?
A variable is a data name that may be used to store a data value. Unlike constants that remain unchanged during the execution of a program, a variable may take different values at different times during execution.

 
40. What is the difference between argument and parameter?
A Parameter is the required data defined in the function's header. An Argument is the actual data variable that is passed to that function during a call to execute the function

 
41. What is the use of register variable?
We can tell the compiler that a variable should be kept in one of the machine's registers, instead of keeping in the memory (where normal variables are stored). Since aregister access is much faster than a memory access, keeping the frequently asked variables in the register will lead to faster execution of programs. 

 
42. What is the use of fopen() function?
fopen() function performs following actions:

1. Creates a new file for use

2. Open an existing file for use.


 
43. What is smart pointer?
In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the misuse of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.

 
44. What is the use of  preprocessor directive #include?
Many functions are stored in C library. Library functions are grouped category-wise and stored in different files known as header files. If we want to access the function stored in the library, it is necessary to tell the compiler about the files to be accessed. This is achieved by using #include.

 
45. C is highly portable. what is it mean?
C is highly portable. This means that C program written for one computer can be on run on another with little or no modification. Portability is important if we plan to use a new computer with a different operating system.

 
46. What is the difference between keywords and identifiers?
Every v word is classified as either a keyword or an identifier. All the keywords have fixed meanings and these meanings cannot be changed. Identifiers refer to the names of variables, functions and arrays. These are user-defined names and consist of a sequence of letters and digits.

 
47. What is the use of declaring a variable as volatile?
ANSI standard defines anothere qualifier volatile that could be used to tell explicitly the compiler that a variable's value may changed at any time by some external source(from outside the program).

 
48. What is a switch statement?
C has a biult-in multiway decision statement known as a switch. The switch statement tests the value of a given variable against alist of  case values and when a match is found, a block of statements associated with  that case is executed.

 
49. What is a loop?
In looping, a sequence of statements are executed until some conditions for terminating of the loop are satisfied. A program loop contain two parts, body of the loop and control statements. 

 
50. What is the difference between array and structure?
An array is the collection of related data elements of same type. Structure can have elements of different types. An array is derived data type where as structure is user-defined data type.

 

 
Subscribe Newsletter
Register to the site for free, and subscribe to the newsletter. You will receive new articles and special content not available elsewhere on the site, right into your e-mail box!