C Program-Error

senewimala1988

Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    Meka mama study karana book eke thibba example ekak functions valata..Eth meka compile karanna be errors enava...Mata meke errors tika monaada kiyala kiyanavaada....???
    I dont know C programming well....

    #include<stdio.h>
    #define pi 3.141

    void area(int r);
    void area(float r);

    int main()
    {
    int radius;
    printf("Enter the radius: ");
    scanf("%d",&radius);

    area(radius);
    area((float)radius);

    return 0;
    }

    void area(int r)
    {
    printf("\nInteger input");
    printf("\nArea is: %.2f\n",(pi*r*r));
    }
    void area(float r)
    {
    printf("\nFloating point input");
    printf("\nArea is: %.2f\n",(pi*r*r));
    }




    screenshotckd.png
     

    kosandpol

    Well-known member
  • Jun 10, 2008
    45,329
    1,492
    113
    there already is a function named area() so it conflicts with the one you're defining here. Rename your function to something else and try to compile again
     

    lankanatha

    Well-known member
  • Dec 8, 2008
    3,560
    212
    63
    senewimala1988 said:
    Meka mama study karana book eke thibba example ekak functions valata..Eth meka compile karanna be errors enava...Mata meke errors tika monaada kiyala kiyanavaada....???
    I dont know C programming well....

    #include<stdio.h>
    #define pi 3.141

    void areai(int r);
    void areaf(float r);

    int main()
    {
    int radius;
    printf("Enter the radius: ");
    scanf("%d",&radius);

    areai(radius);
    areaf((float)radius);

    return 0;
    }

    void areai(int r)
    {
    printf("\nInteger input");
    printf("\nArea is: %.2f\n",(pi*r*r));
    }
    void areaf(float r)
    {
    printf("\nFloating point input");
    printf("\nArea is: %.2f\n",(pi*r*r));
    }



    mehema karala balanna. oya koheda c igena ganne, potha mokakda?
     

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    Mage note eke thiyenne mehema...

    It is also possible to define several functions with the same name but with different parameters.
    As given below..

    float area(int r);
    float area(float r);

    Both these functions calculate the area of a circle given radius. However the first function accepts an integer as the input while the second function accepts a floating point number. based on the type of the given input the program dynamically calls the correct function.
     
    Last edited:

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    machan area() kiyalama function 2k thiyenwane..ekaka area1 karapn
    GCC ta witharak newe matath pshyco gahanwa ohoma thibbama
     

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    senewimala1988 said:
    Mage note eke thiyenne mehema...

    It is also possible to define several functions with the same name but with different parameters.
    As given below..

    float area(int r);
    float area(float r);

    It is also impossible to define several functions with the same name but with different parameters.
    As given below..

    mehema wenna athi bn
     

    silence

    Member
    Feb 17, 2009
    81
    0
    0
    What you're trying to do is overload the function 'area'... problem is "pure C" doesn't allow function overloading.

    How ever C++ does... so does the g++ compiler. So instead of using gcc, use g++ to compile this, it should work right out.
     
    Last edited:

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    nagaya said:
    It is also impossible to define several functions with the same name but with different parameters.
    As given below..

    mehema wenna athi bn
    Apo ne ban...Mama first post eke daala thiyenne note eke okata adaala coding tika....Oke out put eka deela thiyenne as beelow.

    Enter radius: 1

    Integer input
    Area is: 3.14
    Floating point input
    Area is: 3.14
     

    silence

    Member
    Feb 17, 2009
    81
    0
    0
    nagaya said:
    It is also impossible to define several functions with the same name but with different parameters.
    As given below..

    mehema wenna athi bn

    Umm.. no, it's called function overloading. 'pure C' does not support it, but since the C99 spec it's been included, and almost all the other languages support it including C++/Java/etc
     

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    silence said:
    Umm.. no, it's called function overloading. 'pure C' does not support it, but since the C99 spec it's been included, and almost all the other languages support it including C++/Java/etc
    but he's using pure C here as he uses gcc to compile :oo:
    so that note should be revised as i guess :baffled:
     

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    nagaya said:
    but he's using pure C here as he uses gcc to compile :oo:
    so that note should be revised as i guess :baffled:
    Ane mama danne ne banz..
    Mata mehema avulak vechcha first velaava..(kalin avulak lihaa ganna oyath help kala)
    next week eke functions valin practical ekak..
     

    kosandpol

    Well-known member
  • Jun 10, 2008
    45,329
    1,492
    113
    senewimala1988 said:
    Mage note eke thiyenne mehema...

    It is also possible to define several functions with the same name but with different parameters.
    As given below..

    float area(int r);
    float area(float r);

    Both these functions calculate the area of a circle given radius. However the first function accepts an integer as the input while the second function accepts a floating point number. based on the type of the given input the program dynamically calls the correct function.

    I think your notes are missing an important bit of information. That this applies only to Microsoft C.
    see : http://en.wikipedia.org/wiki/Name_mangling#C_name_decoration_in_Microsoft_Windows
     

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka

    kosandpol

    Well-known member
  • Jun 10, 2008
    45,329
    1,492
    113
    senewimala1988 said:
    Ane ban mama C valata aluth..Ovva therenne ne...Kalin program tika kara gaththe ElaKiri eke pihiten thama...mata tikak sinhalen kiyanavaada...Nathnam okkoma avul venava banz...Sorry!!!

    Microsoft's version of C has 'the same function with different parameters' feature.
    Not GNU C.
    So your notes are only for MS C.
     

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    kosandpol said:
    Microsoft's version of C has 'the same function with different parameters' feature.
    Not GNU C.
    So your notes are only for MS C.
    Ethakota ban C programmin languages keepayak thiyenavaada???
    api C karanne Redhat vala...eke wada karanavaada???:confused:
    Mokakda ban GNU kiyana eke meaning eka???
     

    kosandpol

    Well-known member
  • Jun 10, 2008
    45,329
    1,492
    113
    senewimala1988 said:
    Ethakota ban C programmin languages keepayak thiyenavaada???
    api C karanne Redhat vala...eke wada karanavaada???:confused:

    most probably. instead of ubuntu, use RedHat or CentOS to try your C lessions.
    There's a lot of C versions. didnt they teach you the history of C ? or were you asleep during that bit of the lecture ?
     

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    kosandpol said:
    most probably. instead of ubuntu, use RedHat or CentOS to try your C lessions.
    There's a lot of C versions. didnt they teach you the history of C ? or were you asleep during that bit of the lecture ?
    Ohoma ahanna epa banz...
    Mama lectures giye eka 3 paarak withara wage...Sometimes eeta kalin thiyena practical eka ivara venna godak wela yanava...Ethkotath yanna bari wenawa...
    anika lecturer hoda nida gannama thama...ekkenek enava...Eya nam charrrr...


    Mokakda ubuntu vala thiyena avula???
    mata therenne ne...Fedora valata hariyaida???
    Ubuntu valata solution ekak aththema nadda???
     

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,707
    2,577
    113
    Kadawatha, Sri Lanka
    kosandpol said:
    most probably. instead of ubuntu, use RedHat or CentOS to try your C lessions.
    There's a lot of C versions. didnt they teach you the history of C ? or were you asleep during that bit of the lecture ?
    Mama turbo C++ use kala windows compiler ekak viddihata..Eketh error ekak enavane banz???