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 8465355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:01:13+00:00 2026-06-10T15:01:13+00:00

I have the code below: #include <stdio.h> #include <unistd.h> int main () { int

  • 0

I have the code below:

#include <stdio.h>
#include <unistd.h>

int main () {

    int fd = open("filename.dat", O_CREAT|O_WRONLY|O_TRUNC, 0600);
    int result = write(fd, "abcdefghijklmnopqrstuvxz", 100);
    printf("\n\nfd = %d, result = %d, errno = %d", fd, result, errno);
    close(fd);
    return 0;
}

I am trying to understand what happens when I try to write more bytes to a file than I have available. So I am calling write and asking the program to write 100 bytes while I have much less than that. The result: a bunch of stuff from stdout ends up on filename.dat. If instead of 100 I use strlen("abcdefghijklmnopqrstuvxz"), I get the desired result. My question then is: why is the program trying to write beyond the '\0' character on my string? Is there some undefined behavior going on here?

  • 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-06-10T15:01:15+00:00Added an answer on June 10, 2026 at 3:01 pm

    My question then is: why is the program trying to write beyond the
    ‘\0’ character on my string?

    The function write(2) doesn’t care about 0-terminators. It actually doesn’t care about buffer contents at all: it will try to write as many bytes as you tell it.

    Is there some undefined behavior going on here

    Of course, trying to write more than you have might incur the wrath of the OS who could decide to terminate your process if it touches inaccessible memory.

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

Sidebar

Related Questions

I have some doubt on below code #include<stdio.h> int i=6; int main() { int
I have this code below #include <stdio.h> int main(){ int i=0; for(i=0; i<1000; i++)
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int
Consider the really simple code below: #include <stdio.h> #include <stdlib.h> int main() { int*
I have implemented below code : #include<stdio.h> #include<string.h> #include<ctype.h> #include<cstdlib> #include<sys/types.h> main() { int64_t
I have a piece of code shown below #include <stdio.h> #include <stdlib.h> void Advance_String(char
I have the code below. void *timer1_function(void * eit); pthread_t timer1; int thread_check1 =
I have a sample c file called itoa.cpp as below: #include <stdio.h> #include <stdlib.h>
I came across the sample code. assuming_order_of_execution_pitfall.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server

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.