Programming in C. Getting Started with C" 6. Simple Outputs and Inputs

When you take the time to consider it, a computer will be useless without some way to talk to the people who use it. Just as we need information in order to accomplish tasks, so do computers. Just as we provide information to others in order tothey arecan accomplish tasks just as well as computers.

The supply and return of this information to the computer is calledimportationrespond in singingexports"Input" is information provided to a computer or program. "Input" is information provided to a computer or program. "Output" is the information provided by the computer or program. Often, computer programmers will focus their discussions on the more general termsInput/Outputor simplyIn I/OThe

In C, there are many different ways for a program to communicate with the user. Surprisingly, the simplest methods usually taught to beginning programmers can also be the most powerful. InHello, world!In the example at the beginning of this article, we introduced a standard library filestdio.h, and one of its functions.printf(). Here we discuss morestdio.hGive us the features.

utilizationprintf()exports

Recall the demo program at the beginning of this article, repeated as follows:

#include  <stdio.h>

int main (void )
printf ("Hello, World!" ); {
     printf ("Hello, World!");; { printf ("Hello, World!") 
     come (or go) back 0 ; 
}

If you compile and run this program, you will see the following sentence on your screen:

Hello, world!

By using thefunctionality This amazing achievement was realizedprintf()The Functions are like a "black box" that can do things for you without exposing the inner workings. We can write our own functions in C, but we'll get to that later.

You have seenprintf()Use a text enclosed in quotation marks between the brackets. We will enclose the quotedcopiescall sth (by a name)text string(or just astring (computer science)), we will call this string the printf'sparametersThe

As a note of explanation, it is sometimes convenient to include open and right parentheses after a function name to remind us that it is indeed a function. However, this is usually not necessary in understanding the names of the functions we are discussing.

As you can see in the example above, using theprintf()It can be as simple as typing some text, enclosed in double quotes (note that these are double quotes and not two single quotes). So, for example, you can add a text file to a text file by placing it as a parameter to theprintf()function to print any string:

printf("This sentence prints what you see at ......");;

Once it is included in an appropriatemain()function in which it will be displayed:

The sentence will print out as you see it ......

Printing numbers and escape sequences

Placeholder code

ought toprintf()function is a powerful function, probably the most commonly used function in C programs.

For example, let's look at a problem. Suppose we want to calculate: 19 + 31.Let's use C to get the answer.

We started writing.

#include  "stdio.h"// This is important because printf
                   //If this title is not present, it cannot be used

int main (void )
{
    printf ("19 + 31 is");;

But here we're stuck!printf()Only strings are printed! Thankfully, printf has methods for printing numbers. What we do is place the string in theplaceholderFormat code. We write:

    printf ("19 + 31 is '''%d'''' , 19 + 31 );

placeholder%dLiterally "preserving the position of the actual number", which is the result of adding 19 to 31.

These placeholders are calledformat specifier. Many other format descriptors can be usedprintf(). If we have a floating point number, we can%fUsed to print floating point numbers, decimal points and all. Other format descriptors are:

  • %d - int (same as %i)
  • %ld - long int (same as %li)
  • %f - float
  • %lf- pair[1]
  • %c - char (computing)
  • %s - string
  • %x - hexadecimal

A complete list of all formatting descriptors for theprintf()is a printf format string.

Tabs and Line Breaks

If, on the other hand, we want to implement some output that looks like:

   1905 
  312 + 
  -----

printf()There will be no line breaks at the end of each statement: we have to do that ourselves. But how about this?

What we can do is use line breaksescape character. Escape characters are special characters that we can write, but will perform special actions on the screen, such as beeping, writing tabs, etc. Write a new line that we write\n. All escape characters begin with a backslash.

So to achieve the output above, we write

    printf ("1905 \ n 312 + \ n ----- \ n ").

Or to be clearer, we can break this long printf statement in a few lines. So our plan will be

#include  <stdio.h>

int main (void )
printf ("1905")
    printf ("1905 \ n "); printf ("312 + ")
    printf ("312 + \ n ").
    printf ("----- \ n "); printf ("%d", 1905 + 312)
    printf ("%d", 1905 + 312). 
    come (or go) back 0 ; 
}

There are other escape characters that we can use. Another common one\tfor writing tabs. You can\aIt is used to ring the computer's bell, but you should not use it in your program because overuse of sound is not very user-friendly.

Other output methods

puts()

puts()This function is a very simple way to send strings to the screen when you don't have placeholders or variables to focus on. It works in the same way asprintf()The functionality we see in "Hello, World!" is very similar. Example:

    puts ("Print this string."); puts ("Print this string."); puts ("Print this string.") ).

will print to the screen:

  Prints this string.

followed by a line break (as described above). (Theputsfunction appends newlines to its output.)

utilizationscanf()importation

ought toscanf()function is the same as theprintf()Equivalent input methods for output functions - simple but powerful. In the simplest call, scanf format stringContains aplaceholders.Indicates the type of value the user will enter. These placeholders are the same as theprintf()The functions are roughly the same - %dFor integers, the%fFloating point and%lfDouble precision numbers.

However, in contrast to thescanf()In comparison, there is a changeprintf(). Thescanf()function requires the memory address of the variable where you want to save the input value. Although it is possible to use thepointer on a gauge(variables that store memory addresses), but this concept is not mentioned until later in the text. Instead, the simple technique is to useaddressoperator (computing)&. Now, before we discusspointer on a gaugeIt's best to think about this "magic" before you do.

A typical application might look like this:

#include  "stdio.h"

int main (void )
int main (void )
    int a ;

    printf ("Please enter an integer value: " ); scanf ("%d", &a ); printf ("You entered: %d", &a ); printf ("You entered: %d", &a )
    scanf ("%d", &a); printf ("You have typed: %d \ n ", a).
     

    come (or go) back 0 ; 
}

If you want to describescanf()The effect of the above function call, which might read, "Read an integer from the user and store it in the variableaof the address."

If you want to enter astring (computer science)utilizationscanf functionYou should.(negative prefix)Including with operators. The following code will generate a runtime error and the program may crash.

    scanf ("%s", &a);;

The correct usage is:

    scanf ("%s", a );

This is because whenever you use string (%s) format descriptor, the variables used to store values will be an array, and the array name (-a in this case) itself indicates their base address, so there is no need for the operator'saddressThe

(Although, it is susceptible to buffer overflows.fgets()Preferredscanf()).

Notes on Inputs: When data is typed on the keyboard, the information is not directly displayed in the running program. It is first stored in the so-calledbuffer - A small amount of memory reserved for an input source. When a program wants to read from an input source, it sometimes leaves data in the buffer, and thescanf()The function will read that data instead of waiting for the user to enter something. Some people may suggest that you use this functionfflush(stdin)This may work as expected on some computers, but is not considered good practice, as you will see later. If you take your code to another computer with a different compiler, then performing this action will fail and your code may not work properly.

Guess you want to read:Programming in C. Getting Started with C" 7. Operators and Type Casting

THE END
share (joys, benefits, privileges etc) with others