type cast from integer to char array

J

Jay

How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

thanks in advance for any help.
 
P

Park Sung Jae

sprintf, sscanf

function will help you

if your environment is linux(unix), man page will give about those functions
to you.
 
A

Allan Bruce

Jay said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

thanks in advance for any help.

int number = 450;
char string[10];

sprintf(string, "%9d", number);

HTH
Allan
 
M

Mike Wahler

Jay said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c
program?

You don't. Are you sure you understand what a cast is?
It converts one type to another.

What you're really asking is how to store the textual
representation of a numeric type into an array of
characters. Use the 'sprintf()' function, which
works just like 'printf()' except its output goes
to a char array instead of stdout.

#include <stdio.h>

int main()
{
char array[10] = {0};
int i = 450;
sprintf(array, "%d", i);
printf("%s\n", array);
return 0;
}

-Mike
 
D

Dan Pop

In said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

Please read the FAQ *before* posting questions to this newsgroup.

Dan
 
D

Dan Pop

In said:
Jay said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c
^^^^^^^^^^
program?

#include <stdio.h>

int main()
{
char array[10] = {0};
int i = 450;
sprintf(array, "%d", i);
printf("%s\n", array);
return 0;
}

You're not solving the OP's problem: he wants the digits 450 followed by
7 spaces, i.e. not a string. The solution is a bit more complicated:

char array[10], buff[sizeof array + 1];
sprintf(buff, "%-10d", 450);
memcpy(array, buff, sizeof array);

Then again, maybe the OP actually wanted your solution, but didn't
formulate his question properly.

Dan
 
D

Dan Pop

Jay said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

thanks in advance for any help.

Do you mean a char[10] array *including* the tailing null character ?

What "tailing null character"? The word "string" does not appear in OP's
request, does it?

Dan
 
J

Jay

Jeff said:
Jay said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

thanks in advance for any help.

Do you mean a char[10] array *including* the tailing null character ?

char array[10];
sprintf(array, "%-9d", 450);
array[9] = '\0';

This answers my question, thank you all for your help.
 
D

Dan Pop

In said:
Jeff said:
Jay said:
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

thanks in advance for any help.

Do you mean a char[10] array *including* the tailing null character ?

char array[10];
sprintf(array, "%-9d", 450);
array[9] = '\0';

This answers my question, thank you all for your help.

Then, your question was extremely poorly phrased (your example shows 3
digits followed by 7 spaces). BTW, there is no point in overwriting
the null character appended by the sprintf call with array[9] = '\0'.
sprintf is quite good at generating strings...

Dan
 

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top