// #define tells the preprocessor to replace // one string with another // here, everywhere that the preprocessor finds // "NUMBER_OF_PLANETS" it will replace it with "9" // use of capital letters to name constants is a convention // a rule of the language must be obeyed // (program will not work if rule is violated) // a convention should be obeyed // (teammates will be annoyed and you might be confused // if convention is violated) #define NUMBER_OF_PLANETS 9 #define MAX_STRING_LENGTH 24 #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char** argv ) { /* average distance of planets from sun mercury: 57 million km. venus: 108 million km. earth: 150 million km. mars: 228 million km. jupiter: 779 million km. saturn; 1430 million km. uranus: 2880 million km. neptune: 4500 million km. pluto: 5910 million km. */ // "char" means "character" // a character can be a letter, a digit, punctuation // trivia: punched cards had 80 columns // some early video terminals displayed 80 char // names is an array of arrays of characters // another way to say this: it is an array of strings char names[NUMBER_OF_PLANETS][MAX_STRING_LENGTH]; // "str" stands for "string" // "cpy" stands for "copy" // strcpy() is a function that copies a string // "mercury" is a literal (a value) // function call stores "mercury" in the location names[0] strcpy( names[0], "mercury" ); strcpy( names[1], "venus" ); strcpy( names[2], "earth" ); strcpy( names[3], "mars" ); strcpy( names[4], "jupiter" ); strcpy( names[5], "saturn" ); strcpy( names[6], "uranus" ); strcpy( names[7], "neptune" ); strcpy( names[8], "pluto" ); // distances is an array of integers // "double" means "double precision floating point" // double type allows fractions and greater range // of values than does int type double distances[NUMBER_OF_PLANETS]; distances[0] = 57; distances[1] = 108; distances[2] = 150; distances[3] = 228; distances[4] = 779; distances[5] = 1430; distances[6] = 2880; distances[7] = 4500; distances[8] = 5910; // 2^{10} = 1024 // 2^{x} * 2^{x} * 2^{x} = 2^{3x} // 2^{10} * 2^{10} * 2^{10} = 2^{30} = 1024 * 1024 * 1024 //int value = 1024 * 1024 * 1024; //value = value + (value - 1); // when positive count exceeds maximum, count resumes // at most negative value //for( int i = -4; i < 4; i++ ) { // printf( "value = %12d\n", value + i ) ; //} // for for( int i = 0; i < NUMBER_OF_PLANETS; i++ ) { printf( "distance of %s from the sun is %12.2f km.\n", names[i], distances[i] * 1000000 ); } // for exit( 0 ); } // main( int, char** )