Put int value into char array

Hello everyone!I'm a beginner in C programming. I'd like to ask if there is any way that I can use a char array to simulate a int variable. As I knew char use 1 byte and int type using 4bytes.

I tried:
int a=10;
char b[4];

for (int i=0;i<4;i++)
b[i]=(char)a;


but when i print out the char array, nothing is shown on the screen.
* I only included <stdio.h>

Many Thanks to all!
Last edited on
try by setting a to 97, that should be ASCII 'a', 10 is the newline character
The char type holds a byte of information, it's true. What this means is that you can represent either 0 to 255 (unsigned) or -128 to 127 (signed) in integral representation with it.

Here is an example of how to do this:

1
2
3
4
unsigned char uc = 200;
char sc = 200;
std::cout << "unsigned char: " << (int)uc << std::endl;
std::cout << "signed char: " << (int)sc << std::endl;


Output should be:


unsigned char: 200
signed char: -56



Notice that you do need to cast the char variable to an int in the cout statements.

~psault
Topic archived. No new replies allowed.