Abstract Classes · IGNOU Solved Assignments · UPSC Maths Optional · CBSE Maths
a = 2b = 4c = 5a = b + ca = 4 + 5a = 9b = a + cb = 9 + 5b = 14c = a - bc = 9 - 14c = -5printf("%d,%d,%d", a, b, c);9,14,-59,14,-5.printf function is adding the ASCII values of the characters ‘C’, ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, and ‘m’, and then printing the sum as an integer.795.j = 1i = 1, 3, 5, 7, 9.j is updated as j = i - j.j <= 2, printing i + j and incrementing j by 1 each time.j = 1 - 1 = 0j <= 2, so it prints 1 + 0 = 1, then j becomes 1 and prints 1 + 1 = 2, then j becomes 2 and prints 1 + 2 = 3, then j becomes 3 and the loop stops.j = 3 - 2 = 1j <= 2, so it prints 3 + 1 = 4, then j becomes 2 and prints 3 + 2 = 5, then j becomes 3 and the loop stops.j = 5 - 1 = 4j > 2, so it doesn’t print anything.j = 7 - 4 = 3j > 2, so it doesn’t print anything.j = 9 - 3 = 6j > 2, so it doesn’t print anything.1 2 3 4 5.int *p = 2; is trying to assign an integer value to a pointer, which is not correct in C. Pointers should be assigned the address of a variable or memory allocated using functions like malloc.p a pointer to an integer with the value 2, it should be done like this:int val = 2;
int *p = &val;
p as a normal integer variable (not a pointer) with the value 2, it should be declared as:int p = 2;
p is meant to be a normal integer variable), the corrected code would look like this:main()
{
int a = 10, b = 20, c = 30;
int p = 2;
a = c / p;
b = c;
printf("a = %d, b = %d", a, b);
}
a is assigned the value of c / p, which is 30 / 2 = 15.b is assigned the value of c, which is 30.a = 15, b = 30
printf statement is fun, but the function is defined as func. These names should match. Assuming the function name is intended to be fun, here’s the corrected code:#include <stdio.h>
int fun(int x)
{
static int a = 0;
a += x;
return a;
}
int main()
{
int p;
for (p = 1; p <= 5; ++p)
printf("%d ", fun(p));
return 0;
}
fun: This function takes an integer x as an argument and has a static integer variable a. The static variable a retains its value between function calls. In each call, x is added to a, and the updated value of a is returned.p = 1 to p = 5.fun is called with the current value of p, and the returned value is printed.p = 1): a = 0 + 1 = 1, prints 1.p = 2): a = 1 + 2 = 3, prints 3.p = 3): a = 3 + 3 = 6, prints 6.p = 4): a = 6 + 4 = 10, prints 10.p = 5): a = 10 + 5 = 15, prints 15.1 3 6 10 15.getchar(), putchar(), gets(), and puts() are standard input/output functions in C used for character and string handling.getchar(): This function is used to read a single character from the standard input (usually the keyboard). It doesn’t take any arguments and returns the read character. If there’s an error or end of file is reached, it returns EOF.putchar(): This function is used to write a single character to the standard output (usually the console). It takes a character as an argument and returns the written character. If there’s an error, it returns EOF.gets(): This function is used to read a string from the standard input into a buffer until a newline character is encountered or end of file is reached. It’s considered unsafe because it can lead to buffer overflow, so it’s better to use fgets() instead.puts(): This function is used to write a string to the standard output followed by a newline character. It takes a string as an argument and returns a non-negative number if successful, or EOF if there’s an error.#include <stdio.h>
int main() {
char c;
char str[100];
printf("Enter a character: ");
c = getchar(); // Read a character
printf("You entered: ");
putchar(c); // Write the character
printf("\n");
printf("Enter a string: ");
gets(str); // Read a string (unsafe, consider using fgets instead)
printf("You entered: ");
puts(str); // Write the string with a newline
return 0;
}
getchar() is used to read a single character from the user.putchar() is used to display the entered character.gets() is used to read a string from the user (though it’s unsafe and not recommended).puts() is used to display the entered string with a newline at the end.fgets() instead of gets() for reading strings to avoid buffer overflow issues.* (unary): Unary operators have the highest precedence among the listed operators. Associativity: Right to left.++ (unary): Unary increment operator has the same precedence as the unary * operator. Associativity: Right to left.* (binary): Binary multiplication operator. Associativity: Left to right.%: Remainder operator has the same precedence as the binary * operator. Associativity: Left to right.+: Binary addition operator. Associativity: Left to right.-: Binary subtraction operator has the same precedence as the binary + operator. Associativity: Left to right.!=: Inequality operator. Associativity: Left to right.+=: Addition assignment operator has the lowest precedence among the listed operators. Associativity: Right to left.* (unary), ++ (unary) [Right to left]* (binary), % [Left to right]+, - (binary) [Left to right]!= [Left to right]+= [Right to left]isalpha(), isdigit(), and isspace() from the ctype.h header file to classify each character. Here’s an example in C:#include <stdio.h>
#include <ctype.h>
int main() {
char text[] = "Hello, World! 12345 \t\n";
int letters = 0, digits = 0, whitespace = 0, others = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) {
letters++;
} else if (isdigit(text[i])) {
digits++;
} else if (isspace(text[i])) {
whitespace++;
} else {
others++;
}
}
printf("Letters: %d\n", letters);
printf("Digits: %d\n", digits);
printf("Whitespace characters: %d\n", whitespace);
printf("Other characters: %d\n", others);
return 0;
}
isalpha() checks if a character is an alphabetic letter.isdigit() checks if a character is a digit.isspace() checks if a character is a whitespace character (space, tab, newline, etc.).'\0' at the end of the string.