What will be values for $a$ and $c$ after execution of the following code if $a$ is $10$, $b$ is $5$, and $c$ is $10$?
if ((a > b) && (a <= c))
a = a + 1;
else
c = c + 1;
In C programming, the mode 'a' in the fopen() function is used to open a file in append mode,
which allows new data to be added to the end of the file without deleting existing content.
Consider the following C language declarations & statements.
Which statement is erroneous?
float f1 = 9.9;
float f2 = 66;
const float *ptrF1;
float * const ptrF2 = &f2;
ptrF1 = &f1;
ptrF2++;
ptrF1++;
- `const float *ptrF1;` → pointer to constant float, can move pointer, not modify value.
- `float * const ptrF2 = &f2;` → constant pointer to float, cannot change address, but can modify value.
- Statement `ptrF2++` tries to move a **constant pointer**, which is **not allowed**.
Hence, `ptrF2++` is **erroneous**.
Solution:
Operator `^` is **bitwise XOR**.
$n1 = 3 \Rightarrow 011_2$
$n2 = 6 \Rightarrow 110_2$
$n1 \oplus n2 = 101_2 = 5$
Variable `a` is declared but **not initialized**.
Using an uninitialized variable in expression `(a ^ a)` causes **undefined behavior**.
Hence, the program compiles but produces a **run-time error or unpredictable result**.
Solution:
In C, the range of `char` is from $-128$ to $127$.
When we assign $130$ to a `char`, it overflows:
$130 - 256 = -126$
Thus the value stored in `ch` is $-126$.
Output:
value of ch = -126
Solution:
When a local variable has the same name as a global variable,
the **local variable overrides** (takes precedence over) the global one inside its block.
Thus (C) is **invalid** for C.
Solution:
String: "\nhello"
Character positions:
0:'\n' 1:'h' 2:'e' 3:'l' 4:'l' 5:'o'
→ "\nhello" + 3 points to index 3, i.e., `"llo"`
Hence it prints “llo”.
```ruby
Solution:
The function `count()` counts number of 1-bits in the binary representation of `x`.
For `a = 3`
Binary of 3 = `11`
→ Number of 1 bits = 2
Hence output = 2.
Solution:
In the **ternary operator** `(expr₁ ? expr₂ : expr₃)`,
the **data type of result** is the **common type** of `expr₂` and `expr₃`.
Here,
- `expr₁` → condition (float type, irrelevant for result type)
- `expr₂` → int
- `expr₃` → (not specified but implied same as expr₂ type logic)
When int and float are combined → **result type = float** (implicit type conversion).
Solution:
Operator precedence (from high to low in C):
`[]` > `<` > `?:` > `,`
Therefore, the highest precedence among these is **array subscript `[ ]`**.
Operator associativity in C:
- Most unary operators (!, ++, --, sizeof, etc.) → **Right to Left**
- Conditional (`?:`) → **Right to Left**
- Comma operator (`,`) → **Left to Right**
Hence, only the **comma operator** has **left-to-right** associativity.
Solution:
In C, **“else” is always paired with the nearest unmatched “if”** (no braces rule).
Here:
- The inner `if (array[i] > 0)` is the **nearest unmatched `if`** before `else`.
So, the `else` is paired with the **second (inner)** `if`.
Solution:
The **argc (argument count)** includes the program name itself.
So the arguments are:
1️⃣ "copy" (program name)
2️⃣ "file1"
3️⃣ "file2"
4️⃣ "file3"
Thus, total arguments = 4.
Solution:
- `"r+b"` or `"rb+"` → open **existing file** for both reading and writing (no truncation).
- `"w+b"` or `"wb+"` → open file for reading and writing but **creates/truncates** file.
- `"ab"` → append binary mode.
Hence, the correct mode for **existing file in read-write** is `"r+b"`.
Solution:
- `p--` → post-decrement (use then decrement)
- `--p` → pre-decrement (decrement then use)
Here, we need to **decrement p first**, then fetch the value it points to.
Hence, correct expression: `*--p`
How many times this statement will execute:
for (; *s == *t && *t != '\0'; s++, t++)
if both character pointers ‘s’ and ‘t’ point to the same string “abc”.
Solution:
C99 added these new keywords:
- `_Bool`
- `inline`
- `restrict`
- `_Complex`
- `_Imaginary`
`register` is an **old ANSI C keyword**, not added in C99.
Solution:
Valid C versions are:
- C89 (ANSI standard, 1989)
- C90 (ISO standard, 1990)
- C99, C11, C17, and C23
There is **no version called C2007 or CIX**.
(interpreting “contains 1” as **exactly one** ‘1’, repetitions allowed):**
Case-1: ‘1’ in the thousand’s place → remaining $3$ places from $\{0,\dots,7\}\setminus\{1\}$ with repetition:
$7^3=343$ ways.
Case-2: ‘1’ in any one of the last three places ($3$ choices). Thousand’s place from $\{2,\dots,7\}$ ($6$ ways). Remaining two places from $\{0,\dots,7\}\setminus\{1\}$ with repetition: $7^2=49$ ways.
Total $=343+3\cdot6\cdot49=343+882
(interpreting “contains 1” as **exactly one** ‘1’, repetitions allowed):**
Case-1: ‘1’ in the thousand’s place → remaining $3$ places from $\{0,\dots,7\}\setminus\{1\}$ with repetition:
$7^3=343$ ways.
Case-2: ‘1’ in any one of the last three places ($3$ choices). Thousand’s place from $\{2,\dots,7\}$ ($6$ ways). Remaining two places from $\{0,\dots,7\}\setminus\{1\}$ with repetition: $7^2=49$ ways.
Total $=343+3\cdot6\cdot49=343+882