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.
In C++, this is explicitly an error. No undefined functions allowed, period.
C on the other hand, will assume undefined functions take integers and return integers. int printf(int); This works okay-ish if your function takes nothing but 32-bit sized types -- a reasonable guess on a 32-bit system, potentially disastrous anywhere else. If you tried to declare a FILE * or anything else which really needs stdio.h, it would fail to compile.
On 64-bit, your program will segfault due to the 64-bit pointer being mangled through a 32-bit type. The compiler will warn you, too, though the error is less than obvious -- "truncation from assigned type" or some such, rather than the kind of message you'd expect, "you're putting the wrong type into an undefined function and it will blow up in your face".
In short, don't do that. I've seen lots of code blow up when ported to 64-bit because people didn't bother including something they needed.
Last edited by Corona688; 11-28-2012 at 11:30 AM..
If you're using gcc, the generated code is probably not even calling printf. Given a format string without conversion specifiers (e.g. %s) which ends with a newline, a builtin optimization has likely replaced printf with puts.
Everything that Corona explained still applies to puts' pointer argument.
Also, if you're using GCC, printf() is a built-in function - though you should get a warning about "incompatible declaration of built-in function printf()" or similar, since your use of the function gives the implicit declaration mentioned in the previous posts.
In C++, this is explicitly an error. No undefined functions allowed, period.
C on the other hand, will assume undefined functions take integers and return integers. int printf(int); This works okay-ish if your function takes nothing but 32-bit sized types -- a reasonable guess on a 32-bit system, potentially disastrous anywhere else. If you tried to declare a FILE * or anything else which really needs stdio.h, it would fail to compile.
On 64-bit, your program will segfault due to the 64-bit pointer being mangled through a 32-bit type. The compiler will warn you, too, though the error is less than obvious -- "truncation from assigned type" or some such, rather than the kind of message you'd expect, "you're putting the wrong type into an undefined function and it will blow up in your face".
In short, don't do that. I've seen lots of code blow up when ported to 64-bit because people didn't bother including something they needed.
IIRC, and to be pedantic, the arguments to undeclared functions in C are subject to argument promotion, where all arguments are promoted to the same size. What that distinct size is defined to be is probably one of those "implementation specific" details that break things when code violates the C standard. Like here.
Interestingly (at least to me....), if you don't declare a function in C, the source code for that function has to be implemented using the old K&R style so the arguments are "unpromoted" in the actual function call. With K&R C code, you could pass as many arguments as you wanted to any function call, and in the function itself you could actually use as many or as few as you wanted. That's why the prototype for open() today is:
With K&R C, the open() implementation could refer to the mode argument if needed, and ignore it if not. And woe betide anyone who called open() without the mode argument when the other args meant it was required.
As you imply, if code calling ANSI C functions works without prototypes, it's due to luck.
So yes, don't do that.
I agree with John Graham's recommendation to always treat warnings as errors. If the programmers who wrote the tool that's converting your source code to an executable binary think there are problems with your code even though it's syntactically correct, they're almost certainly correct. Because they know what their tool is doing with your source code, and you really don't. And if the guys who know what's going on think what you're doing is sketchy, it's probably a really good idea to listen to them. Especially when you consider that they didn't have to put that warning in - they put in extra effort to give you that warning.
Also, if you're using GCC, printf() is a built-in function
The whole thing? That'd be an awful big built-in. It can probably turn it into a built-in for certain basic circumstances but there's a reason it's in libc...
Hello,
I have a requirement to ignore few lines in a file before keyword FILEHEADER . As soon as there is keyword FILEHEADER is identified in file , it will form another file with data from FILEHEADER to whatever in file after FILEHEADER.
I wrote
filename=$1
awk... (4 Replies)
Trying to compile a C program recievin this
hello.c:1:19: fatal error: stdio.h: No such file or directory
gcc is installed on the system.
echo $PATH
/usr/bin:/usr/sbin:/usr/gcc/4.5/include/c++/4.5.2/tr1
root@Sol11swtb01:/media/NO NAME/Programming/C/Testing# cd... (2 Replies)
Hi guys
I have two file which I sdiff.
ie
file 1: AA,12,34,56,,789,101,,6666
file 2: AA,12,34,56,,789,101,,7777
The last comma separated value will always change from one day to the next.
Is there another unix utility I can use that will sdiff two files but ignore the last comma... (1 Reply)
Hi All,
I am new to linux and Programming.
Inside the file stdio.h, there is a description about FILE structure. Which has many internal data members like _p, _r, _flags etc.
I have written a sample code to find out the contents of the FILE structure.
It opens a sample file ( FILE *fp ),... (5 Replies)
Dear Friends,
I want to decrypt 2 different file types in a folder (ZIP files and GPG files).
Each file type need different decryption syntex.
Hence, the script should identify file type and should act accordingly ignoring file name case i.e. upper or lower case.
Also, the extention can be... (6 Replies)
HI,
command to cat a readable file by ignoring the first line and last line
or command to cat a readable file by ignoring the lines with delimiter
Please advise on this. (2 Replies)
Hi,
sumdays before i had posted a query with same subject.
i got sum great help from great ppl which solved my problem then.
But now there is a small problem with the code that i need the experts help upon.
for parsing a text
like this
where $ had been the delimiter between... (3 Replies)
i have a file called Cleaner1.log . This files have some blank lines also.My requirement is that it should ignore the blank lines and give me the lines that contain some data.
I m using this logic in a script:
below the contents of file :
Maximum Time Taken for Processing(Failed) RR... (4 Replies)
so i have a simple file called -x and i need it renamed to x
now i dont understand why when using the most basic methods, only the code mv ./-x x changes the file name while using any other type of escape characters around the dash, such as single/double quotations or backslash, doesnt.
... (5 Replies)
Hi everyone,
$ more abcdefg.ksh
abcdef
alpha beta gamma
abcdef
abcdef
lmnop
$ wc sachin1.ksh
5 7 132 abcdefg.ksh
if you see it shows that file has got 240 characters. I actually want to count how many characters... (1 Reply)