Is (TRUE) a good idea in C?

In the C programming language, I understand that variables can only be defined at the beginning of a block of code, and a variable will have the area in which it was declared. With that in mind, I was wondering if it is considered bad practice to artificially create a new scope, as in this example:

void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ... Do some more stuff ... } ... Do even more stuff ... } 

Assuming TRUE is set to 1 in the macro definition, will this code be considered "good code" or will it make experienced programmers cringe at the very thought?

Thanks for your input!

EDIT: In response to some of the answers, the code I'm working with should work with some fairly old legacy systems. Although it would be nice to work on the C99 assumption, we really cannot guarantee that they will have it.

+14
c if-statement
Feb 12 '09 at 15:12
source share
17 answers

You don't even need an if statement. You can create blocks with {}

Perhaps this should be a separate function.

An example is here:

 #include <stdio.h> int main(int argc, char **argv) { int i = 0; { int i = 10; printf("%d\n", i); } printf("%d\n", i); } 
+49
Feb 12 '09 at 15:15
source share

As far as I know, you can create an area without if.

Use only curly braces:

 { int x; } 

And I recommend against

 if (TRUE) 

as this makes it difficult to read.

+16
Feb 12 '09 at 15:15
source share

You probably want to create a new feature for this area.
If it really should have its own scope, this is probably a separate logical function.

+7
Feb 12 '09 at 15:13
source share

Since you can simply make a block of visibility without if, this is a better idea.

 void foo() { ... Do some stuff ... { char a; int b; ... Do some more stuff ... } ... Do even more stuff ... } 
+6
Feb 12 '09 at 15:15
source share

Note that in C99 it is allowed to declare local variables in the middle of blocks.

C99 - Version C of 1999; most modern C compilers support it.

+6
Feb 12 '09 at 15:21
source share

First of all, the new block should not be an if block. It could just be a section of code surrounded by braces, for example:

 void foo() { ... Do some stuff ... { char a; int b; ... Do some more stuff ... } ... Do even more stuff ... } 

Secondly, in any modern C compiler that conforms to the C standard (I think C99), you can declare variables anywhere in the block, so you don’t need to create a new block at all.

+5
Feb 12 '09 at 15:17
source share

You can delete

 if(TRUE) 

and just leave the braces that define the new syntax block themselves - the compound statement .

This is definitely cleaner than false if you had done it before, but you might still be asking yourself why you want to create a new block - would it be better to define a subroutine?

+4
Feb 12 '09 at 15:15
source share

Since so many answers have already been said, you don't need the if things. Just create an empty block. But I want to get to another moment. In C, you can create variable declarations anywhere in a block, not just at the beginning. In C89, you had this limitation. Starting with C99 (it's already 10 years), you no longer have such restrictions, although some compilers will moan anyway. GCC will not, however, if you tell it to use the most “latest” C standard with the option -std = c99.

Since there are still compilers that moan by default, I would not prefer to mix declarations and code. I would continue to place ads at the beginning of the blocks for compatibility reasons.

+4
Feb 12 '09 at 15:20
source share

I would not call myself seasoned, but I kind of grunt.

My problem is that the if statement can make someone believe that something is really being evaluated ... but at run time the macro is either true or false, there are no changes in it like something else. You must either include the code or not.

If you want to do something like #ifdef DEBUG, then you must do this to tell the reader that this is debugging code ...

+3
Feb 12 '09 at 15:14
source share

Leaving the door open for creative people:

 #define TRUE 0 #define FALSE 1 

Just use braces to declare the area.

+3
Feb 12 '09 at 15:18
source share

I do not think you need the if (true) part.

Changing variables requires only {}.

+2
Feb 12 '09 at 15:15
source share

My answer is this:

This makes experienced programmers cringe at the very thought.

+2
Feb 12 '09 at 15:15
source share

Just define your variables at the beginning of the block or use another function. Adding an artificial region with empty {} s or any of the alternatives is not good practice.

+2
Feb 13 '09 at 0:23
source share

C99 allows you to declare variables almost anywhere. However, refrain from doing so without a good reason. Try first splitting your function into smaller (possibly built-in) functions.

The only place this might make sense is when you have a variable that is initialized in the middle of your function, for example. similar to creating an object in C ++.

+1
Feb 12 '09 at 15:19
source share

I think you are working with some outdated assumptions. I coded directly C using GCC for several months, and you do not need to declare variables at the beginning of the block, although the second version of K & R says what you need. You can declare your variable anywhere, for example, this not very useful example:

 char* palstring; palstring = malloc(LARGEST_STRING); memset(palstring, 0, sizeof palstring); fgets(palstring, LARGEST_STRING, fin); char* cur = palstring; char letter; letter = *cur; 

Therefore, you do not need to do what you offer. The tongue has moved.

Another nice addition to the C language is Variable Length Arrays, which allows you to pass an array of a function along with its size. In the old days, all you could do was pass a pointer.

+1
Feb 12 '09 at 15:20
source share

Assuming that you are using the old compiler (for example, I am doing this for the old hardware), you skip if (TRUE), as others did, and that you have a really BIG function (you shouldn't have it in the first place), then I think this is normal. I did it, but I didn’t feel well ...

+1
Feb 12 '09 at 15:37
source share

I seem to be in the minority, but I find that “just brackets” is much harder to read because it differs from the regular pattern. In the (admittedly rare) cases where I want to have a scope without defining another function, I prefer to include an “if”, but without any macros and with a comment to explain why:

 if( 1 ) // just to establish scope { // do stuff here } 
+1
Feb 12 '09 at 16:26
source share



All Articles