Top Most C++ And Oracle Interview Questions Part – 6
What Are The Standard Predefined Macros?
ANSI C has six predefined macro. They are
FILE
– Name of the current file
LINE
– Current line number
TIME
– Current time of compilation
DATE
– Current date of compilation
cplusplus
– If program compiled in C++ compiler
STDC
– If ANSI C is followed by the compiler strictly.
What Is A Pragma?
The #pragma preprocessor allows compiler to include or exclude compiler specific features. For example if there is a feature xxx_yyy then,
#pragma xxx_yyy(on)
Forces compiler to include the feature. Conversely, you can turn off it by the following lines:
#pragma xxx_yyy(off)
How To Redefined Macro With Different Value?
The #undef preprocessor can be used to reset a macro. For example,
#ifdef SAMPLE /* Checking if SAMPLE is defined */
#undef SAMPLE /* If so, then reset it */
#endif
#define SAMPLE 0 /* Then redefine with intended value */
What Is An Lvalue?
An lvalue is an expression to which a value can be assigned. The lvalue expression is the one which is located on the left side a statement, whereas an rvalue is located on the right side of a statement. Each assignment must have a valid lvalue and rvalue. The lvalue expression must refer to a storage where something can be stored. It can’t be a constant.
How To Assign One Array To Another?
You can’t assign an array to other. Arrays are not lvalue, because they don’t refer to one variable, rather a set of variables. So they can’t be placed on the left hand side of an assignment statement. For example the following statement will generate compilation error.
int x[5], y[5];
x = y;
What Is The Order Of Operator Precedence, Left To Right Or Right To Left ?
None of them is standard. C does not always start evaluating left to right or right to left. Normally, function calls are done first, followed by complex expressions and then simple expressions. That is why it is best to use parenthesis in all expressions, without depending on precedence.
What Is The Difference Between ++x And X++?
The ++ operator is called the incremental operator. When the operator is placed before, the variable is incremented by 1 before it is used in the statement. When the operator is placed after the variable, then the expression is evaluated first and then the variable is incremented by 1.
What Happens When We Use Incremental Operator In A Pointer?
It depends upon the type of the pointer. It gets incremented by the size of the data type, the pointer is pointing to. For example
char p; p++; /* here p increments by 1*/
int p; p++;/* here p increments by 4(for 32 bit system)*/
Can The Sizeof Operator Be Used To Tell The Size Of An Array Passed To A Function?
No. The sizeof() operator can’t tell the size of an array, because it is actually a pointer to the data type of the array.
Can You Change The Value Of An Array Tag?
No. An array tag can’t be used as a storage, because it is not an Lvalue. It can be thought as a pointer to the datatype of the array which is constant and which can’t be changed or assigned dynamically.
What Are Text And Binary Modes?
Streams can be classified into two types: text streams and binary streams. The text streams are interpreted as per the ASCII values starting from 0 to 255. Binary streams are raw bytes which C can’t interpret, but application has to interpret it itself. Text modes are used to handle, generally text file where as binary modes can be used for all files.
But they won’t give you the content of a file, rather they will give you the file properties and content in raw binary format.
Which One To Use, A Stream Function Or A System Calls?
Stream files are generally better to use, since they provide sufficient amount of buffer for read and write. That is why it is more efficient.
But in a multiuser environment, files can be shared among users. These shared files are secured with lock, where only one user will be able to write at a time. In this scenario, buffering will not be efficient, since the file content will change continuously and it will be slower.
So, normally it is good to use stream functions, but for shared files system calls are better.
What Is The Difference Between A String Copy (strcpy) And A Memory Copy (memcpy)?
Generally speaking, they both copy a number of bytes from a source pointer to a destination pointer. But the basic difference is that the strcpy() is specifically designed to copy strings, hence it stops copying after getting the first ”(NULL) character. But memcpy() is designed to work with all data types. So you need to specify the length of the data to be copied, starting from the source pointer.
How Can I Pad A String To A Known Length?
printf(“%-20.20s”, data[d]);
The “%-20.20s” argument tells the printf() function that you are printing a string and you want to force it to be 20 characters long. By default, the string is right justified, but by including the minus sign (-) before the first 20, you tell the printf() function to left-justify your string. This action forces the printf() function to pad the string with spaces to make it 20 characters long.
What Does Const Keyword Do?
The access modifier keyword “const” tells compiler that the value of this variable is not going to be changed after it is initialized. The compiler will enforce it throughout the lifetime of the variable.
Char *p=”sampletext” , *q =”sampletext”; Are These Two Pointers Equal ? If Yes , Then Explain?
In C, strings(not array of characters) are immutable. This means that a string once created cannot be modified. Only flushing the buffer can remove it. Next point is, when a string is created it is stored in buffer.
Next time, when a new string is created, it will check whether that string is present in buffer or not. If present, that address is assigned. Otherwise, new address stores the new string and this new address is assigned.
When Should A Type Cast Be Used?
There are two main uses of type cast.
The first one is to convert some value of datatype A to a datatype B. Such as, if you type cast a float variable of value 1.25 to int, then it will be 1.
The second use is to cast any pointer type to and from void *, in order to use it in generic functions such as memory copy functions, where the execution is independent of the type of the pointer.
What Is The Difference Between Declaring A Variable And Defining A Variable?
Declaration is done to tell compiler the data type of the variable, and it inherently meant that somewhere in this scope, this variable is defined or will be defined. And defining a variable means to allocate space for that variable and register it in the stack memory. For
example:
extern int decl1; /* this is a declaration */
int def2; /* this is a definition */
Can Static Variables Be Declared In A Header File?
You can’t declare a static variable without definition (this is because they are mutually exclusive storage classes). A static variable can be defined in a header file, but then every source file with in that scope will have their own copy of this variable, which is intended.