Author Topic: C handling strings \n cubeide  (Read 1312 times)

0 Members and 1 Guest are viewing this topic.

Offline strawberryTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1199
  • Country: lv
C handling strings \n cubeide
« on: May 27, 2023, 10:04:45 am »
"◙ \n"
1:0x0A ASCII
2:\n doent work because \ is escape char
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6044
  • Country: es
Re: C handling strings \n cubeide
« Reply #1 on: May 27, 2023, 11:30:09 am »
What?
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4118
  • Country: nz
Re: C handling strings \n cubeide
« Reply #2 on: May 27, 2023, 11:54:43 am »
The escape character is there exactly so that you get 0xA instead of the normal 'n' = 0x6E.
 
The following users thanked this post: boB, mikerj

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6044
  • Country: es
Re: C handling strings \n cubeide
« Reply #3 on: May 27, 2023, 12:06:50 pm »
If you really want to show "◙ \n", then use "◙ \\n" , special characters need to be escaped.
So \\ will print \. A single \ will escape the next char, but you already knew it!

https://www.w3schools.com/c/c_strings_esc.php
https://www.geeksforgeeks.org/escape-sequence-in-c/
« Last Edit: May 27, 2023, 12:08:34 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline strawberryTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1199
  • Country: lv
Re: C handling strings \n cubeide
« Reply #4 on: May 27, 2023, 12:09:42 pm »
for some reason scanning string doesn't return true
if (char == '\n')
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4118
  • Country: nz
Re: C handling strings \n cubeide
« Reply #5 on: May 27, 2023, 12:41:21 pm »
for some reason scanning string doesn't return true
if (char == '\n')

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: [Select]
void foo(char char)
{
  if (char == '\n');
  {
     printf("char is a newline\n");
  }
}
« Last Edit: May 27, 2023, 12:45:57 pm by brucehoult »
 
The following users thanked this post: agehall

Offline hans

  • Super Contributor
  • ***
  • Posts: 1659
  • Country: nl
Re: C handling strings \n cubeide
« Reply #6 on: May 27, 2023, 01:00:48 pm »
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.
 
The following users thanked this post: MMMarco

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6044
  • Country: es
Re: C handling strings \n cubeide
« Reply #7 on: May 27, 2023, 02:59:29 pm »
Better you start posting proper code instead few lose fragments!
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3492
  • Country: nl
Re: C handling strings \n cubeide
« Reply #8 on: June 10, 2023, 04:06:45 pm »
\n doent work because \ is escape char

It would be nice if you added your intention or comprehension of what it is you suppose or want it to do.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6044
  • Country: es
Re: C handling strings \n cubeide
« Reply #9 on: June 10, 2023, 07:16:14 pm »
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: [Select]
char msg[] = "◙ \n";

uint8_t find_newline(char * s){
  uint8_t c=0;
  while(*s){
    if(*s++ == '\n')
      break;
    c++;
  }
  return c;
}
« Last Edit: June 10, 2023, 07:38:39 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf