0% found this document useful (0 votes)
2 views8 pages

PSC Test 3 (1) .N

The document discusses pointers to functions in C, explaining call by value and call by reference with examples. It also includes a C program to define an Employee structure for storing employee details, a program to copy text from a source file to a destination file, and explanations of pre-processor directives, basic graphics drawing, and differences between structures and unions. Additionally, it covers pointers, command line arguments, and the fseek() function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

PSC Test 3 (1) .N

The document discusses pointers to functions in C, explaining call by value and call by reference with examples. It also includes a C program to define an Employee structure for storing employee details, a program to copy text from a source file to a destination file, and explanations of pre-processor directives, basic graphics drawing, and differences between structures and unions. Additionally, it covers pointers, command line arguments, and the fseek() function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.Briefly discuss the Pointers to function.

Pointers to Functions (call by value and call by reference) In C


programming, a pointer to a function is a pointer that points to the
address of a function, allowing you to pass the function itself as a
parameter or use it indirectly through the pointer. Let's discuss how
pointers to functions can be used with call by value and call by reference.

1. Pointer to Function with Call by Value

In call by value, the function receives a copy of the variable’s value. Any
modifications made to the parameter inside the function do not affect the
original value.

Example: Call by Value with a Pointer to a Function

#include<stdio.h>

// Function that adds two numbers (Call by Value)

int add(int a, int b)

return a + b;

} // Function pointer declaration

int (*func_ptr)(int, int);

int main()

// Assign the address of the 'add' function to 'func_ptr'

func_ptr = &add;

int x = 10,

y = 20; // Calling the function using the pointer (Call by Value)

int result = func_ptr(x, y);

printf("Result (Call by Value): %d\n", result);

// Output: 30

return 0;

In this example, the values of x and y are passed to the function pointer
func_ptr, which points to the add function. The values are passed by
value, meaning the original values of x and y are not modified.
2. Pointer to Function with Call by Reference

In call by reference, the function receives a reference (pointer) to the


variable, so any changes made to the parameter inside the function will
affect the original variable.

Example: Call by Reference with a Pointer to a Function

#include<stdio.h>

// Function to swap two numbers (Call by Reference)

void swap(int *a, int *b)

int temp = *a;

*a = *b;

*b = temp;

} // Function pointer declaration

void (*func_ptr)(int *, int *);

int main()

// Assign the address of the 'swap' function to 'func_ptr'

func_ptr = &swap;

int x = 10,

y = 20;

// Calling the function using the pointer (Call by Reference)

func_ptr(&x, &y);

printf("After Swap (Call by Reference): x = %d, y = %d\n", x, y);

// Output:

x = 20,

y = 10

return 0; }

In this example, the addresses of x and y are passed to the function


pointer func_ptr, which points to the swap function. Since the function
operates on the addresses, it modifies the original variables x and y.
2. Write a C program to define a Structure ‘Employee’ that reads
and display the details such as eno, ename, deptname and salary.
The structure has to store 50 employees in an organization.

#include <stdio.h>

#include <string.h>

#define MAX_EMPLOYEES 50

// Define the Employee structure

typedef struct

int eno; // Employee number

char ename[50]; // Employee name

char deptname[50]; // Department name

float salary; // Salary

} Employee;

int main()

Employee employees[MAX_EMPLOYEES]; // Array to store employee


records

int i, num_employees;

// Get the number of employees from the user

printf("Enter the number of employees (up to %d): ", MAX_EMPLOYEES);

scanf("%d", &num_employees);

// Input employee details

printf("\nInput employee details:\n");

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

printf("\nEmployee %d:\n", i + 1);

// Read employee details

printf("Enter employee number: ");

scanf("%d", &employees[i].eno);
printf("Enter employee name: ");

scanf("%s", employees[i].ename); // Use %s for strings

printf("Enter department name: ");

scanf("%s", employees[i].deptname); // Use %s for strings

printf("Enter salary: ");

scanf("%f", &employees[i].salary);

// Display employee details

printf("\n--- Employee Details ---\n");

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

printf("\nEmployee %d:\n", i + 1);

// Print employee details

printf(" Employee Number: %d\n", employees[i].eno);

printf(" Name: %s\n", employees[i].ename);

printf(" Department: %s\n", employees[i].deptname);

printf(" Salary: %.2f\n", employees[i].salary);

return 0;

3.Write a c program to copy text from source file to destination


file.

#include <stdio.h>

#include<string.h>

int main() {

FILE *sourceFile, *destinationFile;

char sourceFileName[100], destFileName[100];

char ch;
printf("Enter name of source file: ");

scanf("%s", sourceFileName);

printf("Enter name of destination file: ");

scanf("%s", destFileName);

sourceFile = fopen(sourceFileName, "r");

if (sourceFile == NULL) {

printf("Cannot open source file.\n");

return 1;

destinationFile = fopen(destFileName, "w");

if (destinationFile == NULL) {

printf("Cannot open destination file.\n");

fclose(sourceFile);

return 1;

while ((ch = fgetc(sourceFile)) != EOF) {

fputc(ch, destinationFile);

printf("File copied successfully.\n");

fclose(sourceFile);

fclose(destinationFile);

return 0;

4.Explain about pre-processor directives with an example.


A preprocessor is a program that processes its input data to produce
output that is used as input to another program. It is a program that
processes the source program before compilation.

#define AREA(a) (5.18 * a * a)

void main()

float r = 3.5, x;

x = AREA (r);

printf ("\n Area of circle = %f", x);

5.Illustrate to draw basic graphics like line, circle, arc, ellipse and
rectangle.

#include <graphics.h>

#include <stdio.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "path_to_bgi_driver"); // Replace with BGI driver


path

// Draw a line from (10, 10) to (100, 100)

line(10, 10, 100, 100);

circle(300,300,150);

rectangle(100,100,250,350);

// Wait for a key press before exiting the graphics window

getch();

closegraph();

return 0;

}
1.Write down the difference between Structure and Union.

structure union
Keyword struct is used to define a Keyword union is used to define a
structure union.
Each member have the own Memory allocated is shared by
independent memory. individual member of union.
Individual member can be accessed at Only one member can be accessed at
a time. a time.
All members of a structure can be Only the first member can be initialized.
initialized.
Syntax: structstructure_name union union_name
{ {
datatype members1; datatype members1;
datatype members2; datatype members2;
…………………….
…………………….
datatype membersn;
datatype membersn; };
};

2.What is a pointer?

Pointer is a variable that contains the memory address of another


variable.

Syntax:

datatype*pointername;

Eg: int *a;

3.Find out the output of the following code:

Output: 10

4.What is command line argument?

Command line arguments are parameters supplied to a program when it is


executed. They allow users to influence the program's behavior without
modifying its code. These arguments are entered after the program's
name in the command line interface.

5.What is fseek () function?


fseek() is used to move file pointer associated with a given file to a
specific position

SEEK_SET - starts the offset from beginning of the file.

SEEK_CUR- starts the offset from the current location of the cursor in the
file

SEEK_END- starts the offset from the end of the file

You might also like