Standard C does not allow you to perform I/O. If you want to do this, you have to use software from C's standard library. However, if you look at the previous example C Programming on Linux, you will see that it has a printf statement but no functions from C's standard library. So, in order to illustrate this, I looked for another compiler and found this one: /usr/bin/c++ I used it to compile the same C program but it failed:
Linux > cat test.c
int main(void)
{
printf("Andrew was here\n");
return 0;
}
Linux > c++ test.c
test.c: In function `int main()':
test.c:3: error: `printf' was not declared in this scope
Linux >
I added a #include compiler directive at the start of the program. This tells the compiler to incorporate stdio.h in the program. Then I was able to compile and run the program with no errors:
Linux > cat test.c
#include <stdio.h>
int main(void)
{
printf("Andrew was here\n");
return 0;
}
Linux > c++ test.c
Linux > ./a.out
Andrew was here
Linux >
No comments:
Post a Comment