Decimal adder encoder decoder multiplexers and demultiplexer
Decimal adder encoder decoder multiplexers and demultiplexer
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
#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
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
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
• Example:
<data type> <variable name>
[<dimension 1 size>] [<dimension 2 size>]....
[<dimension N size>]
int num[3][2];
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
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
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]);
• BOOKS:
– Yashwant Kanetkar, “Let Us C”, 5th Edition,
BPB Publications
– Chuck Allison, “Thinking in C”, Mindview Inc
• ONLINE RESOURCES
– Microsoft MSDN library
Thank You