Demystifying File Permissions in C: A Beginner's Guide

Photo by Kyle Glenn on Unsplash

Demystifying File Permissions in C: A Beginner's Guide

Mastering File Permissions in C: Best Practices, Practical Examples, Mnemonics, and Tips for Beginners

Introduction to File Permissions in C

To understand the concept of file permissions in C, you can liken it to giving out the keys to your house so that others can have access to it. You own the house and therefore you are the owner; you always have possession of the keys to your house. A select group of people can also have the keys which could be your siblings and this is known as a "group" in C file permissions. If you wish, you can also allow "others" to have the keys and these could be your friends or even strangers!
In summary, file permissions in C are represented by three groups: Owner, Groups, and Others.
Permissions are important to prevent unauthorized access to your house. Hence, you need to give permissions to any of the above groups before they can get the keys to your house and this is exactly how it works in C programming.

Understanding File Permissions

In C programming, you are not dealing with a house but with files.
The three types of permissions allowed for each of the three groups are; Read, Write, and Execute.

  • Read permission as the name implies, allows someone to read the file.

  • Write permission allows someone to modify a file.

  • Execute permission allows someone to run or execute a file.

Numeric Representation of File Permissions

These permissions are represented numerically. Once you understand this little trick I am about to show you, you’ll never have challenges with file permissions.

Mnemonics

  • Read (r) is represented numerically by number 4.

  • Write (w) is represented numerically by number 2.

  • Execute (x) is represented numerically by number 1.

File Permission Codes

Now pay attention. If the owner of a file has Read, Write, and Execute permissions for a particular file, that means, they can read the contents of the file, modify the contents of the file, and also run or execute the file (rwx). Let's also say the group has permission to execute a file (--x) and if others have permission to execute a file (--x).

Let us represent the examples above numerically.

  • Owner: Read-Write-Execute (rwx) 4 + 2 + 1 = 7

  • Group: Execute (--x) 0 + 0 + 1 = 1

  • Others: Execute (--x) 0 + 0 + 1 = 1

    The file permissions code will be represented as 711.

What if an owner has permission to read, write, and execute a file (rwx)? The group has permissions to only read and execute a file (r-x). Others have permissions to only execute a file (--x).
Let us represent this numerically as well;

  • Owner: Read-Write-Execute (rwx) 4 + 2 + 1 = 7

  • Group: Execute (r-x) 4 + 0 + 1 = 5

  • Others: Execute (--x) 0 + 0 + 1 = 1

The file permissions code will be represented as 751.

Changing Permissions Programmatically

Now that you have learned about file permission and the numerical codes that represent different file permissions, here comes the big question;

How do I change file permissions in C?

💡
You can achieve this by using system calls like “chmod”.

Let’s say you have a file called permissions.c and you want all groups (owner, groups, and others) to read, write, and execute (rwx) such file.
In your terminal, you will navigate to the directory where that file is located and type:

chmod 777 permissions.c

I am sure you understand why the numerical code is 777?
Let me explain in case you failed to get it right.

  • Owner: Read-Write-Execute (rwx) 4 + 2 + 1 = 7

  • Group: Read-Write-Execute (rwx) 4 + 2 + 1 = 7

  • Others: Read-Write-Execute (rwx) 4 + 2 + 1 = 7

Best Practices

You should always limit file permissions as much as needed because you do not want unauthorized access to your files or accidental changes to critical files.

Conclusion

Make sure to practice file permissions till you understand the concept fully without looking at a book or Googling. Most importantly, leave a comment if this article was helpful to you.

Here’s a recap of all we’ve learned so far:

  • Introduction.

  • Understanding File Permissions in C.

  • Numerical representation of file permissions.

  • Practical Examples.

  • Changing file permissions programmatically.

  • Best practices.

  • Conclusion.