What is dup() in C?

The C programming language is a general-purpose programming language that provides low-level mechanisms through which the system users can interact with the operating systems kernel, which makes it widely used in system programming.

The dup() function

The dup() function, provided by the unistd.h library, is used in C programs to allow processes to duplicate existing file descriptorsThey are integer numbers that are used to access files to perform file manipulation functions in C programs.. The dup() function replaces the lowest-level unused file descriptor to place the copied file descriptor.

We can understand how the function works with the help of the diagram below.

Working of dup() function
Working of dup() function

In the diagram above, we see four opened file descriptors with one empty slot where another file descriptor can be placed. We close the file descriptor 2 using the close(2) function so we have two empty places to add new file descriptors. Now if we use dup(32),we see that the file descriptor 32 gets copied to the lowest available slot.

Syntax

Now that we have understood the function's working, let's look at its syntax.

int dup(int fileDesciptor);
Syntax for dup() function

The function takes in an integer variable named fileDesciptor that represents the file descriptor that we want to copy.

Upon successful execution, it returns the new file descriptor to access the file descriptor we just copied. If the function fails, it returns a -1 value that can be used for error handling.

Code example

Now let's look at a C code example to understand further how the dup() function works.

Hello Educative User! <3
C code for dup() function

Code explanation

  • Line 9: We open the file named file1.txt in the 'read and write' mode using the open() function.

  • Line 10: Then, we print the file description for the file we opened.

  • Line 13: Now, we close the standard read file for the program using the function close(0).

  • Lines 16–17: Here, we use the dup() function to copy the file descriptor for the file file1.txt and print the new file descriptor returned by the function, which we can use to access the file.

  • Line 20: We create a buffer variable that we will use to store the file's content.

  • Line 23: Now, we use the read() function to read data from the file. However, in the function, we use the new copied file descriptor returned by the dup() function instead of the original one returned by the open() function.

  • Line 26: Finally, we print the contents retrieved from the file and store them in the buffer variable.

When we run the code, we first print the file descriptor for the opened file (file1.txt) that is returned to us via the open() function. Then, we close the standard read file using its descriptor value (0) and copy the file descriptor for the opened file in place of the standard read file using the dup() function.

When we use the file descriptor 0, we are essentially accessing the file named file1.txt. As we can see in the read() function, we use the file descriptor 0 instead of the file descriptor returned by the open() function to read data from the file.

Practice quiz

Now that we have understood what the dup() function is, and how it works in C programs. Let's try to test what we learned by solving the quiz below.

1

Which header file should be included to use the dup() function?

A)

unistd.h

B)

stdlib.h

C)

sys/types.h

D)

fcntl.h

Question 1 of 50 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved