Like Tree2Likes

Warnings: Type Mismatch, functions, parameters? Homework: Guidance, not answers.

This is a discussion on Warnings: Type Mismatch, functions, parameters? Homework: Guidance, not answers. within the C Programming forums, part of the General Programming Boards category; Class assignment (requires use of function). Wrote the code. It compiles, runs and gives me the desired result. However, I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Warnings: Type Mismatch, functions, parameters? Homework: Guidance, not answers.

    Class assignment (requires use of function). Wrote the code. It compiles, runs and gives me the desired result. However, I get warnings... "parameter names (without types) in function declaration, type mismatch with previous implicit declaration, previous declaration of 'is_prime, is_prime was previously implicitly declared to return 'int".

    Code:
    #include<stdio.h>
    
    void Is_Prime (num);
    
    int main (void)
    {
    
    int num;
    
    printf("Enter a Number to Test if it is Prime: ");
    scanf("%d",&num);
    
    is_prime (num);
    
          system("PAUSE");
          return 0;
    
    } /* End of Main */
    
    void is_prime (int num)
    {
    int i;
    int p = 0;
    
    for(i=1;i<=num;i++)
       {
         if (num%i==0)
           {
             p=p+1;
            }
       }
    if(p==2)
    
    printf("%d is a Prime Number\n", num);
    else
    printf("%d is NOT a Prime Number\n", num);
    I have tried renaming the parameter, but then the program does not work. I reordered the code to put the function first and that eliminated all but one warning, "parameter names (without types) in function declaration".

    Guidance or a point in the right direction will be appreciated. Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    the function declaration at the top should name the type not the variable... as in void is_prime (int);
    You have mismatched braces in your function... Better indentation rules would help you spot that.
    Also note that C source files have to end in a blank line...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Also note the difference of is_prime and Is_prime.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Also note the difference of is_prime and Is_prime.
    Nice catch... I do that one to myself all the time. (Old Pascal habit, I'm afraid... Pascal isn't case sensitive.)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by CommonTater
    the function declaration at the top should name the type not the variable... as in void is_prime (int);
    I suggest that you do both, i.e., keep both the type name and the parameter name.

    Also, you should make is_prime return a value to indicate if the argument is prime. The printing of output should be done in the main function instead.
    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

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Worked. Thanks.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Nice catch... I do that one to myself all the time. (Old Pascal habit, I'm afraid... Pascal isn't case sensitive.)
    Ah, the implicit declaration warning got my attention. Specifically with the return type.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    So... return a 1 (prime) or 0 (not prime) and then have the main function print. What is used in the main to print?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by C-bob
    So... return a 1 (prime) or 0 (not prime) and then have the main function print.
    Yes.

    Quote Originally Posted by C-bob
    What is used in the main to print?
    More or less what you currently use in is_prime. The difference is that by moving out this output code from is_prime, is_prime becomes more reusable (and it then does one thing and does it well).
    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

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Well, I would use a printf statement.....

    What Laser is referring to is the design of your program. Functions should usually just do one thing, either perform some task such as calculations or user interfacing (input/ouput), or whatever; just one of those things though. It is about proper modularizing your code.
    stahta01 and CommonTater like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    OK. I gave it a shot and it works... but again, with warnings ("comparison between pointer and integers".

    Code:
    #include<stdio.h>
    
    int is_prime (int p_num);
    
    
    /* start of is_prime function */
    int is_prime (int p_num)
    {
    int i;
    int p = 0;
    
    for(i=1;i<=p_num;i++)
       {
         if (p_num%i==0)
           {
             p=p+1;
            }
            }
     if(p==2)
       return 1;
       else
       {
       return 0;
       }
    } /* is_prime */
    
    
    int main (void)
    {
    
    int num;
    
    printf("Enter a Number to Test if it is Prime: ");
    scanf("%d",&num);
    
    if (is_prime == 1)
    {
    printf("%d is a Prime Number\n", num);
    }
    else
    {
    printf("%d is NOT a Prime Number\n", num);
    }
          system("PAUSE");
          return 0;
    
    } /* End of Main */
    To be honest, I am getting there through trial and error. Is this an appropriate approach?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by C-bob View Post
    So... return a 1 (prime) or 0 (not prime) and then have the main function print. What is used in the main to print?
    And here's a little trick to make your code more efficient... An even number > 2 can never be a prime number ... 4, 6 ,8 etc. as all even numbers are evenly divisible by 2... One extra line of code to check that could save you a lot of time checking even numbers. You may also want to structure your loops to examine only odd numbers...

    Code:
    /* start of is_prime function */
    int is_prime (int p_num)
    {
        int i;
        int p = 0;
     if (! (p_num & 1) )
        return 0;
    
      for(i = 1; i <= p_num; i++)
       {
         if (p_num%i==0)
           {
            p=p+2;
            }
            }
         if(p==2)
           return 1;
        else
          {
         return 0;
       }
    } /* is_prime */
    Last edited by CommonTater; 08-02-2011 at 11:31 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by C-bob
    I gave it a shot and it works... but again, with warnings ("comparison between pointer and integers".
    It does not actually work. You need to call the function, e.g., is_prime(num), not just compare the function pointer with 1.

    Also, as was mentioned to you earlier, you need to improve your indentation.
    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

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by C-bob View Post
    OK. I gave it a shot and it works... but again, with warnings ("comparison between pointer and integers".

    Code:
    int main (void)
    {
    
    int num;
    
    printf("Enter a Number to Test if it is Prime: ");
    scanf("%d",&num);
    
    if (is_prime == 1)
    {
    printf("%d is a Prime Number\n", num);
    }
    else
    {
    printf("%d is NOT a Prime Number\n", num);
    }
          system("PAUSE");
          return 0;
    
    } /* End of Main */
    You need to take a look at functions and how to use them. How to use functions.

    Plus indent your code.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    I think this is it (tested multiple numbers and it does work), but it just doesn't seem like the right approach.

    If I have wore out my welcome here, just say so. I am new to the forum and getting to know the etiquette.

    Code:
    #include<stdio.h>
    
    int is_prime (int p_num);
    
    int is_prime (int p_num)
    {
     int i;
     int p = 0;
    
     for(i=1;i<=p_num;i++)
     {
         if (p_num%i==0)
         {
          p=p+1;
          }
          }
          if(p==2)
             return 1;
                 else
                 {
                  return 0;
                 }
    } /* is_prime */
    
    int main (void)
    {
       int num;
       int Y;
       int N;
    
       printf("Enter a Number to Test if it is Prime: ");
       scanf("%d",&num);
    
       Y = is_prime (num);
       N = is_prime (num);
    
       if (Y == 1)
       {
        printf("%d is a Prime Number\n", num);
        }
        else
        {
         printf("%d is NOT a Prime Number\n", num);
         }
          system("PAUSE");
          return 0;
    
    } /* End of Main */

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data type mismatch in criteria expression ERROR
    By Aga^^ in forum C# Programming
    Replies: 2
    Last Post: 02-11-2009, 12:21 AM
  2. Yahoo Answers Type Website
    By Peckle in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-21-2008, 10:30 AM
  3. Incompatible Pointer Type warnings
    By trillianjedi in forum C Programming
    Replies: 3
    Last Post: 06-11-2008, 04:16 PM
  4. Type mismatch warning
    By Opel_Corsa in forum C Programming
    Replies: 5
    Last Post: 10-11-2006, 10:34 AM

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