C

S

santosh

how to convert integer to char

In C there are several integer types like short, int, long and long
long. Even char is an integer. One example for int would be.

int i;
char c;

i = getc(stdin);
if (i != EOF) {
c = i;
putchar(c);
}

The value in the int, (or any other integer), must be representable by
the char variable. Otherwise there will be loss of data and unexpected
behaviour.
 
S

santosh

how to convert integer to char

What is the idea posting the same question thrice within ten minutes?
Usenet is an asynchronous medium and it will take a while for your
article to propagate to all of Usenet and for any possible answer to
reach your news server.

Be aware that Google Groups is _not_ Usenet, but a rather fragile web
interface to it. It might be better to avail of a dedicated Usenet
server and a newsreader.
 
M

Mark McIntyre

On Sun, 7 Oct 2007 17:31:02 +0000 (UTC), in comp.lang.c ,
sprintf()

that converts it to a string, not a char.

(though I think you're right that this is what the OP probably wants)

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
A

Army1987

how to convert integer to char
With a cast, e.g. (char)i is i converted to char.
But, in practically all places where this makes sense and is
useful, that is done automatically.
Note that library functions dealing with single characters use
int, so in putchar(65) no conversion is done nor needed (though
you could write putchar((char)65) if you find it clearer, because
it'll be converted back to int, but that's useless: in C char's
are just "small" integers).
 
M

Martin Wells

how to convert integer to char

Specify a radix and alphabet.

#define RADIX 10

char const alphabet[RADIX] =
{ '0','1','2','3','4','5','6','7','8','9' };

char UIntToChar(unsigned const i)
{
assert( i < RADIX );

return alphabet;
}

Martin
 
K

Keith Thompson

how to convert integer to char

If you want a post a question here, pick a subject header that
summarizes what you're asking about. "C" is frankly a lousy subject
header, since (in theory at least) C is all we talk about here.

You want to know how to convert an integer to a char. What exactly do
you mean by "convert"? I can think of at least two distinct meanings
for your question.

Providing some examples would help. Providing some actual code, even
if it doesn't work, would also help.
 
U

user923005

how to convert integer to char

Just a reminder to read the FAQ before posting:

13.1: How can I convert numbers to strings (the opposite of atoi)?
Is there an itoa() function?

A: Just use sprintf(). (Don't worry that sprintf() may be
overkill, potentially wasting run time or code space; it works
well in practice.) See the examples in the answer to question
7.5a; see also question 12.21.

You can obviously use sprintf() to convert long or floating-
point numbers to strings as well (using %ld or %f).

References: K&R1 Sec. 3.6 p. 60; K&R2 Sec. 3.6 p. 64.
 
L

lovecreatesbea...

how to convert integer to char

Specify a radix and alphabet.

#define RADIX 10

char const alphabet[RADIX] =
{ '0','1','2','3','4','5','6','7','8','9' };

char UIntToChar(unsigned const i)
{
assert( i < RADIX );

return alphabet;

}


or this one

char inttochar(int i)
{
return i >= 0 && i <= 9 ?
i + ('1' - 1) :
i ;
}
 
K

karthikbalaguru

how to convert integer to char

1) itoa() -> converts an integer to a character array
2) cast tricks with some data loss
3) sprintf tricks w.r.t string
4) a small routine on your own

Karthik Balaguru
 
U

user923005

On Oct 7, 6:26 pm, (e-mail address removed) wrote:
Specify a radix and alphabet.
#define RADIX 10
char const alphabet[RADIX] =
{ '0','1','2','3','4','5','6','7','8','9' };
char UIntToChar(unsigned const i)
{
assert( i < RADIX );
return alphabet;


or this one

char inttochar(int i)
{
return i >= 0 && i <= 9 ?
i + ('1' - 1) :
i ;
}


It does not seem to be terribly effective for integers larger than 9.
 
K

Keith Thompson

karthikbalaguru said:
1) itoa() -> converts an integer to a character array
2) cast tricks with some data loss
3) sprintf tricks w.r.t string
4) a small routine on your own

itoa is non-standard.

The question is ambiguous. There's no point in trying to answer it
until and unless the original poster comes back and explains what he's
actually asking. If he doesn't do so, I think we can assume he's not
really interested in getting an answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C Programming functions 2
C exercise 1
C# DataTable 1
Beginner at c 0
C pipe 1
How can I view / open / render / display a pdf file with c code? 0
C language. work with text 3
Meme generator in c 1

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top