Beruflich Dokumente
Kultur Dokumente
int a, b;
scanf(%d\n%d, a, b);
printf(%d, sum(a, b));
getch();
return 0;
}
int sum(int addend1, int
addend2)
{
return addend1 + addend2;
}
...
A line of code is inserted before main().
This program prints a set of 20 asterisks.
Example 1.2.3. (Passing No Value, Revision)
...
void asterisks(void)
{
printf(*);
}
int main()
{
int i;
for(i = 0; i < 20; i++)
{
asterisks();
}
...
1.3. *Calling Functions
fName(param);
fName function name
param - parameters
2. Arrays
- store large amounts of data in one variable
- could only hold homogenous data
- arrays that store characters are called
strings**
2.1. One-Dimensional Arrays
Syntax:
aType aName[size] = {<data>};
aType array datatype
aName array name
size array size (e.g. 4)
Example 2.1.1. (Array Declaration)
4TH QUARTER
REVIEWER IN COMPUTER SCIENCE
...
int array1[10] = {1, 4, 5, 7,
3, 5, 7, 9, 0, 2};
...
1
0
4
1
5
2
7
3
3
4
5
5
7
6
9
7
0
8
2
9
array index = n - 1
Example 2.1.2. (Array Execution)
...
int main()
{
int array1[10] = {1, 4, 5,
7, 3, 5, 7, 9, 0, 2};
int i;
for(i = 0; i < 10; i++)
{
printf(%d\n,
array1[i]);
}
...
This program prints all contents of array1,
from index 0 to index 9.
2.2. Two-Dimensional Arrays
- store one-dimensional arrays within arrays
Syntax:
aType aName[size1][size2];
}
...
This program prints 12 user-input integers
stored in array1.
Graphical Representation 2.2.1.
x
0
1
array1[0][x]
2
3
array1[1][x]
15
0
array1[2][x]
1
4
assumed data
2
5
21
8
3
6
8
7
3. **Strings
- are character arrays
- hold groups of characters in the form of
words, phrases, sentences, etc.
- string sizes should be one more than the
number of characters it should hold (n + 1)
for the \0, or the null byte.***
Syntax:
char sName[size + 1];
This string should hold size characters.
Example 3.0.
...
char string1[9] = Hello!;
Example 2.2.1.
...
int main()
{
int array1[3][4];
int i, j;
printf(%s, string1);
...
G
7
G
8
4TH QUARTER
REVIEWER IN COMPUTER SCIENCE
...
#include<string.h>
...
3.1.1.
gets(strName);
- reads user-input characters then
stores them to strName
- similar
to
scanf();
and
fgets();
3.1.2. puts(strName);
- displays the contents of strName
- similar to printf();
3.1.3. strlen(strName);
- counts the number of characters
strName is currently holding
- returns and integer
- the string terminator, or \0, is
ignored
3.1.4. strcpy(strDest, strSrc);
- copies all contents of strSrc then
stores them to strDest
3.1.5. strncpy(strDest,
strSrc,
size);
- copies partial contents of strSrc
(at size size) then stores them to
strDest
3.1.6. strcat(strDest, strSrc);
- appends strSrc to strDest
3.1.7. strlwr(strName);
- converts all contents of strName
to its lowercase form
3.1.8. strupr(strName);
- converts all contents of strName
to its uppercase form
3.1.9. strrev(strName);
- reverses all characters in strName
3.1.10. strcmp(strName1, strName2);
- compares
strName1
and
strName2, then returns either -1,
0 or 1, depending on which comes
first alphabetically
- returns -1 if strName1 comes first
before strName2 alphabetically
- returns 0 if no one comes first
- returns 1 if strName2 comes first
before strName1 alphabetically
- doesnt ignore capital letters;
ASCII-based
3.1.11. strcmpi(strName1,
strName2);
- similar to strcmp(strName1,
strName2);, but ignores letter
cases
3.1.12. toupper(letter);
- converts letter to its uppercase
form
3.1.13. tolower(letter);
- converts letter to its lowercase
form.
Keith Badulis