0% found this document useful (0 votes)
3 views

C Programming Answers

The document provides a comprehensive question bank on C programming, covering string functions, memory allocation, predefined macros, directives, command line arguments, pointers, and macros for calculations. It includes explanations, examples, and C code snippets for various topics such as string manipulation, memory management, and pointer operations. Additionally, it highlights differences between macros and functions, as well as the concept of dangling pointers.

Uploaded by

mayankbajaj773
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 views

C Programming Answers

The document provides a comprehensive question bank on C programming, covering string functions, memory allocation, predefined macros, directives, command line arguments, pointers, and macros for calculations. It includes explanations, examples, and C code snippets for various topics such as string manipulation, memory management, and pointer operations. Additionally, it highlights differences between macros and functions, as well as the concept of dangling pointers.

Uploaded by

mayankbajaj773
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/ 6

C Programming Question Bank - Answers

1) Explain the following function strlen(), strcmp(), strrev(), strupr()

strlen(): Returns the number of characters in a string (excluding null character '\0').

Example: strlen("Hello") returns 5.

strcmp(): Compares two strings and returns:

- 0 if they are equal,

- <0 if the first is less than the second,

- >0 if the first is greater.

Example: strcmp("A", "B") returns a negative value.

strrev(): Reverses a string. (Available in Turbo C, not standard in GCC)

Example: strrev("hello") becomes "olleh".

strupr(): Converts all lowercase letters in a string to uppercase. (Not standard in GCC)

Example: strupr("hello") becomes "HELLO".

2) Write a C program to copy one string into another string using a predefined string

function.

#include <stdio.h>

#include <string.h>

int main() {

char source[100], destination[100];

printf("Enter a string: ");

gets(source);
strcpy(destination, source);

printf("Copied string: %s", destination);

return 0;

3) Explain predefined macros with example.

Predefined macros are built-in macros provided by the compiler:

__DATE__ - compilation date

__TIME__ - compilation time

__FILE__ - name of current file

__LINE__ - current line number

__STDC__ - 1 if compiler follows ANSI C

Example:

printf("File: %s, Date: %s, Time: %s\n", __FILE__, __DATE__, __TIME__);

4) Explain directive.

A directive is an instruction to the preprocessor that begins with #.

Types:

- #include: Includes header files

- #define: Defines macros or constants

- #if/#ifdef/#ifndef: Conditional compilation

- #undef: Undefines a macro

They are processed before the actual compilation begins.

5) Explain the malloc() and calloc() function with example.


malloc(): Allocates memory without initializing.

Syntax: malloc(size);

Example: int *p = (int*)malloc(5 * sizeof(int));

calloc(): Allocates and initializes memory to zero.

Syntax: calloc(n, size);

Example: int *p = (int*)calloc(5, sizeof(int));

6) Write a C program to find maximum of two numbers using pointer.

#include <stdio.h>

int main() {

int a = 10, b = 20;

int *p1 = &a, *p2 = &b;

if(*p1 > *p2)

printf("Max: %d", *p1);

else

printf("Max: %d", *p2);

return 0;

7) Explain any three predefined functions used in Strings.

1. strlen(): Returns length of a string.

2. strcpy(): Copies one string to another.

3. strcmp(): Compares two strings.

8) Write a C program to find length of a string using predefined function.

#include <stdio.h>

#include <string.h>
int main() {

char str[] = "Hello World";

printf("Length = %lu", strlen(str));

return 0;

9) Differentiate between macro & function.

Macro:

- Preprocessed before compilation

- No type checking

- Faster execution

Function:

- Compiled at runtime

- Type checking done

- More flexible and safer

10) Write a C program to calculate area of circle using a macro.

#include <stdio.h>

#define PI 3.14

#define AREA(r) (PI * r * r)

int main() {

float r = 5;

printf("Area = %.2f", AREA(r));

return 0;

}
11) Explain command line arguments with an example.

They allow passing values to main() while executing the program.

Example:

#include <stdio.h>

int main(int argc, char *argv[]) {

printf("Program: %s\n", argv[0]);

return 0;

12) Explain any four types of pointers in C programming.

1. Null Pointer - points to nothing.

2. Void Pointer - generic pointer without type.

3. Wild Pointer - uninitialized pointer.

4. Dangling Pointer - points to freed or invalid memory.

13) Write a macro SI that will compute the simple interest.

#include <stdio.h>

#define SI(p, r, t) ((p * r * t) / 100.0)

int main() {

float p, r, t;

scanf("%f %f %f", &p, &r, &t);

printf("SI = %.2f", SI(p, r, t));

return 0;

14) Write a C program to swap two integers using pointer (Hint: use call by reference)
#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

swap(&x, &y);

printf("x = %d, y = %d", x, y);

return 0;

15) What is Dangling Pointer?

A dangling pointer is a pointer that points to a memory location that has been freed or no longer

valid.

Example:

int *ptr;

int x = 5;

ptr = &x;

// x goes out of scope; ptr becomes dangling.

You might also like