COMPUTER SCIENCE
COMPUTER SCIENCE
(Discipline Specific Core)
Paper Name: Introduction to C Programming
Paper Code: BCA-DSC-143
Full Marks: 45
1. Answer the following questions as directed. 1x4=4
(a) All keywords in C are in ______ ? [Choose the correct option](i) Upper Case Letters
(ii) Lower Case Letters
(iii) Camel Case Letters
(iv) Both (i) and (ii).
Ans: (ii) Lower Case Letters
(b) The statements in the statement block are encapsulated within a _____? [fill in the blank]Ans: block
[*Note - The statements in the statement block are encapsulated within a block, also known as a compound statement.]
(c) In C, an array subscript starts from ______ [fill in the blank]Ans: 0
Ans: Pointer
2. Answer the following questions: 2x3=6
(a) What is the significance of the following declaration?
Ans: Declarations in a C program are crucial because they provide essential information to the compiler, enabling it to correctly manage and utilize program elements.
(b) Differentiate between call by value and call by reference.
Ans: In C programming, "call by value" and "call by reference" are two distinct methods for passing arguments to functions, impacting how data is handled and modified within the function and back in the calling environment.
Call by Value:
Mechanism: When using call by value, a copy of the actual argument's value is passed to the function's formal parameter.
Memory: The actual argument and the formal parameter reside in separate memory locations.
Modification: Any changes made to the formal parameter inside the function do not affect the original value of the actual argument in the calling function. The changes are confined to the function's scope.
Call by Reference:
Mechanism: When using call by reference, the address (or a reference in C++) of the actual argument is passed to the function's formal parameter (which is typically a pointer in C).
Memory: Both the actual argument and the formal parameter (pointer) refer to the same memory location.
Modification: Any changes made to the data pointed to by the formal parameter inside the function directly affect the original value of the actual argument in the calling function.
(c) Explain any two operators used in C language.
Ans:
Arithmetic Operators: These operators are used to perform mathematical calculations on numeric values.
Addition (+): This operator adds two operands.
Subtraction (-): This operator subtracts the second operand from the first.
Multiplication (*): This operator multiplies two operands.
Division (/): This operator divides the first operand by the second and returns the quotient. For integer division, any fractional part is truncated.
Modulus (%): This operator returns the remainder of an integer division.
Relational Operators: These operators are used to compare two values and return a Boolean result (true or false, represented as 1 or 0 in C).
Equal to (==): Checks if two operands are equal.
Not equal to (!=): Checks if two operands are not equal.
Greater than (>): Checks if the left operand is greater than the right operand.
Less than (<): Checks if the left operand is less than the right operand.
Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
3. Answer the following questions (any three) 5x3=15
(a) List the conditional branching statements in "C". Explain anyone with an example.
Ans: The conditional branching statements in C are:
- if statement: Executes a block of code if a specified condition is true.
- if-else statement: Executes one block of code if a condition is true, and another block if the condition is false.
- else-if ladder: Allows testing multiple conditions sequentially, executing the block associated with the first true condition.
- switch statement: Provides an alternative to the if-else-if ladder for handling multiple conditions based on the value of a single variable or expression.
Explanation of if-else statement with an example:
The if-else statement in C allows a program to make a decision between two alternative paths of execution based on whether a given condition is true or false.
Syntax
Example
Explanation:
In this example, the program checks the value of the age variable.
- If age is greater than or equal to 18 (the condition age >= 18 is true), the message "You are eligible to vote." is printed.
- If age is less than 18 (the condition age >= 18 is false), the code within the else block is executed, and the message "You are not yet eligible to vote." is printed.
(b) What is a pointer? Explain how the pointer variable is declared and initialized.
Ans: Pointer is a variable that can stores the address of another variables.
[*Note - Pointer variable is always integer.]
Declaration of a Pointer Variable:
To declare a pointer variable, specify the data type of the variable it will point to, followed by an asterisk * and the pointer variable's name. The asterisk indicates that the variable being declared is a pointer.
Initialization of a Pointer Variable:
Initializing a pointer variable involves assigning it the memory address of another variable. This is done using the address-of operator &, which returns the memory address of a variable.
Alternatively, you can declare the pointer and then initialize it in a separate statement:
It is crucial to initialize pointers to a valid memory address or to NULL (or nullptr in C++) if they are not immediately pointing to a variable. An uninitialized pointer contains a garbage value and can lead to unpredictable behavior or crashes if dereferenced.
(c) Explain the basic structure of a "C program".
Ans: A "C program", at its most basic, follows a structured format designed for clarity and functionality. The "Hello World" program is a classic example demonstrating this structure.
Here's the basic structure of a C program, illustrated with "Hello World":
Explanation of the Structure:
- #include <stdio.h>: This is a preprocessor directive that includes the standard input/output library. This library provides functions like printf() for displaying output to the console.
- #include <conio.h>: This preprocessor directive includes the console input/output library, which is specific to some compilers (like Turbo C/C++). It provides functions like getch(), used here to pause the console window after execution until a key is pressed.
- void main(): This declares the main function, which is the entry point of every C program.
- void indicates that the main function does not return any value. While int main() is the standard and preferred return type for main in modern C, void main() was common in older compilers.
- The empty parentheses () signify that the main function takes no arguments.
- { ... }: These curly braces define the body of the main function, containing the instructions to be executed.
- printf("Hello World!\n");: This statement calls the printf() function to print the string "Hello World!" to the console.
- \n is an escape sequence that inserts a newline character, moving the cursor to the next line after printing.
- getch();: This function waits for a single character input from the user without displaying it on the screen. In this context, it effectively pauses the console window after the "Hello World!" message is displayed, preventing it from closing immediately, allowing the user to see the output.
(d) Compare between interpreter and compiler.
Ans: While the C language is primarily associated with compilation, understanding the differences between compilers and interpreters provides valuable insight into how programming languages are processed.
Compiler:
- Translation Method: A compiler translates the entire source code of a program into machine code (or an intermediate bytecode) before execution. This results in a standalone executable file.
- Execution Speed: Compiled code generally runs faster because the translation process occurs only once, and the resulting machine code can be executed directly by the processor.
- Error Handling: Compilers typically report all errors found in the source code during the compilation phase, before the program can even be run.
- Memory Usage: Compilers may require more memory during the compilation process to generate and store the intermediate object code or executable file.
- Debugging: Debugging can be more challenging as errors are reported for the entire program, and pinpointing the exact location might require more effort.
- Examples in C: When you compile a C program using gcc, you are using a compiler.
Interpreter:
- Translation Method: An interpreter translates and executes the source code line by line during runtime. It does not produce a separate executable file.
- Execution Speed: Interpreted code generally runs slower than compiled code due to the overhead of translating and executing each line in real-time.
- Error Handling: Interpreters typically report errors as they are encountered during execution, stopping the program at the point of the error.
- Memory Usage: Interpreters generally require less memory as they do not generate and store a complete intermediate or executable file.
- Debugging: Debugging can be easier as errors are reported immediately when they occur, allowing for quick identification and correction.
- Examples: While C is not typically interpreted, languages like Python, JavaScript, and Ruby are commonly interpreted.
Key Differences Summarized:
| Feature | Compiler | Interpreter |
| Translation | Entire program before execution | Line by line during execution |
| Executable | Generates a standalone executable file | Does not generate a standalone executable |
| Speed | Faster execution | Slower execution |
| Error Report | All errors reported at once (compilation) | Errors reported line by line (runtime) |
| Memory | May require more (for object/executable) | Generally requires less |
| Debugging | Can be more challenging | Often easier |
(e) Write a C-program to generate the following series: 0, 1, 1, 2, 3, 5,8,13,........., upto 20th terms.
Ans:
Fibonacci series up to 20 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,1597, 2584, 4181
4. Answer the following questions (any two) 10x2=20
(a) Define structure. How does it differ from an array? Describe array of structure with an example.
Ans: A structure is a user-defined data type that groups together variables of potentially different data types under a single name. It allows for the creation of complex data types, representing real-world entities with various attributes.
Difference from an Array:
The primary distinction between a structure and an array lies in the data types of their elements:
Arrays
are collections of elements of the same data type, stored in contiguous memory locations and accessed by an index.
Structuresare collections of elements (members) of different data types, grouped together logically and accessed by their member names using the dot operator. Memory allocation for structure members may not be contiguous.
Array of Structures with an Example:
An array of structures is a collection where each element of the array is a structure. This allows for storing multiple records of the same type, where each record (structure) contains various related data points.
Example (in C):
Consider a scenario where you need to store information for multiple students, each having a name, roll number, and marks.
In this example, students is an array where each element students[0], students[1], and students[2] is a Student structure, holding the name, roll number, and marks for a particular student.
Output
Student 1:
Name: Alice
Roll Number: 101
Marks: 85.50
Student 2:
Name: Bob
Roll Number: 102
Marks: 92.00
Student 3:
Name: Charlie
Roll Number: 103
Marks: 78.20
(b) What is file? Describe the use of getc() and putc() functions. Write a C program to copy the contents of one file to another.
Ans: A file is a named collection of related data or information stored on a computer's secondary storage, such as a hard drive or solid-state drive. Files can contain various types of data, including text, images, audio, video, or executable program instructions. They provide a persistent way to store and retrieve information, allowing programs to access and manipulate data even after they have finished executing.
Use of getc() and putc() functions in C:
getc(): This function is used to read a single character from a specified input stream (typically a file). It takes a FILE pointer as an argument and returns the character read as an int. If the end of the file is reached or an error occurs, getc() returns EOF.
putc(): This function is used to write a single character to a specified output stream (typically a file). It takes an int representing the character to be written and a FILE pointer as arguments. It returns the character written on success, or EOF on failure.
C program to copy the contents of one file to another:
(c) Explain the switch statement with syntax and example.
Ans: The switch statement is a control flow mechanism used in many programming languages to execute different blocks of code based on the value of a single expression or variable. It offers an alternative to using multiple if-else if statements when dealing with a fixed set of possible values.
Syntax:
The general syntax of a switch statement is as follows:
- switch (expression): The switch keyword is followed by an expression (or variable) in parentheses. The value of this expression is compared against the case values.
- case value: Each case keyword is followed by a constant value. If the expression's value matches a case's value, the code block associated with that case is executed.
- break: The break statement is crucial. It terminates the switch statement, preventing "fall-through" where execution continues into subsequent case blocks even if their values do not match.
- default: The default block is optional. If present, its code is executed if none of the case values match the expression's value.
Example (C language):
Explanation of the example:
- The day variable is initialized to 3.
- The switch statement evaluates day.
- It finds a match with case 3.
- The code printf("It's Wednesday.\n"); is executed.
- The break statement then exits the switch block.
- The output will be: It's Wednesday.
(d) List the differences between while loop and do-while loop. Write a C program to find sum of Natural Numbers from 1 to N using for loop.
Ans: The differences between a while loop and a do-while loop are as follows:
- Condition Check Timing: A while loop checks the condition before executing the loop body (entry-controlled), while a do-while loop executes the loop body at least once and then checks the condition (exit-controlled).
- Guaranteed Execution: Consequently, a while loop's body may not execute at all if the initial condition is false, whereas a do-while loop's body is guaranteed to execute at least once.
- Syntax: A while loop's syntax does not require a semicolon after the condition (e.g., while (condition) { /* body */ }), while a do-while loop requires a semicolon after the while condition (e.g., do { /* body */ } while (condition);).
Here is a C program to find the sum of natural numbers from 1 to N using a for loop:
*********
OR DOWNLOAD
Please Wait, Files are Loading...
Please wait 15s
