2 Replies - 307 Views - Last Post: 25 November 2012 - 11:36 AM Rate Topic: -----

#1 BP-S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-November 12

Conflicting Types on Debian, but not on Windows

Posted 25 November 2012 - 07:46 AM

This is my attempt at doing Exercise 1-16 of "The C Programming Language" by Kerninghan & Ritchie. The code works fine on Windows using CodeBlocks, but when I compile this on a Debian VM (gcc -o exercise exercise116.c), I keep getting:

Exercise_1-16.c:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
Exercise_1-16.c:15: error: conflicting types for ‘getline’
/usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here


#include <stdio.h>

#define maxlength 1000

int getline(char line[]); // Forward declaration, but it's not working

int main() {
	int c;
	char string1[maxlength];
	while ((c = getline(string1)) > 0) // Print the line
		printf("%s", string1);
	return 0;
}

int getline(char line[]) {
	int c, i, j, linebreak1 = 0;
	for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
		line[i] = c; // Write the characters to the string.
    if (c == '\n') // So we know if we need a linebreak at the end
        linebreak1 = 1;
	j = i - 1;
	for (j; (j > -1) &&( line[j] == '\n' || line[j] == '\t' || line[j] == ' ' || line[j] == EOF); --j) {
		line[j] = '\0'; // Start from the end of the string and keep inserting the "end character" until
						// we hit a non-space character. This makes it so we don't print spaces.
	}
	if (linebreak1 == 1 && line[0] != '\0') {
        line[j + 1] = '\n'; // If the line isn't empty, and a linebreak is necessary, print a linebreak.
        line[j + 2] = '\0'; // End of character.
    }
    else
        line[j + 1] = '\0'; // There must be some reason as to why I wrote this, but I can't remember what it was.
	return i;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Conflicting Types on Debian, but not on Windows

#2 BP-S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-November 12

Re: Conflicting Types on Debian, but not on Windows

Posted 25 November 2012 - 07:58 AM

Sorry about the double post, but it doesn't seem like I can delete or edit my original post. I probably should have searched more thoroughly. I noticed a very similar topic right after I posted this. getline is defined in stdio.h which is why it's causing errors. Anyway, problem solved.
Was This Post Helpful? 0
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Conflicting Types on Debian, but not on Windows

Posted 25 November 2012 - 11:36 AM

Well, it's important to note that you were compiling with extensions on. There is no standard getline() in C, however there is non-standard one if you have GNU extensions on.

So, to get strict rules and non-standard extensions disabled, try compiling with these compilation flags:
-ansi -pedantic -Wall -Wextra --std=VERSION where VERSION can be c90, c99 or c11 depending on which standard you want to follow.

edit: There's nothing inherently wrong in using custom (i.e. nonstandard) code, you're making one yourself every time you code. ;)
The point is to know when from apparently standard headers you get NON-standard functions. This may have influence on your code portability between compilers and/or "platforms".

This post has been edited by Xupicor: 26 November 2012 - 03:43 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1