What is the difference between getc and get

Beginning back next

2. Some input and output functions

The input of information to the running program or the output of data on the screen is an essential part of every program. In C itself there are no keywords for these operations. Rather, external input / output functions are used for this purpose.

Input / output of characters - getchar (), getch () / putchar ()

The input and output of individual characters is achieved with the functions and. The declarations of the functions are: int getchar (void); int putchar (int); A character is entered using char c; ... c = getchar (); Actually, these definitions represent macros that can be found in the file and are to be integrated via an instruction. reads an input from the keyboard, the reading being triggered with the enter key. If several characters have been entered one after the other and only then the Enter key is pressed, the characters are buffered and can be read out of the buffer character by character. The characters entered are also displayed on the screen. In contrast to this, int getch (void); an unbuffered input without an echo and accepts the first character from the keyboard buffer.

Input / output of character strings - gets () / puts ()

The char * gets (char * s) functions are used for inputting and outputting character strings; int puts (char * s); e.g. puts ("This is an example of a string"); the text
with a subsequent line break on the screen. In addition to the functions for unformatted input and output, two functions are available for formatted input and output. These functions offer a wide range of options for designing the input and output of text and variables.

Formatted output - printf ()
(from definition file stdio.h)

int printf (const char * format [, var1, var2, ...]);

printf converts output and writes it under the control of the string format to standard output.

The format string contains two types of objects: ordinary characters, which are copied to the output, and conversion specifications, which cause the conversion and output of the next argument.
Each conversion specification begins with the% character and ends with a conversion character.
Some of the "common" characters may or may not be entered using the keyboard. Substitute representations are used for these special characters.

The following can appear between% and the conversion symbols in sequence:

  • Minus sign: the argument is output to the left
  • number: indicates the minimum field width; if the size (width) of the argument is larger than the field width, the field becomes wider
  • Point: separates field width from precision
  • number: indicates the accuracy;
  • the letter H if the argument is short int, the letter l if the argument is long int, or L. for a long double argument.

The conversion characters from are explained in the following table:

Conversion sign
character Argument; Output as
d, iint;decimal number
Oint; Unsigned octal number
x, Xint; unsigned hexadecimal number,
With abcdef or ABCDEF for 10, ..., 15.
uint; unsigned decimal number
cint; single character
schar *; the character string becomes characters
issued until before '\0', or as many characters as the accuracy is.
fFloating point; [-] m.dddddd (the accuracy indicates the number of decimal places)
e, EFloating point; [-] m.dddddde± xx or [-] m.ddddddE.± xx,
where the precision defines the number of decimal places.
g, GFloating point;% e or % E is used when the exponent is less than -4
or is not less than the accuracy; otherwise will % f used.
pPointer; Outputs the input argument as a pointer in hexadecimal form
(near pointer as YYYY, far pointer as XXXX: YYYY).
%no argument is converted; a% is output

printf thus uses its first argument (the format string) to decide how many further arguments follow for the output and which data types are involved. Unpredictable effects can occur if there are insufficient arguments or if the data types are incorrect.

printf (s); / * ERROR if s contains a% * /
printf ("% s", s); /* FOR SURE */

The result of the function is the number of characters output or EOF in the event of an error.

Formatted input - scanf ()
(from definition file stdio.h)

The function scanf is that too printf analog input function. int scanf (const char * format, & var1 [, & var2, ...]); scanf reads characters from the standard input and interprets them under the control of the character string format and stores the results in the remaining arguments. The arguments must all pointer be!
The number of successful assignments is returned as the function result.

The format string contains conversion information that is used to interpret the input. Each format instruction begins with the percent sign (%).

The format string can contain the following:
  • Spaces or tabs that are ignored.
  • Ordinary characters (but not%) that must match the next character after space in the input stream.
  • Conversion information, consisting of%; an optional character * that prevents assignment to an argument; an optional number that defines the maximum field width; an optional letter h, l or L, which describes the length of the target; and a conversion character.

The conversion characters from are explained in the following table:

Conversion sign
character Input data; Argument type
d decimal, integer; int *
i integer; int *
O octal integer; int *
u unsigned decimal; unsigned int *
x hexadecimal integer; int *
c one or more characters; char *
s String (without ""); char *,
which must point to a vector containing the string and
can accommodate the final '\ 0' appended.
e, f, g Floating point number with optional sign, optional
Decimal point and optional exponent; float *
%recognizes %; an assignment does not take place

scanf also uses its first argument (the format string) to decide how many other arguments follow and what data types are involved.

The format string of scanf can contain common characters; they must then appear exactly like this in the input. e.g. date entry such as dd / mm / yy:

  int day, month, year;
  scanf ("% d /% d /% d", & day, & month, & year);

When entering character strings using the function, note that the ENTER-key terminates the input, but the character string is only accepted up to a tab or space. If, for example, a gap appears in the input stream, only the text up to the gap is transferred to the string variable, the rest of the input (from the gap) remains in the input buffer. So if a whole sentence is to be transferred from the keyboard to a string, this is better done using the function!
The following example shows the problem that occurs:

#include int main (void) {char string1 [81]; char string2 [81]; printf ("\ n \ nEnter a sentence (at least with a space!): \ n"); puts ("The input is read into the variable string1 with scanf! \ n"); scanf ("% s", string1); printf ("\ nContents of the variable string1: \"% s \ "\ n", string1); gets (string2); printf ("The rest of the input is: \"% s \ "\ n", string2); getch (); return 0; }

Entering a single character using can also cause problems if an entry has already been made using. In the first part of the program, a line break remains in the input stream, so that the next part produces an empty input. In the second part of the program a solution to the problem is shown.

/ * scnfxmpl.c Reading in single characters when using scanf beforehand * / #include #include int main (void) {float nmbr; char ch; clrscr (); printf ("\ nEnter a number:"); scanf ("% f", & nmbr); printf ("\ nEnter a character:"); scanf ("% c", & ch); printf ("\ nnumber =% f \ ncharacter =% c \ n", nmbr, ch); printf ("\ nEnter another number:"); scanf ("% f", & nmbr); printf ("\ nEnter another character:"); scanf ("% 1s", & ch); printf ("\ nnumber =% f \ ncharacter =% c \ n", nmbr, ch); return 0; }

continue to the C data types