Defined int variable with numbesr in parentheses

What does numbers in parentheses mean when for int variable?
"const int host = (192,168,1,10);"
In this case it is IP number, but I have tried also:
"const int a=(312,545,757,1212,10,15,25);" and compilier is OK with that.
when Serial.println(a) is used, the last number is presented.
Is it array, or what exactly such a definition/declaration mean?

No, but the programmer might have intended it to be and got it wrong

It means whoever created the code probably does not know what they are doing

That is exactly what I would expect

Google "C comma operator" for an explanation or see Comma operator - Wikipedia

2 Likes

In the "Preferences" turn on the verbose output for the compilation.
The compiler gives a warning: "warning: left operand of comma operator has no effect [-Wunused-value]".

braces, "{", "}", ar commonly used to initialize arrays and structs.

more likely, the following was intended

byte host [] = { 192, 168, 1, 10 };

amazing. looks like this is an error in C but not C++

hum... the difference I know of is that the comma operator may be an lvalue in C++, but never in C

I don't know why const int host = (192,168,1,10); would be an error in C if you have the parenthesis. if you don't have the parenthesis, the comma is considered as the separator for the next declaration and then it's an error.

main.c:3:18: error: initializer element is not constant
    3 | const int host = (192, 168, 1, 10);
      |                  ^
make: *** [<builtin>: main] Error 1

don't understand why this makes sense in c++. how can the thing inside the parenthesis be considered a (const) int

Working left to right:
192, 168 = 168 (a constant)
168, 1 = 1 (a constant)
1, 10 = 10 (a constant)

Post the source of the code so that we may be able to see context.

Perhaps the code came from,

I wrote this

#include <stdio.h>

int main ( int argc , char * argv[] ) {
  const int host = (192, 168, 1, 10);
  printf("%d\n", host);
  return 0 ;
}

and compiled with
cc main.c -o main

I only got 3 warnings

main.c:4:21: warning: expression result unused [-Wunused-value]
  const int host = (192, 168, 1, 10);
                    ^~~
main.c:4:26: warning: expression result unused [-Wunused-value]
  const int host = (192, 168, 1, 10);
                         ^~~
main.c:4:31: warning: expression result unused [-Wunused-value]
  const int host = (192, 168, 1, 10);
                              ^
3 warnings generated.

which is fair since I'm not using those

running main does print 10 as expected.

What if the declaration const int host = (192,168,1,10); was for a global variable? Does "C" allow using such a statement outside of a function?

that's what i had done

obviously the values are constants, but i didn't think a comma separated list inside parenthesis is an "expression".

while this may be legitimate, how could such a statement be used? and if it is, is it good practice?

It's not a comma-separated list. It several successive applications of the Comma Operator (which has been in the 'C' language since forever).

11 posts later we are back at post #2

but the thing between the commas doesn't make sense

What "thing"?

It compiles for an Uno with only the warnings that @J-M-L mentioned:

void setup() {
  Serial.begin(115200);
  delay(1000);
  const int host = (192, 168, 1, 10);
  Serial.println(host);
}

void loop() {
}

It also prints the answer I stated:

10

So what doesn't "make sense"?

BTW, the result is identical using a global declaration:

const int host = (192, 168, 1, 10);

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println(host);
}

void loop() {
}

So, that also "makes sense" ... at least for C++. But perhaps (as I opined) it doesn't work as a global in 'C'.

@Tibor79 Do you understand the replies ? The code is wrong and we are discussing the comma operator.

The sketch below is "valid" code. It is harder to read, and sometimes it does not make sense. Good code shows what it is doing, so this is bad code:

void setup() 
{
  Serial.begin(115200);

  int a = (1,2,3,4,5);              // a=5
  int b,c,d;
  b = (c=100,d=-100,10);            // b=10, c=100, d=-100
  int f;
  int e = (f = b > 10 ? 1 : 2, 3);  // e=3, f=2

  Serial.print( "a "); Serial.println( a);
  Serial.print( "b "); Serial.println( b);
  Serial.print( "c "); Serial.println( c);
  Serial.print( "d "); Serial.println( d);
  Serial.print( "e "); Serial.println( e);
  Serial.print( "f "); Serial.println( f);
}

void loop() {}

GCC and Clang behave differently:

moo.c

const int host = (192,168,1,10);
void foo(void)
{
    const int foo_host = (192,168,1,10);
}
> gcc -fsyntax-only moo.c   # version 11.3.0
moo.c:1:18: error: initializer element is not constant
    1 | const int host = (192,168,1,10);
      |                  ^
> clang -fsyntax-only moo.c    # version 8.0.1
moo.c:1:19: warning: expression result unused [-Wunused-value]
const int host = (192,168,1,10);
                  ^~~
moo.c:1:23: warning: expression result unused [-Wunused-value]
const int host = (192,168,1,10);
                      ^~~
moo.c:1:27: warning: expression result unused [-Wunused-value]
const int host = (192,168,1,10);
                          ^
moo.c:4:27: warning: expression result unused [-Wunused-value]
    const int foo_host = (192,168,1,10);
                          ^~~
moo.c:4:31: warning: expression result unused [-Wunused-value]
    const int foo_host = (192,168,1,10);
                              ^~~
moo.c:4:35: warning: expression result unused [-Wunused-value]
    const int foo_host = (192,168,1,10);
                                  ^
6 warnings generated.

guess it depends on the platform or version

on my Mac with -fsyntax I only get the 3 warnings on this code

#include <stdio.h>

const int host = (192, 168, 1, 10);

int main ( int argc , char * argv[] ) {
  printf("%d\n", host);
  return 0 ;
}

I don't know why the compiler would not agree that 10 is a constant.

It might make sense as:
const int host = IPAddress(192,168,1,10);

That would make the (192,168,1,10) an Argument List for the constructor function of the IPAddress class. If the IPAddress class then has a conversion to 'int' it would be possible to store the value in 'host'. That might work for an ESP32 or ESP8266 where an 'int' is 32 bits but not on an UNO/Nano/Mega where 'int is only 16 bits.

1 Like