Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4268090
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:59:17+00:00 2026-05-21T06:59:17+00:00

#include <stdio.h> #define MAXLENGTH 77 int getline(char[], int); main() { char in_line[MAXLENGTH]; int count,

  • 0
#include <stdio.h>

#define MAXLENGTH 77

int getline(char[], int);

main() {
    char in_line[MAXLENGTH];
    int count, in_line_length;

    in_line_length = 0;

    for (count = 0; count < MAXLENGTH; ++count)
        in_line[count] = 0;
    in_line_length = getline(in_line, MAXLENGTH);
    printf("%s", in_line);
}

int getline(char line[], int max_length) {
    int count, character;

    character = 0;

    for (count = 0; count < (max_length -2) && (character = getchar()) != EOF && character != '\n'; count++) {
        line[count] = character;
    }
    if (character = '\n') {
        line[count++] = '\n';
    }
    line[count] = '\0';
    return count;
}

I’m having a peculiar issue with the above code. As a little background, I’m compiling the above C code with the Tiny C Compiler (TCC) and running the executable in a Windows XP command prompt (cmd.exe).

The problem that appears to be occuring has to do with the EOF character. When running, if I issue the EOF character (CTRL+Z) and then hit return, the loop seems to exit correctly, but then character is populated with a newline character (\n).

So, something like this (as input):

test^Ztesttwo

Would output:

test\n

Due to the fact that the if statement following the for loop is always executed. What I’m wondering, is why the loop successfully exits upon recognition of the EOF character if it continues to grab another character from the stream?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:59:18+00:00Added an answer on May 21, 2026 at 6:59 am

    Your problem is this line:

    if (character = '\n') {
    

    it is an assigment instead of a test operator:

    if (character == '\n') {
    

    You can prevent erros like this by putting the constant left, that way you will get a compiler error if you make a mistake.

    if ('\n' = character) {
    //       ^ wont compile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include<stdlib.h> #define LIST.H onus; int main () { char *p,*s; printf( LIST.H
#include <stdio.h> #define MAX 5 int stk[MAX]; int top=-1; main() { char ch; void
#include vss.h #include vswriter.h #include <VsBackup.h> #include <stdio.h> #define CHECK_PRINT(result) printf(%s\n,result==S_OK?S_OK:error) int main(int argc,
#include stdio.h #define COUNT(a) (sizeof(a) / sizeof(*(a))) void test(int b[]) { printf(2, count:%d\n, COUNT(b));
#include <stdio.h> #define OPT //define for assembly int main() { char chr; for(chr =
#include<stdio.h> #include<conio.h> #define SQUARE(x) (x*x) void main() { clrscr(); int i=3,j,k; j=SQUARE(i++); k=SQUARE(++i); printf(\n%d\n%d\n%d,j,k,i);
#include <stdio.h> #include <stdlib.h> #define rsize 3 #define csize 3 int main() { char
#include <stdio.h> #define SIZE 5 void func(int*); int main(void) { int i, arr[SIZE]; for(i=0;
#include <stdio.h> #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() {
Got this in char.h #ifndef _CHAR_H_ #define _CHAR_H_ #include <stdio.h> template <unsigned int TChar>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.