What happens if you try to compile and run this program?
#include
int fun(int i) {
return i++;
}
int main (void) {
int i = 1;
i = fun(i);
printf("%d",i);
return 0;
}
Choose the correct answer:
In the fun function:
cCopy code
int fun(int i) { return i++; }
The post-increment operator i++ returns the current value of i and then increments it. So, fun(i) will return the current value of i (which is 1) and then increment i to 2.
In the main function:
cCopy code
int i = 1; i = fun(i); printf('%d', i);
Here, i is assigned the result of fun(i), which is 1. So, the program prints the value of i, which is 1.
Therefore, the correct answer is D. The program outputs 1.
What happens when you compile and run the following program?
#include
int fun (void) {
static int i = 1;
i += 2;
return i;
}
int main (void) {
int k, 1;
k = fun ();
1 = fun () ;
printf ("%d", 1 - k);
return 0;
}
Choose the right answer:
The provided program has a few key points to consider:
1. fun is a function that uses a static variable i. This means i retains its value between function calls. It's initialized to 1 and then incremented by 2 each time fun is called.
2. The main function calls fun twice, assigning the results to k and l (though there's a typo in the variable name l, it should be l = fun();, not 1 = fun();).
Let's step through the code:
* First call to fun: i starts at 1, increments by 2, so i becomes 3. This value (3) is as-signed to k.
* Second call to fun: i is now 3, increments by 2 again, so i becomes 5. This value (5) is assigned to l.
Finally, the printf statement attempts to print l - k, which is 5 - 3, resulting in 2.
So, the correct answer is:
A . The program outputs 2.
What happens if you try to compile and run this program?
#include
int main (int argc, char *argv[]) {
char i = 20 + 020 + 0x20;
printf("%d",i);
return 0;
}
Choose the right answer:
* The program is a valid C program that can be compiled and run without errors.
* The variable i is declared as a char, which is an 8-bit signed integer type that can store val-ues from -128 to 127.
* The expression 20 + 020 + 0x20 evaluates to 68, because:
o 20 is a decimal literal with the value 20
o 020 is an octal literal with the value 16 (8^1 * 2 + 8^0 * 0)
o 0x20 is a hexadecimal literal with the value 32 (16^1 * 2 + 16^0 * 0)
o The + operator performs arithmetic addition on the operands and returns the sum
* The printf function prints the value of i as a decimal integer using the %d format specifier.
* The output of the program is 68.
What happens if you try to compile and run this program?
#include
int main (int argc, char *argv[]) {
int i = 2;
int d= i << 2;
d /= 2;
printf ("%d", d) ;
return 0;
}
Choose the right answer:
The program outputs 4 because the expression i << 2 performs a left shift operation on the binary representation of i, which is 00000010, by two bits, resulting in 00001000, which is equivalent to 8 in decimal. Then, the expression d /= 2 performs a division assignment operation, which divides d by 2 and assigns the result back to d, resulting in 4. The printf function then prints the value of d as a decimal integer using the %d format specifier.
Reference = CLA -- C Certified Associate Programmer Certification, [C Essentials 2 - (Intermediate)], C Bitwise Operators, C Assignment Operators
-
What happens if you try to compile and run this program?
#include
int *f();
int main (int argc, char *argv[]) {
int *p;
p = f();
printf("%d",*p);
return 0;
}
int *f() {
static v = 1;
return &v;
}
Choose the right answer:
The program outputs 1 because the static variable v is initialized to 1 inside the f function, and it is visible to the main function. The f function returns the address of v, which is a pointer to an int. The main function dereferences the pointer and assigns it to p, which is another pointer to an int. Then, the main function prints the value of *p, which is the same as dereferencing p again. Therefore, the output of the program is:
f() = &v p = f() printf(''%d'',*p) = &v = 1
The other options are incorrect because they either do not match the output of the program or do not use the correct concept of static variables.
Jessica Wilson
15 days agoHyun Lim
26 days ago