How do you cast from integer to string in C?
Please give me an example.
In addition,
My msn is: peteryiu007@hotmail.com
I appreciate if someone who is good in C, and interested in helping me to get better at C to add me. Thank you very, very much.
How do you cast from integer to string in C?
Use the itao() function.
Example:
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
int main()
{
char IntegerBuffer[128];
printf("Enter the integer\n");
scanf("%i",%26amp;Integer);
itoa(Integer,IntegerBuffer,10);
}
The first argument is the integer, the second is the character buffer, and the last is the base. base 10 is regular decimal.
Reply:the previous person's answer will convert your integers to strings which we suppose is what you're after
you cannot strictly speaking CAST from one to the other as an INT is only the same size as a CHAR while a string is an
array of char. You can only cast from ints to doubles or some other larger numeric type (casting is a way of increasing the size of a variable on the run, so to speak, for example if you only need the larger size once)
for learning more, look up
http://www.amazon.com
and put in "kernighan" in the search field
and buy a copy of the Ansi C Reference, written by Kernighan and Ritchie, the 2 guys who invented the C language
Amazon has them around US$50 new or right now they have some used ones around $19
work your way through the exercises (some are easy some make you really think) to get a good understanding of what does what.
C u
funeral flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment