c-preprocessor

Why use do { } while (0) in macro definition? [duplicate]

亡梦爱人 提交于 2019-12-02 18:45:28
Possible Duplicate: Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? I met code like below: #define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) I want to know why the author use do { } while (0) here. Is there any difference with this? #define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } BTW: the code is from libev, ev_local.h Consider if( something ) function1(); else function2(); If function1() is actually a macro, just using { } requires you to

displaying #define values in C

久未见 提交于 2019-12-02 18:37:35
问题 I have a series of #defines from a library file header of this sort: typedef int Lib_error; #define LIB_ERROR_A ((Lib_error) 0x0000) #define LIB_ERROR_D ((Lib_error) 0x0100) #define LIB_ERROR_F ((Lib_error) 0x0200) #define LIB_ERROR_K ((Lib_error) 0x0300) #define LIB_ERROR_O ((Lib_error) 0x0400) #define LIB_ERROR_P ((Lib_error) 0x0500) #define LIB_ERROR_R ((Lib_error) 0x0600) #define LIB_ERROR_X ((Lib_error) 0x0700) #define LIB_ERROR_Y ((Lib_error) 0x0800) #define LIB_ERROR_M ((Lib_error)

Class Constants

十年热恋 提交于 2019-12-02 18:28:55
I have several obj-c classes, each of which require a number of constants that are used in switch statements. I had tried defining these numerical constants in the .m file using the #define preprocessor instruction. All these constants begin with 'kCell'. This seems to work well but Xcode's code sense is presenting me with every 'kCell' prefixed constant no matter where I am in the project. Do #define constants have global scope? If so, what is the best way to define purely local class constants that won't class with similarly named constants in other classes? Manjunath Have your own constant

Unexpected Result in Macro

荒凉一梦 提交于 2019-12-02 18:12:39
问题 I have a simple program to calculate the volume of a cube. It runs fine, but the result I get is wrong. It comes out as "Y is 392". Can anyone help me understand why it is 392? I have just begun C, so I do not understand all of the code. I realise this Macro is badly written, I am just trying to understand its behavior before I rewrite it. #define CUBE(x) (x*x*x) void main(void); void main(void){ int x, y; x = 5; y = CUBE(++x); printf("Y is %d \n", y); } 回答1: This is because the macro expands

Why use #if 0 for block commenting out?

谁说我不能喝 提交于 2019-12-02 18:01:36
Reverse engineering code and I'm kind of appalled at the style, but I wanted to make sure there's no good reason for doing these things.... Is it just me or is this a horrible coding style if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name); else sprintf(username,"%d",user_id); And why wrap code not intended for compilation in an #if 0 .... #endif Instead of comments? EDIT: So as some explained below, this is due to the possibility to flummox /* */ which I didn't realize. But I still don't understand, why not just use your programming environment tools or favorite text editor's macro's to block

How do I compile this Fortran code with new 2017 ifort?

混江龙づ霸主 提交于 2019-12-02 17:58:09
问题 I have the following fortran code that compiles with pre 2017 ifort: program parallel_m contains character(500) function PARALLEL_message(i_ss) character(50) :: Short_Description = " " integer :: i_s =0 integer :: n_threads = 0 ! PARALLEL_message=" " ! if (i_s>0) then if (len_trim("test this ")==0) return endif ! if (i_s==0) then PARALLEL_message=trim("10")//"(CPU)" if (n_threads>0) PARALLEL_message=trim(PARALLEL_message)//"-"//trim("200")//"(threads)" else PARALLEL_message=trim("a")//"

What is the purpose of a single pound/hash sign (#) on its own line in the C/C++ preprocessor?

时间秒杀一切 提交于 2019-12-02 16:53:47
I have been looking at the Boost libraries source code, and I have noticed that often there are single pound signs without any preprocessor directives attached to them. I read through the GCC preprocessor manual and specification guide and can't find anything about it. (1) #ifndef BOOST_CONFIG_HPP (2) # include <boost/config.hpp> (3) #endif (4) # (5) #if defined(BOOST_HAS_PRAGMA_ONCE) (6) # pragma once (7) #endif On line 4, there is nothing after the pound sign. What effect does this have? Is it defined in the C preprocessor (CPP) specification? As Boost is a cross-platform library, I would

Difference between preprocessor directives #if and #ifdef

守給你的承諾、 提交于 2019-12-02 16:29:47
What is the difference (if any) between the two following preprocessor control statements. #if and #ifdef You can demonstrate the difference by doing: #define FOO 0 #if FOO // won't compile this #endif #ifdef FOO // will compile this #endif #if checks for the value of the symbol, while #ifdef checks the existence of the symbol (regardless of its value). #ifdef FOO is a shortcut for: #if defined(FOO) #if can also be used for other tests or for more complex preprocessor conditions. #if defined(FOO) || defined(BAR) 来源: https://stackoverflow.com/questions/3802988/difference-between-preprocessor

Redundant macro usage in initializer lists

穿精又带淫゛_ 提交于 2019-12-02 14:15:22
After I were able to successfully reduce code duplication with this I want to further reduce my code duplication with totally different problem #include <iostream> #include <array> #define A1 #define B2 #define C3 #define D4 #define E5 enum Enum { #ifdef A1 one, #endif #ifdef B2 two, #endif #ifdef C3 three, #endif #ifdef D4 four, #endif #ifdef E5 five, #endif end }; const char* map(Enum e) { switch(e) { #ifdef A1 case one : return "one"; #endif #ifdef B2 case two: return "two"; #endif #ifdef C3 case three : return "three"; #endif #ifdef D4 case four: return "four"; #endif #ifdef E5 case five:

The most useful user-made C-macros (in GCC, also C99)? [closed]

老子叫甜甜 提交于 2019-12-02 14:10:43
What C macro is in your opinion is the most useful? I have found the following one, which I use to do vector arithmetic in C : #define v3_op_v3(x, op, y, z) {z[0]=x[0] op y[0]; \ z[1]=x[1] op y[1]; \ z[2]=x[2] op y[2];} It works like that: v3_op_v3(vectorA, +, vectorB, vectorC); v3_op_v3(vectorE, *, vectorF, vectorJ); ... for-each loop in C99: #define foreach(item, array) \ for(int keep=1, \ count=0,\ size=sizeof (array)/sizeof *(array); \ keep && count != size; \ keep = !keep, count++) \ for(item = (array)+count; keep; keep = !keep) int main() { int a[] = { 1, 2, 3 }; int sum = 0; foreach(int