[comp.lang.c] C declaration styles

ajmyrvold@violet.waterloo.edu (Alan Myrvold) (03/15/88)

I need help porting C programs around.

When I write my C functions on my personal computer, my declarations
are often like this :

          void foo(int x, double y)
          {
              /* Body of function */
          }

When I want to run my programs on UNIX, my declarations need to be
like this :

          #define void
          void foo(x,y)
          int x;
          double y;
          {
              /* Body of function */
          }

Does anyone have a C program to convert from the one style of 
declarations to the other ??

Please EMAIL responses to me at ajmyrvold@violet.waterloo.edu
and I will summarize.


-------------------------------------------------------------------
What? You were in the next room all this time?
I would have just come over to see you!
-------------------------------------------------------------------
Alan Myrvold     ajmyrvold@violet.waterloo.edu
-------------------------------------------------------------------

coleman@sask.UUCP (Geoff Coleman @ College of Engineering) (03/18/88)

> Xref: sask comp.lang.c:6877 comp.sys.ibm.pc:10098
> I need help porting C programs around.
> 
> When I write my C functions on my personal computer, my declarations
> are often like this :
> 
>           void foo(int x, double y)

	Could you please tell us what kind of PC (if not an MS-DOS)
and what kind of compiler you ate using. The above isn't Lattice
or Microsoft and I would be suprised if it is Turbo.


Geoff Coleman                         | BITNET: Coleman@sask
College of Engineering                | UUCP: {utcsri,ihnp4}!sask!skul!geoff
University of Saskatchewan            | Compserve: 76515,1513  just a number 
Saskatoon, Saskatchewan               | voice: (306) 966-5415

	"Why does a hearse horse snicker, hauling a lawyer away?"
						- Carl Sandburg

hwfe@ur-tut (Harlan Feinstein) (03/22/88)

In article <1065@sask.UUCP> coleman@sask.UUCP (Geoff Coleman @ College of Engineering) writes:
>> 
>>           void foo(int x, double y)
>
>	Could you please tell us what kind of PC (if not an MS-DOS)
>and what kind of compiler you ate using. The above isn't Lattice
>or Microsoft and I would be suprised if it is Turbo.
>

Sure enough, you shall be surprised.  That's Turbo C syntax.  It will also
accept the more commonly acceptable method of argument syntax, but you have
the option of doing it the above way.  I use the style illustrated here, as
it's easier for me to remember, having done most of my C work with Turbo C.

Disclaimer:
If what I say seems wrong or offends you, consider this: I'm writing in my
own language, not English, and it's coincidence that it looks like English.
 
------------------------------------------------------------------------------
          Exiting the universe without a passport and other offenses
    punishable by death can be appealed on the second Tuesday of each week.
 
Harlan Feinstein                       hwfeccss@uorvm.bitnet
Student, University of Rochester
------------------------------------------------------------------------------

ajmyrvold@violet.waterloo.edu (Alan Myrvold) (03/22/88)

In article <1065@sask.UUCP> coleman@sask.UUCP 
(Geoff Coleman @ College of Engineering) writes:

> Xref: sask comp.lang.c:6877 comp.sys.ibm.pc:10098 
+ (ajmyrvold@violet.waterloo.edu)
>> I need help porting C programs around.
>> 
>> When I write my C functions on my personal computer, my declarations
>> are often like this :
>> 
>>           void foo(int x, double y)
>
>	Could you please tell us what kind of PC (if not an MS-DOS)
>and what kind of compiler you ate using. The above isn't Lattice
>or Microsoft and I would be suprised if it is Turbo.
>

Sorry for the omission - I assumed that most people would recognize 
the above as ANSI standard C.
I am using Turbo C version 1.0 on an IBM XT under PC DOS 3.3. 
Turbo C supports both ANSI and K&R.

My other mistake was assuming that most people would recognize the
advantage of the above prototype over K&R C - It lets the compiler
do type checking and coerce the arguments of a function call to
proper type if necessary. Unfortunately, the most desirable 
style is not always the most portable.

A program to convert from ANSI C to K&R has not yet been brought
to my attention ...

The moral for C (as for every other programming language)
is to code in a manner to be as portable as possible across machines,
compilers, and dialects of the language, and thus obviate the need
for non-standard preprocessors in the conversion process. 

-------------------------------------------------------------------
Siento mucho no poderme casar con ella.
Mi mujer se opondria.
-------------------------------------------------------------------
Alan Myrvold     ajmyrvold@violet.waterloo.edu
-------------------------------------------------------------------

cox@bentley.UUCP (MH Cox) (03/22/88)

In article <1065@sask.UUCP> coleman@sask.UUCP writes:
>> Xref: sask comp.lang.c:6877 comp.sys.ibm.pc:10098
>> I need help porting C programs around.
>> 
>> When I write my C functions on my personal computer, my declarations
>> are often like this :
>> 
>>           void foo(int x, double y)
>
>	Could you please tell us what kind of PC (if not an MS-DOS)
>and what kind of compiler you ate using. The above isn't Lattice
>or Microsoft and I would be suprised if it is Turbo.

It _is_ MSC 5.0, Turbo 1.0, Ecosoft, or any other proposed ANSI C
compiler the alternate function declaration form.

-- 

==========================================================================
Michael H. Cox			ARPA:  cox@garage.nj.att.com
AT&T Bell Labs			UUCP:  ihnp4!bentley!cox
184 Liberty Corner Road		COMPU$ERVE:  76525,3703
Rm 3N-D04
Warren, NJ  07060
(201) 580-8622
==========================================================================

jas@llama.rtech.UUCP (Jim Shankland) (03/23/88)

In article <1065@sask.UUCP> coleman@sask.UUCP (Geoff Coleman @ College of Engineering) writes:
>	Could you please tell us what kind of PC (if not an MS-DOS)
>and what kind of compiler you ate using.

And whether you ate the compiler top down or bottom up :-).

Jim Shankland
  ..!ihnp4!cpsc6a!\
               sun!rtech!jas
 ..!ucbvax!mtxinu!/

mdf@tut.cis.ohio-state.edu (Mark D. Freeman) (03/23/88)

In <1065@sask.UUCP> coleman@sask.UUCP (Geoff Coleman @ College of Engineering) writes:
>> When I write my C functions on my personal computer, my declarations
>> are often like this :
>> 
>>           void foo(int x, double y)
>
>	Could you please tell us what kind of PC (if not an MS-DOS)
>and what kind of compiler you ate using. The above isn't Lattice
>or Microsoft and I would be suprised if it is Turbo.

Actually, Microsoft C 5.0 supports this syntax.  Soon, most C compilers
will, as it is part of the emerging ANSI C standard.  It allows the
compiler to do argument count and type checking, helping you to avoid
silly errors that are easy to miss when checking by hand.  

Another great idea:  make your declarations look like this...

void	foo(int x, double y)	/* DECLARATION */

and you can create quick and dirty documentation for yourself with one
grep piped through sort, resulting in an alphabetized list of all your
functions showing the number and types of arguments for each!  Great
for libraries that you are writing.

-- 
Mark D. Freeman						  (614) 262-1418
					      mdf@tut.cis.ohio-state.edu
2440 Medary Avenue	   ...!cbosgd!osu-cis!tut.cis.ohio-state.edu!mdf
Columbus, OH  43202-3014      Guest account at The Ohio State University

karl@haddock.ISC.COM (Karl Heuer) (03/23/88)

In article <1465@ur-tut.UUCP> hwfe@tut.cc.rochester.edu.UUCP (Harlan Feinstein) writes:
>In article <1065@sask.UUCP> coleman@sask.UUCP (Geoff Coleman) writes:
>>>           void foo(int x, double y)
>>[What kind of PC and compiler uses that syntax?]
>That's Turbo C syntax.

More correctly, it's ANSI C syntax.  Expect to see it in all new compilers.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

Devin_E_Ben-Hur@cup.portal.com (03/23/88)

>hwfeccess@uorvm.bitnet (Harlen Fienstein) writes:
>coleman@sask.UUCP (Geoff Coleman @ College of Engineering) writes:
>>> 
>>>           void foo(int x, double y)
>>
>>	Could you please tell us what kind of PC (if not an MS-DOS)
>>and what kind of compiler you ate using. The above isn't Lattice
>>or Microsoft and I would be suprised if it is Turbo.
>>
>
>Sure enough, you shall be surprised.  That's Turbo C syntax.  It will also
>accept the more commonly acceptable method of argument syntax, but you have
>the option of doing it the above way.  I use the style illustrated here, as
>it's easier for me to remember, having done most of my C work with Turbo C.

More significantly, it is Draft ANSI C.  And the latest releases
of MSC and Lattice do support the function prototype declaration style.
There are significant advantages to this style -- parameter type checking
(and implied type promotion) and better code generation by avoiding
normal parameter type promotion (chars pushed as chars, not ints; floats
instead of doubles).

davidsen@steinmetz.steinmetz.ge.com (William E. Davidsen Jr) (03/23/88)

In article <8695@tut.cis.ohio-state.edu> mdf@tut.cis.ohio-state.edu.UUCP (Mark D. Freeman) writes:
| Another great idea:  make your declarations look like this...
| 
| void	foo(int x, double y)	/* DECLARATION */
| 
| and you can create quick and dirty documentation for yourself with one
| grep piped through sort, resulting in an alphabetized list of all your
| functions showing the number and types of arguments for each!  Great
| for libraries that you are writing.

A very good idea. Here are two things to look for in MSC:
 1) you can just use the -Zg option which will create the type of
    documentation you want (almost) without the names of the args.
    This file may then be included in all modules, giving type
    coersion to all calls, and type checking on the returns. I
    use this in several packages, and it works well.

 2) I have a note saying that there may be a problem when using
    float, in that "float x" expects a float as the argument,
    while "foo(x) float x; {" expects a double (as per K&R).
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

phil@sci.UUCP (Phil Kaufman) (03/24/88)

In article <8695@tut.cis.ohio-state.edu>, mdf@tut.cis.ohio-state.edu (Mark D. Freeman) writes:
> In <1065@sask.UUCP> coleman@sask.UUCP (Geoff Coleman @ College of Engineering) writes:
> >> When I write my C functions on my personal computer, my declarations
> >> are often like this :
> >> 
> >>           void foo(int x, double y)
> >
      ...
Declarations for functions are wonderful and avoid many errors but can 
anyone tell me what the effect of the static modifier has in a function
defrinition? I have often seen the following:
       static int foofunction (int, int)
:wq

miker@wjvax.UUCP (Michael Ryan) (03/24/88)

In article <8695@tut.cis.ohio-state.edu>, mdf@tut.cis.ohio-state.edu (Mark D. Freeman) writes:
> Another great idea:  make your declarations look like this...
> 
> void	foo(int x, double y)	/* DECLARATION */
> 
> [ use grep to create listings ]
> Mark D. Freeman						  (614) 262-1418

why not use the cdecl keyword , as supported by Msoft 5.0 ....
	
	void foo(int x, double y); /*only if you have prototypes */

	/* if you do prototypes or not, as well as cdecl or not */
	void cdecl foo( int x, double y) 
	{
	....
if your compiler does not support this syntax, 'cdecl', then declare 
in your software-wide #include file
	...
	#define	cdecl
	...
then you can have cross compatible software AND your grep'ed [ fgrep ]
listings. 

did you say 'D', you meant 'aDa'...
michael
-- 
====	*michael j ryan
	*{..!pyramid,..!decwrl!qubix}!wjvax!miker
	*Watkins-Johnson Co., San Jose CA : (408) 435 1400 x3079
	* above views are not necessarily those of Watkins-Johnson Co.

terry@wsccs.UUCP (terry) (03/24/88)

In article <5699@watdragon.waterloo.edu>, ajmyrvold@violet.waterloo.edu (Alan Myrvold) writes:
} I need help porting C programs around.
} 
} When I write my C functions on my personal computer, my declarations
} are often like this :
} 
}           void foo(int x, double y)

} When I want to run my programs on UNIX, my declarations need to be
} like this :
} 
}           #define void
}           void foo(x,y)
}           int x;
}           double y;

Ahhhh, the pitfalls of the emerging ANSI "standard" :-(.

The answer is simple, really.  Don't use the first method until it IS a
standard.

				terry@wsccs

tneff@atpal.UUCP (Tom Neff) (03/25/88)

In article <18359@sci.UUCP> phil@sci.UUCP (Phil Kaufman) writes:
>Declarations for functions are wonderful and avoid many errors but can 
>anyone tell me what the effect of the static modifier has in a function
>defrinition? I have often seen the following:
>       static int foofunction (int, int)


Microsoft C uses the /static/ storage class on a function definition to
indicate that the function has local scope to the containing source module,
i.e., no public symbol definition (PUBDEF record) will be emitted for that
function (or other symbol with module-wide scope).  Other compilation units
are free to define symbols with the same name (though I wouldn't normally
recommend it, as a matter of style), and MS LINK will not complain as long
as all of them (or all but one) define using /static/.

-- 

Tom Neff 

dsill@NSWC-OAS.arpa (Dave Sill) (03/25/88)

In article <115@atpal.UUCP> Tom Neff <atpal!tneff> writes:
>In article <18359@sci.UUCP> phil@sci.UUCP (Phil Kaufman) writes:
>>... I have often seen the following:
>>       static int foofunction (int, int)
>
>Microsoft C uses the /static/ storage class...

Well, if Microsoft C does it...  Come on now, if that was true `far',
`huge', `mongo', et cetera would be standard.  Let's try to be a
little more definitive in our references.

=========
The opinions expressed above are mine.

"In case of doubt, decide in favor of what is correct."
					-- Karl Kraus

mhyman@cup.portal.com (03/27/88)

>>>... I have often seen the following:
>>>       static int foofunction (int, int)
>>
>>Microsoft C uses the /static/ storage class...
>
>Well, if Microsoft C does it...  Come on now, if that was true `far',
>`huge', `mongo', et cetera would be standard.  Let's try to be a
>little more definitive in our references.

I quote from the second edition of C: A Reference Manual, Harbison and Steele

SEC. 4.3 page 59

"static	This storage class specifier may appear on declarations of functions
	or variables.  On function definitions, it is used only to specify
	that the function name is *not* to be exported to the linker...."

Also see th bottom of page 80 in K&R.

--Marc

--------------------------------
Marco S. Hyman
banglist: ...!sun!portal!cup.portal.com!mhyman
domain:   mhyman@cup.portal.com

sarima@gryphon.CTS.COM (Stan Friesen) (03/28/88)

In article <18359@sci.UUCP> phil@sci.UUCP (Phil Kaufman) writes:
>      ...
>Declarations for functions are wonderful and avoid many errors but can 
>anyone tell me what the effect of the static modifier has in a function
>definition? I have often seen the following:
>       static int foofunction (int, int)

	Quite simply, it means the function is limited in scope to the
current file, it is a *private* function not visible to the rest of the
world. This is very useful for producing modular code without worrying
about name conflicts.

ekb@ho7cad.ATT.COM (Eric K. Bustad) (03/29/88)

In article <1238@wjvax.UUCP>, miker@wjvax.UUCP (Michael Ryan) writes:
> [stuff deleted]
> why not use the cdecl keyword , as supported by Msoft 5.0 ....
> 	
> 	void foo(int x, double y); /*only if you have prototypes */
> 
> 	/* if you do prototypes or not, as well as cdecl or not */
> 	void cdecl foo( int x, double y) 
> 	{
> [more stuff deleted]

No one else has asked, so I guess that I must reveal my ignorance.
Can someone explain the meaning of Microsoft's "cdecl" keyword?

= Eric

jio@cpsc55.ATT.COM (James Odom) (03/29/88)

From article <1065@sask.UUCP>, by coleman@sask.UUCP (Geoff Coleman @ College of Engineering):
>> Xref: sask comp.lang.c:6877 comp.sys.ibm.pc:10098
>> I need help porting C programs around.
>> When I write my C functions on my personal computer, my declarations
>> are often like this :
>>           void foo(int x, double y)
> 
> 	Could you please tell us what kind of PC (if not an MS-DOS)
> and what kind of compiler you ate using. The above isn't Lattice
> or Microsoft and I would be suprised if it is Turbo.
> 
> Geoff Coleman                         | BITNET: Coleman@sask
> 

  The declaration in the original article is a form of 'prototyping' and
is a feature of Borland's Turbo C.  This type of declaration checks the
type of the variables passed to it by the calling function and warns the
user at compile time if the types do not match.  


+------------------------------------------------------------------------+
|James I. Odom                                                           |
|AT&T    CPSC         Denver, Co               Voice:   (303) 889-0211   |
|ATTMAIL:  JODOM      Compuserve: 70070,137    uucp:    ihnp4!cpsc55!jio |
|                     BIX:        jodom        SOURCE:  TCC375           |
|------------------------------------------------------------------------|
|Disclaimer: Any opinions expressed are my own etc.                      |
+------------------------------------------------------------------------+

franka@mmintl.UUCP (Frank Adams) (04/01/88)

In article <297@ho7cad.ATT.COM> ekb@ho7cad.ATT.COM (Eric K. Bustad) writes:
>No one else has asked, so I guess that I must reveal my ignorance.
>Can someone explain the meaning of Microsoft's "cdecl" keyword?

Microsoft (and a number of other PC C vendors) supports two subroutine
calling conventions: "C" style and "Pascal" style.  The Pascal style
supports interface to Pascal, Fortran, and compiled Basic programs, as well
as OS/2 system functions.  The C style supports functions with a variable
number of arguments.

One can use the "pascal" keyword on a function declaration to specify that
it is to be called with the pascal convention.  Alternatively, one can use a
compile switch to default all functions to pascal.  In this case, it is
necessary to have a keyword to specify that a function is to use the C
convention.  "cdecl" is it.

(Some compilers support "fortran" as a synonym for "pascal".)

(For those who care: there are two major differences between the two
conventions.  In C style, the calling function removes the arguments from
the stack after the call; in Pascal style, the called function does so
before returning.  For C style, the arguments are pushed right to left;
Pascal style is left to right.  A minor difference is the way that struct
values longer than 4 bytes are returned: C style is for the called function
to put the result in static storage, and return a pointer to it; Pascal
style is for the calling function to pass an additional argument, the
address where the result is to be put.)
-- 

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Ashton-Tate          52 Oakland Ave North         E. Hartford, CT 06108