Can't print out a string in C - c

I've been learning C with a book and I tried one of its programs on my Linux PC with gcc, but for some reason it doesn't work.
#include <stdio.h>
#define MAXLINE 1000
int getoneline(char line[], int maxline);
void copy(char to[], char from[]);
int main()
{
int line_length, max_length;
char line[MAXLINE];
char longest[MAXLINE];
max_length = 0;
while ((line_length=getoneline(line, MAXLINE)) > 0)
if (line_length > max_length)
{
max_length = line_length;
copy(longest, line);
}
if (max_length > 0)
printf ("%s", longest);
return 0;
}
int getoneline(char s[], int limit)
{
int c, i;
for (i=0; i < limit - 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;
}
It is the same in the book and it's supposed to find the longest line in some text and the output it. However, nothing gets outputted, as if the variable longest disappeared after the while loop. I can print it inside, though, but that's not what I want.
What's going wrong?

Works for me! Try running the program and entering a few lines of text. Then press Ctrl-D so that getchar returns an EOF, causing the program to break out of the while loop and print the longest line
I would point out that there are about a million better ways to write this code that are less confusing. Without getting into all the reasons for this, I sincerely recommend following the 'get a new book' advice in the comments.

Related

Why does this program in Dennis Ritchie's "The C Programming Language" not work?

I'm starting to learn C and came across the following program in Dennis Ritchie's The C Programming Language (2nd edition):
#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]; /* 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)
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;
}
When I run this program exactly as is, it does not compile because there are conflicting definitions of getline. It turns out there is a getline in stdio.h, and that's where the conflict comes from. I assume this is non-standard or was added to the library after the book was published. In any case, that error was easily fixed by simply changing the name of the function to getLine.
After making that change, the program compiles but never actually completes. What I did notice is that getLine adds both a newline character and a null terminator (\0) to the character array s, and the value it returns, while it is meant to be the length of the character array, is actually that length + 1. Modifying the function to return i - 1 instead of i fixes the issue.
My question is: why does it fix the issue? I doubt that it's a typo, but maybe that's possible? Or could it be a compiler issue? Do some compilers count a null terminator as a character (i.e. to be included in the length of the character array) while others don't?
I should also say that I'm using an M1 MacBook, so I guess it's possible that the code translates to different machine code which creates different results?
EDIT:
The following is the modified code that works for me:
#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]; /* 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)
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 - 1;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
Also, when I say that the original code never completes, I mean that I can type anything, press enter, type some more, press enter, etc. then finally just press enter without typing (the code is checking for an array of length zero, so this is where it should print out the line of max length and exit), and the program continues running.
When I run this program exactly as is, it does not compile because there are conflicting definitions of getline
There is a getline function offered as an extension in some implementations, and its prototype is conflicting with the example in the book. That's most likely what's happening with you. Awesome as K&R is, it's an old book at this point and out of date in many respects.
The easiest way to get around this is to rename your getline function to getLine or get_line or something else. Alternately you'll need to undefine _GNU_SOURCE or _POSIX_C_SOURCE before including stdio.h, either in your code or on the command line.

How to use gnu debugger in c with a program that requires user input

I asked this question yesterday but everyone was saying I wasn't providing enough info, so I figured the best way to ask would be just to record myself trying to run the program so you can see what I'm doing wrong. So I deleted the old post and here is the video link: https://youtu.be/NLJ8my_UI7s
I'm reading "The C programming language" book and trying to work through the exercises. They are pretty hard so I've just been looking up the solutions online and if I can read the code and understand I've been counting it as a win. But the exercise I'm stuck on is this one
https://penti.org/~sederlok/misc/lang/c/the_c_programming_language_-_exercises/krx116.html
basically it's a program that takes a user input string in the console and prints out the string up to the first 20 characters and the full length of it. The challenge was to modify the main function to print out the actual length, whereas originally it only printed out the length up to the first 20 characters.
#include <stdio.h>
#define MAXLINE 20
int getLine(char s[], int lim);
void copy(char to[], char from[]);
int main(void)
{
char line[MAXLINE];
char longest[MAXLINE];
char temp[MAXLINE];
int len, max, prevmax, getmore;
max = prevmax = getmore = 0;
while((len = getLine(line, MAXLINE)) > 0)
{
if(line[len - 1] != '\n')
{
if(getmore == 0)
copy(temp, line);
prevmax += len;
if(max < prevmax)
max = prevmax;
getmore = 1;
}
else
{
if(getmore == 1)
{
if(max < prevmax + len)
{
max = prevmax + len;
copy(longest, temp);
longest[MAXLINE - 2] = '\n';
}
getmore = 0;
}
else if(max < len)
{
max = len;
copy(longest, line);
}
prevmax = 0;
}
}
if(max > 0)
{
printf("%s", longest);
printf("len = %d\n", max);
}
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;
}
else if(c == EOF && i > 0)
{
/* gotta do something about no newline preceding EOF */
s[i] = '\n';
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
I was having trouble understanding exactly what's going on so I tried running through it with the debugger. The problem is, normally I would run the code, and it wait for me to enter a string before continuing. When I set a breakpoint it starts running without waiting for me, and what is even more confusing, is that when I step through the part where it says c = getchar(), and I hover over c, it has been given a value of 10. I don't know where that value is coming from and I'm at a loss as to what's going on when I run this code through the debugger, and how I'm supposed to enter user input when the debugger seems to commandeer the console.

maximum line length program prints unreadable characters in c

I am trying to run the maximum line length program as shown in chapter one of "The c programming language" and the output is always a series of question mark boxes like ⍰.
Here is the code:
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
int main()
{
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;
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;
}
Is this a technical issue or something obviously wrong with the code?
Thank-you?
You have a tiny, tiny typo. This line in the getline function:
for (i = 0; i < lim - 1 && (c = getchar() != EOF) && c != '\n'; ++i)
should be
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
The effect of this typo is that you call getchar(), test the returned value to see if it's equal to EOF or not, and assign the 1/0 (true/false) value to the variable c. But what you want to do is assign the returned value from getchar() to c, and then test to see if it's equal to EOF.
So if you type, say, "test", your typo-afflicted getline function was not writing the characters t e s t into line; instead it was writing four 1 values (that is, the character '\001', which is a control-A if you want to think about it that way), and since that's not a printing character, your display system printed it as a little box instead.

GDB Hanging - Not understanding why

I'm going through the book "The C Programming Language" and doing all of the examples plus poking around in GDB to see what's going on.
In the below example code, the goal is to evaluate a few lines of text to determine which is the longest line. I thought my own program was failing because gdb was hanging at the for loop which calls getchar(). I followed the backtrace, found the culprit function, but couldn't determine the exact problem. Then I did the same with the example code, and the exact same problem occurs at the for loop which calls getchar().
// file: ch1/ex16.c
// OBJECTIVE: Revise the main routine o the lnogest program
// so it will correctly print the length of arbitrarily long
// lines and as much as possible of the text.
#include <stdio.h>
#define MAXLINE 1000
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
int main()
{
int len, max;
char line[MAXLINE], 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 = 0;
while ((to[i] = from[i]) != '\0'){
++i;
}
}
Unlike when executing the program through the shell, gdb wouldn't accept ^d (ctrl+d) as EOF. Thanks to the comment that Duck provided above, I found that if I fed it a file through standard input gdb it solved the problem.
Ex:
(gdb) run < file.txt

Does this small C program satisfy the K&R exercise?

I'm on to K&R's Exercise 1-18
Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.
This is what I've came up with so far
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int main () {
int len;
char line[MAXLINE];
while (getline(line, MAXLINE) > 0) {
printf("%s", line);
}
return 0;
}
int getline(char s[], int lim) {
int c, i, lastNonBlankIndex;
lastNonBlankIndex = 0;
for (i=0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
if (c != ' ' && c != '\t') {
lastNonBlankIndex = i + 1;
}
s[i] = c;
}
if (i != lastNonBlankIndex) {
i = lastNonBlankIndex;
c = '\n';
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
The second part sounded hard, as I wasn't sure what I should return if the line only has blanks or tabs. After all, if I return 0, it will halt the getline() calling. Would this be where I should set up a #define, such as ALL_BLANKS.
Anyway, to actual main question, is this a correct way to remove trailing blanks and tabs from lines? I ran a few inputs through, and it seemed to work. However, if I copy and paste text with newlines into the CL, it appears all strung together. And when I type a line into the CL and push enter, it automatically prints it. Should I be building an array of lines, and then looping through and printing them when done ?
Your code looks correct, but I think it would be better if you separate the operations of reading a line from stdin and stripping the line of trailing whitespace (decoupling). Then you can use the unmodified getline from the book (code reuse) and won't have the problem of halting on returning 0.
And if you are interested in other solutions, the CLC-wiki has an almost complete list of K&R2 solutions.
#include <stdio.h>
#define MAXLINE 1024
int getline(char s[], int lim);
main()
{
int i, len;
char line[MAXLINE];
while ((len = getline(line, MAXLINE)) > 0) {
i = len - 2;
while (i >= 0 && (line[i] == ' ' || line[i] == '\t'))
--i;
if (i >= 0) {
line[i+1] = '\n';
line[i+2] = '\0';
printf("%s", line);
}
}
return 0;
}
This is the category 1 solution I wrote some time ago. getline is as on page 28 of the book. It might be nicer to put the removal of whitespace in a separate function rstrip, but I leave this as an exercise for the reader.
Your basic design is sound. It is better, as you did, to print a stripped line as soon as you've built it, so that your program only needs to keep one line at a time in memory and not the whole file.
There is a small problem with your code: it doesn't implement the second part of the question (“delete entirely blank line”). That's because you always tack a '\n' at the end of the string. This is easy to fix, but remember that you must return a nonzero value to your caller since a blank line doesn't indicate the end of the file.
getline should return -1 (a negative value in general) if there is an error or if EOF is reached. Then your loop conditional can check that it returns something >= 0 and still allow for 0 length lines.
for (i=0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
I almost never include an assignment within a loop conditional. I would rather add 10 lines of code to get around doing that because it's difficult to read. I would especially refrain from using them with complicated conditionals.
int i = 0;
while (i < lim) {
c = getchar();
if (c == EOF || c == '\n') {
break;
}
line[i] = (char)c;
i++;
}
line[i] = '\0'; // Null terminate the string
This code should read in a line for you. I would separate the reading in of the line from the removal of the trailing white space. You could very easily work backwards from the end of the string to remove white spaces at the location where I null terminated the line, since after having read in the line you now know its length. Essentially you grow the string and then you prune it back down after it has finished growing.
This is how i did it.
#include <stdio.h>
#define MAXLINE 1000
#define IN 1
#define OUT 0
int state = OUT;
int getline(char s[], int lim);
void copy(char to[], char from[]);
int main(void)
{
int lenght;
int max = 0;
char line[MAXLINE];
char longest[MAXLINE];
while ((lenght = getline(line, MAXLINE)) > 0)
if (lenght > max)
{
max = lenght;
copy(longest, line);
}
if (max > 0)
printf("\n%s\n", longest);
return 0;
}
int getline(char s[], int lim)
{
int i, c;
for (i = 0; i < lim - 1 && ((c = getchar()) != EOF) && (c != '\n'); i++)
{
if (state == IN && c != ' ' && c != '\t')
{
s[i] = ' ';
i++;
state = OUT;
}
if (s[0] == ' ')
{
s[0] = '\b';
}
s[i] = c;
if (c == ' ' || c == '\t')
{
i--;
state = IN;
}
}
if (c == '\n')
{
s[i] = c;
i++;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i = 0;
while ((to[i] = from[i]) != '\0')
i++;
}
#include <stdio.h>
#define MAXLINE 1000
size_t getline(char *s,size_t lim)
{
if( fgets(s,lim,stdin) )
{
while( *s && strchr(" \t\n",s[strlen(s)-1]) )
s[strlen(s)-1]=0;
return strlen(s);
}
return 0;
}
main()
{
int len;
char line[MAXLINE];
while (getline(line,sizeof line)) {
printf("%s", line);
}
return 0;
}

Resources