0

I'm writing a compiler with Flex and Bison using C langage and need to convert a character string to a one simple character . which functions should i use ? i can't find it . forexample :

-string to integer we use : int i = atoi(yytext);
-string to float we use :   float f = atof(yytext);
  • 1
    What do you mean with "convert a character string to a one simple character"? – meaning-matters Feb 20 '21 at 22:22
  • for exemple the string of one character "c" will be one caracter 'c'.we use this approch when converting strings to integer like : "2021" will be int integer= atoi("2021"); // 2021 – khiter med achraf Feb 20 '21 at 22:31
  • Strings in C are just arrays of characters (with a 0 at the end) and characters are small integers. If you're going to try to use flex and bison in C, you really need to understand the way C represents strings. Hopefully, you have some kind of C textbook handy. – rici Feb 20 '21 at 22:55
  • Is the [answer below](https://stackoverflow.com/a/66297158/1971013) what you need? – meaning-matters Feb 21 '21 at 08:20
  • yes it solves the problem . thank you so much my friends – khiter med achraf Feb 25 '21 at 08:32

1 Answers1

1

If I understand your question, a character string with one value is just a character string of [0] so why not just do something like...

char new_char = char_string[0];
TheN00bBuilder
  • 94
  • 1
  • 11
  • 1
    This is probably what the OP was asking, with the _caveat_ that a `char` does not necessarily translate into an actual printable character; that depends on the encoding. For example, æ. – Neil Feb 20 '21 at 22:53