Solved

Interview help.

Posted on 2006-11-30
7
230 Views
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.
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
Comment
Question by:beginer92
[X]
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
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 18051039
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
 

Author Comment

by:beginer92
ID: 18051092
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
 
LVL 86

Expert Comment

by:jkr
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);
0
 
LVL 8

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

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

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

617 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