Electronics > Microcontrollers
C handling strings \n cubeide
brucehoult:
--- Quote from: strawberry on May 27, 2023, 12:09:42 pm ---for some reason scanning string doesn't return true
if (char == '\n')
--- End quote ---
It would help if you showed a complete but minimal program.
Compilers, as a rule, are not buggy, so with 99.99% probability what you are doing is not what you think you are doing.
As a stupid example:
--- Code: ---void foo(char char)
{
if (char == '\n');
{
printf("char is a newline\n");
}
}
--- End code ---
hans:
I would be cautious in mixing unicode with char in regular C code. Normally a string is a sequence of 'char' type, but in e.g. UTF8 encoding, one character can be 1 or more bytes long. Where only if a byte of a string is lower than 0x80, it means its a char from the standard ASCII set: https://en.wikipedia.org/wiki/UTF-8#Encoding
E.g.: https://godbolt.org/z/671PKrzGe
But, there are many other types of unicode encoding sets. It depends on your text editor how the unicode characters are saved in the file.
Also don't confuse char and strings. And maybe post a bit more because at present the question statement is a riddle.
DavidAlfa:
Better you start posting proper code instead few lose fragments!
Doctorandus_P:
--- Quote from: strawberry on May 27, 2023, 10:04:45 am ---\n doent work because \ is escape char
--- End quote ---
It would be nice if you added your intention or comprehension of what it is you suppose or want it to do.
DavidAlfa:
I very much doubt it's a compiler / IDE problem.
You might have run into thinking it's ASCII, so 1 byte for each character, 4 bytes including termination.
But that unicode string is written as { 0xE2, 0x97, 0x99, 0x20, 0x0A, 0x00}, so 6 bytes, char[4] is the newline.
Anyways there's no reason it wouldn't detect '\n'.
This works as expected, finding it at char[4];
--- Code: ---char msg[] = "◙ \n";
uint8_t find_newline(char * s){
uint8_t c=0;
while(*s){
if(*s++ == '\n')
break;
c++;
}
return c;
}
--- End code ---
Navigation
[0] Message Index
[*] Previous page
Go to full version