Unfamiliar Function Syntax

This is a discussion on Unfamiliar Function Syntax within the C Programming forums, part of the General Programming Boards category; I was recently reading a C tutorial and came across an unfamiliar way of making a function. I was hoping ...

  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
    32,825
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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
    32,825
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10: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, 10: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, 11:00 AM

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