I was wondering if there is any possible way to write C/C++
program without using header files at all.
#include <iostream.h> //I want this same code to work without including this line
#include <conio.h> //I want this same code to work without including this line
int main ()
{
clrscr();
char str [80];
int num;
cout<<"Enter the string : "<<str;
cin>>str;
cout<<"Enter the number : "<<num;
cin>>num;
getch();
return 0;
}
I learned that in C
we can use scanf()
function without using stdio.h
Example:
#include <stdio.h>
int main ()
{
char str [80];
scanf ("%s",str);
return 0;
}
The above code can be written as
extern int scanf(const char *format, ...);
int main ()
{
char str [80];
scanf ("%s",str);
return 0;
}
Here by calling extern int scanf(const char *format, ...);
, scanf()
will work seamlessly.
I am a trainer, my students asked me about this weird doubt and its my reason. I need to how it can be achieved with some code, just like extern int scanf(const char *format, ...);
for replacing stdio.h
Best Answer
I am not sure I understand the reasons behind your question but there is a (very ugly) way.
Simply use your preprocessor (and not the compiler) with the right options (which depend on your environment) and it will generate a header-free equivalent CPP file which can be compiled.
Doing the same thing manually is just very prone to errors... and in my opinion completely useless.
Below is an example based on provided code in the question.
Using the command
gcc -E main.cpp|grep -v "^#" |grep -v "^$"
i extracted the following code (note that lines starting with # and empty lines were removed by my grep commands) :Yes, it does hurt the eye a bit...