0% found this document useful (0 votes)
3 views9 pages

ppc

The document is a question bank focused on C programming concepts, covering topics such as strings, functions, recursion, and variable types. It includes definitions, examples, and explanations of various functions like strlen(), strcat(), and recursion for calculating factorials. Additionally, it discusses user-defined functions, parameters, and the differences between local and global variables.

Uploaded by

IRFAN MUQTADIR
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)
3 views9 pages

ppc

The document is a question bank focused on C programming concepts, covering topics such as strings, functions, recursion, and variable types. It includes definitions, examples, and explanations of various functions like strlen(), strcat(), and recursion for calculating factorials. Additionally, it discusses user-defined functions, parameters, and the differences between local and global variables.

Uploaded by

IRFAN MUQTADIR
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

3rd IA Question Bank

1. Define string with an example


In C programming, a string is a sequence of characters stored in a character array, terminated
by a special null character (\0).
Example: char greeting[] = "Hello, World!";

2. Define gets() and getchar()


The gets() and getchar() functions are standard input functions in C programming, used for
reading data from the standard input stream
gets() function:
Reads an entire line of text (a string) from the standard input until a newline character (\n) or
end-of-file (EOF) is encountered.
getchar() function:
Purpose: Reads a single character from the standard input.
3. Define strupr() with an example
The strupr() function in C is used to convert all lowercase characters within a given
string to their uppercase equivalents. It modifies the original string in place and returns a
pointer to the modified string.
Example:
char str[] = "hello world";
printf("Original string: %s\n", str);
strupr(str);
printf("Uppercase string: %s\n", str);

4. Define Strlwr() with an example


The strlwr() function is a string manipulation function, commonly found in C and C++,
that converts all uppercase letters within a given string to their lowercase equivalents.
char *strlwr(char *string);

Parameters:

string: A pointer to the null-terminated character array (string) that needs to be


converted to lowercase.

5. Define function. List different types of functions


In C programming, a function is a self-contained block of code designed to perform a
specific task. It can take input values (parameters), process them, and optionally
return a result.
Different types of functions available in C are:
a. In built Function
b. User Defined Function

6. Define library functions and user defined functions


Library Functions: These are pre-written, pre-compiled functions that are part of a
programming language's standard library or external libraries. They are readily available for
use by programmers and perform common, frequently needed tasks.
3rd IA Question Bank

User-Defined Functions:These are functions created by programmers to perform specific


tasks within their own programs. They allow for modularity, reusability, and better
organization of code.

7. What do you mean by function prototype


A function prototype in C is a declaration of a function that specifies its name, return type,
and the types of its parameters, without providing the actual function body
(implementation). It acts as a blueprint or interface for the compiler, informing it about the
function's signature before the function is actually defined or called.

8. List three components of user defined function


The three primary elements of a user-defined function are function declaration, function
definition, and function call.

1. Explain strlen(), strrev() and strcpy() with suitable example (8marks)


The C language provides several standard library functions for manipulating strings, declared
in the <string.h> header. Three commonly used functions are strlen(), strrev(), and strcpy().

1. strlen() (String Length)


The strlen() function calculates the length of a given string, excluding the null terminator (\0).
It takes a pointer to a constant character array (the string) as input and returns the length as a
size_t value.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char myString[] = "Hello";
size_t length = strlen(myString);
printf("The length of \"%s\" is: %zu\n", myString, length);
return 0;
}

2. strrev() (String Reverse)


The strrev() function reverses the characters of a string in place. It modifies the original
string, changing its order of characters.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char originalString[] = "World";
printf("Original string: %s\n", originalString);
strrev(originalString);
printf("Reversed string: %s\n", originalString);
return 0;
3rd IA Question Bank

3. strcpy() (String Copy)


The strcpy() function copies the contents of a source string to a destination string. It takes two
arguments: a pointer to the destination character array and a pointer to the source character
array. It copies characters from the source until it encounters the null terminator, including
the null terminator itself. It's crucial to ensure the destination buffer is large enough to
accommodate the source string to prevent buffer overflows.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char sourceString[] = "Programming";
char destinationString[20];
strcpy(destinationString, sourceString);
printf("Source string: %s\n", sourceString);
printf("Destination string: %s\n", destinationString);
return 0;
}

2. Explain strcat() and strcmp() with suitable example(8marks)


The functions strcat() and strcmp() are standard library functions in C, found in
the <string.h> header, used for string manipulation.
1. strcat() (String Concatenation): The strcat() function is used to concatenate (join)
two strings. It appends the source string to the end of the destination string.
Syntax:
char *strcat(char *dest, const char *src);
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
2. strcmp() (String Comparison): The strcmp() function is used to compare two
strings lexicographically (based on their ASCII values).
Syntax:
int strcmp(const char *str1, const char *str2);
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "apple";
char s2[] = "banana";
3rd IA Question Bank

int result1 = strcmp(s1, s2);


int result3 = strcmp(s2, s1);
printf("strcmp(\"%s\", \"%s\") = %d\n", s1, s2, result1);
printf("strcmp(\"%s\", \"%s\") = %d\n", s2, s1, result3);
return 0;
}

3. Write a C program to perform the following operations without using Library


functions.(10marks)
a. Concatenate two strings
b. Copy a string from source to Destination

A. Concatenate two Strings:


#include <stdio.h>
void concatenateStrings(char *dest, const char *src)
{
int i = 0;
int j = 0;

while (dest[i] != '\0')


{
i++;
}
while (src[j] != '\0')
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
}
int main()
{
char str1[100] = "Hello, ";
char str2[] = "World!";
concatenateStrings(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}

B. Copy a string from source to Destination


#include <stdio.h>
void stringCopy(char *destination, const char *source)
{
int i = 0;
while (source[i] != '\0')
{
destination[i] = source[i];
i++;
}
3rd IA Question Bank

destination[i] = '\0';
}

int main()
{
char sourceString[] = "Hello, World!";
char destinationString[50];

stringCopy(destinationString, sourceString);

printf("Source string: %s\n", sourceString);


printf("Destination string: %s\n", destinationString);

return 0;
}

4. Define recursion. Write a C program to find factorial of a given number using


recursion (8marks)
Recursion in C is a programming technique where a function calls itself, either directly or
indirectly, to solve a problem.

#include <stdio.h>

int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}

int main()
{
int number;
printf("Enter a non-negative integer: ");
scanf("%d", &number);
if (number < 0)
{
printf("Factorial is not defined for negative numbers.\n");
} else
{
int result = factorial(number);
printf("The factorial of %d is %lld.\n", number, result);
}
3rd IA Question Bank

return 0;
}
5. Explain arguments and parameters with suitable example(8marks)
In C programming, parameters and arguments are distinct but related concepts used in
the context of functions:
Parameters:
Definition: Parameters are the variables declared in the function's definition or
prototype. They act as placeholders for the values that the function expects to receive
when it is called.
Example: In the function declaration int add(int a, int b);, a and b are the parameters

Arguments:
Definition: Arguments are the actual values passed to a function when it is called.
Example:
In the function call int sum = add(5, 10);, 5 and 10 are the arguments.

#include <stdio.h>

// Function definition with parameters 'x' and 'y'


int multiply(int x, int y)
{
return x * y;
}

int main()
{
int num1 = 7;
int num2 = 3;

// Calling the function 'multiply' with arguments 'num1' and 'num2'


int result = multiply(num1, num2);

printf("The product is: %d\n", result);

return 0;
}

6. Explain global variable and local variable with suitable example(8marks)


In C programming, the distinction between local and global variables lies in their
scope (where they can be accessed) and lifetime (how long they exist in memory).
Local Variables:
Declaration: Declared inside a function or a specific block of code (e.g., within a
loop or an if statement).
Scope: Accessible only within the function or block where they are declared. They
are not visible or usable outside of that specific scope.
Lifetime: Created when the function or block is entered and destroyed when the
function or block exits. Their memory is typically allocated on the stack.
Example:
void myFunction()
{
3rd IA Question Bank

int LV = 10; // LV is a local variable


}
Global Variables:
Declaration: Declared outside of any function, usually at the top of the source file.
Scope: Accessible from anywhere within the entire program, including all functions
and blocks in the same file.
Lifetime: Created when the program starts and destroyed only when the program
terminates. Their memory is typically allocated in the data segment of the program.
Example:

int globalVariable = 20; // globalVariable is a global variable

void anotherFunction()
{
globalVariable = 30;
}

int main()
{
printf("Value: %d\n", globalVariable);
anotherFunction();
printf("New Value: %d\n", globalVariable);
return 0;
}

7. Briefly explain function call with suitable example(8marks)


A function call is an instruction in a program that executes a previously defined
function. It involves invoking the function by its name, followed by parentheses that
may contain arguments (values passed to the function). When a function is called, the
program's control transfers to the function's body, where its instructions are executed.
After the function completes its task, control returns to the point in the program where
the function was called.
Example:
#include <stdio.h>
// Function definition
int add(int a, int b)
{
return a + b;
}

int main()
{
int num1 = 5;
int num2 = 10;
int sum_result;

// Function call
sum_result = add(num1, num2);

printf("The sum is: %d\n", sum_result);


3rd IA Question Bank

return 0;
}
8. Briefly explain components of user defined function(10marks)
A user-defined function (UDF) in programming is a block of code designed to
perform a specific task, created by the programmer.
Its key components include:
Return Type: This specifies the data type of the value that the function will send
back to the calling part of the program after its execution. If a function does not return
any value, its return type is typically specified as void.
Function Name: This is a unique identifier given to the function, allowing it to be
called and referenced within the program. Function names should follow the naming
conventions of the specific programming language, typically consisting of letters,
numbers, and underscores, and usually not starting with a digit.
Parameters (Arguments): These are variables or values that are passed into the
function when it is called. They allow the function to receive data from the calling
code and use it in its operations. Parameters are specified with their data types within
the function's definition.
Function Body: This is the core of the function, enclosed within curly braces or
similar delimiters depending on the language. It contains the actual instructions and
logic that the function executes to perform its designated task. This can include
declarations of local variables, computational statements, and control flow structures.
Return Statement (Optional, depending on return type): If the function has a non-
void return type, a return statement is used within the function body to send the
computed value back to the caller. It also terminates the function's execution.

9. Briefly explain categories of function with suitable example. (10marks)


1. Functions with no arguments and no return values: These functions perform actions
without needing input and don't produce a result to be used elsewhere.
void greet()

// This function prints a message

printf("Hello, world!\n");

2. Functions with arguments and no return values: These functions take input to perform actions
but don't return a value.

void printSum(int a, int b)

// This function calculates and prints the sum of two integers

printf("Sum: %d\n", a + b);

}
3rd IA Question Bank

3. Functions with no arguments and return values: These functions produce a result without
needing any specific input

int generateRandomNumber()

// This function returns a random integer

return rand();

4. Functions with arguments and return values: These functions take input and produce a result.

int add(x, y)

return x + y;

You might also like