Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is the difference between #define and typedef in C?

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
one question on c

what is the difference between #define and typedef???
 

Re: one question on c

The C LANGUAGE is composed of a PREPROCESSOR and THE C COMPILER itself.
#define is a PREPROCESSOR KEYWORD not a C LANGUAGE KEYWORD .
The Preprocessor helps you to assigns HIGH LEVEL LANGUAGE NAMES TO THINGS .. This is just TO HELP THE PROGRAMMER OUT ..The C COMPILER DOESN'T care ..
EXAMPLE .. #define PI 3.14159 .. SO now you can use PI in your program instead of writing all those numbers time after time.

Typedefs is COMPLETLY different this is a C LANGUAGE Keyword .
It allows you to create new memory OBJECTS so you can refer to them as new data entities
The C compiler will use this TYPEDEFS declarations to assign MEMORY space and to keep track on how to read and write to this new data structures .
 

Re: one question on c

everything that the previous guy said was correct however since i am assuming you are a newbie, you probably didn't understand much of what he said,, here is the layman's version,, typedef is used to create a new type,, similar to a structure etc,, define simply replaces one value with another i.e. pi with 3.1415 etc
 

one question on c

#define PI 3.14159 , PI is a number, after the propressor's process, what you can see is 3.14159 and you can't see PI . If you don't agree,you can try to find PI in a debugger and you will understand.
typedef int PI, now PI is a synonym of int
 

Re: one question on c

with #define cannt declare a variable ,
typedef creates a new datatype for new just like a class.

but the good thing about typedef is you can declare even datatypes that are infact pointers.

i.e.

struct
{
int a;
int b;
}twoInts;

typedef twoInts* ptrTwoInts;

now you can create a pointer to the struct using
ptrTwoInts p;
 

one question on c

has anyboby used VOLATILE keyword!

Added after 38 seconds:

hi!
has anyboby used VOLATILE keyword!
 

Re: one question on c

vasureddym said:
has anyboby used VOLATILE keyword!

Added after 38 seconds:

hi!
has anyboby used VOLATILE keyword!

Sure volatile means that a variable can be change by an external event. The optimizer can not assume that a variable is unchanged during access.

Most of the time volatile is used when a variable is changed in a function and during interrupt.

Also look here:


best regards
 

one question on c

I read a lot of text saying "PREPROCESSOR", but what is the meaning of it? Could some explain please?
 

Re: one question on c

saturn said:
I read a lot of text saying "PREPROCESSOR", but what is the meaning of it? Could some explain please?

Read for example this info:
**broken link removed**

best regards
 

Re: one question on c

PREPROCESSOR is used to increase readablitiy of code. they are handled before the compilation process ;

For example if you have code piece like this

#define PI 3.14

float area = PI * r *r ;

source is preprocessed and converted to

float area = 3.14 * r * r ;

typedef is used for type decleration .
 

Re: one question on c

I read a lot of text saying "PREPROCESSOR", but what is the meaning of it? Could some explain please?

From the moment you finish your text file till you have binary form of compiled and linked file there are several steps that are performed. You should first understand what happens when you want to compile your source file. In short:
1. First C compiler changes all #define statements in your original file but in background. This means that if you declared #define max_time 10 than compiler changes all max_time labels in 10. This way if you have lines like:

#define max_time 10
......
int k;
k=max_time;

after first stage of compile process (usally called preprocessor stage) these lines are changed to:

int k;
k=10;

This is only a part of the functions that are accomplished by preprocessor. This is similar to #include directive. Preprocessor on some compiler are inserting texsts from other files in one file that is later compiled.

2. In this stage compiler makes tables of all used variables and size of them.
..........
..........
..........

After all stages are finished compiler generates object files that is not jet usable executable file. This is made lake this because in your initial source you maybe use some function from other file or from library. Functions that are not included in your file are still unresolved. This means that in object file created from your source there is only information that these function are called from other file or library. This way once you make, lets say function for addition of two LWORD variables, you don't need to include this function in all of your source codes, but you'll have to make object file from this function, and later you can call it from you source files.
Let say that you have allready made function for addition of two LWORD variables and you need this function in your latest program. In your latest source you should only include prototype of this function. Than you will compile your latest code and you'll get object file(in this file addition function is not included). Now you should call liker program that will resolve function calls from all object fles you have. This way your latest code will be connected with older object file that contains function for addition. Similar process is for linking functions from c library.

Added after 10 minutes:

About typedef.
typedef is used its name says for definition of non standard types of variables. Let say that you need unsigned integer variable. Whan can you do? You can put everywhere
unsigned int declaration or you can define new type of variable:

typedef unsigned int uint;

and you can use uint instead of unsigned int. Yes, you can make similar with #define, but for more complex definitions typedef has to be defined. Using typedef you can also use sizeof (type) function, and for definitions using #define you can not use sizeof(type) function....
 

Re: one question on c

Preprocessor is a feature in C Language that works with only the preprocessor keywords. Preprocessor keywords are user-defined variables to represent frequently-used constants in the C program.

Examples:

#define PI 3.14
#define g 9.81

It's like making some templates or stencils before and you simply use the templates when you need one, instead of re-doing a new one.

#typedef as the name implies is define a user-defined datatype explicitly. Physically, it is a set of memory spaces created under this datatype. Any variable declared in this structure will use the created spaces.

#typedef is frequently used in the enumerated form to allow customed or enumerated types.

Example 1:

struct menu_tag
{
char* cod_fish;
char* red_wine;
}french_cuisine;

#typedef french_cuisine menu1;

Alternatively you can also use, #typedef struct menu_tag menu1. It means the same as the previous line.

menu1 is the user-defined type of french_cuisine. It has members of cod_fish and red_wine in the form of unbounded character arrays.

Example 2:

#typedef enum chinese_tag
{
char* chow_mee;
int order_no;
}chinese_cuisine;

chinese_cuisine menu2;

The style is different but #typedef and enum makes the user definition easier for use.

Added after 5 minutes:

In case you wonder what is char*, it's simply a character pointer or the base address of a character array, commonly used to address the first character address of a string, but you do not need to specify how many characters you need to use, hence you can ignore how many you actually need.
 

Re: one question on c

C-Man said:
vasureddym said:
has anyboby used VOLATILE keyword!

Added after 38 seconds:

hi!
has anyboby used VOLATILE keyword!

Sure volatile means that a variable can be change by an external event. The optimizer can not assume that a variable is unchanged during access.

Most of the time volatile is used when a variable is changed in a function and during interrupt.

Also look here:
h**p://&highlight=

best regards

Allow me to add my two-bits :)

The C compiler can try to optimize the code so as to remove unnecessary operations. This can result in a faster and a more efficient program. But sometimes the compiler can be dead wring in performing this kind of optimization. For example, if the compiler encounters this sequence of statements:

Code:
x = 10 * ((10 + 20) / (100 - 40));
/* some statements not modifying x */
x = 20;
/* some more statements not modifying x */
printf("\n%d", x);

then it can quickly figure out that it is useless to perform the first calculation for assigning the value of x as later on 20 will be assigned. Thus optimization will reult in removal of the first statement from ever seeing the light of the day.

But consider if x were a shared variable. In that case, you cannot safely assume that x will have the value 20. You can't even neglect the first statement as it may be necessary for some other part of the code. So you will want to instruct the compiler to not perform the optimization for this particular variable. The method to do this is to qualify the variable with the volatile keyword. Now the compiler will not attempt any optimization and your code will work as you desire.

Hope that helped...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top