Verification: a143cc29221c9be0

Php char from int to

Add '0' to Convert an int to char

The '0' has an ASCII value of 48. so, we have to add its value to the integer value to convert it into the desired character. The program is as below:

#include
int main(void)
{
    int number=71;

    char charValue = number+'0';
    printf("The character value is :%c",charValue);

    return 0;
}

Output:

The character value is: w

Below is a program that will convert an integer to the character only between 0 to 9.

#include
int main(void)
{
    int number=7;  
    char charValue=number+'0';
    printf("The value is :%c",charValue);
    return 0;
}

Output:

The value is: 7

Another program to convert an integer value to a character is as below:

#include

int main(void)
{
    char charValue[] = "stringValueX";
    int anyNumber;
    for (anyNumber= 0; anyNumber

Output:

stringValue0
stringValue1
stringValue2
stringValue3
stringValue4
stringValue5
stringValue6
stringValue7
stringValue8
stringValue9

Assign an int Value to char Value

Another way to convert an integer value to a character value is to assign an integer value to a character value as below: The character value corresponding to an integer value is printed here.

#include

int main(void)
{
    int number = 65;
    char charvalue = number;

    printf("The character value :%c", charvalue); 

    return 0;
}

Output:

The character value : A

Below is another way to convert an integer value to a character value. Here, the value is typecasted, so the value 67 gets converted into the corresponding ASCII value.

#include
int main(void)
{
    int number = 67;
    char charValue = (char)number;
    printf("The character value = %c", charValue);

    return 0;
}

Output:

The character value = C

sprintf() Function to Convert an Int to a Char

The sprintf() function works the same as the printf() function but instead of sending output to console, it returns the formatted string.

The first argument to the sprintf() function is a pointer to the target string. The remaining arguments are the same as for the printf() function.

Syntax of sprintf()

int sprintf(char *strValue, const char *format, [arg1, arg2, ... ]);
  • strValue is a pointer to the char data type.
  • format is used to display the type of output along with the placeholder.
  • [arg1,arg2...] are the integer(s) to be converted.

The function writes the data in the string pointed to by strValue and returns the number of characters written to strValue, excluding the null character. The return value is generally discarded. If any error occurs during the operation, it returns -1. The program to convert an integer to a character is as below:

#include
int main(void)
{
  int number = 72;
  char charValue[1];

  sprintf(charValue, "%c", number);
  printf("The character value = %s", charValue);

  return 0;
} 

Output:

The character value = H

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.