Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
College aantekeningen

Arrays

Beoordeling
-
Verkocht
-
Pagina's
34
Geüpload op
16-01-2025
Geschreven in
2022/2023

A well-crafted document on Arrays, String Handling Functions, Structures, Unions, and Bit Fields in C provides a thorough exploration of these fundamental concepts with in-depth explanations, examples, and practical applications. It begins with arrays, detailing their declaration, initialization, and usage for single-dimensional and multi-dimensional data storage, along with operations like traversal, searching, and sorting. It transitions to string handling functions, explaining built-in functions like strlen(), strcpy(), strcmp(), and strcat(), with use cases and example programs. The document then introduces structures, explaining how to define, initialize, and access structured data types, followed by unions, highlighting their memory-efficient nature and differences compared to structures. A specialized section on bit fields discusses their role in memory optimization, how they enable compact storage of data within structures, and their practical applications in system programming and embedded systems. With illustrative diagrams, annotated code examples, and comparative tables, the document provides clarity on these topics. It concludes with problem-solving exercises, advanced use cases, and debugging tips, serving as a comprehensive guide for both beginners and advanced learners of C programming.

Meer zien Lees minder
Instelling
Vak

Voorbeeld van de inhoud

UNIT III Arrays and Strings

Arrays: An array is defined as collection of logically related data items of the same data type,
stored in contiguous memory locations, sharing a common name.
Each element of the array can be referred by using a value called index (or) subscript in
brackets after the array name. The index must be an integral value or an expression that evaluates
to an integral value.
Depending on the number of subscripts used, arrays can be categorized into one-
dimensional arrays, two-dimensional arrays and so on.
One-dimensional arrays: An array with only one subscript is called as one-dimensional array
(or) 1-d array. It is used to store a list of values, all of which share a common name.

Declaration of one-dimensional arrays: Arrays must be declared before they are used. Array
declaration tells the compiler the name of the array, type of each element, and the size (or)
number of elements in the array. The general form of declaring a one-dimensional array is
data_type array-name[size];
The type specifies any valid data type supported by C such as int, float or char. array-
name is any valid C identifier, and size indicates the maximum number of elements that can be
stored in an array.
Ex: 1.The statement int score[9]; declares score is an array containing maximum of 9 integer
elements.
2. The statement float height[50]; declares height is an array containing maximum of 50 real
elements.
Accessing elements in one-dimensional Arrays: Each element of the array can be referred by
using a value called index (or) subscript in brackets after the array name. The index must be an
integral value or an expression that evaluates to an integral value. The subscript value is indexed
from 0 to size-1.when the subscript value is 0, first element in the array is selected, when the
subscript value is 1, second element is selected and so on.

Storing values in one-dimensional arrays: Declaration and definition only reserve space for
the elements in the array. No values are stored. If we want to store values in the array, we must
initialize the elements, read values from the keyboard (or) assign values to each individual
element.




PSC VVIT, CSE Dept Page 1

,UNIT III Arrays and Strings

Initialization of one-dimensional arrays: We can initialize the elements of the one-dimensional
array when the array is declared. The general form is
data_type array-name[size]= {list of values};
The values in the list are separated by commas.
Ex: The statement int x[6]={10,20,30,40,50,60}; declares that x is an array containing 6 integer
elements whose values are given in the list which are as follows.
10 20 30 40 50 60
x[0] x[1] x[2] x[3] x[4] x[5]
Note: when the array is completely initialized, there is no need to specify the size of the array.
Ex: The statement int x[ ]={3,6,9,10,11,12}; declares that x is an array containing 6 integer
elements whose initial values are given in the list which are as follows. since the number of
values in the list for the array x is six, the size of x is automatically supplied as six.
3 6 9 10 11 12
x[0] x[1] x[2] x[3] x[4] x[5]
Note: If the number of elements in the list is less than the size of the array, then only that many
elements will be initialized. The remaining elements will be set to zero automatically.
Ex: The statement int x[6]={3,6,9,10}; will initialize the first four elements to 3,6,9 and 10 and
the remaining elements two elements are set to zero.
3 6 9 10 0 0
x[0] x[1] x[2] x[3] x[4] x[5]

Inputting Values from the Keyboard: We can store the values in to the array from the
keyboard. This can be done using a loop in which the index can be varied as shown below. For
example, the statement.
for(i=0;i<6;i++)
{
scanf(“%d”,&x[i]);
}
Stores 6 values in to the array integer array x[ ]. Here index i varies from 0 to 5.




PSC VVIT, CSE Dept Page 2

,UNIT III Arrays and Strings

Ex program:
/*program to read 10 values into one dimensional array using keyboard and printing them*/
#include<stdio.h>
int main( )
{
int i,x[10];
printf(“enter the elements of the array\n”);
for(i=0;i<10;i++)
scanf(“%d”,&x[i]);
printf(“the elements of the array are \n”);
for(i=0;i<10;i++)
printf(“%d\t”,x[i]);
return 0;
}
Assigning Values: Values to individual elements can be assigned using the assignment operator.
For example, simple assignment statement x[0]=100; assigns 100 to x[0].
Similarly the statement number[0]=35; assigns 35 to number[0].
Similarly we can write number[2]=number[0]+number[1];
Note: one array cannot be copied to another using assignment statement.
/*program to find maximum and minimum element from the list of elements*/
#include<stdio.h>
int main( )
{
int i,n,max,min,x[50];
printf(“enter the total number of elements of the array\n”)
scanf(“%d”,&n);
printf(“enter the elements of the array\n”);
for(i=0;i<n;i++)
scanf(“%d”,&x[i]);
printf(“the elements of the array are \n”);
for(i=0;i<n;i++)
printf(“%d\t”,x[i]);
max=min=x[0];
for(i=0;i<n;i++)
{
if(max<x[i])
max=x[i];
if(min>x[i])
min=x[i];
}
printf(“maximum element =%d\n”,max);
printf(“minimum element =%d\n”,min);
return 0;
}

PSC VVIT, CSE Dept Page 3

, UNIT III Arrays and Strings

Two Dimensional Arrays: An array with two subscripts is called as two dimensional array. A
two dimensional array can be thought of as a group of one or more one-dimensional arrays all of
which share a common name and are separable by subscript values.
Declaration of Two-dimensional arrays: Arrays must be declared before they are used. Array
declaration tells the compiler the name of the array, type of each element, and the size (or)
number of elements in the array. The general form of declaring a two-dimensional array is
data_type array-name[rowsize][colsize];
The type specifies any valid data type supported by C such as int, float or char, array-
name is any valid C identifier. rowsize indicates the number of rows and colsize indicates the
number of elements in each row.
Ex: 1.The statement int marks[3][3]; declares marks is a two dimensional array containing 3
rows and each row contains maximum of 3 elements.
Accessing elements in Two-dimensional Arrays: Each element of the two dimensional array
can be referred by using two indexes (or) subscripts in brackets after the array name. The first
index and second index must be an integral value or an expression that evaluates to an integral
value. The first subscript value is indexed from 0 to rowsize-1. The second subscript value is
indexed from 0 to colsize-1.
Memory gets allocated to store the array marks as follows.
Column numbers
0 1 2
0 Marks[0][0] Marks[0][1] Marks[0][2]
numbers




1 Marks[1][0] Marks[1][1] Marks[1][2]
Row




2 Marks[2][0] Marks[2][1] Marks[2][2]

Storing values in two-dimensional arrays: Declaration and definition only reserve space for
the elements in the array. No values are stored. If we want to store values in the array, we must
initialize the elements, read values from the keyboard (or) assign values to each individual
element.
Initialization of two-dimensional arrays: We can initialize the elements of the two-
dimensional array when the array is declared. The general form is
data_type array-name[rowsize][colsize]= {list of values};
The values in the list are separated by commas.


PSC VVIT, CSE Dept Page 4

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
16 januari 2025
Aantal pagina's
34
Geschreven in
2022/2023
Type
College aantekeningen
Docent(en)
Vijaya anandaratnam
Bevat
Alle colleges

Onderwerpen

$8.49
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
srilathalakshmisetty04

Ook beschikbaar in voordeelbundel

Maak kennis met de verkoper

Seller avatar
srilathalakshmisetty04 Vasireddy Venkatadri Institute of Technology
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
1 jaar
Aantal volgers
0
Documenten
5
Laatst verkocht
-

0.0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen