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.
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)
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)
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)
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)
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)
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 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)
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)
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)
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)
Discussion started by: callmatkarna
4 Replies
LEARN ABOUT REDHAT
stdarg
STDARG(3) Linux Programmer's Manual STDARG(3)NAME
stdarg - variable argument lists
SYNOPSIS
#include <stdarg.h>
void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);
DESCRIPTION
A function may be called with a varying number of arguments of varying types. The include file stdarg.h declares a type va_list and
defines three macros for stepping through a list of arguments whose number and types are not known to the called function.
The called function must declare an object of type va_list which is used by the macros va_start, va_arg, and va_end.
va_start
The va_start macro initializes ap for subsequent use by va_arg and va_end, and must be called first.
The parameter last is the name of the last parameter before the variable argument list, i.e., the last parameter of which the calling func-
tion knows the type.
Because the address of this parameter may be used in the va_start macro, it should not be declared as a register variable, or as a function
or an array type.
va_arg
The va_arg macro expands to an expression that has the type and value of the next argument in the call. The parameter ap is the va_list ap
initialized by va_start. Each call to va_arg modifies ap so that the next call returns the next argument. The parameter type is a type
name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to type.
The first use of the va_arg macro after that of the va_start macro returns the argument after last. Successive invocations return the val-
ues of the remaining arguments.
If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default
argument promotions), random errors will occur.
If ap is passed to a function that uses va_arg(ap,type) then the value of ap is undefined after the return of that function.
va_end
Each invocation of va_start must be matched by a corresponding invocation of va_end in the same function. After the call va_end(ap) the
variable ap is undefined. Multiple transversals of the list, each bracketed by va_start and va_end are possible. va_end may be a macro or
a function.
va_copy
An obvious implementation would have a va_list a pointer to the stack frame of the variadic function. In such a setup (by far the most
common) there seems nothing against an assignment
va_list aq = ap;
Unfortunately, there are also systems that make it an array of pointers (of length 1), and there one needs
va_list aq;
*aq = *ap;
Finally, on systems where parameters are passed in registers, it may be necessary for va_start to allocate memory, store the parameters
there, and also an indication of which parameter is next, so that va_arg can step through the list. Now va_end can free the allocated mem-
ory again. To accommodate this situation, C99 adds a macro va_copy, so that the above assignment can be replaced by
va_list aq;
va_copy(aq, ap);
...
va_end(aq);
Each invocation of va_copy must be matched by a corresponding invocation of va_end in the same function. Some systems that do not supply
va_copy have __va_copy instead, since that was the name used in the draft proposal.
EXAMPLES
The function foo takes a string of format characters and prints out the argument associated with each format character based on the type.
#include <stdio.h>
#include <stdarg.h>
void foo(char *fmt, ...) {
va_list ap;
int d;
char c, *p, *s;
va_start(ap, fmt);
while (*fmt)
switch(*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s
", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d
", d);
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c
", c);
break;
}
va_end(ap);
}
CONFORMING TO
The va_start, va_arg, and va_end macros conform to ANSI X3.159-1989 (``C89''). C99 defines the va_copy macro.
COMPATIBILITY
These macros are not compatible with the historic macros they replace. A backward compatible version can be found in the include file
varargs.h.
COMPARISON
The historic setup is:
#include <varargs.h>
void foo(va_alist) va_dcl {
va_list ap;
va_start(ap);
while(...) {
...
x = va_arg(ap, type);
...
}
va_end(ap);
}
On some systems, va_end contains a closing '}' matching a '{' in va_start, so that both macros must occur in the same function, and in a
way that allows this.
BUGS
Unlike the varargs macros, the stdarg macros do not permit programmers to code a function with no fixed arguments. This problem generates
work mainly when converting varargs code to stdarg code, but it also creates difficulties for variadic functions that wish to pass all of
their arguments on to a function that takes a va_list argument, such as vfprintf(3).
2001-10-14 STDARG(3)