Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Im fairly new to C. I have a mysql query that has variables in it that are type char and they have to be.
Im wondering if i have a while loop
ex.
i=32;
while (i <= 500){
str_concat(sqlblah blah,
i,
null);
}
i++;in the sql stuff that value of i needs to be of type char.So basically how can i make a char variable equal to my integer but still stay a char and then increment with the integer in the while loop.
Is this possible?
Any insight would be great.
Feel free to email if you need more explaination.

If I understand you correctly...
Yes, this is possible. You can
throw a character type cast on your integer.int y = 3;
char x = (char) y;This will put the character representation of the number 3 into the character variable x based on the value of y.
IR

It looks like you'll need to declare an array of char then use sprintf to convert the number i into a string before you can use string concatenation. I assume that is in C and not C++.

IR
It must have been past your bedtime when you answered this question. (I'm sure that you know the stuff below and just mispoke, so my descriptions are geared more to others who don't realize these things.)
Casting doesn't change the data that is cast, it just tells the compiler that it is OK to view the data as a different data type.
The value of the character 3 is 0x33 (meaning 33 hex), while obviously the value of the integer 3 is 0x03.
Your answer says that the 0x03 stored in y will be converted to 0x33 when it is cast into x.
Not true.
x will contain 0x03 also.
But your basic answer is correct (that you can use a char variable like an integer), I believe. I really didn't understand the question asked, so...But a char variable can be used like an integer. A char can be treated just like a one byte integer when desired. How you use the data determines if it seems to be an integer or a character.
Try this:
char c;
c=65;
printf("c = %d\n",c); /* c = 65 */
printf("c = %c\n",c); /* c = A */c = c + 5;
printf("c = %d\n",c); /* c = 70 */
printf("c = %c\n",c); /* c = F */c = 'B' + 1;
printf("c = %d\n",c); /* c = 67 */
printf("c = %c\n",c); /* c = C */
The value inside the char variable is simply an integer, it depends upon how you use it whether it appears to be an integer or a character.

Thank you for your help. I used sprintf() and that worked perfect. It placed the decimal value of i = 32 into str = "32" like i needed. I could have also used itoa() to do the same thing. Recasting would have given me a character representation of 32 rather than "32". Thank you for your answers.
C

![]() |
emailing hyperlinks with ...
|
c++ open internet explore...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |