How to use scanf () without including stdio.h

Are there any possible ways to write a C program without including stdio.h as the header file. It has been suggested that it can be implemented by declaring extern int scanf(char* format, ...);

 #include <stdio.h> //I want this same code to work without including this line int main () { char str [80]; scanf ("%s",str); return 0; } 
+6
source share
2 answers

You can declare a scanf function with

 extern int scanf(const char *format, ...); 

The extern keyword is optional, but I like to include it as a reminder that a function is defined elsewhere.

Your example would look like this:

 extern int scanf(const char *format, ...); int main () { char str [80]; scanf ("%s",str); return 0; } 
+10
source

In C-89, this code will compile without #include, since function prototypes are optional.

Having said that he is on the list of β€œreally bad things” - scanf can be a macro, it can have one or more required parameters, ...

So you can do it, but it's like driving at night without any lights. You can crash even if you think you know the way.

+1
source

Source: https://habr.com/ru/post/951623/


All Articles