Search for variable name in C

I was asked a question in C yesterday, and I did not know the answer, since I had not used C since college, so I thought that maybe I could find the answer here, and not just forget about it.

If a person has a definition, for example:

#define count 1

Can this person find the variable name countusing 1, which is inside it?

I didn’t think so, since I thought that the count would point to 1, but I don’t see how 1 could indicate a count.

+3
source share
18 answers

: , . # , , , . , , - "1" - .

+7

@Cade Roux, #define , :

#define COUNT (1)
...
int myVar = COUNT;
...

:

...
int myVar = (1);
...

, , "" .

+8

"count" 1, ?

+6

, - , , , # define'd , pre-processor, "count", "1".

, , C - , , Java #. , , /, .

. ( )

+5

, .

#define , count 1. , count, .

, , . .

+3

, C, # .

#define displayInt(val) printf("%s: %d\n",#val,val)
#define displayFloat(val) printf("%s: %d\n",#val,val)
#define displayString(val) printf("%s: %s\n",#val,val)

int main(){
  int foo=123;
  float bar=456.789;
  char thud[]="this is a string";

  displayInt(foo);
  displayFloat(bar);
  displayString(thud);

  return 0;
}

:

foo: 123
bar: 456.789
thud: this is a string
+2

#define count 1 - , count.

:

void copyString(char* dst, const char* src, size_t count) {
   ...
}

count 1, :

void copyString(char* dst, const char* src, size_t 1) {
   ...
}
+2

C , . C , , , , . C .

, "", "1". /find, , .

-, , C .

+1

count . . , .

, , :

#define SHOW(sym) (printf(#sym " = %d\n", sym))
#define count 1

SHOW(count); // prints "count = 1"

# .

+1

""?

#define count 1

"count", 1.

( ) 1 , :

if (x > count) ...

:

if (x > 1) ...

, , " " .

0

#define , ""

0

, , , . , "count" 1.

, , , , ? . , .

int count = 1;
int count2 = 1;

, ?

0

, .

-, #define , .

, "count" .

C- . . , Java #, C , .

0

, "#", , "" . , , count, , "count" "1".

, , "".

0

. , , , , "1" "count" .

, - .

0

C ( ), -

int i = count;

,

#define count 1

, , , , iDontKnowWhat, , contans 1, , 'count'.

? #define , ( ). , , "", , , - . , , "count" "1".

0

, /. #defines . -E , , , #directives.

, , . (count == 1) (1 == 1).

, , , , .

0

, ( ?), , #define enums. :

#define ZERO 0
#define ONE 1
#define TWO 2

enum {
  ZERO,
  ONE,
  TWO
};

:

x = TWO;

#defines, TWO, 2.

0

Source: https://habr.com/ru/post/1697460/


All Articles