Fundamental Concepts of C Programming: Short Answer Questions (5 Marks)
Fundamental Concepts of C Programming: Short Answer Questions (5 Marks)
This blog post provides clear and concise answers to common short-answer questions in C programming. Whether you're a student preparing for exams or a beginner looking to strengthen your fundamentals, these explanations and examples will help you understand key concepts in C.
1. Define “constant”, “identifier” and “keyword” in C.
- Constant: A constant is a value that does not change throughout the execution of a program. It can be numeric, character, or string. Example:
#define PI 3.14
- Identifier: An identifier is the name assigned to variables, functions, or arrays. It must begin with a letter or underscore. Example:
int age;
(Here,age
is an identifier). - Keyword: A keyword is a reserved word in C that has a special meaning and cannot be used as an identifier. Example:
int
,return
,if
,while
, etc.
2. What is a variable? List the major data types used in a C program.
A variable is a container for storing data values. It must be declared before use.
Major data types in C:
- int – Integer values (e.g.,
int x = 10;
) - float – Floating-point numbers (e.g.,
float pi = 3.14;
) - char – Single character (e.g.,
char grade = 'A';
) - double – Double-precision floating-point numbers (e.g.,
double value = 123.456;
)
3. What is a structure in a C program? Explain.
A structure is a user-defined data type that groups related variables of different data types.
Example:
cstruct Student {
char name[50];
int roll;
float marks;
};
Here, Student
is a structure containing name
, roll
, and marks
.
4. List out the different types of operators in C. Describe the logical operator with an example.
Types of operators in C:
- Arithmetic (
+
,-
,*
,/
,%
) - Relational (
>
,<
,>=
,<=
,==
,!=
) - Logical (
&&
,||
,!
) - Bitwise (
&
,|
,^
,~
,<<
,>>
) - Assignment (
=
,+=
,-=
,*=
,/=
,%=
)
Logical Operator Example:
cint a = 10, b = 20;
if (a > 0 && b > 0) {
printf("Both are positive numbers");
}
Here, &&
checks if both conditions are true.
5. Define a pointer in C with an example.
A pointer is a variable that stores the memory address of another variable.
Example:
cint x = 10;
int *ptr = &x;
printf("%d", *ptr); // Output: 10
Here, ptr
stores the address of x
, and *ptr
retrieves its value.
6. Describe the different modes of file handling in C.
"r"
– Read mode"w"
– Write mode (creates a new file or truncates an existing file)"a"
– Append mode"r+"
– Read and write"w+"
– Write and read"a+"
– Append and read
7. Explain the file handling concept in C.
File handling allows reading and writing operations on files.
Example:
cFILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "Hello, world!");
fclose(fp);
This code writes "Hello, world!" to file.txt
.
8. Differentiate between an array and a structure with an example.
Feature | Array | Structure |
---|---|---|
Data type | Same type | Different types |
Memory allocation | Contiguous | Separate for each member |
Example:
cint arr[5] = {1, 2, 3, 4, 5}; // Array
struct Student { char name[50]; int roll; }; // Structure
9. Explain the array and structure with examples.
- Array Example:
cint arr[3] = {10, 20, 30};
printf("%d", arr[1]); // Output: 20
- Structure Example:
cstruct Student { char name[50]; int roll; };
10. What is a function? List out the advantages of functions.
A function is a block of code designed to perform a specific task.
Advantages:
- Code reusability
- Easy debugging
- Modular programming
11. Describe fscanf and fprintf functions.
fscanf(FILE *fp, format, variables)
– Reads from a filefprintf(FILE *fp, format, variables)
– Writes to a file
12. Differentiate between a structure and a union.
- Structure: Allocates memory for all members separately.
- Union: Allocates memory for the largest member only.
Example:
cunion Data { int i; float f; };
struct Data { int i; float f; };
13. Describe any two file handling functions.
fopen()
– Opens a file.fclose()
– Closes a file.
14. What is a string function? Write a program to concatenate two strings.
Example:
cchar str1[20] = "Hello ", str2[20] = "World";
strcat(str1, str2);
printf("%s", str1); // Output: Hello World
15. What is a recursive function? Write a program to calculate factorial.
A recursive function calls itself.
Example:
cint factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
16. What is a pointer? Describe the benefits of pointers.
Benefits:
- Dynamic memory allocation
- Efficient array handling
- Faster program execution
17. What is a loop? Describe the types of loops with examples.
Types:
- for loop
- while loop
- do-while loop
Example:
cfor (int i = 0; i < 5; i++) {
printf("%d ", i);
}
18. What is an array? Differences between array and pointer.
| Feature | Array | Pointer |
You said:
ChatGPT said:
Here is the revised and more detailed version of the answers for your blog post:
Fundamental Concepts of C Programming: Short Answer Questions (5 Marks)
This blog post provides detailed answers to common short-answer questions in C programming. Whether you're a student preparing for exams or a beginner looking to strengthen your fundamentals, these explanations and examples will help you understand key concepts in C.
1. Define “constant”, “identifier” and “keyword” in C.
Constant: A constant in C is a value that remains unchanged throughout the execution of a program. Constants can be defined using the
#define
directive or asconst
variables.
Example:c#define PI 3.14 // PI is a constant value const int MAX = 100; // MAX is a constant variable
Identifier: An identifier is the name given to variables, functions, arrays, or any other user-defined item in a C program. An identifier must begin with a letter (uppercase or lowercase) or an underscore (
_
), followed by letters, digits, or underscores. It cannot be a keyword.
Example:cint age; // 'age' is an identifier
Keyword: A keyword is a reserved word that has a predefined meaning in C and cannot be used as an identifier. C has a set of keywords such as
int
,float
,if
,while
, etc.
Example:cint sum = 10; // 'int' is a keyword
2. What is a variable? List the major data types used in a C program.
A variable in C is a memory location where data can be stored, modified, and retrieved. Each variable is associated with a data type, which defines the kind of data it can store.
Major data types in C:
int: Used for storing integer values.
Example:cint age = 25;
float: Used for storing single-precision floating-point numbers.
Example:cfloat temperature = 36.6;
char: Used for storing a single character.
Example:cchar grade = 'A';
double: Used for storing double-precision floating-point numbers (greater precision than
float
).
Example:cdouble pi = 3.14159;
void: Represents an empty type. Used for functions that do not return any value.
3. What is a structure in a C program? Explain.
A structure is a user-defined data type in C that allows grouping different data types into a single unit. A structure can contain variables of different types, which are called members or fields of the structure.
Example:
cstruct Student {
char name[50];
int roll;
float marks;
};
Here, the structure Student
has three members: name
, roll
, and marks
, which can hold different types of data.
To use a structure:
cstruct Student student1;
student1.roll = 101;
strcpy(student1.name, "John");
student1.marks = 85.5;
4. List out the different types of operators in C. Describe the logical operator with an example.
Types of operators in C:
- Arithmetic Operators:
+
,-
,*
,/
,%
(Used to perform mathematical operations) - Relational Operators:
==
,!=
,<
,>
,<=
,>=
(Used to compare two values) - Logical Operators:
&&
(AND),||
(OR),!
(NOT) (Used to perform logical operations) - Bitwise Operators:
&
,|
,^
,~
,<<
,>>
(Used to perform bit-level operations) - Assignment Operators:
=
,+=
,-=
,*=
,/=
,%=
(Used to assign values to variables) - Increment/Decrement Operators:
++
,--
(Used to increase or decrease a variable’s value)
Logical Operator Example:
cint a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both are positive numbers");
}
Here, the &&
operator checks if both conditions are true.
5. Define a pointer in C with an example.
A pointer is a variable that stores the memory address of another variable. Pointers are powerful tools in C, used for dynamic memory allocation, array handling, and function arguments.
Example:
cint x = 10;
int *ptr = &x; // pointer ptr stores the address of variable x
printf("Value of x: %d\n", *ptr); // Dereferencing the pointer gives the value of x
Here, &x
gives the memory address of x
, and *ptr
dereferences the pointer to access the value of x
.
6. Describe the different modes of file handling in C.
In C, file handling allows reading and writing data to files. The file must be opened in one of the following modes:
"r"
: Open for reading. If the file doesn’t exist, it returnsNULL
."w"
: Open for writing. If the file doesn’t exist, it creates a new file. If the file exists, it truncates the file."a"
: Open for appending. If the file doesn’t exist, it creates a new file. If the file exists, data is written at the end."r+"
: Open for both reading and writing."w+"
: Open for both reading and writing. Creates a new file if it doesn’t exist, truncates it if it does."a+"
: Open for both reading and appending.
7. Explain the file handling concept in C.
File handling in C allows programs to read and write data from or to a file. It involves three basic operations:
- Opening a file using the
fopen()
function. - Performing operations like reading and writing using functions such as
fprintf()
,fscanf()
,fputs()
,fgets()
, etc. - Closing the file using the
fclose()
function to release the resources.
Example:
cFILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "Hello, World!");
fclose(fp);
8. Differentiate between an array and a structure with an example.
Array: An array is a collection of elements of the same data type stored in contiguous memory locations. Example:
cint arr[5] = {1, 2, 3, 4, 5};
Structure: A structure is a user-defined data type that can store variables of different types.
Example:cstruct Student { char name[50]; int roll; float marks; };
Feature | Array | Structure |
---|---|---|
Data Type | Same type for all elements | Different types for members |
Memory Layout | Contiguous memory allocation | Non-contiguous memory |
Use Case | Storing homogeneous data | Storing heterogeneous data |
9. Explain the array and structure with examples.
Array Example:
cint arr[3] = {10, 20, 30}; printf("%d", arr[1]); // Output: 20
Structure Example:
cstruct Student { char name[50]; int roll; float marks; }; struct Student student1; student1.roll = 101; strcpy(student1.name, "John"); student1.marks = 85.5;
10. What is a function? List out the advantages of functions.
A function is a block of code that performs a specific task. Functions help in organizing and modularizing the code.
Advantages of Functions:
- Code Reusability: Functions allow code to be reused multiple times.
- Modular Programming: Functions break the code into smaller modules, making it easier to understand and maintain.
- Easier Debugging: Errors can be isolated and fixed in specific functions.
- Improved Readability: Functions help in organizing the code logically.
11. Describe fscanf and fprintf functions.
fscanf(FILE *fp, format, variables): Reads data from a file based on the specified format.
Example:cFILE *fp = fopen("data.txt", "r"); int age; fscanf(fp, "%d", &age); // Reads an integer from the file fclose(fp);
fprintf(FILE *fp, format, variables): Writes data to a file based on the specified format.
Example:cFILE *fp = fopen("output.txt", "w"); fprintf(fp, "Age: %d", 25); // Writes the integer to the file fclose(fp);
12. Differentiate between a structure and a union.
- Structure: A structure allocates memory for each member independently, allowing each member to hold different data types.
- Union: A union allocates memory equal to the size of its largest member, and all members share the same memory space.
Example:
cunion Data { int i; float f; };
struct Data { int i; float f; };
In a structure, both i
and f
can hold different values at the same time, whereas in a union, only one of them can hold a value at any given time.
Discussion (20)
Nice blog