Arrays
Declaration :
float expenses[12];
Initialization:
expenses[11] = 87.90;
expenses[11] = expenses[10];
expenses[2+3] = 100;
i = 4;
expenses[i] = 100;
int a[6];
a[2] = 2;
expenses[a[2]] = 100;
#define MONTHS 12
int expenses[MONTHS];
int MONTHS = 12;
int expenses[MONTHS]; (is not correct)
Initialization with Declaration:
int a[4] = { 100, 200, 300, 400};
int a[] = { 100, 200, 300, 400};
int a[4] = { 100, 200, 300};
int a[4] = { 100, 200, 300, 400, 500}; (not correct)
Example:
#include <stdio.h>
void main()
{
/* Declare an array to hold expenses, and a counter variable */
float expenses[13];
int count;
/* Input data from keyboard into array */
for (count = 1; count < 13; count++)
{
printf("Enter expenses for month %d: ", count);
scanf("%f", &expenses[count]);
}
/* Print array contents */
for (count = 1; count < 13; count++)
{
printf("\nMonth %d = $%.2f", count, expenses[count]);
}
}
int cell[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12};
int cell[4][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };
Example:
#include <stdio.h>
#include <stdlib.h>
void main()
{
/* Declare a three-dimensional array with 1000 elements */
int random_array[10][10][10];
int a, b, c;
/* Fill the array with random numbers. The C library */
/* function rand() returns a random number. Use one */
/* for loop for each array subscript. */
for (a = 0; a < 10; a++)
{
for (b = 0; b < 10; b++)
{
for (c = 0; c < 10; c++)
{
random_array[a][b][c] = rand();
}
}
}
/* Now display the array elements 10 at a time */
for (a = 0; a < 10; a++)
{
for (b = 0; b < 10; b++)
{
for (c = 0; c < 10; c++)
{
printf("\nrandom_array[%d][%d][%d] = ", a, b, c);
printf("%d", random_array[a][b][c]);
}
printf("\nPress a key to continue, CTRL-C to \
quit.");
getchar();
}
}
} /* end of main() */
Example:
/* Demonstrates the sizeof() operator */
#include <stdio.h>
void main()
{
/* Declare several 100 element arrays */
int intarray[100];
float floatarray[100];
double doublearray[100];
/* Display the sizes of numeric data types */
printf("\n\nSize of int = %d bytes", sizeof(int));
printf("\nSize of short = %d bytes", sizeof(short));
printf("\nSize of long = %d bytes", sizeof(long));
printf("\nSize of float = %d bytes", sizeof(float));
printf("\nSize of double = %d bytes", sizeof(double));
/* Display the sizes of the three arrays */
printf("\nSize of intarray = %d bytes", sizeof(intarray));
printf("\nSize of floatarray = %d bytes",
sizeof(floatarray));
printf("\nSize of doublearray = %d bytes",
sizeof(doublearray));
}
Character Strings
Character Variables:
char a, b, c; char code = 'x'; code = '!';
Character Constants:
#define EX 'x'
char code = EX
Example 1:
/* Demonstrates the numeric nature of char variables */
#include <stdio.h>
int main()
{
/* Declare and initialize two char variables */
char c1 = 'a';
char c2 = 90;
/* Print variable c1 as a character then as a number */
printf("\nAs a character, variable c1 is %c", c1);
printf("\nAs a number, variable c1 is %d", c1);
/* Do the same for variable c2 */
printf("\nAs a character, variable c2 is %c", c2);
printf("\nAs a number, variable c2 is %d", c2);
return 0;
}
Example 2:
/* Demonstrates printing extended ASCII characters */
#include <stdio.h>
int main()
{
unsigned char x; /* Must be unsigned for extended ASCII */
/* Print extended ASCII characters 180 through 203 */
for (x = 180; x < 204; x++)
{
printf("\nASCII code %d is character %c", x, x);
}
return 0;
}
Character Strings
\0.char name[10] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '\0'};
char name[10] = ``Computers'';
char name[] = ``Computers'';
This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 -html_version 3.0 c4.tex.
The translation was initiated by Vijay & on Thu Feb 19 11:47:48 EST 1998