Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert integer to char in C? [duplicate]

Tags:

c

How to convert integer to char in C?

like image 993
anik Avatar asked Feb 17 '10 09:02

anik


People also ask

Can you convert int to char in C?

We can convert an integer to the character by adding a '0' (zero) character. The char data type is represented as ascii values in c programming. Ascii values are integer values if we add the '0' then we get the ASCII of that integer digit that can be assigned to a char variable.

Can we convert int to double in C?

The %d format specifier expects an int argument, but you're passing a double . Using the wrong format specifier invokes undefined behavior. To print a double , use %f .

How do you turn a number into a character?

char a = Character. forDigit(num1, 10); We have used the forDigit() method converts the specified int value into char value. Here, 10 and 16 are radix values for decimal and hexadecimal numbers respectively.

Can we store integer in char?

Yes, you can store an integer value in a char type variable. yes, you can store an integer in a char but compiler will consider it as string.


1 Answers

A char in C is already a number (the character's ASCII code), no conversion required.

If you want to convert a digit to the corresponding character, you can simply add '0':

c = i +'0'; 

The '0' is a character in the ASCll table.

like image 131
Ofir Avatar answered Oct 06 '22 10:10

Ofir