I got
program.cpp: In function ‘int view_next(FILE*)’:
program.cpp:118: warning: unknown conversion type character ‘)’ in format
when I try to compile (gcc -o program program.cpp) but I don't know how to fix it. Can someone please give me a hand?
printf("\033[7m--More--(%.0f%)\033[m", float(file_size) /
float(buffIn.st_size) * 100);
this:
printf("\033[7m--More--(%.0f%)\033[m", float(file_size) /
should be:
printf("\033[7m--More--(%.0f%%)\033[m", float(file_size) /
read (or google) man 3 printf
What is happening here, is that the % character is used in functions of the printf family to signal that a format specifier is following. To print a literal % character, you can escape it with another % character.
printf("%%\n"); // prints a literal %
This is a minimal example that reproduces your error:
printf("%)\n"); // errors
And here is the fix to the minimal example:
printf("%%)\n"); // prints "%)"
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.
What should be the output of a single percent sign?
#include <stdio.h>
void main()
{
printf("%");
}
"Unknown format code" is not like "Unknown escape sequence".
What you're doing is undefined behavior.
Section 7.21.6.1p9 of the C standard regarding format specifiers for fprintf (and by extension printf) states:
If a conversion specification is invalid, the behavior is undefined. If any argument is
not the correct type for the corresponding conversion specification, the behavior is
undefined.
Also, gcc will generate a warning with -Wall if you do this:
warning: spurious trailing ‘%’ in format [-Wformat=]
The correct way to print a % character is with the format specifier %%.
printf("%%");
% is used to signal that you want to print a variable e.g.
int i = 10;
printf("%d", i); //prints 10
In order to just print the '%' sign you must escape it as
printf("%%");
This program doesn't work and I don't know why.
#include <stdio.h>
int main()
{
printf("%а", 20.0);
}
I am compiling in C99. Expected output is 0x1.4p+4
The а is CYRILLIC SMALL LETTER A (U+0430), but you have to use LATIN SMALL LETTER A (U+0061): a.
If you're using a compiler that is able to check format strings like Clang or GCC, then compile with (at least) -Wall which includes -Wformat (GCC Warning documentation)
5 : warning: unknown conversion type character 0xffffffd0 in format [-Wformat]
5 : warning: too many arguments for format [-Wformat-extra-args]
Its really wired. When I copypaste your code it turns into printf("%?", 20.0);
You should check your character encoding.
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).
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 "%%".