0% found this document useful (0 votes)
3 views

docs

The document explains key programming concepts in C, including arrays, structures, return types of functions, and pointers. It provides sample code for each concept, demonstrating how to define and use arrays and structures, the purpose of different return types, and the utility of pointers for variable handling and dynamic memory allocation. Overall, it serves as a concise guide to understanding these fundamental programming elements.

Uploaded by

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

docs

The document explains key programming concepts in C, including arrays, structures, return types of functions, and pointers. It provides sample code for each concept, demonstrating how to define and use arrays and structures, the purpose of different return types, and the utility of pointers for variable handling and dynamic memory allocation. Overall, it serves as a concise guide to understanding these fundamental programming elements.

Uploaded by

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

An array is a data structure in programming that allows you to store multiple values of the same

data type in a single variable.

Program

#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum: %d\n", sum);
return 0;
}

Output

Sum: 15

Simple Explanation:
1. Array Definition:
• We define an array arr[] containing 5 numbers: {1, 2, 3, 4, 5}.
2. Sum Calculation:
• We start with a sum of 0 (int sum = 0;).
• Then, we use a for loop to go through each number in the array. For each number,
we add it to the sum.
3. Printing the Result:
• Once the loop is done, the sum (which is now 15) is printed out using printf.

What Happens:
• The program adds all the numbers in the array (1 + 2 + 3 + 4 + 5 = 15), and then
prints "Sum: 15" on the screen.
Question 2

A structure in C is a user-defined data type that allows you to group different types of variables
under one name.

#include <stdio.h>

struct Book {
char title[100];
char author[100];
float price;
int year;
};

int main() {
struct Book book1 = {"C Programming", "Dennis Ritchie", 29.99, 1978};
printf("Title: %s\n", book1.title);
printf("Author: %s\n", book1.author);
printf("Price: %.2f\n", book1.price);
printf("Year: %d\n", book1.year);
return 0;
}

Output
Title: C Programming
Author: Dennis Ritchie
Price: 29.99
Year: 1978

Explanation:
1. Defining the Structure:
• struct Book defines a structure named Book that has 4 members:
• title: A string for the book's title.
• author: A string for the book's author.
• price: A floating-point number for the book's price.
• year: An integer for the year of publication.
2. Declaring a Structure Variable:
• struct Book book1; declares a variable book1 of type Book.
• We can assign values to the members directly when initializing, as shown in the
book1 initialization.
3. Accessing Structure Members:
• We access the individual members of the structure using the dot (.) operator, like
book1.title, book1.author, etc.
Question 3

In C, functions can return different types of values. The return type of a function indicates what
kind of value the function will give back after it finishes. Here's a simple explanation of the
common return types:
1. void:

• Means the function doesn't return anything.


• Used when the function only performs actions (like printing something) but doesn't
need to give back a value.
2. int:

• Returns an integer (whole number).


• Used when the function is supposed to return a number.
3. float:

• Returns a floating-point number (decimal number).


• Used when the function needs to return a number with decimals.
4. double:

• Returns a double precision floating-point number (more precise decimal number).


• Used for more accurate decimal values than float.
5. char:

• Returns a single character (like 'a', 'b', etc.).


• Used when the function is expected to return a character.
Question 4

A pointer in C is a variable that stores the memory address of another variable. Instead of holding
a direct value like an integer or a character, a pointer holds the address of where that value is stored
in memory.

How Pointers Can Be Used for Variable Handling:


1. Passing Variables to Functions:
• Without pointers: When a variable is passed to a function, only a copy of the value
is passed.
• With pointers: A pointer can pass the memory address of a variable, allowing the
function to modify the original variable.
2. Dynamic Memory Allocation: Pointers are used to allocate memory dynamically during the
program's execution using functions like malloc() or calloc(). This is helpful when the exact
amount of memory required is unknown at compile time.
3. Array Handling: Pointers can be used to work with arrays. In fact, the name of an array is a
pointer to its first element, and array elements can be accessed using pointer arithmetic.

Pointers allow you to work with memory addresses, which is powerful for passing large data to
functions, manipulating data directly, and allocating memory dynamically.
• They are essential for efficient memory management and help in managing resources like
arrays, structures, and dynamic memory.

You might also like