The C Prgramming Language(Book)

G

gnosis

Whenever the getline() function is used in a program, the declaration
is different than the definition. Declaration: int getline(char
line[], int max);
And for the definition: int getline(char s[], int lim). Can you please
tell me why this is like this? Thank You.
 
E

Emmanuel Delahaye

gnosis wrote on 26/07/04 :
Whenever the getline() function is used in a program, the declaration

First of all, getline() is not a standard function.
is different than the definition. Declaration: int getline(char
line[], int max);
And for the definition: int getline(char s[], int lim). Can you please
tell me why this is like this? Thank You.

The name of the parameter in the separated prototype (what you call the
'declaration') are meaningless and actually optional.

int getline (char*, int);

is also a valid prototype for the same function.

That said, IMO, it would be nuts to not to have names in the
prototypes, or names different than the ones of the definition, for
obvious clarity and self documenting reason.

I would have done:

/* protoype */
int getline (char line[], int max);

/* definition */
int getline (char line[], int max)
{
<...>
}
 
F

Fabian Kurz

gnosis said:
Whenever the getline() function is used in a program, the declaration
is different than the definition. Declaration: int getline(char
line[], int max);
And for the definition: int getline(char s[], int lim). Can you please
tell me why this is like this? Thank You.

It's not neccessary to use the same parameter names in the function
prototype and in the function itself, atually in the prototype function
header the parameter names are optional, so

int getline(char, int);

would work just as well (although not recommended since it is difficult
to find out quickly what the parameter stands for). This is described at
the end of Section 1.7 in K&R2.

HTH,
Fabian
 
B

Ben Pfaff

Emmanuel Delahaye said:
That said, IMO, it would be nuts to not to have names in the
prototypes, or names different than the ones of the definition, for
obvious clarity and self documenting reason.

There are sometimes good (in my opinion) reasons to have
different names in prototypes and definitions, or to omit
parameter names from prototypes. In public header files, for
example, it is necessary to give parameter names in prototypes
the same prefix that other identifiers in the library use;
otherwise a stray macro can wreak havoc. That means that it's
easier not to use names at all. Indeed, fairly often the
function name and the parameter types make it obvious how the
arguments will be used. How much more do you think you would
learn from the following prototype if I added parameter names?
void copy_bytes (void *, const void *, size_t);

Another case where I might use different names is in functions
where the first action is to convert an argument to a different
pointer type, as in a callback function passed to bsearch() or
qsort():

int compare_foos (const void *a, const void *b);

....

int compare_foos (const void *a_, const void *b_)
{
const foo *a = a_;
const foo *b = b_;
return a->key < b->key ? -1 : a->key > b->key;
}

The trailing underscore is my own personal convention for this
kind of thing. But in my opinion it would be equally reasonable
to just leave off the name entirely in the prototype.

(I'm aware that my definition here includes a prototype. When I
say "prototype" in this article I mean a prototype separate from
a definition.)
 
K

Karthik

gnosis said:
Whenever the getline() function is used in a program, the declaration
is different than the definition. Declaration: int getline(char
line[], int max);
And for the definition: int getline(char s[], int lim). Can you please
tell me why this is like this? Thank You.

When you talk about functions, you are referring to the prototypes as
far as C is concerned.
C does not use 'call by name' as according to PL theory books.
rather, it uses call by value. The corollary is that you really don't
need to worry about the symbolic names, but just be concerned about
function prototypes - order and types of args and type of the function (
return type that is).

HTH

- Karthik.
------------ And now a word from our sponsor ---------------------
For a secure high performance FTP using SSL/TLS encryption
upgrade to SurgeFTP
---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----
 
K

kal

Karthik said:
When you talk about functions, you are referring to the
prototypes as far as C is concerned.

Not quite.
C does not use 'call by name' as according to PL theory
books. rather, it uses call by value.

This may be true after a fashion, but...

1. Call By Name - Macros.
2. Call By Value - Functions.
3. Call By Reference - Simulated using pointers.
The corollary is that you really don't need to worry about
the symbolic names,

Odd use of the word "corollary."
but just be concerned about function prototypes - order and
types of args and type of the function (return type that is).

Shouldn't it be "parameters" instead of "args"?
 
A

Arthur J. O'Dwyer

This may be true after a fashion, but...

1. Call By Name - Macros.

Nope. Macros in C might be called "Call by Text," but
"Call by Name" they most certainly are not. Consider
(this macro is evil, BTW)

#define stcmp(x,o,y) (strcmp(x,y) o 0)

as an example of something macros do that call-by-name does not.

Shouldn't it be "parameters" instead of "args"?

Both. :)

-Arthur
 
K

kal

Arthur J. O'Dwyer said:
Nope. Macros in C might be called "Call by Text," but
"Call by Name" they most certainly are not. Consider
(this macro is evil, BTW)

#define stcmp(x,o,y) (strcmp(x,y) o 0)

as an example of something macros do that call-by-name does not.

Merci beaucoup. I think you are right. I still haven't grasped
it fully. I will have to ponder that some more.

What I need is the book "Theory of Programming for Dummies."

My understanding is that they mean somewhat different things.
Appended herewith are some definitions from the C99 standard
(just so M. Bos will be happy.)

3.3
1 argument
actual argument
actual parameter (deprecated)
expression in the comma-separated list bounded by the parentheses
in a function call expression, or a sequence of preprocessing
tokens in the comma-separated list bounded by the parentheses in
a function-like macro invocation

3.15
1 parameter
formal parameter
formal argument (deprecated)
object declared as part of a function declaration or definition
that acquires a value on entry to the function, or an identifier
from the comma-separated list bounded by the parentheses
immediately following the macro name in a function-like macro
definition
 

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

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top