I'm learning C with "The C programming language" book. And I'm stucked with this error.
Here is the code:
#include <stdio.h>
#define MAXLINE 1000 // Max input line length
int getline(char line[], int maxline);
void copy(char to[], char from[]);
// Printing the longest input line
int main()
{
int len; // Current line length, max line length ever seen
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while((len = getline(line, MAXLINE)) > 0)
{
if(len > max) // If current line length > previous max length - rewrite.
{
max = len;
copy(longest, line);
}
}
if(max > 0) // If there was a line
printf("%s", longest);
return 0;
}
int getline(char s[],int lim) // return length of line
{
int c, i;
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
{
s[i] = c;
}
if(c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
This code is an example from the book :)
Trying to compile it on Linux: Linux 5.4.97-gentoo.
GCC version: 10.2.0.
getline is already declared in <stdio.h>. Change the method name to my_getline.
Related
So I'm working on learning C by going through The C Programming Language and trying to do the exercises as I go. Exercise 1-16 seems really simple as it's just making an edit to a program that is already given.
What I can't figure out is why I can't get the code from the book, or from any of the online solutions to this exercise to compile... Everything I've tried throws an error that says: "error: conflicting types for ‘getline’"
Here's the original code from the book:
/* Write a program to print all input lines that are longer than 80 characters. */
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int main ()
{
int len; /* current line length */
int max; /* maximum line length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /* there was a line */
printf("%s", longest);
return 0;
}
/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
Any glaringly obvious mistakes here? Is it just my machine or how I'm trying to compile it?
This is an example program from the book The C Programming Language by Dennis Ritchie and Brian Kernighan (Second Edition). My question is if we are passing the value(variable line) but not the reference to the function length, then how does the change reflect in the main function?
#include <stdio.h>
#define MAXLINE 1000
int length(char s[], int lim);
void copy(char to[], char from[]);
int main() {
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = length(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
}
if (max > 0) {
printf("%s", longest);
}
return 0;
}
int length(char s[], int lim) {
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from []) {
int i;
i = 0;
while ((to[i] = from[i]) != '\0') {
++i;
}
}
This definition:
int length(char s[], int lim)
can be rewritten as:
int length(char *s, int lim)
which might make it easier to understand. We don't pass the variable by value, we pass the pointer, so the function can access the data and modify it if necessary.
len = length(line, MAXLINE)
in C all arrays are passed by pointer. so the function length receives the pointer to first char oh the array line and it modifies this array
I have tried to compile and run the program below which is the example 1.9 of the book The C Programming Language. It is compiled OK and no errors are found. However, when I run it and see if the program works, instead of returning me the longest line, it returns a sequence of garbage characters with the same length of the longest line.
Here is the program:
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
}
if (max > 0) {
printf("%s", longest);
}
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i] != '\0')) {
++i;
}
}
This expression:
(to[i] = from[i] != '\0')
is not doing what you think. It will compare from[i] != '\0' first, and assign the result to to[i]. This means the destination string will be the correct length but full of characters with code 1.
So probably you copied the parenthesis in the wrong position:
(to[i] = from[i]) != '\0'
Here is my code in c.
I compiled and run it on ubuntu terminal and the maximum length(max) is printed out but not the string(longest) - it just outputs a small box with zero and one.
I am a beginner in programming.
Thank you
#include<stdio.h>
#define MAXLINE 1000
int my_getline(char line[], int maxline);
void copy(char to[], char from[]);
main() {
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = my_getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
}
if (max > 0)
printf("%d\t %s \n",max, longest);
return 0;
}
int my_getline(char s[], int lim) {
int c, i;
for (i = 0; i < lim -1 && (c = getchar()) != EOF && c != '\n'; ++i) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from []) {
int i;
i = 0;
while ((to[i] == from [i]) != '\0')
++i;
}
Your copy routine doesn't copy anything. Probably you mean to use = instead of ==.
To my taste these ideas of using assignments inside control expressions is a really bad habit that buys you nothing but trouble. Something like
for(;;) {
to[i] = from[i];
if (to[i]) ++i;
else break;
}
would be clearer.
Inside your copy routine, you want assignment, not comparison:
void copy(char to[], char from []) {
int i;
i = 0;
while ((to[i] = from [i]) != '\0')
++i;
}
You need to re-implement your copy routine as it is not getting anything. A simple solution is as following:
strcpy(to, from);
You need to include string.h and call this instead of your copy function. If you want to implement sting copy yourself, use the following:
void copy(char * to, char * from) {
int i = 0;
while (from[i] != '\0') {
to[i] = from[i];
i++;
}
to[i] = '\0';
}
You need to be sure that string to is at least the same size of string from.
Why is my program reading the program itself instead of my text file? What I'm trying to do is read a text file and find out which line of text is the longest, then output that line and how many characters it contains.
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getline1(char line[], int maxline);
void copy1(char to[], char from[]);
/* print longest input line */
int main(void)
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */
max = 0;
FILE *ptr_file;
ptr_file =fopen("textfile.txt","r");
if (!ptr_file)
return 1;
while (fgets(line,1000, ptr_file)!=NULL)
//printf("%s",line);
//fclose(ptr_file);
//return 0;
while((len = getline1(line, MAXLINE)) > 0)
{
printf("%d: %s", len, line);
if(len > max)
{
max = len;
copy1(longest, line);
}
}
if(max > 0)
{
printf("Longest is %d characters:\n%s", max, longest);
}
printf("\n");
return 0;
} //end main
/* getline: read a line into s, return length */
int getline1(char s[], int lim)
{
int c, i, j;
for(i = 0, j = 0; (c = getchar())!=EOF && c != '\n'; ++i)
{
if(i < lim - 1)
{
s[j++] = c;
}
}
if(c == '\n')
{
if(i <= lim - 1)
{
s[j++] = c;
}
++i;
}
s[j] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy1(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
{
++i;
}
}
The while loop in you main should look like (don't need the inner loop in which inside getline1() getchar() reads from stdin):
while (fgets(line, 1000, ptr_file)!=NULL) {
len = strlen(line);
if (len > max) {
max = len;
strncpy(longest, line, 1000);
}