pic class test
pic class test
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.
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.
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:
Changes made to the parameter inside the function do not affect the original variable.
Call by Reference:
Changes made to the parameter inside the function will affect the original variable.
& (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.
c
Copy
#include <stdio.h>
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];
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>
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.
Character Array:
List the categories of functions and explain any one with example.
Categories of Functions in C:
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];
return 0;
}
Explain pointer arithmetic operations with example C program to demonstrate use of it.
Pointer Arithmetic:
Example Operations:
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.
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;
}