Programming in C Assignment
Programming in C Assignment
Semester 1st
Code DCA1102
1) Explain basic structure of C program. Write a program to calculate the area of a circle.
Answer:
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.
#include <stdio.h>
int main()
{
float radius, area; // Declare variables
return 0;
}
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.
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) {
break;
break;
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:
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.
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 [].
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.
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.
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.
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.
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.
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;
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.
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:
#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp;
return 0;
}
When we run this program, the output should look something like this:
Name: Zaid
Age: 35
Salary: 35000.00