Case 1:
int main(void)
{
 int printf(const char *n,...);
 int a=1;
 printf("abcdef");
return 0;
}
Compile using gcc, no warnings.
No <stdio.h> is included.  Compiler takes the local printf
declaration, and thus gives no warning.
<extern int printf (__const char *__restrict __format, ...)> is the
declaration in stdio.h, similar to your  local declaration. If you
declare two function twice, there is no issues.
So, no compile time issues. Now when the printf call is linked, it
links to glibc, where it finds the definition for printf, so
successful linkage as well.
And you get same output, as the printf defined in glibc is used only.

Case 2:

int main(void)
{
 //int printf(const char *n,...);   //commented
 int a=1;
 printf("abcdef");
return 0;
}

No <stdio.h> is present.
Compiling:
gcc check.c
check.c: In function ‘main’:
check.c:6: warning: incompatible implicit declaration of built-in
function ‘printf’

gcc -Wall check.c
check.c: In function ‘main’:
check.c:6: warning: implicit declaration of function ‘printf’
check.c:6: warning: incompatible implicit declaration of built-in
function ‘printf’

As in this case, compiler is not able to find any prototype (neither
is your local printf there, nor stdio.h is included).
So, it says using implicit declaration.

When linking links, with glibc, so same as previous case.

Case 3:

g++ check.c
check.c: In function ‘int main()’:
check.c:6: error: ‘printf’ was not declared in this scope

Gives error, as i told earlier.

Thanks
















On Sun, Feb 7, 2010 at 4:51 PM, Prashant Batra <[email protected]> wrote:
> On Sat, Feb 6, 2010 at 4:29 PM, rakesh kumar <[email protected]> wrote:
>> Hi all,
>>         Today while studying a book i just saw the following lines of code:
>> int main(void)
>> {
>>  int printf(const char *n,...);
> The local declaration(prototype of the function)
>>  int a=1;
>>  printf("abcdef");
> Calls the C library printf() function.at the time of linking.
>> return 0;
>> }c
>>
>>  and the output was: abcdef
>> When i tried to run this code, it ran well without any error and this was
>> asked in GATE exam also. I am confused, how it ran without <stdio.h>, when i
>> used printf() function and gave the same output as printf gives. Okay after
>> that i commented the third line "int printf(const char *n,....);", the
>> output didn't get affected. Now i am thinking when we can use printf()
>> function without any prototype or defining any header, then why do we need
>> to use #include<> in our program, because when i changed this program a bit
>> by substituting printf() with scanf() it worked fine like scanf() does
>> normally.
>>  Can anyone explain me,what is this?
> The C compiler is a bit lenient in syntactic analysis part for
> functions. It assumes a default declaration. And while linking links
> your printf() call to the library fucntion.But it should give
> warning(saying some implicit declaration).
> just try compiling with -Wall option set, you will get the warning.
> Also compile the code using g++, it will give error, saying prototype not 
> found.
>> Thanks
>> _______________________________________________
>> Ilugd mailing list
>> [email protected]
>> http://frodo.hserus.net/mailman/listinfo/ilugd
>>
>

_______________________________________________
Ilugd mailing list
[email protected]
http://frodo.hserus.net/mailman/listinfo/ilugd

Reply via email to