Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why strlen function works without #include<string.h>?

Quick question:

strlen[char*] works perfectly regardless whether I #include <string.h> or not

All I get from compiler is a warning about implicit declaration, but functionally it works as intended.

Why is that?

like image 727
James Raitsev Avatar asked Dec 07 '22 22:12

James Raitsev


2 Answers

When you invoke undefined behavior, one possible behavior is that the program behaves as you expected it to, one your system, and with the current version of libraries and system software you have installed. This does not mean it's okay to do this. Actually a correct C99 compiler should not allow implicit function declarations; it should give you an error.

like image 87
R.. GitHub STOP HELPING ICE Avatar answered Dec 28 '22 02:12

R.. GitHub STOP HELPING ICE


The function prototypes in C are not compulsory. They're useful indications to the compiler so that it can do type checking on types which are passed into them. When you don't include string.h, a default signature is assumed for the function which is why you get the warning.

like image 32
Noufal Ibrahim Avatar answered Dec 28 '22 00:12

Noufal Ibrahim