prototypes

U

Uno

Why did the commented-out prototype for printf not work?

$ gcc -Wall -Wextra u3.c -o out
$ ./out
42
$ cat u3.c

// int printf(const char *restrict format, ...);
int printf(char *, ...);

int main (void)
{

printf("42\n");
return 0;
}


// gcc -Wall -Wextra -dD -E u3.c >text1.txt
// gcc -Wall -Wextra u3.c -o out
$

Compiler complaints before I changed the prototype to something I
understood were these:

$ gcc -Wall -Wextra u3.c -o out
u3.c:2: error: expected ‘;’, ‘,’ or ‘)’ before ‘format’
u3.c: In function ‘main’:
u3.c:7: warning: implicit declaration of function ‘printf’
u3.c:7: warning: incompatible implicit declaration of built-in function
‘printf’
 
J

Jens Thoms Toerring

Uno said:
Why did the commented-out prototype for printf not work?
$ gcc -Wall -Wextra u3.c -o out
$ ./out
42
$ cat u3.c
// int printf(const char *restrict format, ...);
int printf(char *, ...);
int main (void)
{
printf("42\n");
return 0;
}

// gcc -Wall -Wextra -dD -E u3.c >text1.txt
// gcc -Wall -Wextra u3.c -o out
$
Compiler complaints before I changed the prototype to something I
understood were these:
$ gcc -Wall -Wextra u3.c -o out
u3.c:2: error: expected ‘;’, ‘,’ or ‘)’ before ‘format’
u3.c: In function ‘main’:
u3.c:7: warning: implicit declaration of function ‘printf’
u3.c:7: warning: incompatible implicit declaration of built-in function
‘printf’

'restrict' is a keyword introduced with C99 but gcc defaults
to C89 (or it's own language that's nearer to C89 than C99)
unless told otherwise and then, of course, doesn't recognizes
'restrict' as a keyword but treats it as a plain variable name.
Try it again with

gcc -std=c99 -pedantic -Wall -Wextra ...

to get the best C99 conformance gcc can manage (and avoid using
'-ansi' since it's an alias for '-std=c89').

Regards, Jens
 
A

Andrey Tarasevich

Compiler complaints before I changed the prototype to something I
understood were these:

$ gcc -Wall -Wextra u3.c -o out
u3.c:2: error: expected ‘;’, ‘,’ or ‘)’ before ‘format’
u3.c: In function ‘main’:
u3.c:7: warning: implicit declaration of function ‘printf’
u3.c:7: warning: incompatible implicit declaration of built-in function
‘printf’

Your are compiling in C89/90 mode. C89/90 does not have `restrict`
specifier. In C89/90 mode GCC supported restrict-like declaration
specifier as an extension, but the corresponding keyword is spelled as
either `__restrict__` or alternatively `__restrict`. The standard
`restrict` keyword is only supported in C99 mode. So, either compile in
C99 mode or use the above non-standard GCC-specific keywords.
 
K

Keith Thompson

Andrey Tarasevich said:
Your are compiling in C89/90 mode. C89/90 does not have `restrict`
specifier. In C89/90 mode GCC supported restrict-like declaration
specifier as an extension, but the corresponding keyword is spelled as
either `__restrict__` or alternatively `__restrict`. The standard
`restrict` keyword is only supported in C99 mode. So, either compile in
C99 mode or use the above non-standard GCC-specific keywords.

Or, far better, just use "#include <stdio.h>" and let the implementation
worry about providing the appropriate prototype for printf.

There's nothing wrong with messing around with writing your own
declarations for predefined functions if the purpose is to explore the
language and the implementation, but it's almost never a good idea
in real code.
 
U

Uno

Andrey said:
Your are compiling in C89/90 mode. C89/90 does not have `restrict`
specifier. In C89/90 mode GCC supported restrict-like declaration
specifier as an extension, but the corresponding keyword is spelled as
either `__restrict__` or alternatively `__restrict`. The standard
`restrict` keyword is only supported in C99 mode. So, either compile in
C99 mode or use the above non-standard GCC-specific keywords.

Thanks all for replies. You seem to be exactly right:

$ cc -pedantic -Wall -Wextra u3.c -o out
u3.c:2:1: warning: C++ style comments are not allowed in ISO C90
u3.c:2:1: warning: (this will be reported only once per input file)
$ ./out
42
$ cat u3.c
int printf(const char * __restrict format, ...);
// int printf(const char *restrict format, ...);
// int printf(char *, ...);

int main (void)
{
printf("42\n");
return 0;
}


// cc -std=c99 -pedantic -Wall -Wextra -dD -E u3.c >text1.txt
// cc -std=c99 -pedantic -Wall -Wextra u3.c -o out
// cc -pedantic -Wall -Wextra u3.c -o out
$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top