how can I covert int to char in C [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?

If you want to put the number 55 into a string, use sprintf

Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;

Try this code segment:
printf("%d\n", d);

char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii

Related

C code printing hex values but very different [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
int main() {
int i;
int four_ints[4];
char* c;
for(i=0; i<4; i++) four_ints[i] = 18;
c = (char*)four_ints;
for(i=0; i<4; i++) c[i] = 24;
printf("%x\n", four_ints[2]);
}
So if I print like that it will simply print 12.
However if I change it to printf("%x\n", four_ints[11])
It suddenly prints 28ac90
Why would it do that?
In the second statement printf("%x\n", four_ints[11]) you access a position of the array that was not reserved for your program (int four_ints[4]). That is you have no guarantees of what is stored on a not reserved portion of memory.

C initialise 3D array with values [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to initialise all elements of a 3D array "A". The array consists of 2000x100x4 integer elements of the 3D array and is stored in row-major order. Each index at position [i,j,k] in "A" must be initialised with the value i*i*i + j*j*j.
How can I do this using for loops? Any suggestions? Thanks.
for(i=0;i<2000;i++)
for(j=0;j<100;j++)
for(k=0;k<4;k++)
A[i][j][k]= (i*i*i) + (j*j*j);
I hope I understood your question correctly. Or were you looking for something else?
It's not something hard to do:
int A[2000][100][4];
int i,j,k;
for (i=0;<2000;i++)
{
for (j=0;j<100;j++)
{
for (k=0;k<4;k++)
{
A[i][j][k] = i*i*i + j*j*j;
}
}
}

Sprintf affecting other string used in formatting? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
For some reason, my sprintf call is affecting the string that I used to format the new string. Here is my code:
string foo = "bar";
char salt[] = "";
sprintf(salt, "%c%c", foo[0], foo[1]);
When I try printing foo after the sprintf, it has no value. If I print it before the sprintf, it's fine.
Your result buffer(salt) is too small to hold the value.
string foo = "bar";
char salt[3] = "";
sprintf(salt, "%c%c", foo[0], foo[1]);

Why does passing the result of printf to another printf work? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
How does the following code work?
void main()
{
printf("%d", printf("earth"));
}
This gives as output: earth5.
The return value of printf is the number of characters printed. The inner printf is called first. Equivalent to:
int rc = printf("earth");
printf("%d", rc);
This is absolutely fine :-)
The print("earth") outputs earth and return 5 (the number of characters printed).
The other printf gets the 5 as a parameter and outputs it as an integer (because of the %d)
%d is expecting an integer to print it. printf returns the number of printed chars, and you're printing a 5 char string.
It evaluates first the inner print to find out how many character were printed and then it evaluates the outer one printing 5.

What is causing this weird output? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the following code:
void doPlayerMove(void)
{
bool moved = false;
while (!moved)
{
printf("\nWhere is the piece you want to move?(rc):");
int r = getchar() - '0';// gets the row number
int c = getchar() - '0';// gets the column number
printf("%d:%d", r, c);// prints the chosen row/column
clearInput();
printf("\nWhere is the space you want to move to?(rc):");
int r2 = getchar() - '0';
int c2 = getchar() - '0';
printf("%d:%d", r2, c2);
...
}
}
void clearInput(void)
{
while(getchar() != '\n');
}
this is the output i get:
Where is the piece you want to move?(rc):51
5:1
Where is the space you want to move to?(rc):40
4:00
Whats up with the extra 0? Does anyone see where the problem is?
As the OP says in a comment:
Problem solved, it was some output from some function i was calling in
the ... sorry for the false alarm!

Resources