Some common C Question & Answers:
What is the difference between #include <file> and #include “file”?
When writing your C program, you can include files in two ways. The first way is to surround the file you
want to include with the angled brackets < and >. This method of inclusion tells the preprocessor to look for the file in the predefined default location. This predefined default location is often an INCLUDE environment variable that denotes the path to your include files. For instance, given the INCLUDE variable
using the #include <file> version of file inclusion, the compiler first checks the C:\COMPILER\INCLUDE
directory for the specified file. If the file is not found there, the compiler then checks the
S:\SOURCE\HEADERS directory. If the file is still not found, the preprocessor checks the current directory.
The second way to include files is to surround the file you want to include with double quotation marks. This method of inclusion tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations you have set up. Using the #include “file” version of file inclusion and applying it to the preceding example, the preprocessor first checks the current directory for the specified file. If the file is not found in the current directory, the C:COMPILERINCLUDE directory is searched. If the file is still not found, the preprocessor checks the S:SOURCEHEADERS directory.
The #include <file> method of file inclusion is often used to include standard headers such as stdio.h or
stdlib.h. This is because these headers are rarely (if ever) modified, and they should always be read from your compiler’s standard include file directory.
The #include “file” method of file inclusion is often used to include nonstandard header files that you have created for use in your program. This is because these headers are often modified in the current directory, and you will want the preprocessor to use your newly modified version of the header rather than the older, unmodified version.
Write the equivalent expression for x%8?
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 programs as well as your own.
Using this function, you can allow other programs that are running to update the same file you are updating.
The sopen() function takes four parameters: a pointer to the filename you want to open, the operational
mode you want to open the file in, the file sharing mode to use, and, if you are creating a file, the mode to create the file in. The second parameter of the sopen() function, usually referred to as the “operation flag”parameter, can have the following values assigned to it:
Constant Description O_APPEND Appends all writes to the end of the file
O_BINARY Opens the file in binary (untranslated) mode
O_CREAT If the file does not exist, it is created
O_EXCL If the O_CREAT flag is used and the file exists, returns an error
O_RDONLY Opens the file in read-only mode
O_RDWR Opens the file for reading and writing
O_TEXT Opens the file in text (translated) mode
O_TRUNC Opens an existing file and writes over its contents
O_WRONLY Opens the file in write-only mode
The third parameter of the sopen() function, usually referred to as the “sharing flag,” can have the following values assigned to it:
SH_COMPAT No other program can access the file
SH_DENYRW No other program can read from or write to the file
SH_DENYWR No other program can write to the file
SH_DENYRD No other program can read from the file
SH_DENYNO Any program can read from or write to the file
If the sopen() function is successful, it returns a non-negative number that is the file’s handle. If an error occurs, –1 is returned, and the global variable errno is set to one of the following values:
ENOENT File or path not found
EMFILE No more file handles are available
EACCES Permission denied to access file
EINVACC Invalid access code
Can static variables be declared in a header file?
You can’t declare a static variable without defining it as well (this is because the storage class modifiers
static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended.
How do you override a defined macro?
You can use the #undef preprocessor directive to undefine (override) a previously defined macro.
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).
When function say abc() calls another function say xyz(), what happens in stack?
When some function xyz() calls function abc(). all the local variables, static links, dynamic links and function return value goes on the top of all elements of function xyz() in the stack. when abc() exit it’s return value has been assigned to xyz().
How do you print an address?
we can print the address of a variable or a function using the following specifiers %u,%p here %u prints address in decimal form and %p prints in hexa decimal form,but remember these two format specifiers print only offset adress but they doesn’t print code segment address there is a another specifier %Fp which prints both the code segment and offset address
How to find entered number is EVEN or ODD without using conditional statement(not using if.. else,if.. , else if..,while, do… while…., for….)
We can find a number is odd or even by a simple program
a=0; //0–means Even Number
a=1; //1–means Odd number
Big & Little Endian:
Big Endian Little Endian bit fields
For big endian machines (e.g. Motorola) :
uint32_t MSB :1;
uint32_t LSB :1;
For little endian machines (e.g. Intel):
uint32_t LSB :1;
uint32_t MSB :1;