• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Interview help.

I will be interviewing for an internship position and I need answers to the following questions as quickly as possible.
Thanks

 What are static variables?
 What are extern variables?
 Difference between malloc and calloc?
 what are static functions in C?
 How can you connect C program to C++?
 Difference between inline and macros?
 What are default things class provides?
  Write function for strlen?

Any input would be highly appreciated.
 
0
beginer92
Asked:
beginer92
  • 2
2 Solutions
 
jkrCommented:
That should be easy to look up, but anyway:

>> What are static variables?

http://en.wikipedia.org/wiki/Static_variable#Static_Variables_as_Class_Variables

>> What are extern variables?

"The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s)."

>> Difference between malloc and calloc?

'calloc()' allocates an array in memory with elements initialized to 0, 'malloc()' performs no initialization.

>> what are static functions in C?

Used with a function at file scope, the static keyword specifies that the function has internal linkage (its name is not visible from outside the file in which it is declared).

>> How can you connect C program to C++?

By removing the C++ name decoration by declaring the interfaces as 'extern "C"'

>> Difference between inline and macros?

Macros are expanded by the preprocessor, inline functions by the compiler

>> What are default things class provides?

There are none if the class is thought to be non-trivial. Apart from that, a default destructor that cleans up members as good as possible.

>>  Write function for strlen?

size_t my_strlen(char* s) {

size_t len = 0;

while(*s++) len++;

return len;
}
0
 
beginer92Author Commented:
Thanks!
Can u pls expalin more on Difference between inline and macros?

and

How can you connect C program to C++?

Thanks for the help again!
0
 
jkrCommented:
>>Can u pls expalin more on Difference between inline and macros?

That already is the main difference. Macros are explanded before compilation, inline functions are placed into the code during compilation.

>>How can you connect C program to C++?

As explained above, you need to take care that the C++ part does not contain decorated names, which is done by using

extern "C"
int SomeFunction(int n, char* p);
0
 
deepu chandranCommented:
>>Can u pls expalin more on Difference between inline and macros?

   Jkr missed out to tell about performance.In case of macros,execution will be very faster because it embed the code into the function.Macros you can used to avoid some part of code with out commeding it
Here is an example for that,

#define TEST

main()
{
    .....
   .....
#ifdef TEST
      .....    //These will be in your compiled code  
#end if
   ....
#ifndef
   .....     //These willnot be in your compiled code  

#end if

}

Here is the very important advantage of inline function oner macro. Unlike #define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:


 // A macro that returns the absolute value of i
 #define unsafe(i)  \
         ( (i) >= 0 ? (i) : -(i) )
 
 // An inline function that returns the absolute value of i
 inline
 int safe(int i)
 {
   return i >= 0 ? i : -i;
 }
 
 int f();
 
 void userCode(int x)
 {
   int ans;
 
   ans = unsafe(x++);   // Error! x is incremented twice
   ans = unsafe(f());   // Danger! f() is called twice
 
   ans = safe(x++);     // Correct! x is incremented once
   ans = safe(f());     // Correct! f() is called once
 }

I think this will clear out your douts.

cheers

Deepu
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now