Reputation: 340
I recently saw this snippet of code:
static HAL_StatusTypeDef Program_Flash_Page(uint32_t const page_address, \
void* const pBufferData, uint32_t const nBufferSize) {
uint32_t Address = page_address;
uint64_t *DATA_64 = (uint64_t *) pBufferData;
while (Address < (page_address + nBufferSize)) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, *DATA_64) == \
HAL_OK) {
Address = Address + 8;
DATA_64++;
}
else {
return HAL_ERROR;
}
}
return HAL_OK;
}
As you can see, backslashes were used to present a list of arguments and a logical condition in multiple lines.
As I learned from Kernighan and Ritchie (2nd edition, paragraph A.12.2, p.207), "lines that end with the backslash \ character are folded by deleting the backslash and the following newline character" during preprocessing.
What remained unclear to me is that if this syntax is obligatory or if one can just use a new line character (hit the enter button) while coding.
Upvotes: 2
Views: 2907
Reputation: 552
The slashes are optional except for defining multi line macros as Gerhardh mentioned.
#include <stdio.h>
//valid macro
#define BAR(x, y, z) printf("%d %c %f\n", \
x, \
y, \
z);
//invalid macro, this won't compile if uncommented
/*
#define BAR(x, y, z) printf("%d %c %f\n",
x,
y,
z);
*/
void foo(int x, char y, float z) {
printf("%d %c %f\n", x, y, z);
}
int main() {
//valid
foo(5, \
'c', \
0.0f);
//also valid
foo(5,
'c',
0.0f);
//even use of the macro without slashes is valid
BAR(5,
'c',
0.0f);
return 0;
}
Upvotes: 4