@DVoropaev
Ставлю + к карме на хабре за ответы на вопросы

Почему в данном случае не работает #define?

#include <stdio.h>
#define NAME Vitalik

int main(void){
	printf("HELLO, NAME");
	return 0;
}


#define разве не везде делает замену?
  • Вопрос задан
  • 408 просмотров
Пригласить эксперта
Ответы на вопрос 1
#define разве не везде делает замену?
Внутри строк не делает.

Вот так будет работать:
#define NAME "Vitalik"
...
printf("HELLO, "NAME);

Это превратится в printf("HELLO, ""Vitalik"); и компилятор автоматически объединит эти строки в одну.

There is no way to combine an argument with surrounding text and stringize it all together. Instead, you can write a series of adjacent string constants and stringized arguments. The preprocessor replaces the stringized arguments with string constants. The C compiler then combines all the adjacent string constants into one long string.
https://gcc.gnu.org/onlinedocs/cpp/Stringizing.htm...
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы