Convert char to int in C and C++

前端 未结 12 1995
梦毁少年i
梦毁少年i 2020-11-22 02:41

How do I convert a char to an int in C and C++?

12条回答
  •  后悔当初
    2020-11-22 03:01

    C and C++ always promote types to at least int. Furthermore character literals are of type int in C and char in C++.

    You can convert a char type simply by assigning to an int.

    char c = 'a'; // narrowing on C
    int a = c;
    

提交回复
热议问题