Converting int to char in C

后端 未结 7 534
不知归路
不知归路 2021-01-18 03:55

Right now I am trying to convert an int to a char in C programming. After doing research, I found that I should be able to do it like this:

int value = 10;
c         


        
7条回答
  •  无人及你
    2021-01-18 04:20

    A portable way of doing this would be to define a

    const char* foo = "0123456789ABC...";
    

    where ... are the rest of the characters that you want to consider.

    Then and foo[value] will evaluate to a particular char. For example foo[0] will be '0', and foo[10] will be 'A'.

    If you assume a particular encoding (such as the common but by no means ubiquitous ASCII) then your code is not strictly portable.

提交回复
热议问题