Hey fellow coders! Ready for a bit of a challenge? Multiple Choice Questions (MCQs) are the game, and we’re diving into the coolest ones for C programming.
Whether you’re prepping for a test, brushing up your skills, or just love a good brain teaser, these MCQs are here for you. We’ve got a bunch covering everything C-related.
Join us for some coding fun! It’s not just about testing; it’s about making learning C programming a blast. Let’s jump into the world of MCQs and have some coding adventures together! ????
C Programming MCQ
[qsm quiz=25]
Questions and Answers
How do you use the ‘goto’ statement in C?
The ‘goto’ statement in C is used to transfer control to a labeled statement in the code. To use it, define a label using a unique identifier followed by a colon. Then, use ‘goto’ followed by the label name to transfer control to that point in the code. However, excessive use of ‘goto’ can make code difficult to understand and maintain, so it should be used with caution.
What is the difference between ‘int’ and ‘int*’ in C?
‘int’ | ‘int*’ |
---|---|
‘int’ is a data type that stores an integer value. | ‘int*’ is a pointer type that stores the memory address of an ‘int’ variable. |
Example: int num = 10; | Example: int* ptr = # |
Represents the actual value of an integer. | Represents the memory location of an ‘int’ variable. |
Used for storing and performing operations on integer values. | Used for indirect access and manipulation of an ‘int’ variable through the pointer. |
Directly stores the value of an integer. | Stores the address of an ‘int’ variable. |
What is the purpose of the ‘static’ keyword in C?
The ‘static’ keyword in C has multiple purposes depending on its usage context. When used with a variable, it makes the variable retain its value between function calls. When used with a function, it limits the visibility of the function to the current source file. ‘static’ can also be used to declare static global variables, limiting their scope to the current file.
How do you create and use a file in C?
To create and use the file in C, you need to include the header file ‘stdio.h’. Use the ‘fopen’ function to create or open a file, specifying the file name and mode (‘w’ for write, ‘r’ for read, or ‘a’ for append). After that, you can use ‘fprintf’ to write data to the file, or ‘fscanf’ to read data from the file. Finally, close the file using ‘fclose’.
What is the purpose of the ‘feof’ function in C?
In C the ‘feof’ function is used to check whether the end of file has been reached during a file input operation. It takes a file pointer as input and returns a non-zero value if the end of the file has been reached, and 0 otherwise. ‘feof’ is helpful in determining whether further reading from the file is possible, while ensuring that file processing is done correctly.
How do you open a file in binary mode in C?
To open a file in binary mode in C, use the ‘fopen’ function and specify the file mode as “rb” for reading or “wb” for writing. For example, to open a file named “data.bin” for binary reading, use:
FILE *file = fopen("data.bin", "rb");
Similarly, for binary writing:
FILE *file = fopen("data.bin", "wb");
This allows you to read from or write to the file in binary format.
What is the purpose of the ‘fseek’ function in C?
The ‘fseek’ function in C is used to move the file position indicator to a specific location within a file. It allows you to seek a specific byte offset from the beginning, end, or current position of the file. By changing the file position, ‘fseek’ enables reading or writing data from a desired location in the file, facilitating random access and manipulation of file contents.
How do you read and write characters in a file in C?
To read and write characters to a file in C, you can use functions such as ‘fgetc’ and ‘fputc’. ‘fgetc’ reads a character from a file, while ‘fputc’ writes a character to a file. You can use these functions in a loop to read and write characters until you reach the end of the file. Remember to open the file in the proper mode (‘r’ for reading or ‘w’ for writing).
How do you use command-line arguments in C?
To use command-line arguments in C, the ‘main’ function accepts two parameters: ‘argc’ (argument count) and ‘argv’ (argument vector). ‘argc’ indicates the number of arguments passed to the program, while ‘argv’ is an array of strings containing the arguments. You can access individual arguments using ‘argv[index]’ where ‘index’ represents the position of the argument.
What is the purpose of the ‘strcmp’ function in C?
The ‘strcmp’ function in C is used to compare two strings. It takes two string inputs and returns an integer value indicating their relative order. If the strings are equal, strcmp returns 0. If the first string is lexicographically short, it returns a negative value. If the first string is lexicographically large, it returns a positive value. ‘strcmp’ is useful for sorting strings and doing string comparisons in C programs.
How do you concatenate strings in C?
To concatenate strings in C, you can use the ‘strcat’ function from the ‘string.h’ library. ‘strcat’ takes two string inputs and appends the second string to the end of the first, modifying the first. Make sure the first string has enough room to accommodate the concatenated result. Another option is to manually copy the characters from one string to the other using a loop and string indexing.
What is the purpose of the ‘strlen’ function in C?
The ‘strlen’ function in C is used to determine the length of a string, excluding the null character (‘\0’). It takes a string as input and returns the number of characters in the string. ‘strlen’ is useful for various string operations, such as finding the size of a string, iterating over characters, or dynamically allocating memory based on string length.
How do you convert a string to an integer in C?
To convert a string to an integer in C, you can use the ‘atoi’ function from the ‘stdlib.h’ library. ‘atoi’ takes a string input and returns its corresponding integer value. If the string is not a valid integer representation, the result may be undefined. Before using ‘atoi’ it is important to ensure that the string contains only numeric characters.
What is the purpose of the ‘malloc’ function in C?
The ‘malloc’ function in C is used to dynamically allocate memory on the heap. It allows you to request a specific amount of memory, and returns a pointer to the allocated memory block. This memory can be used to store data structures, arrays or other dynamic objects.
How do you use bitwise operators in C?
In C, bitwise operators are used to manipulate individual bits of integer values. The bitwise operators include AND (&), OR (|), XOR (^), left shift (<<), right shift (>>), and complement (~). These operators operate on binary representations of numbers, allowing you to perform bitwise operations such as setting, clearing, or toggling specific bits.
Certainly! Here’s a table summarizing the bitwise operators in C:
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
<< | Left shift |
>> | Right shift |
~ | Bitwise complement (unary operator) |
These operators can be used to perform bitwise operations on integers, allowing manipulation of individual bits within the binary representation of numbers.
What is the purpose of the ‘sizeof’ operator with structures in C?
In C the ‘sizeof’ operator is used to determine the size in bytes of a structure. It helps to calculate the total memory occupied by a structure including its members. ‘sizeof’ is useful for memory allocation, serialization and other operations involving structures in C programming.
How do you define and use a union in C?
To define a union in C, use the ‘union’ keyword followed by the union name and a list of member variables. The members of a union share the same memory location, allowing them to hold distinct values. To access union members, use the dot (.) operator. Only one member should have access at a time.
What is the purpose of the ‘extern’ keyword in C?
The ‘extern’ keyword in C is used to declare a global variable or function that has been defined in another source file. It tells the compiler that the variable or function is defined elsewhere and must be linked during the compilation process.
How do you define and use a macro in C?
To define a macro in C, use the ‘#define’ directive followed by the macro name and its value or expression. Macros are preprocessor directives that are replaced by their defined value during compilation. These are typically used to define constants, perform simple calculations, or create code snippets for code reuse. Macros are invoked using only their names in the code.
What is the purpose of the ‘volatile’ keyword in C?
In C the keyword ‘volatile’ is used to indicate that a variable can be modified by external sources outside the control of the program. This informs the compiler not to optimize away or cache the variable, ensuring that its value is always read from or written to the memory location.
How do you handle errors in C using the ‘errno’ variable?
In C, the ‘errno’ variable is a global variable that stores error codes related to system and library functions. After a function call, you can check ‘errno’ to determine if an error occurred. The ‘errno’ value can be accessed using ‘#include ‘.
How do you use the ‘time’ function in C?
In C, the ‘time’ function from the ‘time.h’ library is used to retrieve the current time from the system clock. Returns the number of seconds elapsed since a specified reference point. The ‘time’ function is often used for tasks such as timestamping, measuring execution time, or generating random numbers based on time.
What is the purpose of the ‘rand’ function in C?
The ‘rand’ function in C is used to generate pseudo-random numbers. It returns an integer value between 0 and RAND_MAX. By using ‘srand’ function, you can set a seed value to generate different sequences of random numbers.