Simple printf program doesn't work - c

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.

Related

What is the 'I' (capital i) flag in C printf?

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.

Format specifier %Lf is giving errors for `long double` variables

I am getting the following errors:
In function 'main':
[Warning] unknown conversion type character 'L' in format [-Wformat=]
[Warning] too many arguments for format [-Wformat-extra-args]
In function 'error_user':
[Warning] unknown conversion type character 'L' in format [-Wformat=]
[Warning] too many arguments for format [-Wformat-extra-args]
In the below code:
#include <stdio.h>
#include <stdlib.h>
void error_user (long double *error);
int main(void)
{
long double error;
printf("What error do you want?\n");
error_user (&error);
printf("%Lf\n", error);
return 0;
}
void error_user (long double *error)
{
scanf("%Lf", error);
}
As far as I know the format specifier of a long double is %Lf so not really sure how to solve this one. Thank you!
Compiled with TDM-GCC 4.9.2 64-bit Release in DEV-C++.
Your compiler doesn't recognize %Lf , you need to provide the compiler flag -D__USE_MINGW_ANSI_STDIO=1
Example:
$ gcc filename.c -Wall -Wextra -pedantic -O3 -D__USE_MINGW_ANSI_STDIO=1
^^^^^^^^^^^^^^^^^^^^^^^^^^
As you are using Dev-C++, you should probably also add -std=c11 flag to enable C11 standard.
This thread explains how you should add flags to Dev-C++:
How to change mode from c++98 mode in Dev-C++ to a mode that supports C++0x (range based for)?
So you need to add the flags -std=c11 and -D__USE_MINGW_ANSI_STDIO=1 using the instructions in the linked thread.
Since Dev-C++ uses an older standard, it's possible that adding only -std=c11 can solve the issue. Try it first.

What's wrong with Geany?

#include <cini.h>
int main() {
int a ;
a = 21 ;
printf(a);
return 0;
}
Questions :
1) on the toolbar, once I've written the code, the "execute" or "compile" functions on Geany (a C compiler) seem to be disabled
2) what's wrong with my code ?
I'm about to learn C, so please be nice with a rookie.
Geany is an editor, not a compiler. If the compile and execute functions are disabled it could be because it is unable to find your actual compiler (make sure you have one installed), or because you haven't saved your file as C code.
The first argument to printf must be the format string. If you want to print an integer as decimal, you need to use the %d conversion specifier, like this:
printf("%d\n", a);
The \n after the %d prints the linefeed character to the output. You can read about the printf function here.
You need to include at least stdio.h, because printf is declared in that header. The only reference I could find to cini.h was this header here which won't work in a C program because it is C++ code.

C Programing: File reader format conversion Error

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 "%)"

Unknown conversion types in this program I'm meant to compile

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 "%%".

Resources