What kind of syntax is this?

This is a discussion on What kind of syntax is this? within the C Programming forums, part of the General Programming Boards category; The sample code accompanying a textbook I'm reading (on image analysis) has function definitions in an unusual syntax that I've ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    595

    What kind of syntax is this?

    The sample code accompanying a textbook I'm reading (on image analysis) has function definitions in an unusual syntax that I've never seen before. The compiler accepts it with no complaints, but I'm wondering where this syntax comes from. Anybody know anything about its history?

    Here's a example (not from the book - just my own simple example to show the syntax):
    Code:
    #include <stdio.h>
    
    int
    main( argc, argv )	//This is the part I find unusual.
    	int argc;	//It's sort of nice because provides convenient space to
    	char* argv[];	//attach comments about each of the function arguments.
    {
    	char buffer[100];
    	strcpy(buffer, "world");
    	if( argc == 2 )
    	{
    		   strcpy( buffer, argv[1] );
    	}
    	printf( "Hello, %s\n", &buffer );
    	return 0;
    }

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    argc, argv are arguments to main in C (Google!)

    though I write it like

    Code:
    int main(int argc, char* argv[])

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by abh!shek View Post
    argc, argv are arguments to main in C (Google!)

    though I write it like

    Code:
    int main(int argc, char* argv[])
    Duh, of course argc and argv are arguments to main. My point is that I've never seen arguments to a function defined on separate lines outside the argument list. Have you?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    That's K&R syntax, which was before ANSI standards.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How old is your book?

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    I've never seen it before either.
    Does it even compile to todays standards?

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    The book is "Practical Algorithms for Image Analysis" by Michael Seul, Lawrence O'Gorman and Michael J. Sammon first published in 2000.

    I don't think it's K&R syntax -- unless it's from K&R's first edition. In my copy of K&R (2nd edition), they say (bottom of pg. 69):
    Code:
    Each function definition has the form:
    return-type function-name( argument declarations)
    {
        declarations and statements
    }
    ...in other words, what I think of as "standard" C.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by sand_man View Post
    I've never seen it before either.
    Does it even compile to todays standards?
    GCC 3.3 compiles it with no complaints.
    GCC 4.1 compiles it, with a warning:
    warning: incompatible implicit declaration of built-in function ‘strcpy’
    which obviously has nothing to do with the format of the function name and arguments.

    (I forgot to #include <string.h>)

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by R.Stiltskin View Post
    I don't think it's K&R syntax -- unless it's from K&R's first edition. In my copy of K&R (2nd edition), they say (bottom of pg. 69.
    and still this style is called K&R style and not used anymore nowedays...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    http://cm.bell-labs.com/cm/cs/cbook/

    The cover advertises it as ANSI C, although the standard wasn't finalized until a year or so later.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    It is still standard in C99 (refer to section 6.9.1 paragraph 13).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by laserlight View Post
    It is still standard in C99 (refer to section 6.9.1 paragraph 13).
    Here int a, b; is the declaration list for the parameters. The difference between these two definitions is that the first form acts as a prototype declaration that forces conversion of the arguments of subsequent calls to the function, whereas the second form does not.
    Interesting. I didn't know it was legal in ANSI/ISO C, much less there was any difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 10:24 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 11:23 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21