Upload
silver
View
18
Download
0
Embed Size (px)
DESCRIPTION
Class #1 Data Types Inputs/Outputs. ACM TRAINING COURSE. Data Types. char (signed or unsigned) int (signed or unsigned) double (signed or unsigned) float (signed or unsigned) void. Inputs. int scanf ( const char * format, ... );. Whitespace Non-whitespace character (except %) - PowerPoint PPT Presentation
Citation preview
ACM TRAINING COURSE
Class #1
•Data Types
•Inputs/Outputs
Data Types
char (signed or unsigned)
int (signed or unsigned)
double (signed or unsigned)
float (signed or unsigned)
void
Inputs
From Stdin From File
scanf gets getchar sscanf
fgets fscanf
int scanf ( const char * format, ... );
Whitespace
Non-whitespace character (except %)
Format specifiers %[*][width][modifier]type
Format Specifiers
Type
Return Value
On success, the function returns the number of items succesfully read. (This count can match the expected number of readings or fewer, even zero, if a matching failure happens.)
In the case of an input failure before any data could be successfully read, EOF is returned.
char * gets ( char * str );
Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached.The ending newline character ('\n') is not included in the string.A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.
fgets vs gets
Notice that gets does not behave exactly as fgets does with stdin as argument: First, the ending newline character is notincluded with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.
Return Value
On success, the function returns the same str parameter.
If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returnes.
If an error occurs, a null pointer is returned.
int getchar ( void );
The character read is returned as an int value.If the End Of File is reached or a reading error happens, the function returns EOF and the corresponding error or eof indicator is set.
Outputs
TO STDOUT TO FILE
printf puts putchar
fprintf fwrite
int printf ( const char * format, ... );
String that contains the text to be written to stdout. It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested.The number of arguments following the format parameters should at least be as much as the number of format tags.The format tags follow this prototype:
%[flags][width][.precision][length]specifier
Flags and Width
Precision and Length
Specifiers
Return Value
On success, the total number of characters written is returned.
On failure, a negative number is returned.
int puts ( const char * str );
Writes the C string pointed by str to stdout and appends a newline character ('\n').The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to stdout.
Using fputs(str,stdout) instead, performs the same operation as puts(str) but without appending the newline character at the end.
Return Value
On success, a non-negative value is returned.
On error, the function returns EOF.
Exercise
TEX Quotes
Soundex Indexing
WERTYU
Word Scramble
bibliography
www.google.com
http://www.cplusplus.com/