LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Primitive questions (https://www.linuxquestions.org/questions/programming-9/primitive-questions-940588/)

alaios 04-19-2012 01:50 AM

Primitive questions
 
Dear all,
I am having few questions regarding c (I am starting a new thread) as I do not want to mix those with me previous network programming questios, I posted here earlier.



1) I found that one can get information about basic c function from linux bash with man... Well I have to admit that I did not expect that. I thought that the man is only related to the bash linux only. Where it gets the information for the the c libraries. How it distingushes between c and bash commands?

2) I have found in my c document a text about macros. What is a macro? Is for example the sizeof myvariable a macro? Where these are declared?

3) IF i have a struct lets call it mystruct and then I do memset(&mystruct,0, sizeof mystruct) this will set to zeros all the variables inside of the struct, regardless what type of variables it includes. Is not that right?

I would like ot thank you in advance for your help
Regards
Alex

pan64 04-19-2012 02:20 AM

1. man has sections. see man man. In general you can ask for a manual of anything available on a linux.
2. macro: http://h30097.www3.hp.com/docs/base_...E/DOCU_077.HTM
3. right

alaios 04-19-2012 03:34 AM

Quote:

Originally Posted by pan64 (Post 4656913)
1. man has sections. see man man. In general you can ask for a manual of anything available on a linux.
2. macro: http://h30097.www3.hp.com/docs/base_...E/DOCU_077.HTM
3. right

3) and what is the difference between define and typedef? Could not be only that define does also the work of typedef?

pan64 04-19-2012 03:38 AM

define is general, you can do strange things with define, for example:
Code:

#define BUBBLE +
int a, b, c;
a=5;
b=7;
c = a BUBBLE b;  # this will work, c will be 12

typedef is only used to declare new types, or give unique names to given types.
with this you can force the compiler for stricter checks

alaios 04-19-2012 04:43 AM

Quote:

Originally Posted by pan64 (Post 4657009)
define is general, you can do strange things with define, for example:
Code:

#define BUBBLE +
int a, b, c;
a=5;
b=7;
c = a BUBBLE b;  # this will work, c will be 12

typedef is only used to declare new types, or give unique names to given types.
with this you can force the compiler for stricter checks

I need a clarification here, can I also do
#define int mytype # (the second # ends the define statement)
and then use mytype to define a new data type

mytype MyNewIntegerVariable

What is the "extra" that typedef offers?
Alex

pan64 04-19-2012 04:53 AM

in case of a macro the preprocessor will evaluate it and therefore they will replace mytype with int, so the syntax checker will see int.
in case of typedef the preprocessor will leave it as is (because it is not a macro) and the syntax checker will be able to handle them as different types.
so in case of macro (#define int mytype) void myfunc(int) and void myfunc(mytype) will be exactly the same, using typedef they will be incompatible, so you can distinguish them.
the compiler will generate the same code anyway.

ta0kira 04-20-2012 11:30 AM

Quote:

Originally Posted by alaios (Post 4657001)
3) and what is the difference between define and typedef? Could not be only that define does also the work of typedef?

Here's an illustrative example:
Code:

typedef int size_type;

#define MAIN_START int main() {
#define MAIN_STOP  }

MAIN_START
    size_type some_value = 0;
    return some_value;
MAIN_STOP

A macro can be used to define arbitrary text replacements, whereas a typedef has semantic meaning in C.
Kevin Barry


All times are GMT -5. The time now is 11:03 PM.