Store an integer in char array in C
Solution 1
You did store the integer in the character, it's just that %c
converts a character to its ASCII value. All ASCII values below 31 are non-printable.
If you run
printf("b = %d and i = %d\n", (int)b, i);
it will print 15.
If you want a representation of i
as a string:
char buf[12]; //Maximum number of digits in i, plus one for the terminating null
snprintf(buf, 12, "%d", i);
This will store a string representation of i
in buf
.
Solution 2
Clarification:
The range of char is -128..127. The range of unsigned char is 0..255.
If capturing ASCII values is the goal, declaring buffer variable to unsigned char type seems to be more appropriate.
Solution 3
The problem here is, variable b
already has a value 15
but since this does not constitute to a printable ASCII, using %c
format specifier, you won't be able to see any output.
To print the value, use %hhd
format specifier.
At the end I'm trying to have a char array of size 1024 which has i (15) as the first character and rest 0.
Well, you can define an array and assign values accordingly. Something like
#define SIZE 1024
char arr [SIZE] = {0}; //initialization, fill all with 0
arr[0] = 15; //first value is 15
should do the job.
Solution 4
A char is an 8-bit unsigned value (0 - 255) and it does indeed store 15 in it, the problem is, that in ASCII table 15 means "shift in" non-printable character, and %c interprets the value as an ascii character.
char b = (char) i;
printf("b = %d and i = %d\n", b, i);
to get
b = 15 and i = 15
if you used i = 90
in your current code, this would be printed:
b = Z and i = 90
Nicky Mirfallah
A software engineer specializing in the .NET platform.
Updated on July 09, 2022Comments
-
Nicky Mirfallah almost 2 years
Running the code below prints
b = and i = 15
.int i = 15; char b = (char) i; printf("b = %c and i = %d\n", b, i);
How can I store this integer in the character? At the end I'm trying to have a char array of size 1024 which has
i
(15) as the first character and rest 0.update: I tried :
int i = 15; char buffer[1024]; snprintf(buffer, 10, "%d", i); printf("buffer[0] = %c, buffer[1] = %c\n", buffer[0], buffer[1]);
And the result printed was:
buffer[0] = 1 , buffer[1] = 5
-
Dmitri about 7 years
char
isn't always unsigned. -
Nicky Mirfallah about 7 yearsI tried: char buffer[1024]; snprintf(buffer, 1, "%d", i); printf("buffer[0] = %c, buffer[1] = %c\n", buffer[0], buffer[1]); But i'm still not getting 1 and 5 for buffer (i=15)
-
Dmitri about 7 yearsShouldn't that be
snprintf(buf, 10, "%d", i);
? -
anonymoose about 7 years@Dmitri Oops. Thanks for the catch.
-
anonymoose about 7 years@NickyMirfallah As Dimitri mentioned, I had a minor typo.
char buffer[1024]; snprintf(buffer, 1024, "%d", i); printf("buffer[0] = %c, buffer[1] = %c\n", buffer[0], buffer[1]);
should work. Note thatprintf("%s\n", buffer);
will print out the whole string ("15" in this case). -
chux - Reinstate Monica about 7 years
-
chux - Reinstate Monica about 7 years
%hhd
converts the integer argument tosigned char
before printing.char
might not besigned char
. IAC,printf("b = %dn", b);
is sufficient unless rarechar
is same width/signness asunsigned
. -
Nguai al about 7 yearsYes. But any ASCII value that is greater than 127 will require unsigned char type.
-
chux - Reinstate Monica about 7 years
char b = (char) i;
is fine. "You can't store Integer datatype in Character datatype" is not supported by the C spec.char
is an integer type. -
chux - Reinstate Monica about 7 yearsHmmm, typical
int
can store the value of -2,147,483,648. Magic number 10 is insufficient for a string that may need size 12. -
chux - Reinstate Monica about 7 yearsSince ASCII is defined in the range 0 to 127, "ASCII value that is greater than 127" is a false premise. Perhaps you are thing of various extensions to ASCII?
-
Nguai al about 7 yearsYes. I am considering ASCII extensions.
-
anonymoose about 7 years@chux The comment next to the declaration was meant as an instruction to change it based on the range of values needed for the application, not a claim that 10 is the number needed. But good point. I'll update it.