Another function not working...

This is a discussion on Another function not working... within the C Programming forums, part of the General Programming Boards category; This one compiles but gives me an error when I execute it. Itīs supposed to return 0 if a number ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    Another function not working...

    This one compiles but gives me an error when I execute it.
    Itīs supposed to return 0 if a number is prime and 1 if itīs not.

    Translation form spanish:
    esprimo=isprime
    ingrese un entero=input an integer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        printf("Ingrese un entero: ");
        scanf("%d",n);
        printf("%d",esprimo(n));
        
      
      system("PAUSE");	
      return 0;
    }
    
    int esprimo(int n)
    {
    int i;
    i=2;
    while(n%i!=0)
        i++;
    if(i<n)
        return 0;
    else
        return 1;    
    }
    thank you!!!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    It looks like you are using scanf() incorrectly. This:
    Code:
    scanf("%d",n);
    should be:
    Code:
    scanf("%d", &n);
    You might want to increase the warning level of your compiler so that it would report such potential problems.

    EDIT:
    Oh, and you should declare (as in write a prototype) the esprimo function before the main() function.
    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

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Yes, I totally passed that out... and the compiler said nothing.

    Regarding the prototype issue, why is that?? Why should I write the function again as a prototype?
    Iīm aware that when I write a prototype it must have the ";" symbol, but I donīt get the purpose of that...

    Any explanation is welcome.

    Thank you, laserlight!!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Inti
    Regarding the prototype issue, why is that?? Why should I write the function again as a prototype?
    Iīm aware that when I write a prototype it must have the ";" symbol, but I donīt get the purpose of that...
    To tell the compiler of the signature and return type of the function. Without the function declaration (prototype), the compiler would assume that the function returns an int, possibly leading to an error if it was not defined as returning an int.
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Prototypes are function declarations to tell the compiler what arguments and return value to expect for a funciton. The function definition can be given later on in the same file (the declaration is then called forward declaration) or the definition is given in a different source file (sometimes called extern declaration).

    Another option is to define (that is, write the ENTIRE function) before it is being called. Some languages are defined to prefer this variant (Pascal for example). As to which you prefer is a matter of style, really.

    In C99 (the current C standard), prototypes are required for all functions that aren't defined before the call to the function. Likewise in C++.

    I probably took too long to write this, so someone else has already answered it.

    Edit: Rephrase dubious wording regarding forward declaration and definitions.

    --
    Mats
    Last edited by matsp; 02-07-2009 at 10:53 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  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

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