Several questions about C programming functions

Status
Not open for further replies.

sacrpio

Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
HELP ON C

Dear All,
Here r few problems on C, I am not getting the answer of these...
Please help??????????
1. How can u implement a class in C.
2. What is difference between MACRO & INLINE Function.
3. What is the application of UNION.
4. Why we use pointer to functions.
5. What is difference between STATIC & GLOBAL STATIC.

Rgds,
bye......
 

Re: HELP ON C

Let's see what we can say about these

sacrpio said:
1. How can u implement a class in C.
You can't ;-)
class is a keyword that isn't supported in C (it's a C++ keyword)
You can use a struct and do some object oriented programming in C. But it will be limited and some construction will be not very intuitive.

sacrpio said:
Dear All,
2. What is difference between MACRO & INLINE Function.
A MACRO is a preprocessor statement. The C preprocessor just replaces the code where you use the macro with the code defined in the macro. It is done before compilation! MACRO's can cause strange errors since the compiler doesn't know the definition of the macro. It only sees the code generated by the preprocessor.
An inline function is a piece of code that is replaced by the compiler. If you make a call to an inline function, no function call is compiled.
for example:
if you create a function to update a variable, the compiler translates this into a function call, update the variable, and a return from the function.
if you create an inline function this is just the update of the variable.
=> inline is faster but expands your code size allot if it is used incorrect!

Both a Macro and an inline function have the same purpose. Avoid the function call. But they are done on a different level (preprocessor<->compiler). Best is to use inline functions when possible. It will be easier to debug.

sacrpio said:
3. What is the application of UNION.
A union is used when you want to address the same memory in different ways without having to use those ugly casts!
eg. you want to use a 16-bit value as a 16-bit integer or as 2 8-bit chars (high and low).

sacrpio said:
4. Why we use pointer to functions.
Don't use them if you don't know why they are used ;-).
Pointers to functions can be used to create more general functions.
eg. if you create a general sort function for objects (not specified) and define the biggerThen function for each object you want to sort.
Or you could use it to define a callback function. (eg, if you want to specify the function to be called when a timer is finished)

sacrpio said:
5. What is difference between STATIC & GLOBAL STATIC.
Static variables (source: http://crasseux.com/books/ctutorial/Static-variables.html)
A second important storage class specifier is static. Normally, when you call a function, all its local variables are reinitialized each time the function is called. This means that their values change between function calls. Static variables, however, maintain their value between function calls.

Every global variable is defined as static automatically. (Roughly speaking, functions anywhere in a program can refer to a global variable; in contrast, a function can only refer to a local variable that is "nearby", where "nearby" is defined in a specific manner. See Scope, for more information on global variables. See Expressions and operators, for an example of a static local variable.)

Hope this helps
Antharax
 

HELP ON C

For item 5 :

If variable is defined outside of function scope without static specifier it can be accessed from other modules or files (known at link time). If static is used in its definition it can not be referenced from functions located in different files (will not added to names at link time ).
But it (static variable) can be accessed by pointer to that variable .
 

Re: HELP ON C

sacrpio said:
1. How can u implement a class in C.
you can't implement a class in C, but you can mimic quite a lot of it's functionality using :
1. Function pointers in a structure for the "member function".
2. Ordinary structure "member" for the other "member variables".

sacrpio said:
4. Why we use pointer to functions.
It depends on you, but I use it to achieve "object oriented approach" in C language. Also it can be used to implement a "Function Jump Table", I'm not so sure about it's efficiency, but some argue that using a "Function Jump Table" will make your programm faster :?:

here i attach an example code of my "object oriented approach" to C programming language (compiled using Make and GCC).
 

Re: HELP ON C

refer DIETEL & DIETEL ...u can get all ur answers in this text
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…