Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a variable:
int a = 0x0304;
I print it out like this:
printf("the value is 0x4x\n", a);
but it shows the value is 0x304, I want the result should be the value is 0x0304, how to print it out like this?
Try adding a leading 0 to your format specifier - %04x
printf("the value is 0x%04x\n", a);
Just in case its omission from your question wasn't a typo, note that I've also added a % to make this a format specifier. As it stands, the code in your question would have just echoed the value is 0x4x
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I am learning C right now and I understand that I can't add an integer with a decimal like so:
#include <stdio.h>
int main() {
printf("%d",15+9.0);
return 0;
However when running this I expected some sort of an error. Instead, I got a weird output:
-1866308488
Can someone help me understand why it gave me this output?
As #PaulMcKenzie said, C expected an int based on the %d format specifier. You gave it a double instead. C often gives unexpected behavior instead of throwing an error like Java or C#. What happens to a double variable when %d is used in a printf? says that the resulting behavior is undefined and OS-specific.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
void main()
{
printf("%d","");//printing output
getch();
}
The output is 173 .I am not getting why the output is 173.
First, you're trying to print a string as decimal integer, which means the decimal you try to print is going to be the pointer to the string (actually a pointer to the array of characters) and not the string itself. To use an individual character use single-quotes, not double-quotes.
To accomplish what you're actually trying to do, do this:
printf("%d", ' ');
Note there is an actual space between the two single-quotes.
The result will be 32, which is the decimal value for the ASCII Space character.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Given the following function:
image_ret* minify_1(image_src img_src, CLIENT* cl) {
image_ret* img_ret;
magickminify_init();
magickminify(img_src.image_src_val, img_src.image_src_len, (ssize_t*)&img_ret->image_ret_len);
return image_ret;
}
The compiler is telling me "expected expression before ‘image_ret’" with regard to the last line. I'm sure I'm missing some fundamental aspect of syntax here, but I don't know what. Lil' help?
You need to return a value, not a type. image_ret is a type, img_ret is a poitner to a value of that type and probably what you want to return, except I see nowhere in your code where you allocate any storage to it, or initialising any of the fields except image_ret_len
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This code prints both "equal" and "1 0".
Why is this happening even though 1 != 0 ?
if(2/2 == 2/2.0)
printf("equal \n");
printf("%d %d", 2/2, 2/2.0);
The result of 2/2.0 is of type double. But you are using %d to print it, which is undefined behaviour. Use %lf instead to print a double. That's why you get 0 which is a possible result of invoking undefined behaviour.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm learning C and trying to understand the printf function better.
When I print:
printf("%d", 6 << 1);
it returns 120x0 instead of the expected 12. Why is that? I'm using gcc compiler.
Change this
printf("%d", 6 << 1);
to
printf("%d\n", 6 << 1);
and try to always add a newline character to distinguish the printed text from other output, unless you need to print things next to each other.
You should now see
12
0x00
most likely you're code is working properly printing 12 and later you print 0x0 somewhere.