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

Decimal adder encoder decoder multiplexers and demultiplexer

The document provides a comprehensive overview of arrays and strings in C programming, detailing their definitions, characteristics, types, and memory management. It explains the need for arrays, how to declare and initialize them, and the differences between static and dynamic arrays. Additionally, it covers the concept of strings as character arrays, including initialization and printing methods.

Uploaded by

plogeswaran347
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)
8 views

Decimal adder encoder decoder multiplexers and demultiplexer

The document provides a comprehensive overview of arrays and strings in C programming, detailing their definitions, characteristics, types, and memory management. It explains the need for arrays, how to declare and initialize them, and the differences between static and dynamic arrays. Additionally, it covers the concept of strings as character arrays, including initialization and printing methods.

Uploaded by

plogeswaran347
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/ 48

ARRAYS AND

STRINGS IN C
Kiranpal Singh Virk
Assistant Professor
Guru Nanak Khalsa College
Yamuna Nagar
[email protected]
TOPICS

• Arrays - Introduction
• Need of Arrays
• Characteristics of Arrays
• Types of Arrays
• Why indexing of arrays start at 0 (zero)
NEED OF ARRAYS

• An ordinary variable is capable of storing


only one value at a time

• However, there are situations in which we


would be requiring to store more than one
value at a time in a single variable
NEED OF ARRAYS

• e.g. Store the % marks obtained by 100


students
• Two options to store the marks are:
– Construct 100 variables to store % marks
obtained by 100 different students i.e. Each
variable containing one student‟s marks
– Construct one variable (called a subscripted
variable) capable of storing all the hundred
marks
NEED OF ARRAYS

#include<stdio.h>
void main()
{
int marks1, marks2, marks3, marks4, marks5;
scanf(“%d”,&marks1);
scanf(“%d %d %d”%d”,&marks2, marks3,marks4,
marks5);
}
NEED OF ARRAYS

1000 1001
marks1 1002 1003
marks2 1004 1005
marks3
1006 1007
marks4 1008 1009
marks5 1010 1011
1012 1013 1014 1015 1016 1017
ARRAY

• General Definition
– “Array is an ordered collection of
homogeneous elements”

Collection is ordered
Elements are homogeneous
ARRAY

• Collection is ordered
– items added to the collection are maintained in
the order it were added to the collection

• Elements are homogeneous


– elements are of same type, group or class
ARRAYS IN C

• Array is a collection of same type elements


under the same variable identifier
referenced by index number
• Arrays
– Are a sequence of memory locations referred to
by a common name
– Can store multiple values of the same data type
• The individual data items are called
elements of the array
VARIABLES IN
MEMORY
1000 1001 char
1002 c=„A‟;
1003 1004
1005
int num=10; 1006 1007 1008 1009
1010 1011 1012 1013 1014
float price=24.56;
1015 1016 1017 1018 1019
1020 1021 1022 1023 1024
1025 1026 1027 1028 1029
1030 1031 1032 1033 1034
ARRAYS IN MEMORY

1000
<Array>1001 1002 1003 1004
1005 1006 1007 1008 1009
1010 1011 1012 1013 1014
1015 1016 1017 1018 1019
1020 1021 1022 1023 1024
1025 1026 1027 1028 1029
1030 1031 1032 1033 1034
ARRAYS IN C

• Array data structure, an arrangement of


items at equally spaced addresses in
computer memory

• Array data type, used in a programming


language to specify a variable that can be
indexed
CHARACTERISTICS OF
ARRAYS IN C
• Array is a subscripted variable
• Random access via indexing operator []
• Indexing
– starts at 0
– Ends at N-1 (where N is maximum the
array size)
• The name of the array refers to the
address of the first element in the array
TYPES OF ARRAYS

ARRAYS

Single Dimension Multi-Dimension

Static Dynamic Static Dynamic


STATIC vs DYNAMIC

• Static arrays have their sizes declared from


the start and the size cannot be changed
after declaration

• Dynamic arrays that allow you to


dynamically change their size at runtime, but
they require more advanced techniques
such as pointers and memory allocation.
DECLARING SINGLE
DIMENSION ARRAYS
• SYNTAX:
<data type> <variable name>[<dimension size>]
– data type specifies the data type of the
values that will be stored in the array
– dimension size indicates the maximum
number of elements that can be stored in the
array
• The name of the array refers to the
address of the first element in the array
DECLARING SINGLE
DIMENSION ARRAYS
• Example:
<data type> <variable name>[<dimension size>]
int num[5];
• This declares an array num of size 5
• Elements of the array are num[0], num[1],
num[2], num[3] and num[4]
• 0, 1, 2, 3 and 4 are called subscripts or indexes
• The array name num refers to the address of
num[0] i.e. first element of the array
INITIALISING ARRAYS

• Initialisation of an array
– Refers to assigning values to the elements of
the array
– Can happen along with declaration or after
declaration
SINGLE DIMENSION
ARRAYS
1000
num[0]
17 1001 1002
num[1]
2 1003 1004
num[2]
3 1005
1006 44 1007
num[3] 1008 15 1009
num[4] 1010 1011
1012 1013 1014 1015 1016 1017

int num[5]={17,2,3,44,15};

printf(“%d”,num[2]);
INITIALIZING ARRAYS
#include<stdio.h>
void main()
{
int marks[5]={34,21,33,12,15};
}
#include<stdio.h>
void main()
{
int marks[]={34,21,33,12,15};
}
INITIALIZING ARRAYS
#include<stdio.h>
void main(){
int marks[5];
marks[0]=34; marks[0]=34;
marks[1]=21; marks[3]=12;
marks[2]=33; marks[4]=15;
marks[3]=12; marks[1]=21;
marks[4]=15; marks[2]=33;
}
READING ARRAYS

#include<stdio.h>
void main()
{
int marks[5], int i=0;
for(i=0;i<5;i++)
{
scanf(“%d”,&marks[i]);
}
}
ARRAYS – MEMORY
MAP
1000
marks[0]
34 1001 1002
marks[1]
21 1003 1004 33 1005
marks[3]
1006 12 1007 marks[4]
1008 15 1009 1010 1011
1012 1013 1014 1015 1016 1017
MULTI-DIMENSIONAL
ARRAYS
• Don‟t really exist in C
• C allows “arrays of arrays”
• Use one set of bracket for each dimension
• Can be initialized with nested initializer lists
MULTI-DIMENSIONAL
ARRAYS
• Consider the following 3 x 2 array:
1 2 1 2
3 4 3 4
5 6 5 6

• C stores it linearly: 1 2 3 4 5 6
MULTI-DIMENSIONAL
ARRAYS
1 2 3 4 5 6

1st Array 2nd Array 3rd Array


of 2 of 2 of 2
integers integers integers

•The compiler interpret it as:


an array of “3 arrays of 2 integers”
C allow “arrays of arrays”
DECLARING ARRAYS

• Example:
<data type> <variable name>
[<dimension 1 size>] [<dimension 2 size>]....
[<dimension N size>]

int num[3][2];

• This declares an array num of size 3 x 2


• Elements of the array are num[0][0], num[0][1],
num[1][0], num[1][1], num[2][0] and num[2][1]
INITIALISING ARRAYS

1000
num[0][0]
1 1001 1002
num[0][1]
2 1003 1004
num[1][0]
3 1005
num[1][1]
1006 4 1007 num[2][0]
1008 5 1009 num[2][1]
1010 6 1011
1012 1013 1014 1015 1016 1017
int num[3][2]={1,2,3,4,5,6};
int num[3][2]={
{1,2},
{3,4},
{5,6}
};
INITIALISING ARRAYS

num[3][2] array name num 1000


points to first
element

1 2 3
1000 1001 1002 1003 1004 1005

4 5 6
1006 1007 1008 1009 1010 1011
INITIALISING ARRAYS

num 1 2
num[0]
1000 1001 1002 1003

num[0][0] 3 4
1004 1005 1006 1007
5 6
1008 1009 1010 1011
INITIALISING ARRAYS

num 1 2
num[0]
1000 1001 1002 1003

num[0][0] 3 4
1004 1005 1006 1007
5 6
1008 1009 1010 1011
INITIALISING ARRAYS

num 1 2
num[0]
1000 1001 1002 1003

num[0][0] 3 4
1004 1005 1006 1007
5 6
1008 1009 1010 1011
INITIALISING ARRAYS

num 1 2
num[0]
1000 1001 1002 1003

num[0][0] 3 4
1004 1005 1006 1007
5 6
1008 1009 1010 1011
INITIALISING ARRAYS

num 1 2
num[0]
1000 1001 1002 1003

num[0][0] 3 4
num[1]
1004 1005 1006 1007
5 6
1008 1009 1010 1011
INITIALISING ARRAYS

num 1 2
num[0]
1000 1001 1002 1003

num[0][0] 3 4
num[1]
1004 1005 1006 1007

num[2] 5 6
1008 1009 1010 1011
ACCESSING ARRAYS

1 2
1000 1001 1002 1003
3 4
1004
printf(“%d”,num[0][0]); 1005 1006 1007

printf(“%d”,*num[0]); 5 6
1008 1009 1010 1011
printf(“%d”,**num);
TWO DIMENSIONAL
ARRAY
#include<stdio.h>
void main(){
int marks[3][2], int i=0,j=0;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
scanf(“%d”,&marks[i][j]);
}
}
}
DECLARING STRING

• Example:
<data type> <variable name>[<dimension size>]
char str[10] ;
• Creates a character array str that can store a
maximum of 9 characters
• The last element of a character array must always
be the null character or string terminator [\0]
• It indicates the end of the character array
• The null character cannot be printed
INITIALISING ARRAYS

char str[6]; I
str[0] = „I‟;
str[1] = „N‟;
N
Both statements
str[2] = „D‟; D
assign the name
str[3] = „I‟; INDIA to the I
str[4] = „A‟; array str A
Str[5] = „\0‟;
„\0‟
char str[ ] = “INDIA”;
PRINTING ARRAYS

• All elements of a character arrays can be


printed using a single printf() function
• The format code for printing a string is %s
printf(“%s”, str);
The above statement prints the value of the
character array str
 The printf() function displays all the characters
in the array until the null character „\0‟ is
reached
INITIALISING ARRAYS

1000
I 1001
N 1002
D 1003
I 1004
A
; „\0‟
1005 1006 1007 1008 1009
1010 1011 1012 1013 1014
;
;

‟;
EFFECT OF NULL
CHARACTER „\0‟ON
STRING
void main(){
char name[5]={„i‟,‟n‟,‟d‟,‟i‟,‟a‟};
char name1[5]=“india”;
printf(“NAME = %s”,name);
printf(“NAME1= %s”,name);
} First Execution Second Execution
STRING FORMAT WITH
PRECISION
main()
{
char name[6]={'I','N','D','I','A'};
printf("\n%.2s",name);
printf("\n%.10s",name);
printf("\n%10.4s",name);
printf("\n%10s",name);
printf("\n%-10s",name);
}
CHARACTER ARRAY
vs STRING
main(){
char text[]="have a nice day";
int i=0;
while(i<=15)
{
printf("%c",text[i]);
i++;
}
printf(“%s”,text);
}
WHY ARRAY STARTS
AT 0
char str[ ] = “INDIA”; printf(“%c”,str[3]);

Index str Address


str[3]
0 I 100
1 N 101
*(str+3)
2 The nameD of
102the array
refers to the address of
3 I 103 *(100+3)
the first element in the
4 A array
104
5 „\0‟ 105 *(103)
WHAT IF ARRAY
STARTS AT 1
char str[ ] = “INDIA”; printf(“%c”,str[4]);

Index str Address


str[4]
1 I 100
2 N 101
*(str+4)
3 D 102
4 I 103 *(100+4)
5 A 104
6 „\0‟ 105 *(104)
SUGGESTED READING

• BOOKS:
– Yashwant Kanetkar, “Let Us C”, 5th Edition,
BPB Publications
– Chuck Allison, “Thinking in C”, Mindview Inc
• ONLINE RESOURCES
– Microsoft MSDN library
Thank You

You might also like