Interview help.

Posted on 2006-11-30
Last Modified: 2013-11-20
I will be interviewing for an internship position and I need answers to the following questions as quickly as possible.

 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.
Question by:beginer92
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 86

Accepted Solution

jkr earned 250 total points
ID: 18051039
That should be easy to look up, but anyway:

>> What are static 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;

Author Comment

ID: 18051092
Can u pls expalin more on Difference between inline and macros?


How can you connect C program to C++?

Thanks for the help again!
LVL 86

Expert Comment

ID: 18051119
>>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);

Assisted Solution

by:deepu chandran
deepu chandran earned 250 total points
ID: 18052159
>>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

#ifdef TEST
      .....    //These will be in your compiled code  
#end if
   .....     //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
 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.



Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 120
Host to IP 7 82
Define unique primary key 9 95
Increment column based of a FK 8 48
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question