0% found this document useful (0 votes)
6 views

C programming answers

Uploaded by

johnbiscuites
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C programming answers

Uploaded by

johnbiscuites
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

### 1. What is an operator?

In C programming, an operator is a symbol that represents a specific operation performed on one or


more operands. Operators are the building blocks of expressions and are used to manipulate data.
They can perform operations such as arithmetic, logical, relational, and bitwise operations.

### 2. Operators in C:

#### I. Assignment operators:

- Syntax: `variable = expression;`

- Example: `int x = 5;`

#### II. Arithmetic operators:

- Syntax: `result = operand1 operator operand2;`

- Example: `int sum = 10 + 5;`

#### III. Increment and decrement operators:

- Syntax: `++variable` (increment), `--variable` (decrement)

- Example: `int a = 5; a++; // a is now 6`

#### IV. Relational operators:

- Syntax: `expression1 operator expression2;`

- Example: `if (x > y) { /* code */ }`

#### V. Logical operators:

- Syntax: `logical_expression1 && logical_expression2` (logical AND)

- Example: `if (x > 0 && y > 0) { /* code */ }`

### 3. C program demonstrating operators:

```c

#include <stdio.h>
int main() {

// Arithmetic operators

int a = 10, b = 5, result;

result = a + b;

// Conditional operator

int max = (a > b) ? a : b;

// Increment and decrement operators

int c = 7;

c++;

// Relational operators

if (a != b) {

// code

return 0;

```

### 4. Right shift and left shift operators:

- Right shift (`>>`): Shifts the bits of the left operand to the right by the number of positions specified
by the right operand.

- Left shift (`<<`): Shifts the bits of the left operand to the left by the number of positions specified by
the right operand.

### 5. Convert character case program:

```c
#include <stdio.h>

int main() {

char ch = 'a';

// Convert to uppercase

char upper = ch - 32;

// Convert to lowercase

char lower = ch + 32;

return 0;

```

### 6. Ways C programs are executed:

C programs are executed in two main ways:

1. **Interpreted:** The source code is directly executed by an interpreter.

2. **Compiled:** The source code is translated into machine code or intermediate code by a
compiler, and then the compiled code is executed.

### 7. Conditional control statements:

#### I. If statement:

```c

if (condition) {

// code

```
#### II. If else statement:

```c

if (condition) {

// code

} else {

// code

```

#### III. Switch statement:

```c

switch (expression) {

case constant1:

// code

break;

case constant2:

// code

break;

default:

// code

```

#### IV. Nested if statement:

```c

if (condition1) {

if (condition2) {

// code

```
### 8. C program with conditional operators:

```c

#include <stdio.h>

int main() {

int x = 10, y = 5, result;

// Conditional operator

result = (x > y) ? x : y;

return 0;

```

### 9. Why is the if statement called one-way branching?

The `if` statement is called one-way branching because it allows the program to take one of two
paths based on whether the condition is true or false. It provides a single alternative for the
execution of code.

### 10. Why is the if-else statement called two-way branching?

The `if-else` statement is called two-way branching because it provides two possible paths for the
program to follow. If the condition is true, the code inside the `if` block is executed; otherwise, the
code inside the `else` block is executed.

### 11. Conditional operator as an if-else statement:

Yes, the conditional operator (`? :`) can be used as a compact form of the if-else statement. However,
it's important to note that it is not a direct replacement for more complex if-else structures.
### 12. What is a loop?

A loop is a control structure in C programming that allows a set of instructions to be executed


repeatedly as long as a specified condition is true. It helps in reducing redundancy and makes the
code more efficient.

### 13. Three types of loops in C:

#### I. For loop:

```c

for (initialization; condition; update) {

// code

```

#### II. While loop:

```c

while (condition) {

// code

```

#### III. Do-while loop:

```c

do {

// code

} while (condition);

```

### 14. C program with looping control structures:

```c
#include <stdio.h>

int main() {

// i. For loop

for (int i = 0; i < 4; i++) {

// code

// ii. While loop

int j = 0;

while (j < 5) {

// code

j++;

// iii. Do-while loop

int k = 0;

do {

// code

k++;

} while (k < 3);

return 0;

```

### 15. Print name 20 times using a loop:

```c

#include <stdio.h>
int main() {

for (int i = 0; i < 20; i++) {

printf("Your Name\n");

return 0;

```

### 16. What is the `break` keyword used for?

The `break` keyword is used to exit from a loop or switch statement prematurely. When
encountered, it terminates the nearest enclosing loop or switch statement and transfers control to
the statement immediately following the terminated loop or switch.

### 17. C program with the `break` keyword:

```c

#include <stdio.h>

int main() {

for (int i = 0; i < 10; i++) {

if (i == 5) {

break;

printf("%d\n", i);

return 0;

```
### 18. Use of the `continue` keyword:

The `continue` keyword is used to skip the rest of the code inside a loop for the current iteration and
jump to the next iteration of the loop.

### 19. C program with the `continue` keyword:

```c

#include <stdio.h>

int main() {

for (int i = 0; i < 5; i++) {

if (i == 2) {

continue;

printf("%d\n", i);

return 0;

```

### 20. Importance of the `continue` keyword:

The `continue` keyword is important for skipping certain iterations of a loop based on a condition,
allowing more control over the flow of the loop. It helps in avoiding the execution of unnecessary
code for specific cases, improving efficiency.

You might also like