C program assignment
C program assignment
ROLL_NUMBER
PROGRAM MASTER_OF_COMPUTER_APPLICATIONS (MCA)
SEMESTER I
COURSE_CODE_&_NAM DCA6110, PROGRAMMING & PROBLEM-SOLVING
E USING C
(DCA6110_AUG_2024 MCA_Sem1)
SET - I
Question1.Describe the basic structure of a C program. Explain scanf() function with an
example.
1. Directives for Preprocessors These lines, which start with #, are used to specify constants
or include libraries.
2. Function Delineations Every C program requires at least one function, generally called
main(), which serves as the entry point for program prosecution.
3. Variable Affirmations Before variables can be utilized in the program, they must be
declared., specifying their types( e.g., int, pier, housekeeper).
4. Statements and Expressions These are the factual instructions that the program executes,
including computations and control statements.
5. Statement of Return A return statement that returns an integer value—often 0—signaling
successful prosecution typically follows the main() function.
```int main(){
printf("Hello, World!");//Hello,World!
return 0; }```
Here's how it operates: A format string that indicates the expected input format is the first
argument. The variables where the input data will be saved are indicated by the posterior
arguments.
illustration of scanf()
```int main(){
int age;//age
printf("Enter your age");
scanf("%d",&age);//enter_age
printf("You're %d times old.",age);
return 0;}```
In this example, the stoner is prompted to enter their age by the application. After reading
the input as an integer, the scanf() function stores it in the variable age. It eventually brings
the stoner back to life.
Que2. Decision control statements in C: what are they? Describe the many kinds of C
programming decision control statements.
1. if Statement
The if statement runs a block of code if the given condition is true; else, the block is
skipped.
Syntax
If ( condition){
/ code then if the condition is true
illustration
if( age> = 18){
printf(" Eligible to bounce.");}
differently{
/ code if the condition is false
differently{
/ additional code then
4. Nested if Statements
An if statement can be placed inside another if or else to check multiple conditions
crescively.
Syntax
if( condition1){
if( condition2){
/ code for both conditions true
5. switch Statement
The switch statement simplifies the execution of one out of multitudinous blocks of code
predicated on a variable's value. It’s necessary to multiple if- else statements for separate
values.
Syntax
Switch ( expression ){
case value1
// code for value1
Break ;
case value2
// code for value2
Break ;
dereliction
// additional code then
Question 3: What function do C storage classes serve? Talk about the various kinds of
storage classes that C offers.
Answer:-
Storage classes in C programming control a variable's scope, lifetime, visibility, and
default value. They specify the management, access, and storage of variables while a
program is running. Program modularity and efficient memory management depend on an
understanding of storage classes.
Que4. What distinguishes call by reference in C from call by value? Give a relevant
example to illustrate the concept of recursion..
Answer:-
The distinction between C's Call by Reference and Call by Value
Call by Value
This method involves passing the function a fake version of the factual variable.
Changes made to the function's parameter have no effect on the original variable.
Because the original data is unaltered, this system is secure.
Example
o ```void modify( int x){
o x = 20;// Only the dupe is modified
o int main(){
o int a = 10;
o modify( a);
o printf("%d", a);// Affair 10( unchanged)
o return 0;}```
Call by Reference
In this approach, the address of the factual variable is passed to the function.
Changes made to the parameter directly modify the original variable.
This system is effective for handling large data structures as no data copying occurs.
Example
o ```void modify( int * x){
o x = 20;// Original variable is modified
o int main(){
o int a = 10;
o modify( &a);
o printf("%d", a);// Affair 20( modified)
o return 0;}```
Recursion in C
Recursion refers to a function calling itself to break lower cases of a problem. Every
recursive function requires
1. A base case to terminate the recursion.
2. A recursive step reduces the problem’s size.
How It Works
1. The base case( n == 0) ensures the recursion stops.
2. For factorial( 5), the function calculates
5 * factorial ( 4)
4 * factorial ( 3)
3 * factorial ( 2)
2 * factorial ( 1)
1 * factorial ( 0)( returns 1 and ends recursion)
3. These results are multiplied during backtracking, performing in 120.
Answer .:-
A indicator is a variable in C that stores the mind address of another variable. Hands are
important tools for handling mind directly and are essential for tasks like dynamic mind
allocation, working out with arrays, and manipulating data structures. They also enable
effective parameter passing in places.
Example
o ```int main(){
o int a = 10;
o int * ptr = & a;// indicator stores the address of' a'
indicator computation
Example
o ```int main(){
o int arr() = { 10, 20, 30, 40};
o int * ptr = arr;// indicator to the first component of the batch
o printf("original value %d ", * ptr);// Affair 10
o ptr;// shift to the coming component
o printf(" After proliferation %d", * ptr);// Affair 20
o ptr = 2;// shift two rudiments ahead
o printf(" After extension %d", * ptr);// Affair 40
o ptr--;// shift to the former component
o printf(" After diminishment %d", * ptr);// Affair 30
o return 0; }```
elucidation
1. indicator computation is performed in way determined by the size of the data type. For
case, incrementing a indicator to an int( 4 bytes) moves the address forth by 4 bytes.
2. This point is especially useful for covering arrays or repeating over mind blocks in an
effective manner.
Q.6.a) In C, what distinguishes union from structure?
Structure
A structure allows grouping variables of nonidentical manners, each having its own mind
position.
Example
o ```struct Pupil{
o int id;
o float marks;
o };
o int main(){
o struct Pupil s= { 1, 85.5};
o printf(" ID %d", s.id);
o printf("Marks %2f ", s.marks);
o return 0; }```
elucidation
In the below illustration, id and jokes are stored in separate mind locales, allowing
independent access.
Union
A union also groups variables of nonidentical manners, but all ingredients partake the
same mind position. Only one member can store a value at a time.
Example
o ```union Data{
o int i;
o float f; };
o int main(){
o union Data d;
o d.i= 10;// Assign value to' i'
o printf(" Integer %d ", d.i);
elucidation
Then, d.i and d.f partake the same mind. When f is assigned, the value in i is overwritten.
Answer:-
Places in C for Dynamic Mind Allocation
For data structures whose size is unknown at collect time, dynamic mind allocation in C
gives programs the option to demand mind during runtime. There are multiple locations for
dynamic mind allocation and deallocation in the C Standard Library.
Then are the main places exercised for dynamic mind allocation in C
1. malloc () ( Mind Allocation )
Example
o int * ptr = ( int *) malloc( 5 * sizeof( int));// distributing mind for 5 integers
o if( ptr == NULL){
o printf(" Mind allocation failed.");
2. calloc () ( conterminous Allocation )
Parameters
num_elements Number of rudiments to allow.
element_size Size of each component in bytes.
Return Value
responses a indicator to the distributed mind.
responses NULL if the allocation fails.
operation
o calloc() initializes the mind to zero, which can be useful when you need
initialized mind.
Example
o int * ptr = ( int *) calloc( 5, sizeof( int));// distributing mind for 5 integers and
initializing to 0
o if( ptr == NULL){
o printf(" Mind allocation failed.");
3. realloc () ( Redistribution)
Parameters
ptr indicator to the preliminarily distributed mind block.
new_size The new size, in bytes, to which the mind block should be resized.
Return Value
responses a indicator to the recently distributed mind.
If the redistribution fails, it returns NULL, and the initial mind block remains
unchanged.
operation
o realloc() is exercised to expand or reduce the size of an being mind
block. It may remove the mind block to a new position if necessary.
illustration
o int * ptr = ( int *) malloc( 5 * sizeof( int));// original allocation for 5 integers
o ptr = ( int *) realloc( ptr, 10 * sizeof( int));// Resize to 10 integers
o if( ptr == NULL){
o printf(" Redistribution failed.");
4. free() ( Deallocation)
Example
Summary of places