This document introduces the C++ programmer to the rudiments of C. For those who wish to learn more about C, refer to Appendix B of the COSC220 text: Data Abtraction & Structures Using C++ for a comparison of C and C++. An excellent reference on C is THE C PROGRAMMING LANGUAGE, 2nd ed (or later?) by Kernighan and Ritchie, Prentice Hall.
This document looks only at:
scanf
Reads from keyboard,
ignoring blank space.
Examples:
To read in 02/25/99 with month as type int, you can embed single characters
(such as
the slash / ) in the string, as in --
...
scanf("%i/%i/%i", &month,&day,&year);
d,i
int
integer (base 10)
u
int
unsigned integer
c
int
single character
s
char*
f
double
double floating point
e,E
double
double floating point Ex:
-5.43234e+3
g,G
double
double floating point in exponent notation
Ex: Let age=29 and ht=70:
printf("His age is %i and his height is %i inches.", age, ht);
Output: His age is 29 and his height is 70 inches.
See printf.c and tstio.c on the k drive and in mlmalone's demos directory on ssu037.
stdio.h must be included.
A file variable is a pointer to the type FILE, as in FILE* inputFile;
To OPEN a file:
You can use fscanf as a function, returning an integer which can be tested for the end-of-file constant (as defined in stdio.h, typically -1), as in--
status = fscanf(inputFile,"%s
%i", name, &age);
while (status
!= EOF)
... /* continue processing
the file */
Pass-by-reference:
Nothing required for strings
of characters (as in C++) or other arrays!
For all other data:
The formal parameter is expressed as a pointer:
Ex:
void Swap (int* num1, int* num2);
The prototype:
Ex:
void Swap (int*, int*);
Every reference to the pass-by-reference parameter inside the procedure
must use pointers:
Ex:
The only difference betwen C and C++ here is that once you've defined a struct data type, when you declare a variable to be of that struct data type you must still use the word "struct" at the beginning of the declaration. C++ does not require that you do that.
Ex: typedef struct someStruct
{
...
<-- the fields declared here (same as in C++)
};
struct someStruct
s1,s2;
<-- declares s1 and s2 as type someStruct.
struct someStruct
ListOfStructs[10]; <-- declares an array of
10 structs, each of type someStruct
stdio.h
I/O functions
fopen, fclose, rename, printf, scanf, fprintf, fscanf, sscanf,
fflush, remove, sprintf, vprintf, fgetc, fgets, fputc, fputs,
getc, getchar, gets, purc, putchar, puts, ungetc, fseek, ftell,
rewind, fgetpos, fsetpos, clearerr, feof, ferror, perror, ...
ctype.h
Testing chars (Similar to C++)
isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint,
ispunct, isspace, isupper, isxdigit, toupper, tolower, ...
string.h
For strings (Same or very close to C++)
strcpy, strncpy, strcat, strncat, strcmp, strncmp, strchr,
strrchar, strspn, strcspn, strpbrk, strstr, strlen, strerror, strtok, ...
math.h
Math functions (Same or very close to C++)
sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, exp,
log, log10, pow, sqrt, ceil, floor, fabs, ldexp, frexp, modf, fmod, ...
stdlib.h
Utility functions
atof, atoi, atol, strtod, strtol, strtoul, rand, srand, calloc, malloc,
realloc, free, abort, exit atexit, system, getenv, bsearch, qsort,
abs, labs, div, ldiv, ...
time.h
Day, time functions
tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year,
tm_wday, tm_yday, tm_isdst, clock, time, difftime, mktime,
asctime, ctime, gmtime, localtime, strftime, ...
limits.h, float.h Max,min for integer and floating point types.
assert.h Allows use of assertions. (Similar to C++)
signal.h Functions for signal processing, handling interrupts.
And others...