C Programming Exam Questions and Tasks
C Programming Exam Questions and Tasks
Conversion characters in C are used in 'printf' functions to specify the type of data to be printed. The character 'c' prints a single character. 'd' is for decimal integers, 'e' is for scientific notation of floats, 'f' for floating-point numbers in normal notation, and 's' prints string of characters. Each conversion character adjusts how the data is formatted, ensuring values are displayed properly based on their type. For example, 'printf("%d", 10);' outputs '10', while 'printf("%f", 10.5);' outputs '10.500000' .
Evaluating assignment expressions in C involves following operator precedence rules and using initially declared variable values. For example, with 'int i=8, j=5, k;', evaluating 'k=(j==5)?i:j' results in k=8 as the condition is true, so 'i' is assigned. For 'k=(j>5)?i:j', k=5 since the condition is false, assigning 'j'. In 'z=(x>=0)?x:0' with float x=0.005, 'x' is positive, so z=0.005. With 'i-=(j>0)? j:0', because j>0, 'i' becomes i-j=3. These evaluations show how expressions are assessed according to precedence and conditions .
Unary operators in C programming operate on a single operand and perform operations such as incrementing/decrementing a value or determining its type. Common unary operators include the increment ('++') and decrement ('--') operators. They are unique because they change or utilize only one operand, whereas binary or ternary operators require two or more operands. For example, using '++i' or 'i++' increases the value of 'i' by one .
In C programming, managing input and output can be done effectively using functions like 'scanf' and 'printf' for formatted user input and output. However, for character-by-character manipulation, 'getchar()' and 'putchar()' are available, providing finer control. For example, to read and display characters 'a', 'b', 'c', 'getchar()' and 'putchar()' are used: 'a = getchar();', 'putchar(a);'. Alternatively, 'scanf("%c%c%c", &a, &b, &c); printf("%c%c%c", a, b, c);' handles it in a single input/output operation, allowing batch processing and easier management compared to individual character functions .
Some common standard keywords in C programming include 'int', 'return', 'if', 'for', and 'while'. These keywords serve specific roles and instructions within the programming language, such as declaring variables, controlling the flow of execution, and creating loops or conditional statements .
In C programming, the 'FOR' statement is used for loops and has the syntax: 'for(initialization; condition; increment) { //code block }'. The 'WHILE' statement continuously executes a block of code as long as a specified condition is true, with the syntax: 'while(condition) { //code block }'. The 'SWITCH' statement allows the execution of code among various options based on a variable's value: 'switch(expression) { case value1: //code block; break; ... default: //code block; }' .
To calculate simple interest in C, you need the principal amount 'P', time period 'T', and rate 'R'. The formula used is Simple Interest (SI) = P*T*R/100. A simple C program for this is: 'int main() { float P, T, R, SI; scanf("%f %f %f", &P, &T, &R); SI = (P*T*R)/100; printf("Simple Interest is: %.2f", SI); return 0; }'. This program takes user input for principal, time, and rate, and computes the simple interest using the defined formula .
In C programming, an identifier is considered valid if it begins with a letter (either uppercase or lowercase) or an underscore, followed by any combination of letters, digits, or underscores. For example, 'Record1' is valid because it starts with a letter and includes letters and digits. '1record' is invalid because it begins with a digit. 'file_3' is valid as it starts with a letter and follows the rules. 'return' is invalid as it's a reserved keyword in C. Lastly, '$tax' is invalid because the dollar sign is not permitted in identifiers .
In C, to calculate the roots of a quadratic equation given by ax^2 + bx + c = 0, using the formula x = (−b ± √(b^2−4ac))/(2a), one must first check whether a, b, or c are zero to avoid division by zero and handle cases where b^2−4ac ≤ 0 for complex roots. For example: 'int main() { float a, b, c, discriminant, root1, root2; scanf("%f %f %f", &a, &b, &c); discriminant = b*b - 4*a*c; if(discriminant >= 0 && a != 0) { root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a); printf("Roots are: %.2f, %.2f", root1, root2); } else { printf("Complex roots or invalid input"); } }' .
The four basic data types in C are 'int', 'char', 'float', and 'double'. 'int' is used for integers and is significant for numerical operations. 'char' holds single characters and is integral to handling text. 'float' and 'double' are for floating-point numbers, with 'double' providing more precision. Each data type is essential for different programming needs, enabling efficient and appropriate data storage .