Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers
Antivirus Faqs_Questions_And_Answers Software Tips Faqs_Questions_And_Answers ERP Faqs_Questions_And_Answers Internet Faqs_Questions_And_Answers Spoken English Faqs_Questions_And_Answers Web Designing Faqs_Questions_And_Answers SEO Tips Faqs_Questions_And_Answers Hacking
Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers Faqs_Questions_And_Answers
SAP Faqs
C Faqs        C++ Faqs        Java Faqs        J2ee Faqs        Springs Faqs        Hibernate Faqs        Web sphere Faqs        Web Logic Server Faqs   
WordPress        Siebel Faqs        Shell Scripting Faqs        Perl Scripting Faqs        Open Source        Data Ware Housing Faqs        Joomla Faqs
Ajax        Oracle Apps Faqs        Testing Tools Faqs        Mainframes Faqs        Tibco Faqs        PHP Faqs        .Net Faqs       Operating System
 
 
C Interview Questions
Custom Search
54

How can I convert a string to a number?

Ans: The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.
The following functions can be used to convert strings to numbers: Function Name Purpose atof() Converts a string to a double-precision floating-point value. atoi() Converts a string to an integer. atol() Converts a string to a long integer. strtod() Converts a string to a double-precision floating-point value and reports any “leftover” numbers that could not be converted. strtol() Converts a string to a long integer and reports any “leftover” numbers that could not be converted.  strtoul() Converts a string to an unsigned long integer and reports any “leftover” numbers that could not be converted.
55

How do you print only part of a string?

Ans:

/* Use printf() to print the first 11 characters of source_str. */
printf(“First 11 characters: ‘%11.11s’n”, source_str);

56

What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be us

Ans:


 Answer The strcpy() function is designed to work exclusively with strings. It copies each byte of  the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy() function is designed to work with any type of  data. Because not all data ends with a null  character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination.

57

What is a pragma?

Ans:

The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the  #pragma statement. For instance, your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive. To implement this option using the #pragma directive, you would put the following line into your code:
#pragma loop_opt(on)
Conversely, you can turn off loop optimization by inserting the following line into your code:
#pragma loop_opt(off)

58

How can I convert a number to a string?

Ans:

The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa The following functions can be used to convert integers to strings: Function Name Purposeitoa() Converts an integer value to a string. ltoa() Converts a long integer value to a string. ultoa() Converts an unsigned long integer value to a string. The following functions can be used to convert floating-point values to strings: Function Name Purpose ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point. fcvt() Same as ecvt(), but forces the precision to a specified number of digits. gcvt() Converts a double-precision  floating-point value to a string with an embedded decimal point.

59

Is it better to use a macro or a function?

Ans:

The answer depends on the situation you are writing code for. Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at  the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function. However, macros are generally small and cannot handle large, complex coding constructs. A function is more suited for this type of situation. Additionally,macros are expanded inline, which means that the code is replicated for each occurrence of a macro. Your code therefore could be somewhat larger when you use macros than if you were to use functions. Thus, the choice between using a macro and using  a function is one of deciding between the tradeoff of faster program speed versus smaller  program size. Generally, you should use macros  to replace small, repeatable code sections, and
you should use functions for larger coding tasks that might require several lines of code.

60

How do you override a defined macro?

Ans:

You can use the #undef preprocessor directive to undefine (override) a previously defined macro.

61

What are the standard predefined macros?

Ans: 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_ _ Inserts the current time of compilation in your code.
_ _STDC_ _ Is set to 1 if you are enforcing strict ANSI C conformity.
_ _cplusplus Is defined if you are compiling a C++ program.
62

Can you define which header file to include at compile time?

Ans: 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 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
63

How many levels deep can include files be nested?

Ans:

Even though there is no limit to the number of levels of nested include files you can have, your compiler might run out of stack space while trying to include an inordinately high number of files. This number varies according to your hardware configuration and possibly your compiler.

64

Can a file other than a .h file be included with #include?

Ans:

 The preprocessor will include whatever file you specify in your #include statement. Therefore, if you have the line   #include  in your program, the file macros.inc will be included in your precompiled program. It is, however, unusual programming practice to put any file that does not have a .h or .hpp extension  in an #include statement.
You should always put a .h extension on any of  your C files you are going to include. This method makes it easier for you and others to identify which files are being used for preprocessing purposes. For instance, someone modifying or debugging your program might not know to look at the macros.inc file for macro definitions. That person might try in vain by searching all files with .h extensions and come up empty. If your file had been named macros.h, the search would have included the macros.h file, and the searcher would have been able to see what macros you defined in it.

65

What is Preprocessor?

Ans:

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.
The preprocessor contains many features that are powerful to use, such as creating acros,performing conditional compilation, inserting predefined environment variables into your code, and turning compiler features on and off. For the professional programmer, in-depth knowledge of the features of the preprocessor can be one of the keys to creating fast, efficient programs.

66

Question How can you avoid including a header more than once?

Ans: 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 check whether that symbolic name has already been assigned. If it is assigned, you should not include the header, because it has already been preprocessed. If it is not defined, you should define it to avoid any further inclusions of the header. The following header illustrates this technique:
 
#ifndef _FILENAME_H
#define _FILENAME_H
#define VER_NUM “1.00.00”
#define REL_DATE “08/01/94”
#if _ _WINDOWS_ _
#define OS_VER “WINDOWS”
#else
#define OS_VER “DOS”
#endif
#endif
 
When the preprocessor encounters this header, it  first checks to see whether  _FILENAME_H has been defined. If it hasn’t been defined, the header has not been included yet, and the _FILENAME_H symbolic name is defined. Then, the rest of the
header is parsed until the last #endif is encountered, signaling the end of the conditional #ifndef _FILENAME_H statement. Substitute the actual name of the header file for “FILENAME” in the preceding example to make it applicable for your programs.
67

How can I make sure that my program is  the only one accessing a file?

Ans:

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. For example, the following snippet of code shows a file being opened in shared mode, denying access to all other files: /* Note that the sopen() function is not ANSI
compliant... */
fileHandle = sopen(“C:DATASETUP.DAT”, O_RDWR,
SH_DENYWR);
By issuing this statement, all other programs are denied access to the SETUP.DAT file. If
another program were to try to open SETUP.DAT for reading or writing, it would receive an EACCES error code, denoting that access is denied to the file.

68

How can you restore a redirected standard stream?

Ans: The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state. The dup() function duplicates a file handle. You can use the dup() function to save the file handle corresponding to the stdout standard stream. The fdopen() function opens a stream that has been duplicated with the dup() function.
69

What is the quickest searching method to use?

Ans:

A binary search, such as bsearch() performs, is much faster than a linear search. A hashing algorithm can provide even faster searching. One particularly interesting and fast method for searching is to keep the data in a “digital trie.” A digital trie offers the prospect of being able to search for an item in essentially a constant amount of time,
independent of how many items are in the data set. A digital trie combines aspects of binary searching, radix searching, and hashing. The term “digital trie” refers to the data structure used to hold the items to be searched. It is a multilevel data structure that branches N ways at each level.

Page :    1 | 2 | 3 | 4 | 5 | 6
Top