getchar and print c

hallo, a starters qwestion

i wand to get char from the terminal window,
but get a gcc error compiling it.
.........................................
test_demo.c:12:3: warning: format not a string literal and no format arguments [-Wformat-security]
12 | printf(text);
..........................................


1
2
3
4
5
6
7
8
9
  int main ()
{

char text;
text = getchar();
// pub to the oder int printf 
  printf(text);

}
You can use putchar(text)
https://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm

printf expects a null-terminated string as input (the type being pointer-to-char), but you have just a char.

Btw, when posting small code samples, you should also add the #include "..." parts to make your code actually compileable.
Last edited on
printf() requires a format string and the argument(s) for output to work.

https://en.cppreference.com/w/c/io/fprintf

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main( )
{
   char text;

   text = getchar( );

   printf("%c\n", text);

   return 0;
}


As Ganada suggests you could use putchar() at line 9.

https://en.cppreference.com/w/c/io/putchar
i wand to get char from the terminal window


Note that whilst getchar() will return a char from the terminal windows, this is only after a line-termination char (cr/lf) has been entered.

Are you using c or C++?
Topic archived. No new replies allowed.