0% found this document useful (0 votes)
14 views5 pages

pic class test

The document covers fundamental concepts in C programming, including pointers, arrays, functions, and recursion. It provides syntax examples for pointer declaration, one-dimensional array initialization, and the use of the strlen() function. Additionally, it includes C programs demonstrating matrix addition, call by value, structure declaration for employees, pointer arithmetic, and a recursive function for calculating factorial.

Uploaded by

aryantelang264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

pic class test

The document covers fundamental concepts in C programming, including pointers, arrays, functions, and recursion. It provides syntax examples for pointer declaration, one-dimensional array initialization, and the use of the strlen() function. Additionally, it includes C programs demonstrating matrix addition, call by value, structure declaration for employees, pointer arithmetic, and a recursive function for calculating factorial.

Uploaded by

aryantelang264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2 Mark Questions:

Define pointer, write syntax for pointer declaration.

A pointer is a variable that stores the memory address of another variable. Instead of
holding the actual value, it holds the address where the value is stored.

Syntax for pointer declaration:

c
Copy
data_type *pointer_name;
Example:

c
Copy
int *ptr; // declares a pointer to an integer
How to declare and initialize a one-dimensional array?

To declare and initialize a one-dimensional array in C, you specify the data type,
followed by the array name, and initialize it with the required values enclosed in curly
braces.

Syntax:

c
Copy
data_type array_name[size] = {value1, value2, ..., valueN};
Example:

c
Copy
int arr[5] = {1, 2, 3, 4, 5};
State advantages of functions.

Functions help in code reusability, as they allow you to write a block of code once and
use it multiple times.

They increase modularity, making the program more organized and easier to maintain.

Functions provide abstraction, hiding the implementation details and allowing the
programmer to focus on higher-level logic.

Explain strlen() used in C, state its syntax.

strlen() is a library function in C that returns the length of a null-terminated string


(excluding the null character).

Syntax:

c
Copy
size_t strlen(const char *str);
Example:

c
Copy
char str[] = "Hello";
int len = strlen(str); // len will be 5
Distinguish between call by value and call by reference.

Call by Value:

The actual value of the argument is passed to the function.

Changes made to the parameter inside the function do not affect the original variable.
Call by Reference:

The address (reference) of the argument is passed to the function.

Changes made to the parameter inside the function will affect the original variable.

Write the meaning of & and * with respect to pointer.

& (Address-of operator): It is used to get the memory address of a variable. Example:
&x gives the address of the variable x.

* (Dereference operator): It is used to access the value at the memory address stored in
the pointer. Example: *ptr gives the value stored at the address pointed to by ptr.

Implement a C program to find addition of two numbers using a function.

c
Copy
#include <stdio.h>

int add(int a, int b) {


return a + b;
}

int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2);
printf("Sum: %d\n", sum);
return 0;
}
4 Mark Questions:
Write a program to add two 3×3 matrices. Display the addition.

c
Copy
#include <stdio.h>

int main() {
int A[3][3], B[3][3], sum[3][3];

// Input elements of matrix A


printf("Enter elements of matrix A:\n");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
scanf("%d", &A[i][j]);

// Input elements of matrix B


printf("Enter elements of matrix B:\n");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
scanf("%d", &B[i][j]);

// Adding matrices A and B


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
sum[i][j] = A[i][j] + B[i][j];

// Displaying the result


printf("Sum of matrices A and B:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
Explain Call by value, create a C program to demonstrate use of Call by value.

Call by Value: In this method, the actual value of the argument is passed to the
function.
Modifications to the argument inside the function do not affect the original variable.

C Program Example:

c
Copy
#include <stdio.h>

void add(int a, int b) {


a = a + b; // changes will not affect the original variables
printf("Sum inside function: %d\n", a);
}

int main() {
int x = 5, y = 10;
add(x, y);
printf("x in main after function call: %d\n", x); // x remains unchanged
return 0;
}
Differentiate between integer array and character array.

Integer Array:

Stores integers.

Each element in the array is of integer type (typically 4 bytes).

Example: int arr[5] = {1, 2, 3, 4, 5};

Character Array:

Stores characters (usually of type char).

Each element is of type char (typically 1 byte).

Often used to store strings.

Example: char str[6] = "Hello";

List the categories of functions and explain any one with example.

Categories of Functions in C:

Library Functions: Predefined functions in C (e.g., printf(), scanf(), strlen()).

User-defined Functions: Functions defined by the user for specific tasks.

Standard Functions: Built-in functions like mathematical functions (sqrt(), abs()).

Example of User-defined Function:

c
Copy
#include <stdio.h>

void greet() {
printf("Hello, World!\n");
}

int main() {
greet(); // Calling user-defined function
return 0;
}
Create a program to declare structure of 10 employees having empid, name, age & salary
and print it.

c
Copy
#include <stdio.h>

struct Employee {
int empid;
char name[50];
int age;
float salary;
};

int main() {
struct Employee emp[10];

// Input details for 10 employees


for (int i = 0; i < 10; i++) {
printf("Enter details for employee %d:\n", i + 1);
printf("Employee ID: ");
scanf("%d", &emp[i].empid);
printf("Name: ");
scanf("%s", emp[i].name);
printf("Age: ");
scanf("%d", &emp[i].age);
printf("Salary: ");
scanf("%f", &emp[i].salary);
}

// Print details of 10 employees


printf("\nEmployee Details:\n");
for (int i = 0; i < 10; i++) {
printf("Employee ID: %d\n", emp[i].empid);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Salary: %.2f\n\n", emp[i].salary);
}

return 0;
}
Explain pointer arithmetic operations with example C program to demonstrate use of it.

Pointer Arithmetic:

Pointers can be incremented or decremented to move across memory locations.

Example Operations:

ptr++ (increments the pointer, pointing to the next memory location)

ptr-- (decrements the pointer, pointing to the previous memory location)

ptr + n (moves the pointer n positions forward)

ptr - n (moves the pointer n positions backward)

Example Program:

c
Copy
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;

// Pointer arithmetic
printf("First element: %d\n", *ptr);
ptr++; // Move pointer to the next element
printf("Second element: %d\n", *ptr);
ptr += 2; // Move pointer 2 positions forward
printf("Fourth element: %d\n", *ptr);

return 0;
}
Describe recursive function, write any one C program.

A recursive function is a function that calls itself in order to solve a problem.

The function typically has a base case to stop the recursion.

Example Program (Factorial):

c
Copy
#include <stdio.h>

int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive call
}
}

int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

You might also like