Prototypes and function return types in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • talonx
    New Member
    • Mar 2008
    • 18

    #1

    Prototypes and function return types in C

    Hello, consider this skeleton of a program I was writing -

    Code:
    main(){
       getline(...
       copystrings(...
    }
    
    void copystrings(char a[], char b[]) {
    ...
    }
    
    int getline(char arr[], int limit) {
    ...
    }
    My problem is not with the logic but with some syntax. Here, copystrings does not return anything, so it's declared as returning void. getline returns an int, and it's declared as such.

    When I compile this using cc on Fedora linux, the warnings I get are these

    Code:
    warning: conflicting types for ‘copystrings’
    note: previous implicit declaration of ‘copystrings’ was here
    If I put a function prototype for copystrings above the main method, there are no warnings.

    My question is, why don't I get a similar warning for getline as well?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You are calling getline and copystrings without a function prototype. Thus the compiler doesn't know what they return. It assumes int. Later it finds out that this assumption was wrong for copystrings, but correct for getline.

    Best is to put prototypes for these functions at the top of your file.

    Comment

    • talonx
      New Member
      • Mar 2008
      • 18

      #3
      Thanks donbock - that was helpful!

      Comment

      Working...