0% found this document useful (0 votes)
11 views23 pages

C-programming (1) (1)

The document explains key concepts in C programming, including Pass by Value and Pass by Reference, array declaration and initialization, storage classes, string manipulation, and structures. It provides code examples for each concept, demonstrating how to declare and manipulate arrays, perform matrix multiplication, and use string functions. Additionally, it outlines the characteristics of different storage classes and provides a complete program for structure initialization and display.
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)
11 views23 pages

C-programming (1) (1)

The document explains key concepts in C programming, including Pass by Value and Pass by Reference, array declaration and initialization, storage classes, string manipulation, and structures. It provides code examples for each concept, demonstrating how to declare and manipulate arrays, perform matrix multiplication, and use string functions. Additionally, it outlines the characteristics of different storage classes and provides a complete program for structure initialization and display.
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/ 23

🔄 Pass by Value vs Pass by Reference in C

✅ Pass by Value
●​ A copy of the actual value is passed to the function.​

●​ Changes made inside the function do NOT affect the original variable.​

●​ Useful for computations without altering the original data.​

🔹 Example:
c
CopyEdit
#include <stdio.h>

void changeValue(int x) {
x = 100;
}

int main() {
int a = 50;
changeValue(a);
printf("Value of a after function call: %d", a);
return 0;
}

🖨️ Output:​
Value of a after function call: 50

✅ Pass by Reference
●​ The address of the variable is passed.​

●​ Changes made inside the function directly affect the original variable.​

●​ Ideal when the original data needs to be modified or preserved efficiently.​

🔹 Example:
c
CopyEdit
#include<stdio.h>

void sum(int *a, int *b, int *t) {


*t = *a + *b;
}

int main() {
int num1, num2, total;
printf("\n Enter the first number:");
scanf("%d", &num1);
printf("\n Enter the second number:");
scanf("%d", &num2);
sum(&num1, &num2, &total);
printf("\n Total = %d", total);
return 0;
}

🖨️ Output:
mathematica
CopyEdit
Enter the first number: 2
Enter the second number: 3
Total = 5

2.What is an array? How the single dimensional array is declared & initialised. Explain with
syntax and example

array:An array is the data structure containing a number of data values(All of which
having the same datatype)

✅ Declaration of Single-Dimensional Array


You declare a single-dimensional array like this:

🔹 Syntax:
CopyEdit
data_type array_name[size];

●​ data_type → Type of elements (e.g., int, float, char)​

●​ array_name → Name of the array​

●​ size → Number of elements​

🔹 Example:
CopyEdit
int marks[5];

This creates an integer array named marks that can store 5 integers.

✅ Initialization of Array
🔹 Method 1: Static Initialization at Declaration
int marks[5] = {90, 85, 75, 80, 95};

🔹 Method 2: Partial Initialization

int marks[5] = {90, 85}; // Remaining elements initialized


to 0

🔹 Method 3: Without Size (Compiler Counts Elements)

int marks[] = {90, 85, 75, 80, 95}; // Size = 5

✅ Accessing Array Elements


Use index values starting from 0:

marks[0] = 90; // First element

printf("%d", marks[2]); // Prints third element

✅ Complete Example:
#include <stdio.h>

int main() {
int numbers[3] = {10, 20, 30}; // Declare and initialize array
printf("First number: %d\n", numbers[0]);
printf("Second number: %d\n", numbers[1]);
printf("Third number: %d\n", numbers[2]);

return 0;
}

o/p:
First number: 10
Second number: 20
Third number: 30

​ ​ ​ ​ ​

3.What is an array? How a two dimensional array is declared & initialised? Explain the
applications of array.

array:An array is the data structure containing a number of data values(All of which
having the same datatype)

✅ Declaration and Initialization of Two-Dimensional Array


🔹 Declaration Syntax
data_type array_name[row_size][column_size];

🔹 Example
int marks[2][3] = {
{85, 90, 80},
{78, 88, 92}
};
This creates a 2-row, 3-column matrix to store integer values.
char names[20][30];
Which means 20 student names, each of up to 30 characters.
✅ Applications of Arrays (from Module-04 PDF)
Arrays are used in various ways as shown in your PDF:

1. Arrays of Strings

Used to store names of multiple students:

c
CopyEdit
char names[5][10];

2. String Operations

Arrays are used to:

●​ Read and print names of students​

●​ Perform operations like:​

○​ Length finding​

○​ Uppercase conversion​

○​ Concatenation​

○​ Comparison​

○​ Reversal​

○​ Insertion​

3. Data Handling

Arrays help in organizing and processing data such as:

●​ Student marks​
●​ Names​

●​ Any set of similar items

ex:

char names[5][10];
int i, n;
printf("\n Enter the number of students:");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\n Enter the name of student %d:", i+1);
gets(names[i]);
}

4.Whith a C program, to multiply 2-Davoray matrix by validating the tools roots

#include <stdio.h>

int main() {
int A[10][10], B[10][10], C[10][10];
int i, j, k, r1, c1, r2, c2;

// Input sizes
printf("Enter rows and columns of Matrix A: ");
scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of Matrix B: ");


scanf("%d %d", &r2, &c2);

// Validate matrix multiplication condition


if(c1 != r2) {
printf("Matrix multiplication not possible. Columns of A must equal rows of B.\n");
return 0;
}
// Input Matrix A
printf("Enter elements of Matrix A:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &A[i][j]);
}
}

// Input Matrix B
printf("Enter elements of Matrix B:\n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &B[i][j]);
}
}

// Multiply matrices
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
C[i][j] = 0;
for(k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

// Output result
printf("Resultant Matrix (A x B):\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}

return 0;
}
5.Define storage classes in C and compare the key features of all the storage
classes.

✅ Definition of Storage Classes in C


In C programming, storage classes determine how a variable behaves in terms of:

●​ Scope – where the variable can be accessed (within function, file, or globally)​

●​ Lifetime – how long the variable exists in memory​

●​ Default Initial Value – what value the variable gets if not explicitly initialized​

●​ Memory Location – where the variable is stored (RAM or CPU register)

✅ Types of Storage Classes in C


There are four types of storage classes:

1.​ auto​

2.​ register​

3.​ static​

4.​ extern
void fun()
{
auto int a = 10; // Auto variable
register int b = 5; // Stored in register
static int c = 0; // Retains value between calls
extern int d; // Refers to a global variable defined elsewhere
}

6.Define a string, explain how strings are declared and initialised with suitable examples.

✅ Definition of String in C
In C, a string is a sequence of characters terminated by a null character \0.​
A string is stored in a character array.

char name[10] = "Ravi";

'R' 'a' 'v' 'i' '\0'


✅ Declaration and Initialization of Strings
🔹 1. Declaration Without Initialization
char name[20];

Creates a character array that can store a string of up to 19 characters + \0.

2. Declaration With Initialization (Direct Method)

char name[] = "Ravi";

●​ Compiler automatically adds \0 at the end.​

●​ No need to mention size — compiler calculates it.​

3. Character-by-Character Initialization

char name[5] = {'R', 'a', 'v', 'i', '\0'};

4. Input from User

char name[20];
scanf("%s", name);

✅ Examples
🔸 Example 1: String Initialization
#include <stdio.h>

int main() {
char name[10] = "Kiran";
printf("Name: %s", name);
return 0;
}
🔹 Output:
makefile
CopyEdit
Name: Kiran

🔸 Example 2: Reading String from User


#include <stdio.h>

int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name); // accepts single word
printf("Hello, %s", name);
return 0;
}

7.List and explain any three string manipulation functions with syntax and
examples.

✅ String Manipulation Functions in C


In C, string manipulation is done using functions from the <string.h> library.

Here are three commonly used string functions:

🔹 1. strlen() – String Length


✅ Use:​
Returns the length of the string, excluding the null character \0.

✅ Syntax:
int strlen(char str[]);
2.strrev() – Reverse a String

🔹 Purpose:
The strrev() function is used to reverse a string in C.

🔹 Syntax:
strrev(string);

●​ string → The string you want to reverse.​

●​ After execution, the original string is reversed in place.


🔹 3.. strlwr() – String to Lowercase
✅ Use:​
Converts all uppercase letters in a string to lowercase.

✅ Syntax:
char* strlwr(char str[]);

4. strupr() – Convert String to Uppercase

✅ Use:​
Converts all lowercase letters in a string to uppercase.
✅ Syntax:
char* strupr(char str[]);

5. strcat() – Concatenate Two Strings

✅ Use:​
Joins (adds) the second string to the end of the first string.

✅ Syntax:
char* strcat(char str1[], char str2[]);
6. strcmp() – Compare Two Strings

✅ Use:​
Compares two strings character by character.

✅ Returns:
●​ 0 → if both strings are equal​

●​ < 0 → if first string is less than second​

●​ > 0 → if first string is greater than second​

✅ Syntax:
int strcmp(char str1[], char str2[]);
🔹 7. strcpy() – Copy String
✅ Use:​
Copies the content of one string into another.

✅ Syntax:
c

CopyEdit

strcpy(destination, source);
8.define a structure how structure is declared and intialized?Explain witrh
suitable example
✅ Declaration of Structure
🔹 Syntax:
struct StructureName

data_type member1;

data_type member2;

. . .

};
🔹 Example: Declaring a structure

struct Student {

int rollNo;

char name[20];

float marks;

};

This creates a structure named Student with:

●​ An integer rollNo​

●​ A string name​

●​ A float marks​

✅ Initialization of Structure
🔹 Declaring a variable of structure:

struct Student s1;

🔹 Initializing values:

strcpy(s1.name, "Ravi");

s1.rollNo = 101;

s1.marks = 89.5;
**Complete program

#include <stdio.h>

#include <string.h>

struct Student {

int rollNo;

char name[20];

float marks;

};

int main() {

struct Student s1;

// Initializing structure members

s1.rollNo = 101;

strcpy(s1.name, "Ravi");

s1.marks = 89.5;

// Displaying the values

printf("Roll No: %d\n", s1.rollNo);

printf("Name : %s\n", s1.name);

printf("Marks : %.2f\n", s1.marks);

return 0;

}

You might also like