c arrays with programming examples in detail

Program to accepting and displaying numbers in reverse order */
#define N 10
void main()
int a[N], k;
for(k = 0; k < N; k++)
printf("\nEneter The Element For a[%d] : ", k);
scanf("%d", &a[k]);
printf("\nWriting The Numbers in the Reverse Order...Please Wait...\n");
for(k = N - 1; k >= 0; k--)
printf("\nThe Element in Index a[%d] = %d", k, a[k]);
Handling Strings and Characters in Arrays
In ‘C’ a STRING is defined as a GROUP of COLLECTED CHARACTERS in
Example: “This is Sample String”
DOUBLE QUOTATION marks do not form part of the character string.
A STRING can be of ANY LENGTH, hence the end of the string is marked with
the SINGLE CHARACTER ‘\0’, which is termed as NULL CHARACTER, having
the ASCII value of ZERO.
The declaration of a STRING is similar to the NUMERIC ARRAY.
char <array-name>[size];
char MyString[8] = “COLLEGE” ;
Every STRING must be TERMINATED by a NULL, hence any STRING must be
defined in such a way that it can hold one BYTE LARGER THAN THE LARGEST
STRING, to make a room for the NULL.
A STRING can be initialized at the time of array declaration or using a loop
in the program.
If the number of characters in the string exceed the maximum size, the
string is truncated.
If the number of characters in the string are less than the maximum size,
then the compiler allocates the NULL character at the first empty element
of the string.

Functions used in INPUT and OUTPUT of strings:

scanf() Function
It is used to accept strings from the keyboard.
The format specifiers used are “%s” or “%nc ”
“ % ne ” is used for inputting a string, until the n’th character in keyed
while using scanf(), spaces cannot be embedded into the STRING.
Example: INPUT : “This is Sample String”
Scanf () function cannot be used to input white spaces, hence the only
operation it can perform is on continuious string.
printf() Function
It is used to display the strings.
→the format specifiers used to print a string is “%s” OR “%n.ds”.
1. n →specifies the field width to be displayed
2. d →specifies that only first ‘d’ character of the string have to be
It can also print the white spaces.
Prints character by character and terminates the display when the NULL
character is encountered.
gets() Function
It is a function to accept strings from the standard input device.
It can take spaces as input from the keyboard.
char string [10];
puts() Function
It is a function to display the strings on to the standard output device.
Automatically puts a new line character ‘\n’ after display of each string.
Can handle only one string at a time.
Initializing a character array, accept string into it and display.
void main()
char string[25];
printf("\nEnter The Data : ");
scanf ("%s", string);
printf ("\n%s", string);
puts("\nEnter Data Again : ");
puts("The String is : ");

To read a line of text
void main()
char string[81], ch;
int k = 0;
while((ch = getch()) != '\r')
if (ch >= 'a'&& ch <= 'z')
ch = ch - 32;
printf("%c", ch);
string[k++] = ch;
if (k > 80)
string[k] = '\0';
To find length of a string
void main()
int k;
char string[81];
printf("\nEnter Your Data...\n");
scanf("%s", string);
printf("\nProcessing The String...");
for (k = 0; string[k] != '\0'; k++);
printf ("\nThe Length of %s is %d", string, k);

No comments: