Home > other >  Which header file or definitions to use for using char8_t data type in C ?
Which header file or definitions to use for using char8_t data type in C ?

Time:11-03

So I want to use char8_t data type in my code. But my compiler shows me that it could not find an identifier like char8_t , which probably means it could not find required header file or definitions for it.

So can anyone tell me what header files / definitions should I use to get the char8_t data type? The language is C . I'll be thankful if you can also answer for C.

PS:<cwchar> did not work.

Edit:

My code:

#include <iostream>
#include <cwchar>
using namespace std;
int main()
{
char c='a';
wchar_t w=L'A';
char8_t c8=u8'c';
char16_t c16=u'D';
char32_t c32=U'K';

cout<<"Data Type"<<"\t"<<"Size"<<"\n"
<<"char"<<"\t \t"<<sizeof(c)<<" bytes"<<"\n"
<<"wchar_t"<<"\t \t"<<sizeof(w)<<" bytes"<<"\n"
<<"char8_t"<<"\t"<<sizeof(c8)<<"\n" 
<<"char16_t"<<"\t"<<sizeof(c16)<<" bytes"<<"\n"
<<"char32_t"<<"\t"<<sizeof(c32)<<" bytes"<<"\n";

return 0;
}

My compiler throws this error:

WideCharacters.cpp: In function 'int main()':
WideCharacters.cpp:8:5: error: 'char8_t' was not declared in this 
scope; did you mean 'wchar_t'?
8 |     char8_t c8=u8'c';
  |     ^~~~~~~
  |     wchar_t
WideCharacters.cpp:15:31: error: 'c8' was not declared in this 
scope; did you mean 'c'?
15 |     <<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
  |                               ^~
  |                               c

Edit-2:

I have my C standard set to C 20 in VS code so there is no problem with standard.

CodePudding user response:

char8_t is a keyword. It's built into the language, so you don't need any headers.

If it doesn't work, either your compiler doesn't support it, or you forgot to enable C 20 support (e.g. -std=c 20 in GCC).

CodePudding user response:

The problem here is possibly the compiler is not able to identify that this is a C 20 feature.

Compile using this: g -Wall -std=c 20 "yourFileName".cpp

replace youFileName with that of the file name.

  • Related