Thread: Unfamiliar Function Syntax

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Unfamiliar Function Syntax

    I was recently reading a C tutorial and came across an unfamiliar way of making a function.
    I was hoping someone could clear this up for me ... the code is below - im talking about the line after

    "void swap(pnta, pntb)" where the line "float *pnta, *pntb;" appears ... I was always under the impression that directly after the function header came the blockquote "{"




    Thanks in advance - /\/\artin

    Code:
    #include <stdio.h>
    
    void swap();
    
    int main(){
    
    float a,b;
    
    printf("Enter two numbers: ");
    scanf("%f %f", &a, &b);
    if (a<b)
    swap(&a,&b);
    printf("The required order is: %5.2f %5.2f\n",a,b);
    
    return 0;
    }
    
    
    void swap(pnta, pntb)
    float *pnta, *pntb;
    {
    
    float temp;
    temp = *pnta;
    *pnta = *pntb;
    *pntb = temp;
    
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That's how you declare local variables in C. The arguments passed to a funtion are also local, so they need to be declared too. All declarations have to be placed before the body.

    If you declare them directly inside the ( ) then your C compiler is too kind...
    Most likely a C and C++ compiler.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    void swap(pnta, pntb)
    float *pnta, *pntb;
    This is old-style (also called K&R) C

    void swap(float *pnta, float *pntb)
    This is new-style (also called ANSI) C

    Both are valid, but you should only come across the old one when maintaining old code. Any book / tutor which is still teaching the old form as standard practice is way out of date (by at least 10 years).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ok then I have a question - by K&R C, do you mean 1st or second edition of that book? Just curious, because even though the second edtion of K&R has the big red stamp 'ANSI' it is not exactly ANSI C anymore.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    K&R-I is the original description of the language as implemented in the Bell Labs compiler which they wrote over the several years preceding publication.

    K&R-II is the latest edition of K&R, it's the one with the big red "ANSI-C" stamp, and it more or less describes what is in the C89 standard.

    As far as I know, there is no version of K&R which describes C99, nor do I know if there is any plan for one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Thanks Guys

    Thanks a lot for the prompt replies ... with your help I now realise that the it was older notation and I had only ever seen the newer ANSI way of doing it.

    Thanks again for your help

    -Thunder

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM