0% found this document useful (0 votes)
22 views12 pages

Unit-5 Ip Long

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)
22 views12 pages

Unit-5 Ip Long

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/ 12

Unit-5

Essay Questions-7M
1. What are various types of parameters passing techniques in ‘C’ programming?
Explain with an example program.
Ans.
In C programming, there are two primary
types of parameter passing techniques:
1. Call by Value:In Call by Value, the actual value of the argument is passed to the function.
Any changes made to the argument within the function do not affect the original variable.
Example Program:
#include <stdio.h>
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
printf("Inside swap function: x = %d, y = %d\n", x, y);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swap:a = %d,b =%d\n", a,b);
swap(a, b);
printf("After swap: a = %d, b = %d\n",a, b);
return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 10, b = 20
As you can see, the swap function did not change the original values of `a` and `b`.
2. Call by Reference:In Call by Reference, the address of the argument is passed to the
function. Any changes made to the argument within the function affect the original variable.
Example Program:
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
printf("Inside swap function: x = %d, y =
%d\n", *x, *y);
}
int main()
{
int a = 10,b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 20, b = 10
As you can see, the swap function changed the original values of `a` and `b`.

2. Write a C Program to print Binary Equivalent of an Integer using Recursion.


Ans.
PROGRAM:
// C program that prints the binary equivalent of an integer using recursion//
#include <stdio.h>
void print_binary(int num)
{
if (num == 0)
{
return;
}
print_binary(num / 2);
printf("%d", num % 2);
}
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Binary equivalent of %d is: ", num);
print_binary(num);
printf("\n");
return 0;
}
output:
Enter an integer: 12
Binary equivalent of 12 is: 1100

3. Explain Call by Value and Call by Reference with example each.


Ans.
Here's an explanation of Call by Value and Call by Reference with examples:

Call by Value:In Call by Value, the actual value of the argument is passed to the function.
Any changes made to the argument within the function do not affect the original variable.
PROGRAM:
#include <stdio.h>
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
printf("Inside swap function: x = %d, y = %d\n", x, y);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(a, b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 10, b = 20
As you can see, the swap function did not change the original values of `a` and `b`.

Call by Reference:In Call by Reference, the address of the argument is passed to the
function. Any changes made to the argument within the function affect the original variable.
PROGRAM:
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
printf("Inside swap function: x = %d, y = %d\n", *x, *y);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);

printf("After swap: a = %d, b = %d\n", a, b);


return 0;
}
Output:
Before swap: a = 10, b = 20
Inside swap function: x = 20, y = 10
After swap: a = 20, b = 10
As you can see, the swap function changed the original values of `a` and `b`.
4. Explain the following.
i) fprintf ( ) ii) fscanf ( ) iv) fgets( ) v) fputs ().
Ans.
Here's an explanation of each function:
(i) fprintf(): fprintf() is a function that writes formatted output to a file. It is similar to `printf()`,
but instead of writing to the standard output (usually the screen), it writes to a file.
Syntax: fprintf(file_pointer, format_string, argument1, argument2, ...);
Example:
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, %s!\n", "World");
fclose(file);
This will write the string "Hello, World!" to the file "example.txt".

(ii) fscanf(): fscanf() is a function that reads formatted input from a file. It is similar to
`scanf()`, but instead of reading from the standard input (usually the keyboard), it reads from
a file.
Syntax: fscanf(file_pointer, format_string, argument1, argument2, ...);
Example:
FILE *file = fopen("example.txt", "r");
int x;
char name[20];
fscanf(file, "%d %s", &x, name);
printf("x = %d, name = %s\n", x, name);
fclose(file);
This will read an integer and a string from the file "example.txt" and print them to the
console.

(iv) fgets(): fgets() is a function that reads a line of text from a file. It is similar to `gets()`, but
instead of reading from the standard input (usually the keyboard), it reads from a file.
Syntax: fgets(string, size, file_pointer);
Example:
FILE *file = fopen("example.txt", "r");
char line[100];
fgets(line, 100, file);
printf("%s", line);
fclose(file);

This will read a line of text from the file "example.txt" and print it to the console.

(v) fputs(): fputs() is a function that writes a string to a file. It is similar to `puts()`, but instead
of writing to the standard output (usually the screen), it writes to a file.
Syntax: fputs(string, file_pointer);
Example:
FILE *file = fopen("example.txt", "w");
char *str = "Hello, World!";
fputs(str, file);
fclose(file);
This will write the string "Hello, World!" to the file "example.txt".

5. Write a C program to Display contents of a text file on console.


Ans.
PROGRAM :
//C program that reads and displays the contents of a text file on the console//
#include <stdio.h>
int main()
{
FILE *file;
char filename[100];
char c;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("Contents of %s:\n", filename);
while ((c = fgetc(file)) != EOF)
{
printf("%c", c);
}
fclose(file);
return 0;
}
Output:
Enter the filename: example.txt
Contents of example.txt:
Hello, World!

6. What is recursion? Write a program to find the factorial of a given number using
recursion.
Ans
Recursion is a programming technique where a function calls itself repeatedly until it reaches
a base case that stops the recursion. In other words, a function solves a problem by
breaking it down into smaller sub-problems of the same type, which are then solved by the
same function, until the solution to the original problem is found.

Here's a step-by-step explanation of how recursion works:

1. Base case: A recursive function has a base case that is a trivial case that can be solved
directly.
2. Recursive call: The function calls itself with a smaller input or a modified version of the
original input.
3. Solution: The function combines the solutions to the sub-problems to solve the original
problem.
PROGRAM:
// a program to find the factorial of a given number using recursion//
#include <stdio.h>
int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n-1);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
{
printf("Error! Factorial is not defined for negative numbers.\n");
}
else
{
printf("Factorial of %d = %d\n", num, factorial(num));
}
return 0;
}
Output:
Enter a number: 5
Factorial of 5 = 120

7. What are various standard library input/output functions used in C language?


Explain with simple program.
Ans.
In C, the standard library provides various input/output functions to perform operations
such as reading input from the user, writing output to the screen or files, and manipulating
files. Here are some commonly used standard library input/output functions in C:

Input Functions:
1. scanf(): Reads input from the standard input (usually the keyboard).
2. fscanf(): Reads input from a file.
3. getchar(): Reads a single character from the standard input.
4. gets(): Reads a string from the standard input.
Output Functions:
1. printf(): Writes output to the standard output (usually the screen).
2. fprintf(): Writes output to a file.
3. putchar(): Writes a single character to the standard output.
4. puts(): Writes a string to the standard output.

File Handling Functions:


1. fopen(): Opens a file for reading or writing.
2. fclose(): Closes a file.
3. fread(): Reads data from a file.
4. fwrite(): Writes data to a file.
PROGRAM:
// a simple program that demonstrates some of these functions//
#include <stdio.h>
int main()
{
int num;
char name[20];
printf("Enter your name: ");
gets(name);
printf("Enter your age: ");
scanf("%d", &num);
printf("Hello, %s! You are %d years old.\n", name, num);
FILE *file = fopen("output.txt", "w");
if (file == NULL)
{
printf("Error opening file!\n");
return 1;
}
fprintf(file, "Hello, %s! You are %d years old.\n", name, num);
fclose(file);
return 0;
}

8. How to pass pointers to functions? Explain with an example program.


Ans.
Passing pointers to functions is a powerful technique in C programming that allows you to
modify variables in the calling function. Here's how to do it:

Passing Pointers to Functions:


To pass a pointer to a function, you need to declare the function parameter as a pointer.
SYNTAX:
return-type function-name(pointer-parameter) {
//Function body//
}
When calling the function, you need to pass the address of the variable using the unary `&`
operator:
Syntax:
function-name(&variable);

PROGRAM:
// program that demonstrates passing pointers to functions//
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 10;
int y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Output:
Before swap: x = 10, y = 20
After swap: x = 20, y = 10

In this example, the `swap` function takes two integer pointers as parameters. The
function swaps the values of the integers by dereferencing the pointers. In the `main`
function, we pass the addresses of `x` and `y` to the `swap` function using the unary `&`
operator. After the swap, the values of `x` and `y` are printed to the console.

9. Write a short note on Inter function communication and streams.


Ans.
Inter-function Communication and Streams:

Inter-function Communication: Inter-function communication refers to the exchange of


data between two or more functions in a program. This can be achieved through various
methods, including:

1. Function arguments: Passing data as arguments to a function.


2. Return values: Returning data from a function to the calling function.
3. Global variables: Sharing data through global variables.
4. Pointers: Passing pointers to data as arguments to a function.

Effective inter-function communication is crucial for writing modular, reusable, and


maintainable code.
Streams : A stream is a sequence of data elements made available over time. Streams can
be used for input/output operations, such as reading from a file or writing to the console.

Types of streams:

1. Input streams: Streams that provide input data, such as reading from a file.
2. Output streams: Streams that accept output data, such as writing to the console.
3. Binary streams: Streams that handle binary data, such as image files.
4. Text streams: Streams that handle text data, such as reading from a text file.

Streams provide a convenient and efficient way to perform input/output operations in


programs.

10. Explain the following with respect to functions:


(i) Function prototype
(ii) Function call
(iii) Returning from function.
Ans.
Here's an explanation of the three concepts with respect to functions:

(i) Function Prototype : A function prototype, also known as a function declaration, is a


statement that declares the existence of a function and its characteristics. It specifies the
function's name, return type, and parameter list.

SYNTAX:return-type function-name(parameter-list);
EXAMPLE :int add(int, int);

This prototype declares a function named `add` that takes two `int` parameters and
returns an `int` value.

(ii) Function Call : A function call is a statement that invokes a function, passing arguments
to it. When a function is called, the control is transferred to the function, and the function's
code is executed.

SYNTAX:function-name(argument-list);
EXAMPLE:
result = add(10, 20);

This statement calls the `add` function, passing `10` and `20` as arguments, and assigns the
returned value to the variable `result`.

(iii) Returning from Function:Returning from a function refers to the process of exiting the
function and returning control to the calling function. When a function returns, it can also
pass a value back to the calling function.

SYNTAX : return expression;


EXAMPLE:
int add(int x, int y)
{
int result = x + y;
return result;
}
In this example, the `add` function returns the sum of `x` and `y` using the `return`
statement. The returned value is then assigned to the variable `result` in the calling function.

11. Explain in detail about formatting input/output functions supported by C.


Ans.
In C, formatting input/output functions are used to read and write data in a specific format.
These functions provide a way to control the input/output operations, such as reading and
writing numbers, characters, and strings, with specific formats.

Output Functions:

printf(): Prints formatted output to the standard output (usually the screen).
Syntax: printf(format_string, argument1, argument2, ...);
Example: printf("Hello, %s!\n", "World");

fprintf(): Prints formatted output to a file.


Syntax: fprintf(file_pointer, format_string, argument1, argument2, ...);`
Example: fprintf(file, "Hello, %s!\n", "World");

sprintf(): Prints formatted output to a string.


Syntax: sprintf(string, format_string, argument1, argument2, ...);
Example: sprintf(buffer, "Hello, %s!\n", "World");

Input Functions:

scanf(): Reads formatted input from the standard input (usually the keyboard).
Syntax: scanf(format_string, argument1, argument2, ...);
Example: scanf("%d %s", &number, name);

fscanf(): Reads formatted input from a file.


Syntax: fscanf(file_pointer, format_string, argument1, argument2, ...);
Example: fscanf(file, "%d %s", &number, name);

sscanf(): Reads formatted input from a string.


Syntax: sscanf(string, format_string, argument1, argument2, ...);
Example: sscanf(buffer, "%d %s", &number, name);

Format Specifiers:

Format specifiers are used to specify the type of data to be read or written. Here are some
common format specifiers:
- %c: Character
- %d: Decimal integer
- %f: Floating-point number
- %s: String
- %x: Hexadecimal integer
- %o: Octal integer
- %p: Pointer
- %e: Exponential notation
- %g: General format

Modifiers:

Modifiers are used to modify the behavior of format specifiers. Here are some common
modifiers:

- h: Short integer
- l: Long integer
- L: Long double
- ll: Long long integer
- u: Unsigned integer
- +: Always include a sign
- 0: Pad with zeros
- -: Left-justify
- (space): Leave a space before the value

Flags:

Flags are used to control the behavior of format specifiers. Here are some common flags:

- #: Alternate form
- 0: Pad with zeros
- -: Left-justify
- (space): Leave a space before the value
- +: Always include a sign

These are the basic formatting input/output functions supported by C. By using these
functions, you can control the input/output operations and format the data according to your
needs.

12. What do you mean by streams? What are the applications of it? Differentiate text
and binary streams.
Ans.
In computer science, a stream refers to a sequence of data elements made
available over time, often in a continuous flow. Streams can be thought of as a conduit for
data to flow through, allowing programs to process the data in real-time.

Applications of streams:
1. File input/output: Streams are used to read and write files, allowing programs to process
file data in a continuous flow.
2. Network communication: Streams are used to transmit data over networks, enabling real-
time communication between devices.
3. Audio and video processing: Streams are used to process audio and video data in real-
time, enabling applications like video streaming and audio editing.
4. Data compression: Streams are used to compress data in real-time, reducing the amount
of data that needs to be stored or transmitted.

Now, let's differentiate between text and binary streams:

Text Streams:

1. Character-based: Text streams process data one character at a time.


2. ASCII or Unicode: Text streams typically use ASCII or Unicode character encodings.
3. Formatted data: Text streams often contain formatted data, such as lines of text,
separated by newline characters.
4. Human-readable: Text streams are designed to be human-readable, making them suitable
for editing and viewing.

Examples of text streams include:

- Text files (e.g., .txt, .csv)


- Console input/output
- Web pages (HTML, CSS, JavaScript)

Binary Streams:

1. Byte-based: Binary streams process data one byte at a time.


2. Raw, unformatted data: Binary streams contain raw, unformatted data, which can
represent images, audio, or other types of data.
3. Machine-readable: Binary streams are designed to be machine-readable, making them
suitable for processing by computers.
4. Not human-readable: Binary streams are not human-readable, as they contain raw,
unformatted data.

Examples of binary streams include:

- Image files (e.g., .jpg, .png)


- Audio files (e.g., .mp3, .wav)
- Executable files (e.g., .exe, .dll)
- Database files

In summary, text streams are designed for human-readable, formatted data, while
binary streams are designed for machine-readable, raw, unformatted data.

You might also like