-1

I was trying to create a really simple program to calculate the gravitational force between two objects in C++. So I wanted to declare a macro for G whose value is equal to 6.754*10^-11 and I used this:

#define G 6.754e-11.0;

But it generated a lot of stray errors as shown below:

cd '~/Desktop/CS/C++/Other PSETS'
g++ gravity.cpp -o gravity

Output:

gravity.cpp:5:28: error: stray ‘\342’ in program
 #define G (6.754)*pow(10.0,��11.0);
                            ^
gravity.cpp:10:52: note: in expansion of macro ‘G’
     float gforce(float m1,float m2,float r){return G*m1*m2/(r*r);}
                                                    ^
gravity.cpp:5:29: error: stray ‘\210’ in program
 #define G (6.754)*pow(10.0,��11.0);
                             ^
gravity.cpp:10:52: note: in expansion of macro ‘G’
     float gforce(float m1,float m2,float r){return G*m1*m2/(r*r);}
                                                    ^
gravity.cpp:5:30: error: stray ‘\222’ in program
 #define G (6.754)*pow(10.0,�11.0);
                              ^
gravity.cpp:10:52: note: in expansion of macro ‘G’
     float gforce(float m1,float m2,float r){return G*m1*m2/(r*r);}

So, I decided to use the math library function cmath for using pow(), but still it wasn't of any use. What can I do to create such a macro?

Note: I am Using G++ on Ubuntu as shown below:

g++ --version

Output:

g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aal Ver
  • 21
  • 2
  • 3
    remove the `;` at the end of the `#define...` line – Stephan Lechner Jan 17 '18 at 19:49
  • Also don't use macros. – nwp Jan 17 '18 at 19:50
  • I highly advise not to use 1-letter names such as `G`. – PaulMcKenzie Jan 17 '18 at 19:50
  • 3
    *`stray ‘\342’ in program`* did you do copy&paste from a website? Sometimes characters that look the same but are different are used on websites, mostly `”` is used instead of `"` – Pablo Jan 17 '18 at 19:50
  • 3
    Possible duplicate of [stray '\342' in C++ program](https://stackoverflow.com/questions/2340930/stray-342-in-c-program) – Pablo Jan 17 '18 at 19:51
  • @PaulMcKenzie It's a physical constant and it's `G`, so in this case it is a very good idea to do that. – Iharob Al Asimi Jan 17 '18 at 19:56
  • @nwp, what? why not? – Iharob Al Asimi Jan 17 '18 at 19:56
  • Apart from the `;` that is not the answer. `6.754e-11` works fine, but `6.754e-11.0` does not, trying to find why. – Weather Vane Jan 17 '18 at 19:56
  • @WeatherVane Well, I don't think that you can express numeric literals with a floating point exponent. – Iharob Al Asimi Jan 17 '18 at 19:57
  • @IharobAlAsimi thanks, was just arriving at that conclusion. – Weather Vane Jan 17 '18 at 19:57
  • @IharobAlAsimi Because they make the program harder to read, write and optimize. Use a `constexpr double` instead. – nwp Jan 17 '18 at 19:58
  • @IharobAlAsimi because if macros are use improperly, you can have very well hidden bugs. Consider `#define abs(x) ((x) > 0 ? (x) : (-x))`. This looks harmless, but `int i = -3; abs(--x);` would lead to undefined behaviour. – Pablo Jan 17 '18 at 19:59
  • @Pablo Following that reasoning then if knifes are used improperly, you can have murdering and injuries. – Iharob Al Asimi Jan 17 '18 at 20:02
  • "The stray `\342`" is a red herring. That line copy/pasted into a program compiles when the `.0;` is removed. Nothing to do with any minus sign encoding. – Weather Vane Jan 17 '18 at 20:03
  • @nwp please note that this is c++ specific. Macros are not, and you can suggest that being more specific like "*in c++ it's not recommended to use macros*". Macros as any language feature when used correctly can be very useful, writing bad macros like the example by @[Pablo](https://stackoverflow.com/questions/48308886/what-is-the-proper-way-of-declaring-a-floating-point-number-in-exponential-form#comment83603616_48308886) is possible too, but that's no reason to simple BAN macros. – Iharob Al Asimi Jan 17 '18 at 20:04
  • @IharobAlAsimi if you **really** know how to use macros and understand the ups and downs of using them, then sure, use them. And yes, if you use knifes improperly, you **can** have murdering and injuries. So if you don't know how to use a knife properly, don't use it. – Pablo Jan 17 '18 at 20:05
  • 1
    @WeatherVane The U+2212, the `.0` in the exponent, and the semicolon are _all_ problems, of equal significance to the compiler (that is, they all provoke syntax errors). – zwol Jan 17 '18 at 20:05
  • @zwol I see now the C++ tag, I am using a C compiler. – Weather Vane Jan 17 '18 at 20:05
  • @IharobAlAsimi I'm not from banning macros, I only gave you an example of a common pitfall using macros and why you should avoid them, unless you really know what you are doing. – Pablo Jan 17 '18 at 20:06
  • @WeatherVane C and C++ should both reject `#define G 6.754e−11` (provided that the macro is actually expanded as part of an arithmetic expression). I think it's more likely that somewhere along the copy-and-paste chain from OP's code to your test program the Unicode character got degraded to ASCII. – zwol Jan 17 '18 at 20:09
  • @zwol I guess so (about the stray) - as OP's compiler says. – Weather Vane Jan 17 '18 at 20:13
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 27 '23 at 14:56
  • Prefer proper (`const`) variables over macros when possible. – Jesper Juhl Apr 27 '23 at 15:11
  • There are really two questions here: 1) The stray errors 2) The macro question. They ought to be two separate questions (posts). – Peter Mortensen Apr 27 '23 at 15:30

1 Answers1

7

The error message

stray ‘\342’ in program

means your program contains a character which is not allowed by the syntax of C++ (technically, it is outside the "basic source character set"). The three-digit number is the octal encoding of the numeric value of the problem byte. You got three of these in a row, so the offending byte sequence is 0342 0210 0222, which is the UTF-8 encoding of the Unicode character U+2212 MINUS SIGN.

That's right. You're not allowed to use minus signs in C++. You must use U+002D HYPHEN-MINUSes instead.

To fix that part of the problem, in your editor go to the line

#define G 6.754e-11.0;

backspace over the minus sign and retype it. This probably happened because you copied and pasted the number from a web page or PDF that uses fancy (by which I mean "not just plain ASCII") typography.

You will still need to fix a couple other problems. That semicolon shouldn't be there at all, because #defines end at the end of the line. And the exponent of a floating-point literal in C++ must be an integer. So what you really should have is

#define G 6.754e-11

It is also arguably better to use a const variable for this, as you do not need a macro:

const double G = 6.754e-11;

(note that the semicolon has now returned) ... but I am old-school enough to not object to the use of macros for numeric constants.

There is absolutely no reason to use pow here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zwol
  • 135,547
  • 38
  • 252
  • 361
  • A more direct way is searching in a modern text editor or IDE using the regular expression `\x{2212}` (or `\x{2212}`, for example, in [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code)). This also works where the visuals are not enough, e.g. U+00A0 ([NO-BREAK SPACE](https://www.charset.org/utf-8)) and U+200B ([ZERO WIDTH SPACE](https://www.charset.org/utf-8/9)). – Peter Mortensen Apr 27 '23 at 15:35