Solved

Interview help.

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now