C FAQS
Q: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);
Q:In C, what is the difference between a static variable and global
variable?
Ans:A static variable declared outside of any function is accessible only
to all the functions defined in the same file (as the static variable).
However, a global variable can be accessed by any function (including the ones
from different files).
Q:In C, why is the void pointer useful?
Ans:When would you use it? The void pointer is useful because it is a
generic pointer that any pointer can be cast into and back again without loss
of information.
Q:What is Polymorphism ?
Ans:'Polymorphism' is an object oriented term. Polymorphism may be defined
as the ability of related objects to respond to the same message with
different, but appropriate actions. In other words, polymorphism means taking
more than one form. Polymorphism leads to two important aspects in Object
Oriented terminology - Function Overloading and Function Overriding.
Overloading is the practice of supplying more than one definition for a given
function name in the same scope. The compiler is left to pick the appropriate
version of the function or operator based on the arguments with which it is
called. Overriding refers to the modifications made in the sub class to the
inherited methods from the base class to change their behavior.
Q:What is Operator overloading ?
Ans:When an operator is overloaded, it takes on an additional meaning
relative to a certain class. But it can still retain all of its old meanings.
Examples:
1) The operators >> and << may be used for I/O operations because in the
header, they are overloaded.
2) In a stack class it is possible to overload the + operator so that it
appends the contents of one stack to the contents of another. But the +
operator still retains its original meaning relative to other types of data.
Q:What are Templates
Ans:C++ Templates allow u to generate families of functions or classes
that can operate on a variety of different data types, freeing you from the
need to create a separate function or class for each type. Using templates, u
have the convenience of writing a single generic function or class definition,
which the compiler automatically translates into a specific version of the
function or class, for each of the different data types that your program
actually uses. Many data structures and algorithms can be defined
independently of the type of data they work with. You can increase the amount
of shared code by separating data-dependent portions from data-independent
portions, and templates were introduced to help you do that.
Q:What is the difference between run time binding and compile time binding?
Ans:Dynamic Binding :
The address of the functions are determined at runtime rather than @ compile
time. This is also known as "Late Binding".
Static Binding :
The address of the functions are determined at compile time rather than @ run
time. This is also known as "Early Binding"
Q:What is Difference Between C/C++
Ans:C does not have a class/object concept.
C++ provides data abstraction, data encapsulation, Inheritance and
Polymorphism.
C++ supports all C syntax.
In C passing value to a function is "Call by Value" whereas in C++ its "Call
by Reference"
File extension is .c in C while .cpp in C++.(C++ compiler compiles the files
with .c extension but C compiler can not!)
In C structures can not have contain functions declarations. In C++ structures
are like classes, so declaring functions is legal and allowed.
C++ can have inline/virtual functions for the classes.
c++ is C with Classes hence C++ while in c the closest u can get to an User
defined data type is struct and union.
Why doesn't the following code give the desired result?
int x = 3000, y = 2000 ;
long int z = x * y ;
Here the multiplication is carried out between two ints x and y, and the
result that would overflow would be truncated before being assigned to the
variable z of type long int. However, to get the correct output, we should use
an explicit cast to force long arithmetic as shown below:
long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.
Q:Why doesn't the following statement work?
Ans:char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
The string function strcat( ) concatenates strings and not a character. The
basic difference between a string and a character is that a string is a
collection of characters, represented by an array of characters whereas a
character is a single character. To make the above statement work writes the
statement as shown below:
strcat ( str, "!" ) ;
Q:How do I know how many elements an array can hold?
Ans:The amount of memory an array can consume depends on the data type of
an array. In DOS environment, the amount of memory an array can consume
depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In
general an array cannot consume more than 64 kb. Consider following program,
which shows the maximum number of elements an array of type int, float and
char can have in case of Small memory model.
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}