define

Auto defines in C editors... Why?

When I let Eclipse create a new file (.c or .h file) in a C project the editor always auto creates a #define at the top of the file like this: If the file is named 'myCFile.c' there will be a #define at the start of the file like this #ifndef MYCFILE_C_ #define MYCFILE_C_ I have seen other editors do this as well (Codewright and Slik...

Can this macro be converted to a function?

While refactoring code and ridding myself of all those #defines that we're now taught to hate, I came across this beauty used to calculate the number of elements in a structure: #define STRUCTSIZE(s) (sizeof(s) / sizeof(*s)) Very useful as it is but can it be converted into an inline function or template? OK, ARRAYSIZE would be a bet...

Should I use #define, enum or const?

In a C++ project I'm working on I have a flag kind of value which can have 4 values. Those 4 flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each Record I wish to keep this attribute, so I could use enum: enum { xNew, xDeleted, xModified, xE...

Finding the name of a variable in C

I was asked a question in C last night and I did not know the answer since I have not used C much since college so I thought maybe I could find the answer here instead of just forgetting about it. If a person has a define such as: "#define count 1" Can that person find the variable name "count" using the 1 that is inside it? I did no...

#ifdef vs #if - which is better/safer?

This may be a matter of style, but there's a bit of a divide in our dev team and I wondered if anyone else had any ideas on the matter... Basically, we have some debug print statements which we turn off during normal development. Personally I prefer to do the following: \\---- SomeSourceFile.cpp ---- #define DEBUG_ENABLED (0) ... So...

How efficient is define in PHP?

C++ preprocessor #define is totally different. Is the PHP define() any different than just creating a var? define("SETTING", 0); $something = SETTING; vs $setting = 0; $something = $setting; ...

Fortran Array to C array. Stupid macro tricks wanted

I have this 'simplified' fortran code real B(100, 200) real A(100,200) ... initialize B array code. do I = 1, 100 do J = 1, 200 A(J,I) = B(J,I) end do end do One of the programming gurus warned me, that fortran accesses data efficiently in column order, while c accesses data efficiently in row order. He suggested that I t...

#include header guard format?

I know it makes little difference to a project but, assuming you use #defined header guards for your C++ code, what format do you use? e.g. assuming a header called foo.hpp: #ifndef __FOO_HPP__ ... #ifndef INCLUDED_FOO_HPP ... #ifndef SOME_OTHER_FORMAT I'm sold on the idea of upper-case #defines but cannot settle on a format for th...

I Want To Optimally Check Defined Constants in PHP

In PHP, depending on your error reporting level, if you don't define a constant and then call it like so: <?= MESSAGE ?> It may print the name of the constant instead of the value! So, I wrote the following function to get around this problem, but I wanted to know if you know a way to do it in faster code? I mean, when I did a speed ...

How do I replace this preprocessor macro with a #include?

UPDATE: Obviously, you'd want to do this using templates or a base class rather than macros. Unfortunately for various reasons I can't use templates, or a base class. At the moment I am using a macro to define a bunch of fields and methods on various classes, like this: class Example { // Use FIELDS_AND_METHODS macro to define some...

Defining const values in C

I have a C project where all code is organized in *.c/*.h file pairs, and I need to define a constant value in one file, which will be however also be used in other files. How should I declare and define this value? Should it be as static const ... in the *.h file? As extern const ... in the *.h file and defined in the *.c file? In what...

Is this possible use ellipsis in macro? Can it be converted to template?

Having implemented CLogClass to make decent logging I also defined macro, but it works only with one parameter... class CLogClass { public: static void DoLog(LPCTSTR sMessage, ...); }; #define DebugLog(sMessage, x) ClogClass::DoLog(__FILE__, __LINE__, sMessage, x) Well, it fails when called with more than 2 parameters :( ... I...

error: macro names must be identifiers using #ifdef 0

Hello, I have the source code of an application written in C++ and I just want to comment something using: #ifdef 0 ... #endif And I get this error error: macro names must be identifiers Why is this happening this?. Thanks ...

Simple Rails Q: Where is this variable defined?

I'm following this tutorial (seems good) for Rails. After I run ruby script/generate scaffold Post then this link works in one of the erb files: <%= link_to "My Blog", posts_path %> WHY? I've looked for "posts_path" in the whole app and it's nowhere to be found. On the other hand, this <%= link_to "My Blog", home_path %> does no...

C #define macros

Hi. Here is what i have and I wonder how this works and what it actually does. #define NUM 5 #define FTIMES(x)(x*5) int main(void) { int j = 1; printf("%d %d\n", FTIMES(j+5), FTIMES((j+5))); } It produces two integers: 26 and 30. But i can't seem to understand how it makes this possible.. Thanks. ...

CUDA compiler (nvcc) macro

Is there a #define compiler (nvcc) macro of CUDA which I can use? (Like _WIN32 for Windows and so on.) I need this for header code that will be common between nvcc and VC++ compilers. I know I can go ahead and define my own and pass it as an argument to the nvcc compiler (-D), but it would be great if there is one already defined. ...

Are "#define new DEBUG_NEW" and "#undef THIS_FILE" etc. actually necessary?

When you create a new MFC application, the wizard creates the following block of code in almost every CPP file: #ifdef _DEBUG #define new DEBUG_NEW #endif and sometimes it also adds this: #undef THIS_FILE static char THIS_FILE[] = __FILE__; I would like to remove this code from my CPP files if it is redundant. I am using an MFC app...

.NET - Target platform/processor at compile time

Is there a #define in C# that allows me to know, at compile time, if I'm compiling for x86 (Win32) or x64 (Win64)? ...

Automatically replacing variables with #defines

Hi guys, I've got a tricky problem that I need some help with. Currently, I have a file with about 100 #defines in it, from 1-100, and each with a unique string value. Now I'm trying to print this value, but instead of the value, I want to print what the #define is. For example: #define FIRST_VALUE 1 var = FIRST_VALUE; printf("%s", va...

Use #ifdefs and #define to optionally turn a function call into a comment

Is it possible to do something like this #ifdef SOMETHING #define foo // #else #define foo MyFunction #endif The idea is that if SOMETHING is defined, then calls to foo(...) become comments (or something that doesn't get evaluated or compiled), otherwise it becomes a call to MyFunction. I've seen __noop used, but I don't believe I ca...