Practical C Programming Chapter 5: Arrays, Qualitifiers, and Reading Numbers
to reference an element of an arrya, you use a number named index
the '\0'(NUL) is used to indicate the end of a string
char string_name[length]; /* comment */
C does not allow one array to be assigned to another, instead use the standard library function strcpy
Function Description
strcpy(string1, string2) copy string2 into string1
strcat(string1, string2) concatenate string2 onto the end of string1
length = strlen(string) get the length of a string
strcmp(string1, string2) 0 if string1 equals string2, otherwise nonzero
name: the name of a character array. The line (including the end-of-line charavter) is read into this array;
sizeof(name): indicates the maximum number of characters to read (plus one of the end-of-string character);
stdin: the file to read
C allows the programmer to use as many as dimensions as needed.
the scanf is notorious for its poor end-of-line handling, which makes scanf useless for all but an expert.
Instead use fgets to read a line of input and sscanf to convert the text into numbrs.
short int
signed int
Integer printf/sscanf conversions
%conversion uses
%hd (signed) short int
%d (signed) int
%ld (signed) long int
%hu unsigned short int
%u unsigned int
%lu unsigned long int
long int declarations allow the program to explicitly specify extra precision where it is needed (at the expense of memory).
short int numbers save space but have a more limited range
the most compact integers have type char, they also have the most limited range
unsigned numbers provide a way of doubling the positive range at expense of elminating negative numbers.
double
long double
float printf/sscanf conversions
%conversion uses notes
%f float printf only
%lf double scanf only
%Lf long double Not available on all compilers
on some machines, single-precision, floating-point instructions execute faster (but less accurately) than double-precision instructions. Double-precision intructions gain accuracy at the expense of time and storage.
In most cases, float is adequate; however, if accuracy is a problem, switch to double
const type VARIABLE = value; /* comment */
constants must be initialized at declaration time and can never be changed.
staring a number with "0x" indicates a hexadecimal constant
-- : used for decrementing
operator shorthand equivalent statement
+= x += 2; x = x + 2;
-= x -= 2; x = x - 2;
*= x *= 2; x = x * 2;
/= x /= 2; x = x / 2;
%= x %= 2; x = x % 2;
Never use ++ or -- as part of any other statement, and always put them on lines by themselves
1. Arrays:
type array_name[length]; /* comment */to reference an element of an arrya, you use a number named index
2. Strings:
Strings are sequences of charactersthe '\0'(NUL) is used to indicate the end of a string
char string_name[length]; /* comment */
C does not allow one array to be assigned to another, instead use the standard library function strcpy
Function Description
strcpy(string1, string2) copy string2 into string1
strcat(string1, string2) concatenate string2 onto the end of string1
length = strlen(string) get the length of a string
strcmp(string1, string2) 0 if string1 equals string2, otherwise nonzero
3. Reading Strings
fgets(name, sizeof(name), stdin);name: the name of a character array. The line (including the end-of-line charavter) is read into this array;
sizeof(name): indicates the maximum number of characters to read (plus one of the end-of-string character);
stdin: the file to read
4. Multidimensional Arrays:
type variable[size1][size2]; /* comment */C allows the programmer to use as many as dimensions as needed.
5. Reading numbers
scanf provides a simple and easy way of reading numbers that almost never works.the scanf is notorious for its poor end-of-line handling, which makes scanf useless for all but an expert.
Instead use fgets to read a line of input and sscanf to convert the text into numbrs.
char line[256];
fgets(line, sizeof(line), stdin);
sscanf(line, format, &variable1, &variable2, ... );
6. Initializing variables
C allows variable to be initialized in the declaration statement.
7. Types of Integers:
long intshort int
signed int
Integer printf/sscanf conversions
%conversion uses
%hd (signed) short int
%d (signed) int
%ld (signed) long int
%hu unsigned short int
%u unsigned int
%lu unsigned long int
long int declarations allow the program to explicitly specify extra precision where it is needed (at the expense of memory).
short int numbers save space but have a more limited range
the most compact integers have type char, they also have the most limited range
unsigned numbers provide a way of doubling the positive range at expense of elminating negative numbers.
8. Types of floats:
floatdouble
long double
float printf/sscanf conversions
%conversion uses notes
%f float printf only
%lf double scanf only
%Lf long double Not available on all compilers
on some machines, single-precision, floating-point instructions execute faster (but less accurately) than double-precision instructions. Double-precision intructions gain accuracy at the expense of time and storage.
In most cases, float is adequate; however, if accuracy is a problem, switch to double
9. Constant Declarations
the keyword const indicates a variable that never changesconst type VARIABLE = value; /* comment */
constants must be initialized at declaration time and can never be changed.
10. Hexadecimal and Octal Constants
leading zeros are used to signal an octal constant;staring a number with "0x" indicates a hexadecimal constant
11. operators for performing shortcuts
++ : used for incrementing-- : used for decrementing
operator shorthand equivalent statement
+= x += 2; x = x + 2;
-= x -= 2; x = x - 2;
*= x *= 2; x = x * 2;
/= x /= 2; x = x / 2;
%= x %= 2; x = x % 2;
12. Side effects:
a side effect is an operation that is performed in addition to the main operation executed by the statement.Never use ++ or -- as part of any other statement, and always put them on lines by themselves
13. ++x or x++
14. more side-effect problems
value = 1;
result = (value++ * 5) + (value++ * 3);
in order to avoid trouble and keep the program simple, always put ++ and -- on a line by themselves