Very basic C question

This is a discussion on Very basic C question within the C Programming forums, part of the General Programming Boards category; I'm totally new at C (I've only done some Java and PHP earlier), so this is a very basic question. ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Very basic C question

    I'm totally new at C (I've only done some Java and PHP earlier), so this is a very basic question.

    Why doesn't the following work?

    Code:
    main() {
            float fl1, fl2;
            fl1 = 2.5;
            fl2 = 2.5;
            printf("%f\n", addition2(fl1,fl2));
            //printf("%f\n", fl1+fl2);
    }
    
    float addition2(float fl1, float fl2) {
            return fl1+fl2;
    }
    The commented out printf line works fine, but when I call a method that returns the same calculation it fails with this compilation error (in gcc):

    test.c: In function ‘main’:
    test.c:7: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’
    test.c: At top level:
    test.c:11: error: conflicting types for ‘addition2’
    test.c:7: error: previous implicit declaration of ‘addition2’ was here


    I don't understand the line "argument 2 has type 'int'"...

    If I change all float variables, in main and the addition() method, and the placeholders to int data types, it works fine. So there's apparently a trick to doing this with float variables which I haven't learned.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It's because you don't supply a prototype for the function before you use it. If you moved the addition2 function above main, the warning would go away. Or, if you added a function prototype above main, the warning would also go away.

    By default, any function encountered without first being declared will be assumed to return an int.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Ah, thank you very kindly Sir.

    That should've been obvious to me, really. How about creating a funtion prototype of an int method too? It's not necessary, but maybe it makes the code more tidy?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You wouldn't want an int method, you want a floating point method. But yes, you could use a function prototype:
    Code:
    void foo( void ); /* prototype */
    ...
    int main ( void )
    {
        foo(); /* call function, we can, because we're aware of its prototype */
    
        return 0;
    }
    
    void foo( void ) /* function is defined... */
    {
    }
    It doesn't really matter if there's a prototype separate from the definition; just keep in mind that before you use something, you have to be aware of its existence.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Quote Originally Posted by cnewbie1 View Post
    Ah, thank you very kindly Sir.

    That should've been obvious to me, really. How about creating a funtion prototype of an int method too? It's not necessary, but maybe it makes the code more tidy?
    If you mean something like this:

    Code:
    #include <stdio.h>
    
    float addition2(float fl1, float fl2);
    int addition2(int i1, int i2);
    
    main() {
    		//blablablah
    }
    
    float addition2(float fl1, float fl2) {
            return fl1+fl2;
    }
    
    int addition2(int i1, int i2){
    		return i1 = i2;
    }
    This is called "function overloading" and is NOT allowed in C (but it is allowed in C++)

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    No, I was thinking of this:

    Code:
    #include <stdio.h>
    
    int add(int, int);
    float snitt(float, float, float);
    
    main () {
         blah-blah;
    }
    
    int add(int x, int y) {
         return x+y;
    }
    
    float snitt(float v, float w, float z) {
         return (v+w+z)/3;
    }
    That is, use a function prototype for the int method appearing after main also, even though it's not necessary. It makes no difference, but I thought maybe it was more tidy. Or maybe not. Kind of the same way as it's tidy to declare all variables before initializing/using them.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by cnewbie1
    That is, use a function prototype for the int method appearing after main also, even though it's not necessary. It makes no difference, but I thought maybe it was more tidy. Or maybe not. Kind of the same way as it's tidy to declare all variables before initializing/using them.
    Yes, it is good practice, otherwise it may be ambiguous without checking the function definition as to whether you really intend default int, or if it was a mistake.
    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

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cnewbie1 View Post
    That should've been obvious to me, really. How about creating a funtion prototype of an int method too? It's not necessary, but maybe it makes the code more tidy?
    The same logic applies regardless of the function type (int, float, void, char, whatever).

    "It's not necessary" -- theoretically true, but if you have several functions that reference each other, it may become necessary if you don't want a bunch errors like the one in the first post. And in reality, everybody does list prototypes at the top like you have here. It's a beyond a "tidiness" issue, it is expected and might as well be considered mandatory.

    Vis. tidiness, I like them in alphabetical order, which is a very common practice if you have more than just a few functions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Thanks for the help, tips and answers. Hopefully I can move on a bit now :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM

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