Ignoring the stdio.h file in a c file [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why #include <stdio.h> is *not* required to use printf()?
I am facing a problem in the below given code.
int main()
{
printf("\nHello Stack Overflow\n\n") ;
return 0 ;
}
In the above mentioned code i left including "#include ".
And if i compile and execute this piece of code, the output is printed as expected. But "#include " being the most important thing in a C program, i have ignored it and still the compilation is done without any errors but with warning.
Why is this happening?

In C, functions that are not declared are implicitly considered to return an int and to take int arguments.
This is bad practice and will bite you. For example if you want to print data not the same size of an int, like double or float.

From man gcc
-nostdlib
Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be
passed to the linker. The compiler may generate calls to "memcmp", "memset", "memcpy" and "memmove". These entries are usually
resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

printf symbol is not known at compile time, but libc in linked implicite into binary... and that is how it works.

Related

Without header(stdio.h) file, how can the following code run? [duplicate]

This question already has answers here:
Why is #include <stdio.h> not required to use printf()?
(3 answers)
Closed 5 years ago.
How this following code can run without including header file for printf function?
int main(){
printf("%d",1);
return 0;}
Note: This is a hand-wavy answer that will roughly be correct. Someone else who knows the gory details (for gcc, e.g.) may enlighten both of us. Here goes:
Because in C — at least for some compilers — implicitly defined functions are fine. So it compiles it, then hands it off to the linker. It sees a reference to printf, and since the linker by default links with the C runtime library, it will resolve that symbol to the correct function.
I guess an implicit function like that will get a default signature, typically expecting to return an int. As for the arguments to the function, those can't be type checked at compile time, because the compiler doesn't know what the actual function signature is. So it will just use standard calling convention, e.g. pass arguments by registers or something like that.

Why don't I have "undefined reference to"? [duplicate]

This question already has answers here:
Must declare function prototype in C? [duplicate]
(10 answers)
Closed 6 years ago.
I faced a similar problem in my (big) project.
//# include <string.h> // not included
void foo(char * str, const char * delim)
{
char * tok = strtok(str, delim);
// warning ^ "assignement makes pointer from integer without a cast"
// [...]
}
The answer (just add #include <string.h> to have the prototype of strtok) solved the issue indeed.
However, due to my poor compiler/linker knowledge, I fail to understand how the process accepts a function that have not been prototyped.
I'd rather expected the error undefined reference to function 'strtok' which is typical when you forget to include the right header.
[EDIT] I understand why this question has been marked as duplicate but I do think it is different : I am aware of "good practice" regarding includes, I am just wondering about compiler's behavior. However I admit that I can found (part of) an answer to my question in this post: Are prototypes required for all functions in C89, C90 or C99? or this one: Must declare function prototype in C?
While linking your binary, unless explicitly mentioned, your binary is linked with the default C standard library (glibc, for example) anyways, where the function is defined.
So, when you miss the header file containing the declaration, you end up with the warning (in case the function prototype has a mismatchnote) but during linking time, due to the presence of the default C library, the program is successfully linked anyway.
FWIW, according to C11, support for implicit function declaration has been dropped but most compilers support the bad behavior to keep the backward compatibility for the legacy code.
NOTE:
Implicit function declaration: Earlier, (before C99, AFAIK), the standard did not mandate for a function to have the forward declaration. In case, a function would have been used without a forward declaration, it was assumed to return int and accept any number of incoming parameters.
Because gcc automatically links your code with the C library. The "undefined reference to function" error is typically issued by the linker when it couldn't resolve a symbol and that only occurs if the symbol couldn't be found in any of the libraries linked (the order of linking might also matter). But the C library is linked by default -- it's as if you linked it with -lc. So, you don't get that error.
If you tell gcc to not link the C library using -nostdlib then you'll see the error you expected:
$ gcc -nostdlib file.c
On the other hand, you should always provide prototypes for functions.
You may be interested in other similar linker options such as -nodefaultlibs, nostartfiles etc which you can find in gcc's manual.

Why am I able to use isspace() without including the required header? [duplicate]

This question already has answers here:
Why doesn’t putchar require a header?
(2 answers)
Closed 7 years ago.
Why does this code work:
#include <stdio.h>
int main()
{
int x = isspace(getchar());
printf("%d", x);
return 0;
}
Whenever I enter whitespace isspace() returns 8 and when I don't, it gives 0.
Shouldn't this produce an error at compile time? I didn't add #include <ctype.h> at the top. So why this is allowed?
You're seeing this because your compiler (sadly, still) supports implicit declaration of a function.
If you enable strict checking, you compiler should refuse to compile the code. On and above C99, the implicit function declaration has been made non-standard. (To add, hopefully, future versions of compiler will strictly disallow this, by default.)
Sometimes a specific implementation of the C Standard Libraries will support one header by including another itself: in your case, stdio.h may be including - either directly or indirectly - the isspace declaration, which might itself be directly in ctype.h or in some other file that ctype.h would include. You could test for this by executing just your preprocessing stage, as in...
gcc -E myprog.c | grep isspace
Another thing that can happen is that compilers hardcode implementations of common functions, like say strlen, so they can e.g. perform it at compile time on string literals.
Despite it "working", you would ideally directly include the headers you'll need, so if another compiler/implementation - or a later release of the same - doesn't have the same quirks your code will still compile.

Using printf function without actually importing stdio.h and it worked?! Why is that so? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why #include <stdio.h> is not required to use printf()?
//#include <stdio.h>
//#include <conio.h>
main(){
printf("Hi");
getch();
}
As I was programming this, it shocked me that it worked without actually importing any c libraries such as stdio that contains the printf function. Why is that so? (Used Dev-C++ 4.9.9.2, saved as .c, not .cpp)
C allows you to call functions without first defining the prototypes. (C++ does not do this.) An implicit prototype for printf will be defined as follows:
int printf();
Coincidentally, the calling conventions for this implicit prototype matched the actual calling conventions for printf on your platform.
In general, you cannot count on this working, and there are a large number of cases where it won't work. I recommend enabling compiler warnings to detect implicit prototype declarations so you can fix them (by including the correct header).
Footnote: #include does not import libraries, it merely pastes files into your source code at compile time. The <stdio.h> header contains (directly or indirectly) certain prototypes, but the library has to be linked in separately. Since printf is usually in a library that is linked to programs by default, you usually don't have to do anything to use printf.
The #include preprocessor directive does not import any library; there is no such notion of import in the definition of the C language.
You just happen to call a function named printf and the standard C library (e.g. libc.so on Linux, I don't know how Windows call it) happens to be linked in by default. Since you call a function by a name known to that library, it gets linked, and is being called at runtime.
However, you should enable all warnings in you compiler, and it would warn that you call an undeclared function.

Why do we include stdlib.h?

C function malloc() is defined under stdlib.h.
It should give an error if we don't include this file, but this code works fine with a little warning.
My question is, if malloc() works without this header file, then why do we need to include it? Please help clarify my concepts.
# include <stdio.h>
int main()
{
int a, b, *p;
p = (int*)malloc(sizeof(int)*5);
for(a=0;a<5;a++)p[a]=a*9;
for(b=0;b<5;b++)printf("%d ",p[b]);
}
In C unfortunately you don't need pre-declaration for functions. If the compiler encounters with a new function it will create an implicit declaration for it ("mmm`kay, this how it is used so I will assume that the type of the arguments are..").
Do not rely on this "feature" and in general do not write code that compiles with warnings.
Read the warning. It says it's invalid. The compiler is simply too kind to you. In Clang this works, but it might not in other compilers.
At least include it to suppress the warning. Unnecessary warnings are annoying. Any program should compile with warnings treated as errors (I always enable that).
It appears that that's your compiler's magic. Not including the necessary headers may work on your compiler (which I suppose is by Microsoft), but it won't necessarily compile elsewhere(that includes future versions of the same compiler). Write standard-conforming, portable code.
stdlib.h is of the general purpose standard header which includes functions of Dynamic Memory allocation and other Standard Functions.
For example if you want to display a message at the end of the execution of your program you will need to go for the getch() function,this functions reads a character from keyboard thus giving user the time to read the displayed Information.
The getch() function requires the stdlib header to be Included.
Like many things in c the reason that an error isn't generated when there is no prototype is for historical reasons. In the early days people often didn't bother prototyping functions because pointers and integers were usually the same size and integral types smaller than an integer were promoted to an integer when passed as a parameter (and floating point was rarely used for systems programming).
If at any point they had changed the compiler to give an error if a function was not prototyped then it would have broken many programs and would not have gained widespread acceptance.
With 64 bit addressing we are now entering a period when integers and pointers are not the same size and programs will most likely break if you do not prototype functions like malloc() that return a pointer.
In gcc always set the following options for your own programs: -Werror -Wstrict-prototypes

Resources