0

I want to cast a (long) integer into a char array in C. So, for example:

int a = 65;

// I would like to get: "65"
// But I am having the conversion to the corresponding ascii when I do:

printf("%c", (char) a);
// "A"

EDIT: In my example I am printing, but what I actually want is to store the char into a variable for further manipulation.

I think this should be a super simple question, but I didnt manage to find the answer, so I thought that maybe I am not posing the question correctly. So if anyone can correct the question statement I would be grateful too.

1
  • maybe you need itoa? Commented Apr 10, 2020 at 22:13

4 Answers 4

2

%c string formater will output a character. Use %d to display integer instead

printf("%d", (char)a);
Sign up to request clarification or add additional context in comments.

7 Comments

No need for (char) casting.
Indeed, I assumed he needed it for some other reason. Casting to char is unnecessary (and error-prone) just to display a.
He probably did it because he was getting a warning from the compiler that %c requires a char.
Thank you for the answer, but I need it stored in a variable for further manipulation. So I am not sure how this translates into that, since this seems more like a formatting 'trick'?
printf only displays. If you want to store the char casted value to a variable just do char b = (char) a;. Here b will store the casted value
|
1

I think you want to do something like:

char ch1 = (char)97;  //ch1 = 'a'
char ch2 = (char)98;  // ch2 = 'b'

printf("ch1: %c, ch2: %c\n", ch1, ch2);
printf("ch1: %c, ch2: %c\n", 97, 98);
char str[3];
// store value 97 in to str[3]
sprintf(str, "%d", 97);
printf("str: %s", str);

The test:

#include<stdio.h>

int main(void)
{
    char ch1 = (char)97;  //ch1 = 'a'
    char ch2 = (char)98;  //ch2 = 'b'

    printf("ch1: %c, ch2: %c", ch1, ch2);
    printf("ch1: %c, ch2: %c", 97, 98);

    char str[3];
     // store value 97 in to str[3]
    sprintf(str, "%d", 97);
    printf("str: %s", str);
    return 0;
}

2 Comments

I want the opposite. I would like to have "97" and "98" into variables and not "a" nor "b"
it like: ` int a = 'a'; ` ?
0

Casts are generally used for converting between fixed-size built-in datatypes with different widths or semantics (ie, extending one type to a wider one, truncating a wide type to a narrower one, or telling the compiler that we now consider something "signed" or "unsigned").

String formatting is not like a cast: strings are not a fixed-size datatype and there are many, many ways of formatting the same integer as a string (most obviously, there are multiple bases in common use).

You're already using printf, so you can just use snprintf to write the string to a char array instead of to stdout.

Comments

0

Basic example of storing int variable in char array. Be aware of limits the values of various variable types like char.

#include <stdio.h>
#include <limits.h>

int main(void) {
    int a = CHAR_MAX;
    unsigned char b[1];

    b[0] = (unsigned char) a;

    printf("%d\n", a);
    printf("%hhd\n", b[0]);

    return (0);
}

Output:

➜  Desktop gcc -Wall -Werror -Wextra main.c
➜  Desktop ./a.out
127
127

If you are looking for converting your int into the char array, not just casting, then you probably need own implementation of itoa() function like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* itoa(int val, int base){

    static char buf[32] = {0};

    int i = 30;

    for(; val && i ; --i, val /= base)

        buf[i] = "0123456789abcdef"[val % base];

    return &buf[i+1];

}

int main(void) {
    int a = 127;

    printf("Value = %s\n", itoa(a, 10));

    return (0);
}

Output:

➜  Desktop gcc -Wall -Wextra -Werror file.c
➜  Desktop ./a.out
Value = 127

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.