Thread: What kind of syntax is this?

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

    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
    596
    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
    596
    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
    596
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    It is still standard in C99 (refer to section 6.9.1 paragraph 13).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:23 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread