- 1 year ago
Reading/Printing special characters in C
In C, special characters can be printed or read using escape sequences. Escape sequences are a combination of a backslash () followed by a character that represents the special character. Here are some common escape sequences for special characters:
- \n: newline
- \t: horizontal tab
- \r: carriage return
- ": double quote
- ': single quote
For example, to print a newline character, you can use the following code:
arduinoprintf("Hello\nworld!");
This will output:
Hello
world!
To read a special character from user input, you can use the scanf function with the appropriate format specifier. For example, to read a newline character, you can use the following code:
arduinochar c;
scanf("%c", &c);
if (c == '\n') {
printf("You entered a newline character\n");
}
Note that when reading a character using scanf, you need to include a space before the format specifier to skip any whitespace characters in the input buffer. This is because scanf reads the next character, which could be a whitespace character like a newline or a space.
arduinoscanf(" %c", &c); // include space before %c to skip whitespace characters
Escape sequences can also be used in strings. For example:
rustchar* str = "Hello\tworld!\n";
printf("%s", str);
This will output:
Hello world!