21
ACM TRAINING COURSE Class #1 Data Types Inputs/Outputs

ACM TRAINING COURSE

  • 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

Page 1: ACM TRAINING COURSE

ACM TRAINING COURSE

Class #1

•Data Types

•Inputs/Outputs

Page 2: ACM TRAINING COURSE

Data Types

char (signed or unsigned)

int (signed or unsigned)

double (signed or unsigned)

float (signed or unsigned)

void

Page 3: ACM TRAINING COURSE

Inputs

From Stdin From File

scanf gets getchar sscanf

fgets fscanf

Page 4: ACM TRAINING COURSE

int scanf ( const char * format, ... );

Whitespace

Non-whitespace character (except %)

Format specifiers %[*][width][modifier]type

Page 5: ACM TRAINING COURSE

Format Specifiers

Page 6: ACM TRAINING COURSE

Type

Page 7: ACM TRAINING COURSE

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.

Page 8: ACM TRAINING COURSE

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.

Page 9: ACM TRAINING COURSE

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.

Page 10: ACM TRAINING COURSE

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.

Page 11: ACM TRAINING COURSE

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.

Page 12: ACM TRAINING COURSE

Outputs

TO STDOUT TO FILE

printf puts putchar

fprintf fwrite

Page 13: ACM TRAINING COURSE

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

Page 14: ACM TRAINING COURSE

Flags and Width

Page 15: ACM TRAINING COURSE

Precision and Length

Page 16: ACM TRAINING COURSE

Specifiers

Page 17: ACM TRAINING COURSE

Return Value

On success, the total number of characters written is returned.

On failure, a negative number is returned.

Page 18: ACM TRAINING COURSE

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.

Page 19: ACM TRAINING COURSE

Return Value

On success, a non-negative value is returned.

On error, the function returns EOF.

Page 21: ACM TRAINING COURSE

bibliography

www.google.com

http://www.cplusplus.com/