Re: It works without stdio.h !!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



You did not say if this is a C or C++ (.c or .cpp) program. For backward compatibility
with K&R C, most C compilers will assume that a function that has no prototype is an
extern int, and since you only specify functions that live in the C library, an unresolved
extern is resolved against the library.

Of course, you could have written:
printf(3, 17);
and it would compile, but wouldn't run terribly well. Similarly, scanf(3) would have
compiled, as well as strcpy(a, b).

By the way, this contains approximately one grotesque programming example per line, so in
and of itself, even if stdio.h had been included, it is a horrendous example of
programming style. I hope none of this represents anything you would actually code.

int main(void)

Actually, main takes two parameters, argc and argv. So you are already in violation of
correct programming.

int a = 1, b;
never use a comma in a declaration list. It makes the code hard to read.

Do not use integer values if you mean to use them as boolean values.

char t[10];

never declare a variable of type char unless you well and truly believe you need to be
working in 8-bit characters, and this is a prime example of a case where that is
inappropriate. Never declare a buffer of fixed size on the stack if you plan to use
strcpy, strcat, sprintf etc. with it. In fact, in general, never declare a buffer of
fixed size on the stack. In fact, if you need strings, don't use C. Use C++, and use
std::string or CString. In fact, don't use C.

printf(a ? "one \n" : "zero \n");

of course this is silly, because a could have any nonzero value and you would claim it had
the value 1. Probably caused by the confusion of using an int to mean a boolean value, a
horrible example from K&R C of the 1970s. It is now 2009. This code was obsolete as
written had it been written in 1975. It is a joke today.

I presume there is a solid reason for putting the space before the \n in both cases.

scanf...

Besides using 8-bit characters, this is a horrible way to get a value converted. For
example, you could type "a" and it would tell you "0", not give you an error that the
input is illegal. scanf is never appropriate if you care about correctness. Since you
should always be concerned about correctness, scanf is never appropriate.

printf...

same problem of using obsolete char datatype.

strcpy

Anyone today who writes strcpy needs serious help. Writing strcpy to a stack variable of
fixed size is a firable offense in some companies. How is it you know the destination is
large enough to hold the source?

printf...

Besides using an obsolete character type, why the extra space before the \n?

As you can see, I hope this was a little test-of-concept program and bears no relationship
to anything you would code in real life.
joe

On Sat, 7 Mar 2009 05:54:03 -0800 (PST), karthikbalaguru <karthikbalaguru79@xxxxxxxxx>
wrote:

Hi ,

I find that the below program works without the inclusion
of the stanard input/output library.
I understand that we mandatorily need a valid prototype in scope for
functions like printf to avoid undefined behaviour and
that can be done through #include <stdio.h> only.
But, the below code runs perfectly well without those
statment. Strange !!

int main(void)
{
int a = 1,b;
char t[10];
printf(a ?"one \n" : "zero \n") ;
scanf("%d",&b);
printf("b = %d \n",b);
strcpy(t,"Hello");
printf("%s \n",t);
return 0;
}

It works without the famous #include<stdio.h> .
Strange ? How is it possible ?

I use Visual C++ 2008 express edition for C compilation
and debugging.

Any ideas ?

Thx in advans,
Karthik Balaguru
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: K&R-Style Function Declarations: Good or Bad?
    ... > short parameters getting promoted to int. ... all pre-ANSI compilers widened char and short ... declarations -- some compilers converted the widened types back to the ...
    (comp.lang.c)
  • Re: John McCarthy died today.
    ... safe linkage won't let an int call go to an ... int (int, char *, char *) function. ... That's the real reason for doing this: portability. ... I believe that C compilers ...
    (comp.lang.c)
  • Re: APPEND Option not recognized
    ... >> I remember when this error popped up I was trying to convince some very ... > at least some compilers where that is true. ... > C to assume char is 8 bits. ... In the first Cray C compiler, short, int, and long were all 64 ...
    (comp.lang.fortran)
  • Re: K&R2 , section 5.4
    ... /* A string comparison function from K&R2 ... int mainor int main(int argc, char *argv) are better style. ... strcpy(s1, "s"); ...
    (comp.lang.c)
  • RipperX filename/title patch
    ... char * row, *mark; ... int s, d, n, pass, totlen = 0; ... strcpy(buffer1, ""); ...
    (Ubuntu)