Unit 3
Unit 3
Arrays
Array is a variable which store more than one values of same datatype in a
continuous memory location.
EX: students,employees.
Declaration of arrays:
Syntax:
datatype arrayname[size];
Ex:int,char,float,double.
Initializing array:
#include<stdio.h>
#include<conio.h>
main()
int marks[5][5];
int i,j,r,c;
clrscr();
scanf(“%d%d”,&r,&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf(“%d”,&marks[i][j]);
for(i=0;i<r;i++)
{
printf(“\n”);
for(j=0;j<c;j++)
printf(“%d\t”,marks[i][j]);
getch();
Multidimensional Arrays:
#include<stdio.h>
#include<conio.h>
main()
int a[2][2][2];
int i,j,k;
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
scanf(“%d”,&a[i][j][k]);
for(i=0;i<2;i++)
printf(“\n\n”);
for(j=0;j<2;j++)
{
printf(“\n”);
for(k=0;k<2;k++)
printf(“\t a[%d][%d][%d]=%d”,i,j,k,a[i][j][k]);
getch();
#include<stdio.h>
#include<conio.h>
main()
int a[2][2][2][2];
int i,j,k,l;
clrscr();
for(j=0;j<2;j++)
for(k=0;k<2;k++)
For(l=0;l<2;l++)
scanf(“%d”,&a[i][j][k][l]);
}
for(i=0;i<2;i++)
printf(“\n\n\n”);
for(j=0;j<2;j++)
printf(“\n\n”);
for(k=0;k<2;k++)
Printf(“\n”);
For(l=0;l<2;l++)
printf(“\t a[%d][%d][%d][%d]=%d”,i,j,k,l,a[i][j][k][l]);
}
}
getch();
Strings
Strings:
Declaring strings :
Ex:char s[5];
Initializing strings:
Ex:char c[5]=”hello”;
• while implementing fixed length string,we have to declare the size of the
variable.
Ex:char str[5]=”Have a nice day”;\\memory is not sufficient to store
value
Ex:char str[100]=”Hai”; \\memory is wasted
2.puts and gets-puts is used to write a string onto the output screen.gets is
used to read a string from the keyboard.
Ex1:char str1[]=“hello”;
printf(“%s”,str1);
scanf(“%s”,&str1);
#include<stdio.h>
#include<conio.h>
main()
char str[];
clrscr();
scanf(“%s”,&str);
Printf(“%s”,str);
getch();
Output:
hai
Example program to Read & Write Strings in C using gets() and puts()
functions :
#include <stdio.h>
#include <conio.h>
main()
char name[20];
clrscr();
puts(name);
getch();
Output:
Hello
Hello
1. String length:
Syntax: strlen(variable);
#include <stdio.h>
#include<conio.h>
#include <string.h>
main()
Clrscr();
getch();
Output:
Example program:
#include <stdio.h>
#include<conio.h>
#include <string.h>
main()
clrscr();
getch();
Output:
strlen vs sizeof
strlen returns you the length of the string stored in array, however sizeof
returns the total allocated size assigned to the array.
2.String copy:
strcpy( )- Copies the contents of one string into another
Syntax: strcpy(destination,source);a=10;
Example program:
#include <stdio.h>
#include<conio.h>
#include <string.h>
main()
printf("string s1 is %s\n",s1);
printf("string s2 is %s\n",s2);
strcpy(s1,s2);
getch();
3.String compare:
Syntax: strcmp(stringvariable1,stringvariable2);
The strcmp() function return an integer value as the result.
1.result +ve : String variable1 is greater than string variable2.
2.result –ve : String1 is less than string2.
3.result is zero : Both strings are equal.
Example program:
#include <stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[20];
char str2[20];
int result;
clrscr();
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
result=strcmp(str1,str2);
if(result==0)
printf("strings are same");
else
printf("strings are not same");
getch();
}
4.String concatenation:
• strcat( )- Concatenates two Strings
Example program:
#include <stdio.h>
#include<conio.h>
#include <string.h>
main()
{
char s1[10] = "Hello";
char s2[10] = "World";
clrscr();
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
getch();
}
5.String reverse:
• strrev(): This function is used to get the reverse string of a given string.
Syntax: strrev(string);
Example program:
#include<stdio.h>
#include< string.h>
main()
{
char str[20];
clrscr();
puts(“Enter a string”);
gets(str);
strrev(str);
puts(“Reverse string of the given string is”);
puts(str);
getch();
}
1.atoi:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h.
main()
int x = atoi(str);
printf("Converting '122': %d\n", x);
// Converting an alphanumeric string
int z = atoi(str3);
getch();
2.atof :- This function returns the converted floating point number as a double
value. If no valid conversion could be performed, it returns zero (0.0).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main ()
char str[20];
strcpy(str, "98993489");
float x = atof(str);
strcpy(str, “hello");
float y = atof(str);
getch();
3.atol:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
long int l;
char str[30];
clrscr();
printf("enter a string:\n");
scanf("%s",str);
l=atol(str);
printf("string to long integer is : %ld",l);
getch();
}
A Programming Example – Morse Code:
• Morse code is a method of transmitting text information as a series of on-
off tones, lights, or clicks that can be directly understood by a skilled
listener or observer without special equipment.
• It is named for Samuel F. B. Morse, an inventor of the telegraph.
#include<stdio.h>
#include<string.h>
main()
{
char *alphamorse[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."};
char *nummorse[]={"-----",".----","..---","...--","....-",".....","-....","--...","---
..","----."};
int i;
char str[1000],str1[1000];
printf("Enter a sentence\n");
gets(str);
i=0;
while(str[i]!='\0')
{
if(str[i]!=' '&&(!isdigit(str[i])))
{
printf("%s ",alphamorse[toupper(str[i])-65]);
}
if(str[i]==' ')
{
printf(" ");
}
if(isdigit(str[i])&&str[i]!=' ')
{
printf("%s ",nummorse[str[i]-48]);
}
i++;
}
printf("\n");
Ex:
main()
my_int a=10,b=20,c;
c=a+b;
printf(“sum is %d\n”,c);
}
Typedef usage in arrays:
main()
int i;
Array x ={10,20,30,40,50};
for(i=0;i<5;i++)
{
printf(“%d\n”,x[i]);
Structures:
Syntax of structure:
struct structurename
datatype member1;
datatype member2;
...
};
{
\\ members of structure
int id;
char name[10];
float salary;
};
Typedef declarations:
Syntax:
int rollno;
char name[20];
char course[20];
float fees;
};
struct stud1;
Initialization of structures:
OR
struct structname
{
datatype member_name1;
datatype member_name2;
….
}struct_variable={constant1,constant2,..};
Ex:
struct student
{
int roll_no;
char name[20];
char course[20];
float fees;
} stud1={01,”abcd”,”eee”,45000};
Syntax:
struct variable.member_name;
stud1.roll_no=1;
stud1.name=“abcd”;
stud1.course=“eee”;
stud1.fees=45000;
• To input values for data members of the structure variable stud1,we
can write
scanf(“%d”,&stud1.roll_no);
scanf(“%s”,&stud1.name);
printf(“%s”,stud1.course);
printf(“%f”,stud1.fees);
struct employee
int id;
char name[50];
float salary;
int main( )
e1.name=“abcd”;
e1.salary=56000;
e2.id=102;
e2.name=“cdef”;
e2.salary=126000;
getch();
Output:
employee 1 id : 101
employee 2 id : 102
Union:
Defining union:
union union_name
{
data_type member1;
data_type member2; .
data_type memeberN;
};
union employee
{
int id;
char name[50];
float salary;
};
Initializing and accessing union members is same as structure.
#include <stdio.h>
union employee
int id;
char name[50];
int main( )
{
//store first employee information
e1.id=101;
e1.name=“abcd”;
getch();
Output:
employee 1 id : 1869508435
employee 1 name : abcd
• As you can see, id gets garbage value because name has large memory
size. So only name will have actual value.
• Only the last entered data can be stored in the union. It overwrites
the data previously stored in the union.
Difference between structure and union:
Syntax:
Example:
#include<stdio.h>
main()
getch();
Output:
Programming Application:
Mainly C Language is used for Develop Desktop application and system
software. Some application of C language are given below