Electronics > Microcontrollers

C handling strings \n cubeide

<< < (2/2)


--- 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 ---

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.

Better you start posting proper code instead few lose fragments!


--- 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.

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;
    if(*s++ == '\n')
  return c;

--- End code ---


[0] Message Index

[*] Previous page

There was an error while thanking
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod