Reputation: 23
I just started learning C and I was wondering if there is a difference between an array declared like this one:
int m[10][5];
and an array declared like this:
#define NL 10
#define NC 5
int m[NL][NC];
I tought that the memory allocated might be different,but i'm not really sure.
Upvotes: 1
Views: 86
Reputation: 427
The #define
's are "preprocessor directives". The C preprocessor (CPP) is basically a program that is run by your compiler before actually processing your C code.
For your #define
's, the preprocessor basically does a "search and replace" for NL
and NC
, replacing with 10
and 5
, respectively. It does this without much understanding of C. (In fact, you could run the C preprocessor on any kind of text file and have it do the same search and replace.)
To get gcc
or clang
to stop after running the preprocessor, use the -E
option and it'll write the preprocessor results to stdout.
Here's an example of having GCC stop after running the preprocessor on your source code:
$ gcc -E andre.c
# 1 "andre.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "andre.c"
int m[10][5];
As you can see, after running the preprocessor, NL
and NC
have been replaced with the text you supplied in their #define
's.
If you're using a compiler other than gcc
or clang
, check its documentation for how to stop processing after running the preprocessor. Often, preprocessed source code has the extension .i
.
(On my machine, cpp andre.c
does the exact same thing as the call to gcc
above.)
Upvotes: 1
Reputation: 11
The C/C++ macros will be replaced by the compiler with inline code and there is no difference between declaration with #define and the inline code.
Upvotes: 1