Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by rwniceing
at 2024-08-28 00:37:41
Point:500 Replies:9 POST_ID:829198USER_ID:12079
Topic:
C Programming Language;;C++ Programming Language
My question is on the following testing C code in test.c
Question-1
keyword of __cpluscplus is stand for the file extension of the running file such as test.c, Right ?
so test.c file has c file extension so the following typedef will be active, right ?
#ifndef __cplusplus
typedef unsigned char bool;
static const bool False = 1;
static const bool True = 0;
#endif
Question-2 What is different between defined the function as macro and define function in the code body ?Any advantage over others ? Both I tested to get the same result.
#define getmax_macro(a,b) ((a)>(b)?(a):(b))
int getmax(int a, int b){}
Please advise
Rwniceing
test.c
======
Question-1
keyword of __cpluscplus is stand for the file extension of the running file such as test.c, Right ?
so test.c file has c file extension so the following typedef will be active, right ?
#ifndef __cplusplus
typedef unsigned char bool;
static const bool False = 1;
static const bool True = 0;
#endif
Question-2 What is different between defined the function as macro and define function in the code body ?Any advantage over others ? Both I tested to get the same result.
#define getmax_macro(a,b) ((a)>(b)?(a):(b))
int getmax(int a, int b){}
Please advise
Rwniceing
test.c
======
#include <stdio.h>#ifndef __cplusplustypedef unsigned char bool;static const bool False = 1;static const bool True = 0;#endif#define getmax_macro(a,b) ((a)>(b)?(a):(b))int getmax(int a, int b){ if (a>b)return a;else return b;}int main(){ #if !defined(__BOOL_DEFINED) printf("bool is not supported"); #elif defined(__BOOL_DEFINED) printf("bool is supported%d",__BOOL_DEFINED); #endif bool arr[2] = {false, true}; printf("bool is supported%d",arr[0]); int x=5, y; y= getmax_macro(x,2); printf("getmax_macro: %d",y); y= getmax(x,2); printf("getmax: %d",y); return 0;} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:
Author: rwniceing replied at 2024-08-30 21:44:54
Thanks for your reply
Author: rwniceing replied at 2024-08-28 05:13:55
I mean reliable that is more safe enough to get the final result from function than macro .
Macro is not able to be debugged , Right as zoppo mentioned it ?
Macro is not able to be debugged , Right as zoppo mentioned it ?
Expert: ozo replied at 2024-08-28 05:03:22
I'm not sure what you mean by reliable.
You can rely on no more than that both should behave as specified by the language standard.
There are things that can be done with macros and not with functions, and there are things that can be done with functions and not with macros.
There can be situations where a macro will run faster, and there can be situations where a function will run faster.
You can rely on no more than that both should behave as specified by the language standard.
There are things that can be done with macros and not with functions, and there are things that can be done with functions and not with macros.
There can be situations where a macro will run faster, and there can be situations where a function will run faster.
Author: rwniceing replied at 2024-08-28 04:54:20
ozo, in other words, function is more reliable than macro, right ? macro is good for small or simple code that run
faster than a function.
Which method(macro/function) will run faster and use less memory?
faster than a function.
Which method(macro/function) will run faster and use less memory?
Author: rwniceing replied at 2024-08-28 04:50:43
Thanks for your reply and also I found one process flow for compiler process on C/CPP
at this link, https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.4
and described the header file and enable macro file with .i and .ii file extension
at this link, https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.4
and described the header file and enable macro file with .i and .ii file extension
Assisted Solution
Expert: ozo replied at 2024-08-28 04:49:06
100 points EXCELLENT
getmax(++a,++b) would work behave differently when called as a macro rather than a function
Assisted Solution
Expert: Zoppo replied at 2024-08-28 02:54:00
200 points EXCELLENT
Yes, macros are expanded in a pre-compile step by the preprocessor (http://en.wikipedia.org/wiki/Preprocessor)
When to put functionality into a function or to a macro depends on the need (and a bit on the habit of the programmer).
Macros IMO have some disadvatages compared to functions, i.e.:
- they (at least with IDEs/debuggers I know) cannot be debugged
- it's not possible to overload functions (see http://www.learncpp.com/cpp-tutorial/76-function-overloading/)
- parameters have no type, so i.e. it's not possible to write a SWAP macro
- with functions the compiler can decide whether it's better to use it inlined or not, macros are always inlined and therefor may blow up the code without any benefit.
I for myself in case both macro or function is possible for a needed functionality prefer functions. In a case like your 'getmax' sample this is not the best solution since in this case I would have to implement the same function multiple times for any variable type (i.e. once for int, once for float). In this case I prefer using a function template instead of a macro because types are defined (i.e. with your macro you can compare an int with a float, in a function template you can avoid or allow using different types).
So I would implement such a getmax-functionality as a template somehow like this:
When to put functionality into a function or to a macro depends on the need (and a bit on the habit of the programmer).
Macros IMO have some disadvatages compared to functions, i.e.:
- they (at least with IDEs/debuggers I know) cannot be debugged
- it's not possible to overload functions (see http://www.learncpp.com/cpp-tutorial/76-function-overloading/)
- parameters have no type, so i.e. it's not possible to write a SWAP macro
- with functions the compiler can decide whether it's better to use it inlined or not, macros are always inlined and therefor may blow up the code without any benefit.
I for myself in case both macro or function is possible for a needed functionality prefer functions. In a case like your 'getmax' sample this is not the best solution since in this case I would have to implement the same function multiple times for any variable type (i.e. once for int, once for float). In this case I prefer using a function template instead of a macro because types are defined (i.e. with your macro you can compare an int with a float, in a function template you can avoid or allow using different types).
So I would implement such a getmax-functionality as a template somehow like this:
template < typename _TYPE >_TYPE GetMax( _TYPE a, _TYPE b ){ return ( a > b ? a : b );} 1:2:3:4:5:
So, in other words I only use macros where they are the only method to simplify things which can't be solved another way. A good example for this is a macro to determine the length of a static C-array, i.e.:
#define SIZEOF_ARRAY( x ) ( sizeof( x ) / sizeof( x[0] ) )double numbers[] = { 1.0, 1.5, 1.8, 2.3, 2.7 };for ( int n = 0; n < SIZEOF_ARRAY( numbers ); n++ ){ cout << numbers[n];} 1:2:3:4:5:6:7:8:
Here it's not possible to implement SIZEOF_ARRAY with only one argument as a function instead of a macro.
ZOPPO
ZOPPO
Author: rwniceing replied at 2024-08-28 01:04:53
For question-1: solved
for question-2
macro is defined on preprocessor from #define, so it will be compiled first before compiling other function or main() code. Right ? Whereever we put #define on the top of c file or bottom of c file, it will be compiled first, Right ?
in otherwords, there is no difference between two methods besides compiling priority, right ?
Why not we put all function code to #define macro instead of inside body ?
It is time to think about preprocessor meaning .
Please advise
Rwniceing
for question-2
macro is defined on preprocessor from #define, so it will be compiled first before compiling other function or main() code. Right ? Whereever we put #define on the top of c file or bottom of c file, it will be compiled first, Right ?
So the macro here is comparable to a inline function
in otherwords, there is no difference between two methods besides compiling priority, right ?
Why not we put all function code to #define macro instead of inside body ?
It is time to think about preprocessor meaning .
Please advise
Rwniceing
Accepted Solution
Expert: Zoppo replied at 2024-08-28 00:49:31
200 points EXCELLENT
Hi rwniceing,
1.) No, the __cplusplus macro is set when the file is compiled with a C++ compiler, if it is compiled with a C compiler it is not set. This doesn't really depend on the file extension (i.e. it's possible to compile a *.c file with a C++ compiler), usually you define what compiler is used for the files in a make file or, i.e. with VisualStudio, in the project files' properties.
2.) The difference is the first one (the macro) is simply a placeholder for the code defined for the macro so that code is compiled directly at the place where you put the macro. The second one is a function which is put anywhere in the binary and called the same way I told you at http://www.experts-exchange.com/Programming/Languages/CPP/Q_28506245.html#a40287844
So the macro here is comparable to a inline function.
Hope that helps,
ZOPPO
1.) No, the __cplusplus macro is set when the file is compiled with a C++ compiler, if it is compiled with a C compiler it is not set. This doesn't really depend on the file extension (i.e. it's possible to compile a *.c file with a C++ compiler), usually you define what compiler is used for the files in a make file or, i.e. with VisualStudio, in the project files' properties.
2.) The difference is the first one (the macro) is simply a placeholder for the code defined for the macro so that code is compiled directly at the place where you put the macro. The second one is a function which is put anywhere in the binary and called the same way I told you at http://www.experts-exchange.com/Programming/Languages/CPP/Q_28506245.html#a40287844
So the macro here is comparable to a inline function.
Hope that helps,
ZOPPO