Bird
Raised Fist0
Cprogramming~10 mins

Using errno in C - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to include the header needed to use errno.

C
#include <stdio.h>
#include [1]

int main() {
    return 0;
}
Drag options to blanks, or click blank then click option'
A<errno.h>
B<string.h>
C<stdlib.h>
D<unistd.h>
Attempts:
3 left
💡 Hint
Common Mistakes
Including instead of
Forgetting to include any header for errno
2fill in blank
medium

Complete the code to reset errno before a function call.

C
#include <stdio.h>
#include <errno.h>

int main() {
    errno = [1];
    return 0;
}
Drag options to blanks, or click blank then click option'
ANULL
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting errno to 1 or -1 instead of 0
Using NULL which is for pointers
3fill in blank
hard

Fix the error in checking if fopen failed by using errno.

C
#include <stdio.h>
#include <errno.h>

int main() {
    FILE *file = fopen("missing.txt", "r");
    if (file == NULL) {
        if (errno == [1]) {
            perror("File open error");
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
AENOENT
BEACCES
CEINVAL
DEEXIST
Attempts:
3 left
💡 Hint
Common Mistakes
Using EACCES which means permission denied
Using EINVAL which means invalid argument
4fill in blank
hard

Fill both blanks to print a custom error message with errno and its description.

C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    errno = 0;
    FILE *f = fopen("nofile.txt", "r");
    if (f == NULL) {
        printf("Error %[1]: %s\n", errno, [2](errno));
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ad
Bi
Cstrerror
Dperror
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d with perror (which prints directly)
Using %s with errno (which is an int)
5fill in blank
hard

Fill all three blanks to check errno after a failed open and print a message.

C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    errno = 0;
    FILE *fp = fopen("data.txt", "r");
    if (fp == NULL) {
        if (errno == [1]) {
            printf("Permission denied: %[2] - %s\n", errno, [3](errno));
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
AEACCES
Bi
Cstrerror
DENOENT
Attempts:
3 left
💡 Hint
Common Mistakes
Using ENOENT instead of EACCES for permission errors
Using %s for errno integer
Using perror instead of strerror in printf

Practice

(1/5)
1. What is the primary purpose of errno in C programming?
easy
A. To store error codes set by system or library functions when they fail
B. To count the number of errors in a program
C. To reset the program state after an error
D. To display error messages automatically

Solution

  1. Step 1: Understand what errno stores

    errno is a global variable that stores error codes set by system or library functions when they fail.
  2. Step 2: Differentiate from other options

    It does not count errors, reset program state, or display messages automatically.
  3. Final Answer:

    To store error codes set by system or library functions when they fail -> Option A
  4. Quick Check:

    errno stores error codes = C [OK]
Hint: Remember: errno holds error codes after failure [OK]
Common Mistakes:
  • Thinking errno counts errors
  • Assuming errno resets program state
  • Believing errno prints messages automatically
2. Which of the following is the correct way to include errno in a C program?
easy
A. #include
B. #include
C. #include
D. #include

Solution

  1. Step 1: Identify the standard header for errno

    The standard header file to use errno is <errno.h>.
  2. Step 2: Verify other options

    <error.h>, <err.h>, and <errors.h> are not standard headers for errno.
  3. Final Answer:

    #include <errno.h> -> Option B
  4. Quick Check:

    errno header = <errno.h> [OK]
Hint: Use #include to access errno [OK]
Common Mistakes:
  • Using wrong header files
  • Forgetting to include errno.h
  • Confusing with similar-sounding headers
3. What will the following C code print if the file nonexistent.txt does not exist?
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    FILE *f = fopen("nonexistent.txt", "r");
    if (!f) {
        printf("Error: %s\n", strerror(errno));
    }
    return 0;
}
medium
A. Error: No such file or directory
B. Error: Permission denied
C. Error: File exists
D. Error: Unknown error

Solution

  1. Step 1: Understand fopen failure and errno

    When fopen fails to open a non-existing file for reading, it sets errno to indicate the error, usually ENOENT meaning "No such file or directory".
  2. Step 2: Use strerror to get error message

    strerror(errno) converts the error code to a readable string, which will be "No such file or directory".
  3. Final Answer:

    Error: No such file or directory -> Option A
  4. Quick Check:

    fopen missing file error = "No such file or directory" [OK]
Hint: fopen missing file sets errno to ENOENT message [OK]
Common Mistakes:
  • Assuming errno is zero without checking fopen result
  • Confusing error messages for different errors
  • Not including string.h for strerror
4. Identify the error in this code snippet that uses errno:
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    errno = 0;
    FILE *f = fopen("file.txt", "r");
    if (f == NULL) {
        perror("File open error");
    }
    if (errno != 0) {
        printf("Error code: %d\n", errno);
    }
    return 0;
}
medium
A. Using perror instead of strerror is incorrect
B. Setting errno to 0 before fopen is unnecessary
C. Checking errno after fopen without checking fopen result first
D. Not closing the file pointer after fopen

Solution

  1. Step 1: Analyze errno usage after fopen

    errno should only be checked if the function indicates failure. Here, errno is checked regardless of fopen result, which can be misleading.
  2. Step 2: Confirm correct error checking pattern

    The correct pattern is to check if fopen returned NULL before reading errno. This code checks errno even if fopen succeeded.
  3. Final Answer:

    Checking errno after fopen without checking fopen result first -> Option C
  4. Quick Check:

    Check function result before errno [OK]
Hint: Always check function result before reading errno [OK]
Common Mistakes:
  • Reading errno without function failure
  • Confusing perror and strerror usage
  • Ignoring file close after fopen
5. You want to open a file for reading and print a custom error message if it fails. Which code snippet correctly uses errno and strerror to do this?
A) FILE *f = fopen("data.txt", "r");
   if (!f) {
       printf("Error: %s\n", strerror(errno));
   }

B) errno = 0;
   FILE *f = fopen("data.txt", "r");
   if (f == NULL) {
       printf("Error: %s\n", strerror(errno));
   }

C) FILE *f = fopen("data.txt", "r");
   if (f == NULL) {
       errno = 0;
       printf("Error: %s\n", strerror(errno));
   }

D) FILE *f = fopen("data.txt", "r");
   if (f == NULL) {
       perror("Error opening file");
   }
hard
A. Option A
B. Option B
C. Option C
D. Option D

Solution

  1. Step 1: Check correct error handling with errno

    Option D uses perror, which is the standard and simplest way to print a custom error message along with the error string corresponding to errno. It automatically handles errno correctly.
  2. Step 2: Identify issues in other options

    Option D does not reset errno before fopen, which is less safe. Option D resets errno before fopen and checks failure, but it's generally unnecessary to reset errno before calls that set it on failure. Option D resets errno after failure, clearing the error code before printing, which is incorrect.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Use perror for simple and correct error messages [OK]
Hint: Use perror for automatic error message printing [OK]
Common Mistakes:
  • Resetting errno unnecessarily
  • Resetting errno after failure
  • Using strerror without valid errno
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard