• C

an INT --> string

Is there a way that I can COPY a integer type var into a character array?


int dummy = 10;
char string1[maxlen];
int i;
<copy dummy into string1>

so that when I do a

for (i=0; i<maxlen;i++) printf ("%c", string1[i]);

I will get :

basically, I'm just trying to get the contents of
dummy == string1[];

Who is Participating?
duneramConnect With a Mentor Commented:

I just got back from the Red Cross where I am involved in their apheresis donation program (each donation takes a couple of hours but it helps cancer patients, so its a good thing).

Thats why I am just getting back now.  Yeah, I was just using the original variable names you used (or tried to, even though I mistyped it).  

I guess 'String' was a misleading variable name it could have been
char x[100];  or whatever you pick.

Another interesting function to look at (totally off the subject) but you may need it someday is

strftime().   <--- this function lets you take a time value (like time of day or a date) and format it into a string.   When I worked on my calendar function back in 93 it would have saved me oodles of time if I had known about that function.

Good Luck


There happens to be a fairly easy method for doing this its with the sprintf function.
If you are programming in windows its better to use wsprintf.

To use sprintf you would do this:

int dummy = 10
char String[maxlen];

sprintf(maxlen,"%d", dummy);  /* places the string "10" into maxlen */

Hope this helps!!

I mistyped...

the sprintf should have been

sprintf(String,"%d", dummy);  /* places the string "10" into String */

I was treating the 'maxlen' like a char maxlen[100] or something along those lines.

Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

mrquijaAuthor Commented:
argh..got confused with your statement

What type is String?? Let's say I wrote it like this

int dummy=10;
char stringvalue[100];

sprintf(stringvalue, "%d", dummy);  //  This places "dummy"'s value (10) into the character
                                                       array "stringvalue"?
mrquijaAuthor Commented:
Adjusted points to 15
In answer to your commented answer, yes. duneram deserves the points, why did you flunk him?

You can do in 2 ways.

Method 1
   string1 = itoa(dummy);

Method 2:
  int i,j=0,k;
  i = dummy;
  char string[100],string1[100]
  while ( i > 0 )
     string[j++] = i%10+48;
     i /= 10;
  string[j++] = '\0';
  i = 0;
  while (j >= 0)
     string1[i++] = string[j];
  string1[j] = '\0';
  /* here string1 contains the actual value in string */
  printf( "Character value in array is %s.",string1);

Good luck.
mrquijaAuthor Commented:
Slarti:  I didn't flunk him on purpose..I totally forgot that you can acccept the answer and post a comment afterwards. Take a chill pill. Duneram totally deserves the points..why do you think I adjusted the points from 10-15..cuz he actually helped me out instead of the standard answers of "I dont know" or the answers that give you the most complicated solutions to a simple problem. And I was waiting for him to respond to give him the points.  Thank you for your ignorance.

Sganta:  Duneram gave the information that I needed already. I was merely asking him a question to make sure that I understood what he had written. Sorry.

mrquijaAuthor Commented:
Thanks for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.