Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

C Programming Unit3

Uploaded by

yashpareek746
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C Programming Unit3

Uploaded by

yashpareek746
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Arrays

An array is a variable that can store multiple values. An array is defined as the collection of
similar type of data items stored at contiguous memory locations. By using the array, we can
access the elements easily. Only a few lines of code are required to access the elements of
the array.

Declaration One dimension Array in C


data_type array_name[array_size];

For example

int mark[5];

char str[30];

C Array Declaration with Initialization


int marks[5]={20,30,40,50,60};

int marks[]={20,30,40,50,60}; // in this case size is automatically defined from initial


values

Q. Write a program to input 10 numbers in an array and print them.

#include <stdio.h>

int main()
{
int n[10],i;

printf("Enter 10 integers: ");

for( i = 0; i < 10; ++i)


{
printf(“Input a Number “);
scanf("%d", &n[i]);
}

printf("Displaying integers: ");


for(int i = 0; i < 10; ++i)
{
printf("%d\n", n[i]);
}
return 0;
}

Declaration Two dimension Array in C


data_type array_name[rows][columns];

int arr[3][4];

int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};

Q: Write a program to input numbers in 3 by 3 matrix and print it.

#include <stdio.h>
int main()
{
int num[3][3],i,j;

for ( i = 0; i < 3; ++i)


{
for ( j = 0; j < 3; ++j)
{
printf("Input a number” );
scanf("%d", &num[i][j]);
}
}
printf("\n Displaying values in matrix form \n\n");

for ( i = 0; i < 3; ++i)


{
for ( j = 0; j < 3; ++j)
{
printf("%d ", num [i][j]);
}
printf(“\n”);
}
return 0;
}

String : The string can be defined as the one-dimension array of characters terminated
by a null character ('\0'). The termination character ('\0') is important in a string since it is

the only way to identify where the string ends.

How to declare and initialize string :


char str[10]={'j','a','i','p','u','r','\0'};

char str[10];

char str[]={'j','a','i','p','u','r','\0'};

char str[]=”jaipur”;

char str[10]=”jaipur”;

You might also like