C Programming

How To Use Write System Call In C

The system call serves as the primary gateway in between programs and the Linux operating system kernel. Although you would almost certainly need to have a system call throughout your C programming career, whether you aim for exceptional productivity or a certain style of feature, the Glibc repository or other standard libraries found in popular Linux distros will suffice for the vast majority of your requirements. In this guide, we will establish a concept about the Write System call in C language.



ssize_t write(int fd, const void *buf, size_t count);

In this above syntax, the first line shows the library for system calls. On the second line, fd stands for a file descriptor which is indeed a number that specifies a process’s open file. The keyword *buf stands for buffer. This contains any data in it. Next is count. It is the number of bytes to be written to a file descriptor from the buffer.

Install Prerequisites:

To run any C language code, you have to first install some packages in Linux distribution. If you want to see further information regarding the write system call, you have to install the manpages-dev package to do so. To open the terminal using the Ctrl+Alt+T shortcut key. After opening it, write the below apt install command followed by the manpages-dev keyword to install manpages.

$ sudo apt install manpages-dev

It will take some time to install manpages. Wait for it to get completed.

After the installation, you can see the additional information about the “write” system call via the below man command in the shell.

$ man 2 write

The output man page for the “write” command is given below. You can read the information about it. Press q to quit the manual.

Now it’s time to install the “Gcc” package in your Linux system to work in the C language. For that, open the shell and write the below apt install command followed by the keyword “gcc”.

$ sudo apt install gcc

Meanwhile, during the installation, it may ask you to confirm this action of installation by pressing “y” to continue and “n” to stop it. So, tap the “y” key and hit the Enter key.

It will be installed in your system within some minutes, and the last lines of output will be as shown in the below snapshot.


Let’s have some examples of a “write” system call in our Linux distribution. Therefore, open the command-line shell and create a new C file in it with the dot “c” extension. You have to use the below nano command to quickly create and open it. We have given it a name as “new.c”.

$ nano new.c

The below window will be opened. Now you have to write the affixed code in this file. In this code, we have included a unistd.h library first. Then we have created the main function, and within this function, we have created a “write” system call. In this system call, the very first parameter is the file descriptor. In this case, integer 1 represents the output device screen, and it is fixed. So our output will be shown on the screen. The second parameter shows the buffer data. You can add anything to it. And the last parameter shows the count number for the data given in the buffer parameter. As we have mentioned 5 as the count number, it will only show the first 5 bytes of the buffer data and ignore the bytes left. Save this code using the Ctrl+S shortcut key and exit this file using the Ctrl+X command.


Int main()

{ Write(1, “Aqsa Yasin”, 5); }

Now in the terminal, try the below gcc command to compile this newly created code with a file name.

$ gcc new.c

Let’s check out this code by using the default a.out file created at run time. So, try the below a.out command to check the output of the “write” system call. We have to use it with “./” to pick the file from the current directory.

$ ./a.out

The output you can see has only displayed the word “Aqsa” because of the count number 5.

Let’s change our code a little bit. We have changed the count number to 11 because we have a total of 11 bytes in our buffer data “Aqsa Yasin”. So this time, the output will show the whole bytes of string data “Aqsa Yasin”. Save this file using Ctrl+S and quit using the Ctrl+X shortcut key.

Let’s compile the updated code using the newly installed gcc compiler for Linux distribution. Try the same gcc command along with the name of a file as “new.c”.

$ gcc new.c

Now display the output of the same code using the previous “a.out” command as below. The output shows the full string “Aqsa Yasin” of the buffer.

$ ./a.out

Let’s see how the code works when we take the count number greater than the total number of bytes the buffer data contains. So, we have been using the count as 30. Save and close the file.

Compile the same updated file using gcc command as below.

$ gcc new.c

Now execute the file using the a.out command, and the output will show the buffer data as well as some garbage value with it as displayed in the snapshot output below.$ ./a.out

You can also check the total number of bytes written into the file by using the below code in the same file.

Compile the code by gcc instruction.

$ gcc new.c

The output shows the total number of bytes, including buffer data and line break, using the a.out command.

$ ./a.out


In this tutorial, we have elaborated the concept to use the “Write” system call in C. Try all the examples to understand the idea of the “write” system call using this article.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.