473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The C Prgramming Language(Book)

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.
Nov 14 '05 #1
8 2733
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)
{
<...>
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2
gnosis <lz***@mindless.com> wrote:
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

--
Fabian Kurz, DJ1YFK * http://fkurz.net/
Nov 14 '05 #3
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
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.)
--
Just another C hacker.
Nov 14 '05 #4
gnosis wrote:
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 ----
Nov 14 '05 #5
Fabian Kurz wrote on 26/07/04 :
And for the definition: int getline(char s[], int lim). <...>

<...>
int getline(char, int);


Of course you meant

int getline (char *, int);

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #6
kal
Karthik <ka********************@yahoo.com> wrote in message news:<41057626$1@darkstar>...
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"?
Nov 14 '05 #7

On Tue, 27 Jul 2004, kal wrote:

Karthik <ka********************@yahoo.com> wrote...
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.


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.

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"?


Both. :)

-Arthur
Nov 14 '05 #8
kal
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix44. andrew.cmu.edu>...
On Tue, 27 Jul 2004, kal wrote:

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.


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."
Shouldn't it be "parameters" instead of "args"?


Both. :)


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
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sooha Park Lee | last post by:
I will appreciate it if you let me know where. Thank you. -S
12
by: Guido Mureddu | last post by:
Hello, I'm a student in electronic engineering. I do know you've seen and answered this sort of topic/request countless times, but I haven't found past threads as helpful as I had hoped, and...
6
by: RoSsIaCrIiLoIA | last post by:
d_0=32.000000 d_1=38.000000 Test success d_0=34.000000 d_1=42.000000 Test success d_0=1.000000 d_1=0.000000 Test success
6
by: Zachary Turner | last post by:
What is the most comprehensive, under the hood, detailed, deep down book on C# available? I come from a C++ background but want to get up to speed on C# really quickly. I dont' want something...
15
by: A.M | last post by:
Hi, I am looking for an in-depth book for C#. In Java world, we have thinking in java which is very good. What would be the best book to learn in depth aspects of C#? Thanks, Ali
6
by: Jon Shemitz | last post by:
My 16 yo son is ready to move beyond level editing and "Multimedia Fusion." He's going to ignore his Mom's suggestion of Visual Basic, and take my suggestion of starting with C#. Now, I actually...
9
by: arnuld | last post by:
hai folks, well, it's me again and again, i am asking questions. anyway, with your help i have finalised that i will start learning c++ (as you told me that i do not need to know any OO language...
22
by: Jon Skeet [C# MVP] | last post by:
I'm looking to write a C# book fairly soon, and the publisher I've approached wants me to do a bit of market research to find out what people like and don't like in this kind of book. I've read...
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.