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

  • SEARCH
  • Home
  • 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 6091811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:22:14+00:00 2026-05-23T12:22:14+00:00

#include <unistd.h> int main(int argc, char* argv[]) { char buf[500]; read(0, buf, 5); return

  • 0
#include <unistd.h>
int main(int argc, char* argv[])
{
  char buf[500];
  read(0, buf, 5);
  return 0;
}

The above read 5 characters from stdin,but if I input more than 5:

12345morethan5
[root@ test]# morethan5
-bash: morethan5: command not found

The remaining characters will be executed as shell commands.

Is this kind of behavior defined by standard?

  • 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-23T12:22:15+00:00Added an answer on May 23, 2026 at 12:22 pm

    Sort of 🙂

    Your program reads 5 characters, and that’s it. Not less, not more. The rest remain in the terminal buffer and get sent to your shell once your C program terminates.

    Since you are using read(), which is a raw system call, instead of any of the C stdio buffering alternatives this behaviour is not just expected, but required.

    From the POSIX standard on read():

    The read() function shall attempt to
    read nbyte bytes from the file
    associated with the open file
    descriptor, fildes, into the buffer
    pointed to by buf.

    …

    Upon successful completion, where
    nbyte is greater than 0, read() shall
    mark for update the st_atime field of
    the file, and shall return the number
    of bytes read. This number shall never
    be greater than nbyte.

    …

    Upon successful completion, read()
    [XSI] [Option Start] and pread()
    [Option End] shall return a
    non-negative integer indicating the
    number of bytes actually read.

    I.e. read() should never read more bytes from the file descriptor than requested.

    From the related part on terminals:

    It is not, however, necessary to read
    a whole line at once; any number of
    bytes, even one, may be requested in a
    read() without losing information.

    …

    The last process to close a terminal device file shall cause any output to be sent to the device and any input to be discarded.

    Note: normally your shell will still have an open file descriptor for the terminal, until you end the session.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main (int argc, const char * argv[])
#include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char **argv) { int fd[2]; pid_t childpid; pipe(fd); childpid=fork();
#include <libnotify/notify.h> #include <glib.h> #include <unistd.h> int main(int argc, char** argv) { if(argc ==
#include <unistd.h> #include <stdio.h> int main(int argc, char* argv[]) { int f1[2], f2[2]; char
#include<unistd.h> int main(int argc, char **argv) { int ret; ret = execve(/bin/bash, NULL, NULL);
#include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <stdio.h> int main(int argc, char
Here is my code: #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> int main(int argc,char
Consider this pointless program: /* main.c */ #include <stdlib.h> #include <unistd.h> int main(int argc,
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits

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.