Ø Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
Ø Arrays use subscripted variables to access and manipulate data.Array variables can be equivalently written using pointer expression.
2.What are the advantages of the functions?
Ø Debugging is easier
Ø It is easier to understand the logic involved in the program
Ø Testing is easier
Ø Recursive call is possible
Ø Irrelevant details in the user point of view are hidden in functions
Ø Functions are helpful in generalizing the program
3. 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 operationalmode 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 fileO_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 errorO_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:
Constant Description
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:
Constant Description
ENOENT File or path not found
EMFILE No more file handles are available
EACCES Permission denied to access file
EINVACC Invalid access codeConstant Description
4.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.
5.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 modifiersstatic 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.
6.How do you override a defined macro?
You can use the #undef preprocessor directive to undefine (override) a previously defined macro.
7.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).
8.How do we get Square root of any number Without using sqrt() fuction?
You can use a iterative method for example bisection
#include
main()
{
int n;
float eval=1.0,x1=0,x2=n,x;
//trying to locate root between x1=0 and x2=n and giving a initial dummy value to evalx=(x1+x2)/2;
printf("Enter value for n n n");
scanf("%d",&n);
while((eval > 0.0001 )( eval < -0.0001) )
{ //you can change accuracy by changing 0.0001 to smaller valueeval= x*x-n;printf(" eval = %f t",eval);
//printf to keep track not necessaryif(eval>0){x2=x;x=(x2+x1)/2 ;
printf(" x = %f t",x);
//printf to keep track not necessary
}
else {x1=x ;x=(x1+x2)/2 ;
printf(" x = %f t",x);
//printf to keep track not necessary
}
printf("n n");
}
printf("the root is = %f ",x);
}
9.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, certaincompilers 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 serve the same purpose, and each contains roughly the same definitions. If, however, you are writing a program that is to support Borland C++ and Microsoft C++, you must define which header to include at compile time. The following example shows how this can be done:
#ifdef _ _BORLANDC_ _
#include
#else
#include
#endif
10.Can a variable be both const and volatile?
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in
FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.
11.Can include files be nested?
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 slows down compilation. Many of today’s popular compilers make up for this difficulty by implementing a concept called precompiled headers, in which all headers and associated dependencies are stored ina precompiled state.
Many programmers like to create a custom header file that has #include statements for every header needed for each module. This is perfectly acceptable and can help avoid potential problems relating to #include files, such as accidentally omitting an #include file in a module.
12.Can static variables be declared in a header file?
Yes there is difference between declaring a static variable as global and local. If it is local, it can be accessed only in the function where it's declared. But if it is global, all functions can access it. But, what ever be the case, its value will be retained between functions.
13.When does the compiler not implicitly generate the address of the first element of an array?
Whenever an array name appears in an expression such as
Ø array as an operand of the sizeof operator
Ø array as an operand of & operator
Ø array as a string literal initializer for a character array
Then the compiler does not implicitly generate the address of the address of the first element of an array.
No comments:
Post a Comment