Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
493 views

Programming in C Assignment

The document contains a student's assignment responses for a Programming in C course. It includes: 1) A program to calculate the area of a circle that defines PI as a constant and uses it to calculate the area formula. 2) An explanation of control statements in C including conditional (if-else, switch), loop (while, do-while, for), and jump (break, continue, goto) statements. 3) A description of storage classes in C (automatic, static, register, extern) and how they define the lifespan and scope of variables and functions. 4) Definitions and examples of declaring and initializing one-dimensional and two-dimensional arrays in C. 5

Uploaded by

Demon God
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
493 views

Programming in C Assignment

The document contains a student's assignment responses for a Programming in C course. It includes: 1) A program to calculate the area of a circle that defines PI as a constant and uses it to calculate the area formula. 2) An explanation of control statements in C including conditional (if-else, switch), loop (while, do-while, for), and jump (break, continue, goto) statements. 3) A description of storage classes in C (automatic, static, register, extern) and how they define the lifespan and scope of variables and functions. 4) Definitions and examples of declaring and initializing one-dimensional and two-dimensional arrays in C. 5

Uploaded by

Demon God
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name Khatri Mohammed Zaid Jubair

Roll Number 2314100800

Program Bachelor of Computer Applications BCA

Semester 1st

Course Name Programming in C

Code DCA1102

Session September 2023


Assignment Set –1

1) Explain basic structure of C program. Write a program to calculate the area of a circle.
Answer:

A C program's basic structure consists of various elements, which include:

1) Pre-processor Directives: These are commands that begin with a # symbol and are executed
prior to the compilation of the programme. They include header files, define constants and
macros, and carry many additional pre-processing duties.

2) Global Declarations: Variables and functions declared outside of any function and accessible
throughout the programme are referred to as global declarations.

3) Main Function: This is the program's entrance point and where the programme execution
begins. It includes the programme logic as well as any function calls.

4) Functions: These are blocks of code that perform specific tasks and can be called from other
parts of the program.

Program to calculate area of circle is

#include <stdio.h>

#define PI 3.14159 // Define PI as a constant

int main()
{
float radius, area; // Declare variables

printf("Enter the radius of the circle: ");

scanf("%f", &radius); // Read radius from user input

area = PI * radius * radius; // Calculate area

printf("The area of the circle is %f", area); // Display the result

return 0;
}

2) Describe control statements in C. Explain various control statements in C


programming language.
Answer:
Control statements in the C programming language are used to direct the flow of a program's
execution. They allow the programmer to describe the sequence in which the program should run
and the conditions under which particular portions of the program should run or be skipped.

In the C programming language, there are three sorts of control statements:

1) Conditional statements: If a specific condition is met, these statements will cause a block of
code to run. If-else statements and switch statements are the two categories of conditional
statements in C.

If-else statements enable programmes to run one piece of code when a condition is true and a
different chunk of code when the condition is false.

The syntax of an if-else expression is as follows:

if (condition) {
// code to execute if the condition is true
}
else {
// code to execute if the condition is false
}

Switch Statement: The Switch statement is used to select one of many code blocks to execute. A
switch statement evaluates an expression and the value of the expression is compared with
different cases. If a match is found, the corresponding code block is executed.
The syntax for the switch statement is:

switch(number) {

case C1: //do something

break;

case C2 : //do something

break;

default : //do something

2) Loop Statements: Statements known as "loops" are used to repeatedly run a block of code as
long as a specific condition is met. While loops, do-while loops, and for loops are the three
different forms of loop statements available in C.

while loops: while loops execute a block of code as long as the condition is true. Here is the
syntax of a while loop:

while (condition) {
// code to execute as long as the condition is true

do-while loops: do-while loops execute a block of code at least once, and then repeatedly as long
as the condition is true. Here is the syntax of a do-while loop:

do {
// code to execute at least once
} while (condition);

for loops: for loops execute a block of code a specified number of times. Here is the syntax of a
for loop:

for (initialization; condition; update) {


// code to execute as long as the condition is true
}

3) Jump Statements: You can move control between areas of the programme using these
statements. Break, continue, and goto are the three different sorts of jump statements
available in C.

break: To end a loop or switch statement, use the break statement. The loop or switch statement
is instantly ended when a break statement is met, and programme execution moves on to the next
statement outside the loop or switch statement.

Continue: Continue statements are used to go on to the next iteration of a loop while skipping the
current one.

goto: To move the focus to a labelled statement inside the same function, use a goto statement.
However, goto statements are often avoided since they may make code challenging to read and
comprehend.

3) Discuss the purpose of storage class in C. Explain various types of storage class in C.
Answer:
1) Storage classes are used in the C programming language to define the lifespan, visibility, and
scope of variables and functions. Storage classes' primary job is to regulate how much memory is
allocated and released for variables and functions over the course of a programme.

In the C programming language, there are four distinct storage class types:

Automatic Storage Class: All local variables declared inside a function belong to the automatic
storage class by default. after a function is called, memory is automatically allocated to variables
of the automated storage class on the stack, and the memory is automatically released after the
function completes. Additionally, the automatic storage class can be specified directly using the
phrase "auto".

void foo() {
auto int x; // Same as declaring "int x;"
}

2) Static Storage Class: The static storage class is used to declare variables with a lifetime that
extends beyond the scope of the function in which they are declared. Static variables are
allocated memory in the data segment of the program, and their values persist across function
calls. The keyword "static" is used to explicitly specify static storage class.

void foo() {
static int x; // Static variable with initial value 0
x++; // Value of x persists across function calls
}

3) Register Storage Class: The Register Storage Class is used to tell the compiler that a certain
variable should be saved in a register rather than memory. Register variables are faster to access
than memory variables, but the compiler may reject the proposal if the variable cannot be put in a
register. The term "register" is used to indicate the register storage class directly.

void foo() {
register int x; // Suggests that x be stored in a register
}

4) Extern Storage Class: The extern storage class is where variables and functions defined in
other files are declared. External variables and functions are visible to the entire programme and
may be accessed by any function. The real memory for external variables and functions is kept in
a separate file. The term "extern" is used to indicate the extern storage class directly.

extern int x; // Declaration of an extern variable "x"

These are the various types of storage classes in C programming language. Understanding
storage classes is important for writing efficient and well-structured C programs.

4) Define an array. Explain the declaration and initialization of one dimensional and
two-dimensional array with an example.
Answer:

An array is a collection of pieces of the same data type that are stored in contiguous memory
regions and accessed using a single variable name and an index or subscript. Arrays may store
and modify many values of the same kind.

Arrays in the C programming language can be defined and initialised in the following ways:
1) One-Dimensional Array Declaration and Initialization: A one-dimensional array is defined by
stating the data type of its components, followed by the array name, and then the array size
wrapped in square brackets [].

int arr[5]; // Declaration of an integer array of size 5

The above statement declares an integer array called "arr" with a size of 5. The array items'
indexes or subscripts vary from 0 to 4. At the time of declaration, the array elements can be
initialised by enclosing the initial values in curly brackets and separating them with commas.

int arr[5] = {1, 2, 3, 4, 5}; // Declaration and initialization of an integer array of size 5

The above statement declares and initializes an integer array named "arr" of size 5 with the initial
values 1, 2, 3, 4, and 5.

2) Two-Dimensional Array Declaration and Initialization: A two-dimensional array is defined by


stating the data type of its components, followed by the array's name, and then the number of
rows and columns contained in square brackets [][].

int arr[3][4]; // Declares a two-dimensional integer array with three rows and four columns.

The above sentence declares an integer array called "arr" with three rows and four columns. The
array's elements may be accessed using two indices or subscripts, one for the row and one for the
column. At the time of declaration, the array's elements can be initialised by enclosing the initial
values in curly brackets and separating them with commas, with each row separated by a comma.

int arr[3][4] = {
{1, 2, 3, 4},

{5, 6, 7, 8},
{9, 10, 11, 12}
}; // Declaration and initialization of a two-dimensional integer array of 3 rows and 4 columns

The above sentence defines and initialises the "arr" two-dimensional integer array, which has
three rows and four columns. The first row's starting values are 1, 2, 3, 4, the second row's initial
values are 5, 6, 7, 8, and the third row's initial values are 9, 10, 11, 12.

These are the ways to declare and initialize one-dimensional and two-dimensional arrays in C
programming language. Arrays are a fundamental data structure in programming and are used
extensively in various applications.

5) a.) Define pointer. Explain the declaration and initialization of a pointer variable.
Answer:
A pointer is a variable in the C programming language that stores the memory address of another
variable. Pointers are used to manage memory and to generate complicated data structures like as
linked lists, trees, and graphs.

Declaration and initialization of a pointer variable:

A pointer variable is declared by stating the data type of the variable to which it points, followed
by an asterisk (*) and the variable's name.

int *ptr; // Declaration of an integer pointer variable

The above sentence declares a "ptr" pointer variable that can point to an integer variable. The
pointer variable does not hold the integer variable's value, but rather its memory address.

Initialization of a pointer variable can be done in two ways:

Null initialization: When a pointer is initialised with a null value, it does not point to any valid
memory address. The null value is represented by the constant keyword "NULL" or by the value
0.

int *ptr = NULL; // Initialization of an integer pointer variable with null value

The above statement sets the value of a pointer variable called "ptr" to null. The variable pointer
does not point to any valid memory address.

Initialization with a variable's address: The address-of operator "&" can be used to initialise a
pointer with the memory address of a variable.

int num = 10; // Declaration of an integer variable


int *ptr = &num; // Initialization of an integer pointer variable with the address of "num"

The above statement initializes a pointer variable named "ptr" with the address of an integer
variable named "num". The pointer variable "ptr" now points to the memory location of "num".

b) Write a C program to illustrate the use of indirection operator to access the value
pointed by a pointer.
Answer:

#include <stdio.h>

int main() {
int num = 10;

int *ptr = &num;


printf("The value of num is %d\n", num);
printf("The value of *ptr is %d\n", *ptr);
printf("The address of num is %p\n", &num);
printf("The value of ptr is %p\n", ptr);
printf("The value pointed to by ptr is %d\n", *ptr);

return 0;
}

We begin our program by declaring an integer variable num and assigning it the value 10. Then
we declare an integer pointer ptr and initialise it with num's address.

The indirection operator (*) is then used to access the value referred to by ptr and print it to the
console. We also output num's address, ptr's value, and the value referred to by ptr.

he result from running this programme ought to like the following:

The value of num is 10


The value of *ptr is 10
The address of num is 0x7ffee259b7d4
The value of ptr is 0x7ffee259b7d4
The value pointed to by ptr is 10

As we can see, num and the value referred to by ptr both have values of 10, and num's address
and the value of ptr's address are both locations in memory.

6) a) Define Structure and write the general syntax for declaring and accessing structure
members.
Answer:
A structure is a user-defined data type in the C programming language that enables you to
combine together variables of various data kinds under a single name. The "struct" keyword is
used to define a structure, which is then followed by the name of the structure tag and a list of
member variables surrounded in braces.

The basic syntax for defining and accessing structure members is as follows:

// Define the structure


struct structure_tag_name {
data_type member_name_1;
data_type member_name_2;
// ...
data_type member_name_n;
};

// Declare a variable of the structure type


struct structure_tag_name variable_name;

// Access a member of the structure using the dot operator


variable_name.member_name_1 = value;

b) Write a C program to show employee details using structure.


Answer:

#include <stdio.h>
#include <string.h>

struct Employee {
char name[50];
int age;
float salary;
};

int main() {
struct Employee emp;

// Assign values to the structure members


strcpy(emp.name, "Zaid");
emp.age = 35;
emp.salary = 35000.00;

// Display the employee details


printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);

return 0;
}

When we run this program, the output should look something like this:

Name: Zaid
Age: 35
Salary: 35000.00

You might also like