-1

I have an integer variable (for example int var1;) which shouldn't be "18" in the source code. I mean if any programmer of my project wrote this:

var1 = 18;

The compiler (I use C99) makes it red, and refuses to compile it. I want something like #define, but reversely (a variable MUST NOT have a special value). Is it possible in C?

kmoser
  • 8,780
  • 3
  • 24
  • 40
Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21
  • 3
    Use a function to assign the value, and the function can check that the value isn't 18. – Barmar Dec 01 '20 at 06:33
  • 1
    If C11 is an option you can use `static_assert(var == 18);`, on C99 you can use plain `assert`, but it works at runtime. – David Ranieri Dec 01 '20 at 06:35
  • What if the programmer writes `var1 = rand()`? Would you expect the compiler to refuse it to compile too? – KamilCuk Dec 01 '20 at 07:20
  • @Barmar It's a good idea, but any of the programmers can use "=" to assign the value. – Mohammad Kholghi Dec 01 '20 at 07:31
  • This is code in your own program, how is some other programmer reassigning the value? Are you coding a library? – Barmar Dec 01 '20 at 07:32
  • @DavidRanieri Thanks, but then I have to debug and search for the line which var1 = 18; . I prefer to deny programmers, rather than debugging again. – Mohammad Kholghi Dec 01 '20 at 07:33
  • @KamilCuk No, I just want to deny them from using "=" for the var1 and 18. – Mohammad Kholghi Dec 01 '20 at 07:34
  • @Barmar It's useful for myself too. I may forget and assign 18 to var1. – Mohammad Kholghi Dec 01 '20 at 07:35
  • @Barmar The problem is not if the code is big or small, or about the number of the programmers. I want to know how can I do that in C. – Mohammad Kholghi Dec 01 '20 at 07:36
  • @MohammadKholghi _I prefer to deny programmers,_ `static_assert` does that, you will get a compile time error. – David Ranieri Dec 01 '20 at 07:37
  • 1
    @MohammadKholghi in the debugger you can monitor a variable and break if it's set to some special value. For example [How to monitor variables in GDB and log it if it meets certain conditions?](https://stackoverflow.com/q/3099537/995714), [Break when a value changes using the Visual Studio debugger](https://stackoverflow.com/q/160045/995714) – phuclv Dec 01 '20 at 07:46
  • @DavidRanieri Sorry for the late response. I can't find anything about static_assert. How does it work? Which C compiler should I use? Should I include a library? I get this: "[Linker error] undefined reference to `static_assert' " – Mohammad Kholghi Dec 15 '20 at 12:18
  • @MohammadKholghi https://en.cppreference.com/w/c/language/_Static_assert – KamilCuk Dec 15 '20 at 13:01
  • @MohammadKholghi https://godbolt.org/z/nz5v79 – David Ranieri Dec 15 '20 at 13:32

3 Answers3

2

Don't use an ordinary variable. Make var1 a macro that expands to a function call, then you can't assign to it. You can then require use of another function to assign to the hidden value, and it can check whether the value is 18.

#define var1 (get_var1())

static int hidden_var1;

int get_var1() {
    return hidden_var1;
}

int set_var1(int new_value) {
    if (new_value != 18) {
        hidden_var1 = new_value;
    }
    return hidden_var1;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

Check sources before compilation using text tool like sed. You can look for guilty litteral number assignement

var1 = 18;

using sed like this to check pb002.cpp file:

sed -n '/^[[:blank:]]*[[:alnum:]]\{1,25\}[[:blank:]]*=[[:blank:]]*[0-9][0-9]*[[:blank:]]*;/p' pb002.cpp
Florent
  • 436
  • 3
  • 8
  • Nice try, but easily defeated: `var1 = 0x12;` or jut spread `var1`, `=` and `18;` on 2 or more lines, not to mention embedded comments, or perverse use of the preprocessor: `#define var2 var1`, `var2 = 18;` – chqrlie Dec 01 '20 at 17:29
1

There is no "reverse #define" and there is no magic. This is C. You can:

  • Write a function and do the checking. I would see assert to write an assertion for the programmer:

void set_var1(int val) {
   assert(val != 18);
   var1 = val;
}
  • Write your own compiler or patch existing compiler with a plugin that will specifically do what you want - at compile time it will check if the value is not 18 and at runtime at assigning the variable additional code will be inserted to check if the value is not 18.
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Would you please answer this question? https://stackoverflow.com/questions/65085676/reverse-of-define-in-c/65087362#comment115453199_65085676. – Mohammad Kholghi Dec 15 '20 at 12:23