0
0
Cprogramming~10 mins

Using errno in C - Interactive Code Practice

Choose your learning style9 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