Sponsored Content
Top Forums Programming Ignoring the stdio.h file in a C file Post 302737565 by achenle on Thursday 29th of November 2012 07:57:26 AM
Old 11-29-2012
Quote:
Originally Posted by Corona688
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:
Code:
int open( const char *, int, ... )

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.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

wc of characters in a file ignoring white space

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)
Discussion started by: sachin.gangadha
1 Replies

2. UNIX for Dummies Questions & Answers

ignoring a dash in file name

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)
Discussion started by: LumpSum
5 Replies

3. Shell Programming and Scripting

ignoring blank line in a file

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)
Discussion started by: ali560045
4 Replies

4. Shell Programming and Scripting

How to extract a string from a file ignoring new line

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)
Discussion started by: suresh_kb211
3 Replies

5. Shell Programming and Scripting

ignoring lines in a file

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)
Discussion started by: thelakbe
2 Replies

6. Shell Programming and Scripting

Ignoring file name case and decrypting it.

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)
Discussion started by: anushree.a
6 Replies

7. Programming

FILE structure - stdio.h

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)
Discussion started by: nikunjbadjatya
5 Replies

8. Shell Programming and Scripting

using diff to on two file but ignoring the last comma separate value

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)
Discussion started by: wny201
1 Replies

9. Solaris

fatal error: stdio.h: No such file or directory

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)
Discussion started by: Fingerz
2 Replies

10. Shell Programming and Scripting

Ignoring lines and create new file

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
STDARG(3)						   BSD Library Functions Manual 						 STDARG(3)

NAME
stdarg, va_arg, va_copy, va_end, va_start -- variable argument lists SYNOPSIS
#include <stdarg.h> void va_start(va_list ap, last); type va_arg(va_list ap, type); void va_copy(va_list dest, va_list src); void va_end(va_list ap); 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(), va_end(), and, optionally, va_copy(). The va_start() macro initializes ap for subsequent use by va_arg(), va_copy() 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 function knows the type. Because the address of this parameter is used in the va_start() macro, it should not be declared as a register variable, or as a function or an array type. The va_start() macro returns no value. 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. 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 the type in question is one that gets promoted, the promoted type should be used as the argument to va_arg(). The following describes which types are promoted (and to what): - short is promoted to int - float is promoted to double - char is promoted to int The first use of the va_arg() macro after that of the va_start() macro returns the argument after last. Successive invocations return the values of the remaining arguments. The va_copy() macro makes dest a copy of src as if the va_start() macro had been applied to it followed by the same sequence of uses of the va_arg() macro as had previously been used to reach the present state of src. The va_copy() macro returns no value. The va_end() macro handles a normal return from the function whose variable argument list was initialized by va_start() or va_copy(). The va_end() macro returns no value. EXAMPLES
The function foo() takes a string of format characters and prints out the argument associated with each format character based on the type. void foo(char *fmt, ...) { va_list ap; int d, c; char *s; double f; 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 */ c = va_arg(ap, int); /* promoted */ printf("char %c ", c); break; case 'f': /* float */ f = va_arg(ap, double); /* promoted */ printf("float %f ", f); } va_end(ap); } 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>. STANDARDS
The va_start(), va_arg(), va_copy(), and va_end() macros conform to ISO/IEC 9899:1999 (``ISO C99''). HISTORY
The va_start(), va_arg() and va_end() macros were introduced in ANSI X3.159-1989 (``ANSI C89''). The va_copy() macro was introduced in ISO/IEC 9899:1999 (``ISO C99''). 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). BSD
August 18, 2002 BSD
All times are GMT -4. The time now is 03:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy