How do you determine whether to use a stream function or a low-level function?
Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and write(). In multi-user environments,...
How can I open a file so that other programs can update it at the same time?
Your C compiler library contains a low-level file function called sopen() that can be used to open a file in shared mode. Beginning with dos 3.0, files could be opened in shared mode by loading a special program named share.Exe. Shared mode, as the name implies, allows a file to be shared with other...
How can I make sure that my program is the only one accessing a file?
By using the sopen() function you can open a file in shared mode and explicitly deny reading and writing permissions to any other program but yours. This task is accomplished by using the sh_denywr shared flag to denote that your program is going to deny any writing or reading attempts by other programs....
What will the preprocessor do for a program?
The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part...
How can you avoid including a header more than once?
One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define preprocessor directives. When you create a header for your program, you can #define a symbolic name that is unique to that header. You can use the conditional preprocessor directive named #ifndef to...
What is the benefit of using an enum rather than a #define constant?
The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability. 1) the first advantage is that enumerated...
Can you define which header file to include at compile time?
Yes. This can be done by using the #if, #else, and #endif preprocessor directives. For example, certain compilers use different names for header files. One such case is between borland c++, which uses the header file alloc.H, and Microsoft c++, which uses the header file malloc.H. Both of these headers...
Yes. Include files can be nested any number of times. As long as you use precautionary measures , you can avoid including the same file twice. In the past, nesting header files was seen as bad programming practice, because it complicates the dependency tracking function of the make program and thus...
What are the standard predefined macros?
The ansi C standard defines six predefined macros for use in the C language: macro name purpose _ _line_ _ inserts the current source code line number in your code. _ _file_ _ inserts the current source code filename in your code. _ _date_ _ inserts the current date of compilation in your code. _ _time_...
How can you check to see whether a symbol is defined?
You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined (#ifdef) or whether it has not been defined (#ifndef).
How many levels of pointers can you have?
The answer depends on what you mean by “levels of pointers.” if you mean “how many levels of indirection can you have in a single declaration?” the answer is “at least 12.” int I = 0; int *ip01 = & i; int **ip02 = & ip01; int ***ip03 = & ip02; int ****ip04 = & ip03; int *****ip05 = &...
What does it mean when a pointer is used in an if statement?
Any time a pointer is used as a condition, it means “is this a non-null pointer?” a pointer can be used in an if, while, for, or do/while statement, or in a conditional expression.
How do you use a pointer to a function?
The hardest part about using a pointer-to-function is declaring it. consider an example. You want to create a pointer, PF, that points to the strcmp() function. The strcmp() function is declared in this way: int strcmp(const char *, const char * ) to set up PF to point to the strcmp() function,...
Why should we assign null to the elements (pointer) after freeing them?
This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to “dangle”; it doesn’t point at anything useful. If you “null out” or “zero out” a pointer immediately after freeing it, your program can no longer get in trouble...
When should a far pointer be used?
Sometimes you can get away with using a small memory model in most of a given program. There might be just a few things that don’t fit in your small data and code segments. When that happens, you can use explicit far pointers and function declarations to get at the rest of memory. A far function can...
What is a “null pointer assignment” error? What are bus errors, memory faults, and core dumps?
These are all serious errors, symptoms of a wild pointer or subscript. Null pointer assignment is a message you might get when an ms-dos program finishes executing. Some such programs can arrange for a small amount of memory to be available “where the null pointer points to” (so to speak). If the program...
How can you determine the size of an allocated portion of memory?
You can’t, really. Free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release of the compiler.
Is it possible to execute code even after the program exits the main() function?
The standard C library provides a function named atexit() that can be used to perform “cleanup” operations when your program terminates. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the atexit() function.
Diffenentiate between an internal static and external static variable?
An internal static variable is declared inside a block with static storage class whereas an external static variable is declared outside all the blocks in a file.An internal static variable has persistent storage,block scope and no linkage.An external static variable has permanent storage,file scope...
What are advantages and disadvantages of external storage class?
Advantages of external storage class1)persistent storage of a variable retains the latest value2)the value is GLobally available disadvantages of external storage class1)the storage for an external variable exists even when the variable is not needed2)the side effect may produce surprising output3)modification...