Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

C_Programming_Notes_Answers_Expanded

The document contains several C programming examples and concepts, including linear search, average calculation, and basic I/O operations. It also covers pointers, pass by value vs. pass by reference, and various programming paradigms. Additionally, it discusses control statements, global variables, arrays, function declarations, and memory management in C.

Uploaded by

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

C_Programming_Notes_Answers_Expanded

The document contains several C programming examples and concepts, including linear search, average calculation, and basic I/O operations. It also covers pointers, pass by value vs. pass by reference, and various programming paradigms. Additionally, it discusses control statements, global variables, arrays, function declarations, and memory management in C.

Uploaded by

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

7) Linear Search in C program

----------------------------------------

#include <stdio.h>

int main() {

int a[100], n, i, key;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter %d elements:\n", n);

for(i = 0; i < n; i++)

scanf("%d", &a[i]);

printf("Enter element to search: ");

scanf("%d", &key);

for(i = 0; i < n; i++) {

if(a[i] == key) {

printf("Element found at index %d\n", i);

return 0;

printf("Element not found.\n");

return 0;

8) (i) C Program to find average marks of 30 students

------------------------------------------------------

#include <stdio.h>

int main() {
int marks[30], i, sum = 0;

float average;

printf("Enter marks of 30 students:\n");

for(i = 0; i < 30; i++) {

scanf("%d", &marks[i]);

sum += marks[i];

average = sum / 30.0;

printf("Average marks = %.2f\n", average);

return 0;

(ii) Short note on Reading and Writing using C I/O

------------------------------------------------------

Reading: Use scanf() and getchar() to take input.

Writing: Use printf() and putchar() for output.

Format specifiers (%d, %f, %s, etc.) specify data types.

9) Pointers and their operations in C

------------------------------------------

- Pointer stores the address of another variable.

- * (dereference), & (address-of)

- Used for dynamic memory, arrays, and function args.

10) Pass by Value vs Pass by Reference

-------------------------------------------

- Pass by Value: Copy is passed, original unchanged.


- Pass by Reference: Address is passed, original affected.

- In C, reference is done using pointers.

PART - A

-----------

1) Difference between break and continue:

- The 'break' statement is used to terminate a loop or switch statement prematurely.

Once encountered, control is transferred outside the loop or switch.

Example: Used in searching when the item is found.

- The 'continue' statement skips the current iteration of the loop and moves to the next iteration.

Example: Skipping even numbers in a loop.

2) What is a global variable:

- A global variable is declared outside all functions and is accessible by any function in the

program.

It retains its value throughout the program's execution.

Example: Useful for shared counters, configuration flags, etc.

3) Define paradigm:

- A programming paradigm is a fundamental style of computer programming.

Examples include procedural, object-oriented, functional, and logic programming.

In C, procedural programming is followed where code is organized in procedures or functions.

4) Conditional operators and memory:

- The conditional operator in C is a ternary operator `?:` used to replace simple if-else statements.

Syntax: condition ? expression1 : expression2;


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

- Memory in C includes Stack (for static memory), Heap (for dynamic memory using malloc), Code

segment, and Data segment.

5) Difference between switch and nested if:

- 'switch' is used when we have multiple constant values to compare with a single variable.

It is more readable when comparing many values.

- 'nested if' can handle ranges and complex expressions that 'switch' cannot.

Example: Grading system using ranges of marks.

6) Features of arrays:

- Arrays are collections of elements of the same type stored in contiguous memory.

- They allow random access using index, efficient iteration, and easy traversal.

- Types: One-dimensional, two-dimensional (matrices), etc.

7) Two-dimensional array:

- A 2D array is an array of arrays, resembling a matrix with rows and columns.

Declaration: int arr[3][4];

Access: arr[1][2] represents the element in the 2nd row, 3rd column.

Used in matrix operations, table representations, etc.

8) Syntax for function declaration:

- A function declaration tells the compiler about a function's name, return type, and parameters.

Syntax: return_type function_name(type1 param1, type2 param2);

Example: int sum(int a, int b);

9) Operation of pointers:
- Declaring a pointer: int *ptr;

- Assigning address: ptr = &variable;

- Dereferencing: *ptr gives the value at the address.

- Pointer arithmetic: ptr++, ptr--, etc.

- Pointers can be used in arrays, functions (call by reference), and dynamic memory allocation

(malloc, free).

You might also like