Is flag a keyword in C.

C programming: simple input and output

Probably no program can do without input and output. In C, however, the input / output is not part of the language itself. Rather, input and output exist as independent functions that are then integrated by the linker. You will get to know the most important input and output functions in this chapter.

printf [edit]

We have already used this function in our previous programs. So it's time to take a closer look at them. The function has the following syntax:

intprintf (constchar * format, ...);

But before we discuss, let's take a look at some basic concepts of functions. In a later chapter you will learn how to write a function yourself.

The two round brackets contain the parameter. In our example, the parameter is. The three dots after it indicate that the function can receive further parameters. The values ​​passed to the function are known as Arguments. In our "Hello World" program, for example, we passed the argument "Hello World" to the function.

In addition, a function can have a Return value have. In this case the type of return value is. The type of return can be recognized by the keyword that precedes the function. You can recognize a function that does not return a value by the keyword.

The library function is used to output a character string on the standard output. Usually the standard output is the screen. The function has a pointer to a constant string as a transfer parameter. We'll see what the pointers are all about later. This means here that the function does not change the string. The number of characters output is supplied via the return value. If there was an error in the output, a negative value is returned.

Only strings are allowed as the first argument of. The compiler therefore issues a warning or an error when compiling the following line:

Because the quotation marks are missing, the compiler assumes that 55 is a number. If you enter 55 in quotation marks, the compiler interprets this as text. The compiler therefore does not issue an error for the following line:

printf ("55"); // correct

Format elements of printf [edit]

The function can also process several parameters, these then have to go through Commas separated from each other.


#include intmain () {printf ("% i plus% i is equal to% s. \ n", 3,2, "five"); return0;}


3 plus 2 equals five.

The format elements introduced with the% symbol access the parameters separated by commas one after the other (the first to 3, the second to 2 and the string "five").

Be within Conversion sign (English conversion modifier) ​​used for the other parameters. The correct type must be used here. The most important conversion symbols are:

character conversion
% d or% i int
% c single character
% e or% E double in the format [-] d.ddd e ± dd or [-] d.ddd E ± dd
% f double in the format [-] ddd.ddd
%O Output int as an octal number
% p the address of a pointer
% s Output string
% u unsigned int
% lu long unsigned
% x or% X Output int as a hexadecimal number
%% Percent sign

Further formats and more detailed explanations can be found in the reference of this book.


#include intmain () {printf ("Integer:% d \ n", 42); printf ("Double:% .6f \ n", 3.141); printf ("Character:% c \ n" , 'z'); printf ("String:% s \ n", "abc"); printf ("43 decimal is in octal:% o \ n", 43); printf ("43 decimal is in hexadecimal:% x \ n ", 43); printf (" And finally we output the percent sign: %% \ n "); return0;}

After compiling and running the program, you will get the following output:

Integer: 42 Double: 3,141000 characters: z String: abc 43 Decimal is in octal: 53 43 Decimal is in hexadecimal: 2b And finally we output the percent sign:%

In addition to the conversion character, a conversion specification can contain further elements for formatting. These are a maximum of:

  • a Flag
  • the Field width
  • separated by a point the number of Decimal places (Length specification)
  • and finally the conversion symbol itself

Flags [edit]

Immediately after the percent sign, the Flags (German designation) specified. They have the following meanings:

  • (Minus): The text is justified to the left.
  • (Plus): A sign is output even with a positive value.
  • Space: A space is output if the value is positive.
  • : The effect of the identifier depends on the format used: If a value is output as hexadecimal, then each value is preceded by 0x (unless the value is 0).
  • : Padding is done with zeros instead of spaces when the field width is changed.

The following is an example that shows how the flags are used:

#include intmain () {printf ("Number 67:% + i \ n", 67); printf ("Number 67:% i \ n", 67); printf ("Number 67:% # x \ n ", 67); printf (" Number 0:% 0x \ n ", 0); return0;}

When the program is compiled and executed, we get the following output:

Number 67: +67 Number 67: 67 Number 67: 0x43 Number 0: 0

Field width [edit]

Behind the flag, the Field width (Eng. field width) can be specified. This means that the output is padded with the appropriate number of characters. Example:

intmain () {printf ("Output numbers right-justified:% 5d,% 5d,% 5d \ n", 34,343,3343); printf ("Output numbers right-justified, padded with 0 on the left:% 05d,% 05d,% 05d \ n ", 34,343,3343); printf (" Output numbers left-justified:% -5d,% -5d,% -5d \ n ", 34,343,3343); return0;}

When the program is compiled and executed, we get the following output:

Output numbers right-aligned: 34, 343, 3343 Output numbers right-aligned, padded with 0 on the left: 00034, 00343, 03343 Output numbers left-aligned: 34, 343, 3343

In line 4 we used a 0 instead of the spaces, so that the field width is now padded with zeros.

By default, the output is right-justified. By placing the minus sign in front, the output can also be left-justified, as can be seen in line 5.

Decimal places [edit]

After the field width, separated by a point, follows the accuracy. Otherwise, 6 decimal places are output by default. Of course, this information is only useful for floating point types, because all other types have no decimal places.


#include intmain () {double amount1 = 0.5634323; double amount2 = 0.2432422; printf ("Sum:% .3f \ n", amount1 + amount2); return0;}

When the program has been compiled and executed, the following output appears on the screen:

Total: 0.807

scanf [edit]

You have also already got to know the function. It has a similar syntax like:

intscanf (constchar * format, ...);

The function reads in a value and saves it in the specified variables. But be careful: the function expects the address of the variable. Therefore the following function call leads to a error:

scanf ("% i", x); / * error * /

In contrast, the following is correct:

scanf ("% i", & x);

With the Address operator & you get the address of a variable. You can also have this displayed:

#include intmain (void) {intx = 5; printf ("Address of x:% p \ n", & x); printf ("Contents of the memory cell:% d \ n", x); return0; }

If you compile the program and run it, you get e.g. the following output:

Address of x: 0022FF74 Contents of the memory cell: 5

The output of the address may vary with you. It is even possible that this information changes each time the program is restarted. This depends on where the program (from the operating system) is loaded into memory.

We will deal with addresses in more detail in the Pointers chapter.

The following placeholders can be used for to ensure that the entered value is converted into the "correct" format:

character conversion
% d signed integer as decimal value
% i signed integer as decimal,
Hexadecimal or octal value
% e,% f,% g Floating point number
%O Read int as an octal number
% s Read in character string
% x Hexadecimal value
%% recognizes the percent sign

getchar and putchar [edit]

The function returns the next character from the standard input stream. A electricity Stream is an ordered sequence of characters that a device has as a destination or source. In the case of, that device is the standard input - usually the keyboard. However, the stream can also have other sources or destinations: If we later deal with saving and loading files, then the destination and source of the stream will be a file.

The following example reads a character from standard input and outputs it. You may have to press after entering the character for anything to happen at all. This is because the standard input is usually read in line by line and not character by character.

intc; c = getchar (); putchar (c);

If we enter "hello" on the keyboard, we first get the first character (ie the "h") by calling up. Calling up again gives us the next character, and so on. The function is, so to speak, the reverse of: It returns a single character c on standard output. Usually the standard output is the monitor.

Admittedly, there is little point in using here, unless you intend to read only the first character of an entry. Often used with bows. We will get to know an example of this later.

Escape sequences [edit]

Control characters benefit from a special representation in C. Control characters are characters that are not directly visible on the screen, but rather fulfill a specific task, such as starting a new line, displaying the tab character or outputting a warning signal. For example, leads

printf ("This is a text"); printf ("without line break");

not to the result that a new line is started after the word "text", but the program outputs after compilation:

This is text without a line break

A new line is only started if there is a in the corresponding place. The following list shows all escape sequences available in C:

  • (new line) = moves the cursor to the starting position of the next line.
  • (horizontal tab) = Moves the tabulator to the next horizontal tabulator position. If the cursor has already reached the last tabulator position, then the behavior is unspecified (provided a last tabulator position exists).
  • (alert) = issues an audible or visual alarm without changing the position of the cursor
  • (backspace) = Resets the cursor one character. If the cursor is already at the beginning of the line, the behavior is unspecified.
  • (carriage return) = Places the cursor at the beginning of the line
  • (form feed) = Places the cursor on the start position of the next page.
  • (vertical tab) = Moves the cursor to the next vertical tab position. If the cursor has already reached the last tab position, then the behavior is unspecified (if one exists).
  • "is issued
  • 'is output
  • ? is issued
  • \ is output
  • is the end marker of a character string

Each escape sequence symbolizes a character on an implementation and can be stored in a variable of the type.


#include intmain (void) {printf ("The line break occurs \ n"); printf ("by the escape sequence \ n \ n \ n"); printf ("In the following a carriage return ( carriage return) with \ r generated: \ r "); printf (" Start of sentence \ n \ n "); printf (" The following output demonstrates the function of \ b \ n "); printf (" 12 \ b34 \ b56 \ b78 \ b9 \ n "); printf (" This is readable \ n \ 0 and no longer. "); / * generates a compiler warning if necessary * / return0;}

Generates the following output on the screen:

The line break occurs with the escape sequence \ n start of record a carriage return is generated with \ r: The following output demonstrates the function of \ b 13579 This is readable