C/Preprocesor: Różnice pomiędzy wersjami
Usunięta treść Dodana treść
Usunięcie zbędnego (powtórzonego) linku. |
→#define: program, opis |
||
Linia 87:
#define B ((2)+(A))
Unikniemy w ten sposób [[C/Powszechne_praktyki#Konwencje_pisania_makr|niespodzianek]] związanych z priorytetem operatorów :
<source lang=c>
/*
https://github.com/Keith-S-Thompson/42/blob/master/42.c
This small C program demonstrates the danger of improper use of the C preprocessor.
Keith-S-Thompson
*/
#include <stdio.h>
#define SIX 1+5
#define NINE 8+1
int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}</source>
Po skompilowaniu i uruchomieniu programu otrzymujemy :
6 * 9 = 42
a powinno być :
6 * 9 = 54
Przyczyną błędu jest interpretacja wyrażenia :
1+5*8+1
Ze względu na priorytet operatorów ( wyższy * niż + ) jest to interpretowane jako :
1+(5*8)+1
a nie jak :
(1+5)*(8+1)
===#undef===
|