I have the following code, but I'm not sure what %0x%x means in the following code?
sprintf(buf, "pixel : %0x%x \n", gpbImageData[100]);
OutputDebugString(buf);
gpbImageData[100] is pointing to an image data in the memory.
Your example causes undefined behaviour. The format string will cause sprint to expect two int values:
%0x
%x
Both of these mean exactly the same thing - print a value as a hexadecimal number. However, the call you've shown passes only one argument.
Are you sure it doesn't say 0x%x? If it doesn't, it's probably supposed to... that would be more normal, and will print the passed-in value as a hexadecimal number prefixed with 0x.
Your code as shown should cause a warning. clang gives:
example.c:5:15: warning: more '%' conversions than data arguments [-Wformat]
printf("%0x%x\n", 125987);
~^
1 warning generated.
and gcc says:
example.c: In function ‘main’:
example.c:5: warning: too few arguments for format
example.c:5: warning: too few arguments for format
Both without providing any flags at all.
You certainly meant this format string "0x%x"
sprintf(buf, "pixel : 0x%x \n", gpbImageData[100]);
This adds the 0x prefix to the hexadecimal numbers when they are written in buf.
Note that you can achieve the same thing with the flag character #:
sprintf(buf, "pixel : %#x \n", gpbImageData[100]);
The correct format is "0x%x" as ouah and Carl Norum said. Whatever gpbImageData[100] content (pointer or number), %x will print its value as a hexadecimal number. 0x is just a text. Maybe "gpbImageData" is an array of pointers.
Related
While compiling the following code:
#include <stdio.h>
int main() {
printf("99% Invisible");
return 0;
}
in gcc 7.5.0 I get the following warnings:
test.c: In function ‘main’:
test.c:4:16: warning: ' ' flag used with ‘%n’ gnu_printf format [-Wformat=]
printf("99% Invisible");
^
test.c:4:16: warning: 'I' flag used with ‘%n’ gnu_printf format [-Wformat=]
test.c:4:16: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=]
printf("99% Invisible");
~~~^
What is going on here? I don't see mention of a " " flag or an "I" flag anywhere in documentation. The code outputs 99visible, essentially ignoring the space and I in the format string and following the %n format.
edit: People seem to be misunderstanding the question. I know how to printf a literal %, and what %n do. I am just curious what is happening here.
(also, for those who know the context: I know the system in question didn't use C, I am just curious as to what printf is doing here).
The I flag is a GNU extension to printf. From the man page:
glibc 2.2 adds one further flag character.
I
For decimal integer conversion (i, d, u) the output uses the
locale's alternative output digits, if any. For example, since glibc
2.2.3 this will give Arabic-Indic digits in the Persian ("fa_IR") locale.
So when the compiler checks the format string, it sees % In as a format specifier, i.e. the space and I flags applied to the n conversion specifier. Since neither flag is applicable to the n conversion specifier, the compiler emits a warning for each.
To print the literal %, you must write %%.
https://en.cppreference.com/w/c/io/fprintf
I flag is not in the C standard.
It appears that with your compiler when a % character is encountered in a printf format string, it scans forward to find a valid format specifier, then interprets everything in-between as a modifier. If those are not valid modifiers, an error is flagged.
As others have pointed out, replace "99% Invisible" with "99%% Invisible" to fix the problem.
When I run this:
#include <stdio.h>
int main() {
int x = 1;
printf(x, "\n");
return 0;
}
It gives me these errors:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x1)
Format string is not a string literal (potentially insecure)
Treat the string as an argument to avoid this
Incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'
And it outputs:
(lldb)
However, when I change it to:
printf("%s", x);
It works perfectly fine. (outputs "1", as expected)
Why are the conversion characters (e.g. %s, %d, etc...) needed?
They're needed to tell the printf function what the types (and number) of the parameters are that you are passing.
The language has no mechanism to allow a function to determine this dynamically, so the format string gives it the clues to decode them.
The format string is always the first parameter, because the called function can always access that in the same place. Functions like printf are still written in C typically, so can only use those functions that the language provides.
They parameters are not "conversion characters". I think your confusion comes from the fact that you think printf simply prints all its arguments and automatically deduces how to print each. However, printf cannot work like that because C does not have support for overloading.
Concretely, the first argument is not like the rest. It is not something to print, but a format string. You can see easily what it means by trying this:
printf("My friend %s has %d coins!", "John", 123);
which will print:
My friend John has 123 coins!
%s here specifies the first argument (after the format string) will be interpreted as a string, and %d means the second argument (again, after the format string) will be understood as an integer. Both will be replaced with the actual value from the argument.
Hey what will be the the output of the following code, it is not working as it should.
int j=65;
printf("j>=65?%d:%c",j);
In this case it should print: j>=65, and then match the next %d with the j so print 65, and then %c is printing l how the l is printed.
Instead of
Printf("j>=65?%d:%c",j);
there has to be at least
printf("j>=65?%d:%c",j);
The code has undefined behaviour because the number of format specifiers is greater than the number of following arguments.
I think you mean the following
printf( j >= 65 ? "%d" : "%c", j );
in this case there will be outputed 65.
The format specifier "j>=65?%d:%c" has two %... pieces, they indicate that two values are to be printed. The format is used within printf("j>=65?%d:%c",j); which only provides one value to be printed.
There is no checking in C that the printf is given the correct number of arguments for the format, there is no protection at run time for what actually happens when too few arguments are given. That is undefined behaviour. The C language allows the programmer to write many things that lead to undefined behaviour.
You should not use conditional operation inside double quotes like this printf("j>=65?%d:%c",j);. you are having two format specifier and only one argument. so it results in undefined behavior. so j>=65? is printed first, %d prints 65, %c expects arguments, but there is no arguments, so it prints anything. Results in undefined behavior. You should use No of arguments equal to the no of format specifiers.
When i run your code in my system i got-
root#ubuntu:~/c/basics# cc con1.c
con1.c: In function ‘main’:
con1.c:6:1: warning: format ‘%c’ expects a matching ‘int’ argument [-Wformat=] // see this warning
printf("j>=65?%d:%c",j);
^
root#ubuntu:~/c/basics# ./a.out
j>=65?65:� // see this undefined symbol
Try the following change and fix it-
printf(j>=65 ? "%d" : "%c",j);
when the condition is true it prints 65 and when condition fails it prints the character(For that input value)
or
j>=65 ? printf("%d",j) : printf("%c",j);
You call printf() with two format specifiers, but pass only one parameter after the format string. The function doesn't know how many parameters you pass, so it assumes you did pass two arguments. The result is, it reads the random garbage that just happens to be at the memory location where would be the second parameter if you did pass it. Run it another day, compile it with different compiler or different options, and you get different result (in worst case, segmentation fault).
Upon calling the sprintf function to format a string, something unexpected was printed
printf("%d;%s;%s;%d;%d;\n", ptr->programmaType, ptr->titel, ptr->zender, ptr->start, ptr->einde);
prints "0;Stargate;scifi;0;0;" while
printf("%d;", ptr->einde);
prints "42", which is the value of ptr->einde. I've also tried
printf("%d;%s;%s;%d;%d;", 0, "Stargate", "scifi", 0, 42);
which prints correctly, so I'm guessing the problem is related to the variables. Last thing I tried was
int bug = ptr->einde;
printf("%d;%s;%s;%d;%d;\n", ptr->programmaType, ptr->titel, ptr->zender, ptr->start, bug);
which also failed to print correctly... I have no idea what the hell is happening.
Note: ptr->start and ptr->einde are defined als time_t types, but seeing as printf works with a single argument I doubt that's a problem.
You said that ptr->start and ptr->einde are time_t type. This type is not not fully portable and can be any "integer or real-floating type", so it's possible that they are not being handled correctly on your system when you try to print them as int types.
Try casting it to something known and then printing:
printf("%d;%s;%s;%ld;%ld;\n", ptr->programmaType, ptr->titel,
ptr->zender, (long)ptr->start, (long)ptr->einde);
I guess your definition of ptr->einde is not of type int.
And you are using "%d" in the printf specifier which expects an int value.
Use the correct specifier for the correct type and printf behaves.
No printf is not limited in length, except in embedded implementations where it must be able to print at least 4095 chars.
time_t is probably a 64 bit integer value. The solution is to cast your variable to (uint64_t) and using PRIu64 format specifier.
printf("%d;%s;%s;%"PRIu64";%"PRIu64";\n",
ptr->programmaType,
ptr->titel, ptr->zender,
(uint64_t)ptr->start,
(uint64_t)ptr->einde);
I don't know how this C program I'm meant to compile works exactly. I'm compiling it on a MacBook so maybe that explains the unusual errors? Anyway the compiled program doesn't seem to be working correctly. When compiled, I get these:
ers.c: In function ‘evolve’:
ers.c:205: warning: unknown conversion type character 0xa in format
ers.c: In function ‘print_rule’:
ers.c:304: warning: unknown conversion type character 0xa in format
ers.c: In function ‘test_evaluate’:
ers.c:380: warning: unknown conversion type character 0xa in format
Which refer to these lines of code:
if(i%100==0)printf("best on training set at iteration %d: %g\%\n", i,100.0* population[bestinpop].acc);
printf("ACCURACY on training set %g\%\n\n", 100.0* r->acc);
printf("TEST ACCURACY %g\%\n", 100.0* r->acc);
I suspect it to be something to do with that %g type formatting.
Can anyone see what is being done wrong?
The 0xa in ASCII encoding is the Line Feed character \n, so your errors are indeed coming from the "%\n" constructs
I assume that the original developer meant "%%" and not "\%" (to display '%' characters). But I don't believe that this program ever compiled on any platform.
BTW : %g is an alternative formatting character for double (output is same as %f or %e, depending on the double value).
"%\n" is not a valid format specifier. If you need the % character to be part of the output you need to use "%%".