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]);
Related
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
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 have problem to compile the following lines:
/*This code compiles with error*/
char HeLev1[6];
HeLev1[]="45.0";
/*but this is OK:*/
char HeLev1[6]="45.0";
You cannot assign values to an array. You need to assign values to array elements one-by-one (or, when dealing with strings, using strcpy())
char HeLev1[6];
strcpy(HeLev1, "45.0");
char HeLev2[6];
HeLev2[0] = '4';
HeLev2[1] = '5';
HeLev2[2] = '.';
HeLev2[3] = '0';
HeLev2[4] = '\0'; /* properly "terminate" the string */
Note that in your code, the OK part, you have an array initialization, not assignment.
Also note that, in both cases above, the 6th element (HeLev1[5] or HeLev2[5]) has an undefined value (garbage).
you can assign whole values to an array only while initialization. like these are correct forms,
char HeLev1[6]="45.0";
int array[3]={1,2,3};
char HeLev1[]="45.0";
int array[]={1,2,3};
but once you have skipped this part. you have to assign element by element. like,
char HeLev2[6];
HeLev2[0] = '4';
HeLev2[1] = '5';
HeLev2[2] = '.';
HeLev2[3] = '0';
HeLev2[4] = '\0'; /* properly "terminate" the string */
or you can use memcpy or strcpy.
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.
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;
}
}
}
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.