-8

I am wondering if the following are the same:

#define IDEFAULT (-1)

and

#define IDEFAULT (int) -1
imageguy
  • 1
  • 1
  • 1
    I'm voting to close this question as off-topic because I'm not seeing a lot of value in questions comparing and contrasting the limitless combinations of possible syntax errors. – erik258 Nov 15 '19 at 22:24
  • What is the purpose of this? Are you learning by trial and error? – machine_1 Nov 15 '19 at 22:26
  • ooops i forgot the # at the beginning! Please re-open. – imageguy Nov 15 '19 at 22:28
  • Hi @imageguy and welcome to the site! :) Even if you prepend `#`, and/or append `;`, this will still lead to a syntax error. Try it out online in [Wandbox](https://wandbox.org/permlink/rfGxn6OM16Bs2fhn), if you like. :) – gsamaras Nov 15 '19 at 22:30
  • edited the second time, missed the variable name – imageguy Nov 15 '19 at 22:33
  • 1
    Yes, both are the same. `-1` has type of `int` anyway. However there might be some tricky usages. For example if you have a function named `foo`, then `foo IDEFAULT;` will result in a legal call `foo(-1);`, in the first case, but illegal `foo (int) -1;` thingy in the second case. – Eugene Sh. Nov 15 '19 at 22:34
  • The `(-1)` form is conventional. `(int)-1` is... strange. Under most circumstances they'll behave identically. It's probably possible to contrive an obscure circumstance under which the second one would do something unexpected. I'd stick with the `(-1)` form. – Steve Summit Nov 15 '19 at 22:37
  • 4
    I'm voting to close this question as off-topic because it's unlikely to be useful to future readers. – S.S. Anne Nov 16 '19 at 22:04
  • 1
    These two are the same. They define a macro called `IDEFAULT` with the value `-1`. If you had done instead: `#define IDEFAULT -1` then it would've been slightly different; if someone had written: `5 IDEFAULT` with the above definition, it would be `4`. With your two definitions, it would be a syntax error (which in this case would be good). – S.S. Anne Nov 16 '19 at 22:09

1 Answers1

5

I am wondering if the following are the same :

Both are the same, in the sense that they produce a syntax error.

It seems that you are trying to use a Macro in C, and for that the correct syntax is:

#define macro-name char-sequence

so I would write for example:

#define myMacro -1

where myMacro is going to be equal to -1. If I surround -1 with parentheses, it won't make a difference. Read more about it in Why use Macros in C?


PS: The syntax errors however, are not identical:

main.c:3:9: error: expected declaration specifiers or '...' before '-' token
    3 | define (-1)
      |         ^

and

main.c:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
    3 | define (int) -1
      | ^~~~~~
main.c: In function 'define':
main.c:3:14: error: expected declaration specifiers before '-' token
    3 | define (int) -1
      |              ^
main.c:3:9: error: parameter name omitted
    3 | define (int) -1
      |         ^~~
main.c:8: error: expected '{' at end of input
    8 | }
      | 
gsamaras
  • 71,951
  • 46
  • 188
  • 305