Solved

Interview help.

Posted on 2006-11-30
7
226 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
  • 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 93
while loop over for loop 7 105
modThree challenge 4 91
x-bar in Google Sheets 2 55
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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