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

C programming

The document provides a comprehensive overview of various programming concepts in C, including the differences between compilers and interpreters, dynamic memory allocation, recursion, and string library functions. It includes example C programs for swapping numbers, summing natural numbers, employee information storage, and calculating factorials using recursion. Additionally, it explains operating system functions and memory management techniques.

Uploaded by

vikash8930887509
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)
4 views9 pages

C programming

The document provides a comprehensive overview of various programming concepts in C, including the differences between compilers and interpreters, dynamic memory allocation, recursion, and string library functions. It includes example C programs for swapping numbers, summing natural numbers, employee information storage, and calculating factorials using recursion. Additionally, it explains operating system functions and memory management techniques.

Uploaded by

vikash8930887509
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/ 9

Q. What are the difference between compiler and interpreter?

Ans. In C programming, a compiler and an interpreter are both tools used for
different purposes in the software development process. Here are the main
differences between a compiler and an interpreter:
Compiler: A compiler translates the entire source code written in a high-level
programming language (such as C) into machine code or an intermediate code all
at once. The resulting executable file can be run independently of the original
source code.
Interpreter: An interpreter translates the source code line by line or statement
by statement and executes it immediately. It does not produce a separate
executable file; instead, it interprets and executes the code on-the-fly.
Compiler: The compilation process occurs before the program is executed. The
generated executable file can be executed repeatedly without the need for
recompilation.
Interpreter: The interpretation happens during runtime. The source code is
translated and executed on-the-fly, and the program needs to be interpreted
each time it is run.
Compiler: Produces an independent executable file, which can be executed
directly by the computer's operating system.
Interpreter: Does not produce a separate executable file. The source code is
interpreted and executed line by line.
Compiler: Generally, compiled code tends to be faster in terms of execution
because the entire code is translated before execution, and optimizations can be
applied during compilation.
Interpreter: Interpreted code may have a slower execution speed because it is
translated and executed line by line, and optimizations are limited to the
runtime.
Compiler: Debugging compiled code may be more challenging, as the connection
between the source code and the generated machine code can be less direct.
Interpreter: Debugging is often easier with an interpreter because errors are
reported in the context of the original source code.
Compiler: Compiled code may be less portable, as it is often specific to a
particular architecture or operating system.
Interpreter Interpreted code can be more portable, as long as there is an
interpreter available for the target platform.
Compiler: GCC (GNU Compiler Collection) is a popular C compiler.
Interpreter: CINT is an example of a C interpreter.

Q. Write a program to swap two numbers without using the third variable.
Ans. #include <stdio.h>
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("After swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}

Q. Explain Block diagram of computer and its component.


Ans. Creating a block diagram of a computer and its components using the C
programming language is not a typical use case, as C is a programming language
for developing software, not for creating visual diagrams. However, I can provide
you with a textual representation of a block diagram for better understanding.
A typical block diagram of a computer system includes various components. I'll
describe them briefly, and you can use this information to create a visual
representation using diagramming tools or drawing software.
1.Central Processing Unit (CPU):
- Responsible for executing instructions.
- C code: Functions and algorithms implemented in C are executed by the CPU.
2.Memory:
- RAM (Random Access Memory) for temporary storage of data during program
execution.
- ROM (Read-Only Memory) for storing firmware and BIOS.
- C code: Variables and data structures are stored in RAM during program
execution.
3. Input/Output (I/O) Devices:
- Devices such as keyboard, mouse, display, printer, etc.
- C code: Functions like `printf` and `scanf` are used for input and output
operations.
4. Storage Devices:
- Hard drives, solid-state drives (SSD), etc., for long-term storage.
- C code: File operations (e.g., `fopen`, `fwrite`, `fread`) are used for reading
and writing data to storage devices.
5. Motherboard:
- Connects all components and facilitates communication between them.
- C code: The operating system manages communication between hardware
components and executes programs written in C.
6.Bus System:
- Communication pathways for data transfer between components.
- C code: Data is transferred between components via function calls, memory
access, and I/O operations.
7.Power Supply:
- Provides power to all components.
- C code: Not applicable in the context of C programming; it's a hardware
aspect.
8.Peripheral Devices:
- Additional devices connected to the computer, such as USB devices, network
interfaces, etc.
- C code: Drivers and libraries are used to interact with peripheral devices.

Q. Write a program to find the sum of First 50 Natural Numbers.


Ans. #include <stdio.h>
int main() {
int i, sum = 0;
// Loop to calculate the sum of the first 50 natural numbers
for (i = 1; i <= 50; ++i) {
sum += i;
}
// Output the result
printf("The sum of the first 50 natural numbers is: %d\n", sum);
return 0;
}

Q. What do you understand by operating system? What are the functions of an


Operating System?
Ans. An operating system (OS) is a software program that acts as an
intermediary between computer hardware and the computer user. It provides a
set of services and manages the resources of a computer system, ensuring the
efficient and orderly execution of computer programs. The operating system
serves as the foundation for software applications to run and interact with the
hardware components of a computer.
Key functions of an operating system include:
1. Process Management:
- Creation and termination of processes.
- Scheduling of processes to ensure fair and efficient utilization of the CPU.
- Process synchronization and communication.
2. Memory Management:
- Allocation and deallocation of memory for programs and data.
- Virtual memory management for efficient use of available RAM and
secondary storage.
3. File System Management:
- Organization and storage of files on storage devices.
- File access control and security.
- File naming, creation, deletion, and manipulation.
4. Device Management:
- Management of input and output devices (e.g., keyboard, mouse, printer).
- Device drivers for communication between the operating system and
hardware devices.
5. Security and Protection:
- User authentication and access control.
- Data encryption and protection against unauthorized access.
- Implementation of security policies and mechanisms.
6. User Interface:
- Provides a user-friendly interface for interaction with the computer.
- Can be command-line-based or graphical (GUI) depending on the operating
system.
7.Network Management:
- Networking protocols and communication between computers in a network.
- Internet and network configurations.
8. Error Handling:
- Detection and handling of errors to ensure the stability and reliability of the
system.
- Logging and reporting of errors for diagnosis and troubleshooting.
9. Job Scheduling:
- Efficient allocation of system resources to different tasks or jobs.
- Prioritization and scheduling of tasks to optimize system performance.
10. Utilities:
- System utilities for tasks such as disk cleanup, defragmentation, and system
diagnostics.
- Command-line tools and graphical interfaces for system configuration.
11.Kernel:
- The core component of the operating system that manages hardware
resources and provides essential services.
- The kernel is responsible for low-level system operations and interacts
directly with the hardware.

Q. What do you understand Dynamic Memory Allocation? Write a C program to


allocate memory in heap using Calloc() function.
Ans. Dynamic Memory Allocation in C refers to the allocation of memory at
runtime, allowing the program to request and release memory as needed. The
`malloc()` and `calloc()` functions are commonly used for dynamic memory
allocation. The `calloc()` function is used to allocate a specified number of blocks
of memory, each with a specified size. Unlike `malloc()`, `calloc()` initializes the
allocated memory to zero.
Here's a simple C program that demonstrates the use of `calloc()` to allocate
memory on the heap:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *dynamicArray;
int size, i;
printf("Enter the size of the array: ");
scanf("%d", &size);
dynamicArray = (int *)calloc(size, sizeof(int));
if (dynamicArray == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 1;
}
printf("Enter %d integer values:\n", size);
for (i = 0; i < size; ++i) {
scanf("%d", &dynamicArray[i]);
}
printf("Values stored in the dynamically allocated array:\n");
for (i = 0; i < size; ++i) {
printf("%d ", dynamicArray[i]);
}
printf("\n");
free(dynamicArray);
return 0;
}

Q. Explain Call by value and call by address in C language with example.


Ans. In C, function arguments can be passed to a function in two main ways:
"call by value" and "call by address" (also known as "call by reference"). These
methods determine how the actual parameters (arguments) are passed to the
formal parameters (parameters) of a function. Let's explore both concepts with
examples.
Call by Value:
In the "call by value" method, the value of the actual parameter is passed to the
formal parameter. The changes made to the formal parameter inside the
function do not affect the actual parameter outside the function. It's like making
a copy of the value.
#include <stdio.h>
void incrementByValue(int num) {
num += 1;
printf("Inside function: %d\n", num);
}
int main() {
int x = 5;
printf("Before function call: %d\n", x);
incrementByValue(x);
printf("After function call: %d\n", x);
return 0;
}
Output:
Before function call: 5
Inside function: 6
After function call: 5

Call by Address (Call by Reference):


In the "call by address" method, the address of the actual parameter (memory
location) is passed to the formal parameter. This means that changes made to
the formal parameter inside the function directly affect the actual parameter.
#include <stdio.h>
void incrementByAddress(int *num) {
(*num) += 1;
printf("Inside function: %d\n", *num);
}
int main() {
int x = 5;
printf("Before function call: %d\n", x);
incrementByAddress(&x);
printf("After function call: %d\n", x);
return 0;
}
Output:
Before function call: 5
Inside function: 6
After function call: 6

Q. Write a C program to store information of employees using array of structure.


Ans. #include <stdio.h>
struct Employee {
int employeeID;
char name[50];
float salary;
};
int main() {
int numEmployees;
printf("Enter the number of employees: ");
scanf("%d", &numEmployees);
struct Employee employees[numEmployees];
for (int i = 0; i < numEmployees; ++i) {
printf("\nEnter details for Employee %d:\n", i + 1);
printf("Employee ID: ");
scanf("%d", &employees[i].employeeID);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}
printf("\nEmployee Information:\n");
for (int i = 0; i < numEmployees; ++i) {
printf("\nDetails for Employee %d:\n", i + 1);
printf("Employee ID: %d\n", employees[i].employeeID);
printf("Name: %s\n", employees[i].name);
printf("Salary: %.2f\n", employees[i].salary);
}
return 0;
}
Q. What do you man by recursion? Write a C program to find factorial of a
number using recursion.
Ans. Recursion is a programming concept where a function calls itself, either
directly or indirectly, to solve a problem. In the context of programming, a
recursive function is a function that performs a task in part and delegates the
remaining task to itself.
Here's an example of a C program to find the factorial of a number using
recursion:
#include <stdio.h>
unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
unsigned long long result = factorial(num);
printf("Factorial of %d = %llu\n", num, result);
}
return 0;
}

Q. Explain any three string library functions with examples.


Ans. String library functions in C provide a set of operations for manipulating
strings. Here are explanations and examples for three commonly used string
library functions:
1. `strlen` - String Length
The `strlen` function is used to determine the length of a string, i.e., the number
of characters in the string excluding the null terminator.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}
2. `strcpy` - String Copy
The `strcpy` function is used to copy one string to another.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Copied string: %s\n", destination);
return 0;
}
3. `strcmp` - String Compare
The `strcmp` function is used to compare two strings lexicographically. It returns
an integer that indicates whether the strings are equal, greater, or smaller.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("The first string is smaller than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}
return 0;
}

You might also like