Explanation: The `getchar()` function is used to read a single character from the standard input (keyboard).
Explanation: The `putchar()` function is used to print a single character to the standard output (screen).
Explanation: The `scanf()` function is used to read formatted input from the standard input, allowing the programmer to specify the type of input expected.
Explanation: The `printf()` function is used to print formatted output to the standard output, providing control over the format of the output.
Explanation: The format specifier `%d` is used in `printf()` to print an integer value.
Explanation: The `fgets()` function reads a string of characters from the standard input, which is a safer alternative to `gets()` as it allows specifying the maximum number of characters to read.
Explanation: The format specifier `%s` is used in `printf()` to print a string.
Explanation: The `gets()` function is deprecated because it does not perform bounds checking, which can lead to buffer overflows.
Explanation: To read an integer value, the `%d` format specifier is used with the `scanf()` function, and the address of the variable is passed to store the input.
Explanation: The `getchar()` function returns an `int` because it needs to be able to return any character and the special value `EOF` to indicate the end of input.
Explanation: The format specifier `%f` is used in `printf()` to print a floating-point number.
Explanation: The escape sequence `\n` is used in `printf()` and `puts()` to print a new line.
Explanation: The `fscanf()` function is used to read formatted input from a file, similar to how `scanf()` reads from standard input.
Explanation: The format specifier `%c` is used in `scanf()` to read a single character from the input.
Explanation: The `sprintf()` function is used to write formatted output to a string, similar to how `printf()` writes to standard output.
Explanation: The width specifier in `printf()`, such as `%10d`, sets a minimum field width for the output, ensuring that the printed value occupies at least 10 characters.
Explanation: The `-` flag in `printf()` is used to left-align the output within the specified field width.
Explanation: The format specifier `%.2f` in `printf()` is used to print a floating-point number with 2 decimal places.
Explanation: The `scanf()` function is used to read a floating-point number from the standard input using the `%f` format specifier.
Explanation: The return value of `scanf()` represents the number of input items that were successfully matched and assigned to the provided variables.
Explanation: The format specifier `%u` is used in `printf()` to print an unsigned integer.
Explanation: The format specifier `%x` is used in `printf()` to print an integer in hexadecimal format.
Explanation: The format specifier `%o` is used in `printf()` to print an integer in octal format.
Explanation: The format specifier `%p` is used in `printf()` to print a pointer address.
Explanation: The format specifier `%ld` is used in `printf()` to print a long integer.
Explanation: The format specifier `%lf` is used in `scanf()` to read a double precision floating-point number.
Explanation: The format specifier `%s` is used in `printf()` to print a string.
Explanation: The format specifier `%5.2f` in `printf()` sets the width to 5 and the precision to 2 for a floating-point number.
Explanation: The format specifier `%+d` in `printf()` is used to print a signed integer with a leading plus or minus sign.
Explanation: The format specifier `%e` is used in `printf()` to print a floating-point number in scientific notation.
Explanation: The `fopen()` function is used to open a file in C for reading, writing, or appending.
Explanation: To open a file in read mode, the correct syntax is `fopen(“data.txt”, “r”);`.
Explanation: The mode `”w”` is used with `fopen()` to create a new file for writing. If the file already exists, it will be truncated.
Explanation: If `fopen()` fails to open a file, it returns `NULL`.
Explanation: The `fclose()` function is used to close an opened file in C.
Explanation: The correct way to close a file pointer named `fp` is `fclose(fp);`.
Explanation: The `fread()` function is used to read data from a file into a buffer.
Explanation: The correct syntax is `fread(array, sizeof(int), 10, fp);`, where `fp` is the file pointer.
Explanation: The `fwrite()` function is used to write data from a buffer to a file.
Explanation: The correct syntax is `fwrite(array, sizeof(int), 10, fp);`, where `fp` is the file pointer.
Explanation: The `fprintf()` function is used to print formatted output to a file.
Explanation: To write the string “Hello, World!” to a file, the correct syntax is `fprintf(fp, “Hello, World!”);`.
Explanation: The `fscanf()` function is used to read formatted input from a file.
Explanation: The correct syntax to read an integer from a file using `fscanf()` is `fscanf(fp, “%d”, &num);`.
Explanation: Modes `”r+”`, `”w+”`, and `”a+”` can be used with `fopen()` to open a file for both reading and writing.
Explanation: If you try to open a file that does not exist in `”r”` mode, `fopen()` returns `NULL`.
Explanation: The `rewind(fp)` function moves the file pointer to the beginning of a file.
Explanation: The `fseek()` function is used to move the file pointer to a specific location in a file.
Explanation: The `ftell()` function returns the current position of the file pointer.
Explanation: To determine the size of a file, you can use `fseek()` to move to the end of the file and `ftell()` to get the position of the file pointer, which indicates the file size.
Explanation: The `fputc()` function is used to write a single character to a file.
Explanation: The `fgetc()` function is used to read a single character from a file.
Explanation: The return type of the `fread()` and `fwrite()` functions is `size_t`, which indicates the number of items successfully read or written.
Explanation: The mode `”a”` in `fopen()` is used to open a file for appending. If the file does not exist, it is created.
Explanation: The `ftell()` function is used to get the current position of the file pointer in a file.\
Explanation: The `”r”` mode opens an existing file for reading only. If the file does not exist, the `fopen()` function fails.
Explanation: When a file is opened in `”w”` mode, it is truncated to zero length. If the file does not exist, it is created.
Explanation: The `”a”` mode opens a file for appending data at the end. If the file does not exist, it is created.
Explanation: The `”r+”` mode opens a file for both reading and writing, starting at the beginning of the file. If the file does not exist, the `fopen()` function fails.
Explanation: Opening a file in `”w+”` mode truncates the file to zero length. If the file does not exist, it is created.
Explanation: The `”a+”` mode opens a file for both reading and writing. It appends data at the end of the file. If the file does not exist, it is created.
Explanation: The `”r+”` mode allows a file to be opened for both reading and writing without truncating its content.
Explanation: The `”w+”` mode creates a new file for reading and writing. If the file already exists, it is truncated to zero length.
Explanation: The `”a”` mode will create a new file if it does not exist and append data to it if it does exist.
Explanation: When opening a file in `”r”` mode, if the file does not exist, an error occurs and `fopen()` returns `NULL`.
Explanation: The `”a+”` mode ensures that a new file is created if it does not exist and existing content is preserved while allowing for both reading and writing.
Explanation: In `”a”` mode, the file pointer moves to the end of the file before writing starts, allowing data to be appended.
Explanation: The `”a+”` mode opens an existing file for both reading and writing and starts at the end of the file, allowing for appending.
Explanation: If you try to open a file in `”w”` mode but the file system is read-only, `fopen()` will return `NULL`.
Explanation: The `”r+”` mode should be used to modify the contents of an existing file without truncating it, allowing for both reading and writing.
Explanation: Opening a file in `”w+”` mode truncates the file to zero length, effectively erasing any existing content.
Explanation: In `”a+”` mode, the file pointer moves to the end of the file after each write operation, ensuring that data is always appended.
Explanation: The `”w”` mode opens a file for writing and will overwrite any existing content if the file already exists.
Explanation: When a file is opened in `”a”` mode, any new data written to it will be placed at the end of the file.
Explanation: The `”a+”` mode allows you to read from and write to a file, with all write operations appending data at the end of the file.
Explanation: The `”x”` mode creates a new file for writing, but if the file already exists, the `fopen()` function fails.
Explanation: The `”x”` mode will cause `fopen()` to fail if the file already exists, ensuring that a new file is created only if it does not already exist.
Explanation: The `”a+”` mode opens a file for both reading and writing, ensuring that the file is created if it does not exist, and all content is appended at the end.
Explanation: The `”x”` mode safely creates a new file for writing and will fail if the file already exists, ensuring that no existing content is truncated.
Explanation: The `”r+”` mode opens an existing file for reading and writing without truncating its content, whereas `”w+”` mode creates a new file or truncates an existing file for reading and writing.
int main() {
int x = 7;
printf("%d\n", x);
return 0;
}
Explanation: The `printf` function prints the value of the variable `x`, which is 7.
int main() {
char ch = 'A';
printf("%c\n", ch);
return 0;
}
Explanation: The `printf` function with the `%c` format specifier prints the character value stored in `ch`, which is ‘A’.
int main() {
int a = 10, b = 20;
printf("%d %d\n", a, b);
return 0;
}
Explanation: The `printf` function prints the values of `a` and `b`, which are 10 and 20 respectively.
int main() {
float f = 3.14;
printf("%.2f\n", f);
return 0;
}
Explanation: The `printf` function with the `%.2f` format specifier prints the float value `f` with two decimal places, which is 3.14.
int main() {
printf("%d\n", printf("Hello"));
return 0;
}
Explanation: The inner `printf` prints “Hello” and returns the number of characters printed (5). The outer `printf` prints this return value (5) followed by a newline.
int main() {
int a = 5;
scanf("%d", &a);
printf("%d\n", a);
return 0;
}
Assume the input provided by the user is 10.Explanation: The `scanf` function reads an integer from the user and stores it in `a`. The `printf` function then prints the value of `a`, which is 10.
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
Assume the input provided by the user is 42.Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input, and the second `printf` prints the entered number.
int main() {
int x;
x = printf("Hello");
printf("%d\n", x);
return 0;
}
Explanation: The first `printf` prints “Hello” and returns the number of characters printed (5). The second `printf` prints this return value (5) followed by a newline.
int main() {
char str[10];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
Assume the input provided by the user is “world”.Explanation: The first `printf` prompts the user to enter a string. The `scanf` reads the input, and the second `printf` prints the entered string.
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
return 0;
}
Assume the input provided by the user is ‘A’.Explanation: The first `printf` prompts the user to enter a character. The `scanf` reads the input, and the second `printf` prints the entered character.
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("You entered: %d and %d\n", num1, num2);
return 0;
}
Assume the input provided by the user is “15 30”.Explanation: The first `printf` prompts the user to enter two numbers. The `scanf` reads the input, and the second `printf` prints the entered numbers.
int main() {
float f;
printf("Enter a float: ");
scanf("%f", &f);
printf("You entered: %.2f\n", f);
return 0;
}
Assume the input provided by the user is “3.1415”.Explanation: The first `printf` prompts the user to enter a float. The `scanf` reads the input, and the second `printf` prints the entered float value rounded to two decimal places.
int main() {
int num;
printf("Enter a number: ");
int result = scanf("%d", &num);
printf("scanf returned: %d\n", result);
return 0;
}
Assume the input provided by the user is “25”.Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input and returns the number of successfully read items, which is 1 in this case. The second `printf` prints this return value.
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Hexadecimal: %x\n", num);
return 0;
}
Assume the input provided by the user is “255”.Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input, and the second `printf` prints the entered number in hexadecimal format (lowercase), which is `ff`.
int main() {
double d;
printf("Enter a double: ");
scanf("%lf", &d);
printf("You entered: %.4lf\n", d);
return 0;
}
Assume the input provided by the user is “123.456789”.Explanation: The first `printf` prompts the user to enter a double. The `scanf` reads the input, and the second `printf` prints the entered double value rounded to four decimal places, which is 123.4568.
int main() {
char str[20];
printf("Enter a string: ");
scanf("%19s", str);
printf("You entered: %s\n", str);
return 0;
}
Assume the input provided by the user is “HelloWorld”.Explanation: The first `printf` prompts the user to enter a string. The `scanf` reads the input, and the second `printf` prints the entered string.
int main() {
int a = 10;
int b = 20;
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}
Explanation: The `printf` function prints the values of `a` and `b` and their sum, which is `10 + 20 = 30`.
int main() {
int a = 5, b = 10, c;
c = a * b;
printf("Multiplication result: %d\n", c);
return 0;
}
Explanation: The `printf` function prints the result of the multiplication of `a` and `b`, which is `5 * 10 = 50`.
int main() {
int x = 0;
if (x)
printf("x is true\n");
else
printf("x is false\n");
return 0;
}
Explanation: In C, `0` is considered false. Since `x` is `0`, the else block is executed, printing “x is false”.
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Octal: %o\n", num);
return 0;
}
Assume the input provided by the user is “16”.Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input, and the second `printf` prints the entered number in octal format, which is `20` in octal for the decimal number `16`.