Input and output of data

Source: Internet
Author: User


character output function Putchar  

int Putchar (int c)

Function parameters:C is a character constant or an expression

function return value: The character of the output

function function: Display one character on standard output


Formatted output function printf

int printf (const char *format, ... ), the number of bytes that successfully returned the output, and the failure to return 1(EOF)


Character input function GetChar

int getchar (void)

the meaning of the return value is to store the characters read from the keyboard, and the type of the return value is indeed int, not char

int main ()

{

int ch;

printf ("Enter a Character:");

While ((ch = getchar ())! = EOF)

printf ("%c--->% #x \ n", CH, ch);

printf ("End main\n");

return 0;

}

Format input function scanf

int scanf (const char *format, ... ), the number of variables entered successfully returned, failed to return 1(EOF)

When you enter data, the data is considered to be the end of the following scenario:

    - spaces, TAB or enter

    - width End

    - Illegal input

Illegal input, such as: The program needs to enter a floating-point number, the user entered the letter, this property is illegal input

The return value is the number of variables entered successfully, and when illegal input is encountered, the return value is less than the number of actual variables

int main ()

{

int A, b, N;

printf ("Input numbers:");

While ((n = scanf ("%d%d", &a, &b)) = = 2)

    {

printf ("a=%d, b=%d\n", A, b);

printf ("Input numbers:");

    }

printf ("n=%d\n", n);

return 0;

}


string input and OUTPUT functions

1. string output function puts

int puts (const char *s), which displays the string s on the standard output , successfully returns a number other than 0 ; Back -1 or EOF

int main ()

{

Char s[]= "welcome";

puts (s);  

return 0;

}

2. string input function gets

Char *gets (char *s)

function: Enter a string that ends with a carriage return from the keyboard into the character array and automatically add ' / '

successful return of the start address of the character array, failed or input end returned NULL .

#define N

int main ()

{

int i = 0;

char S[n] = {0};

 

printf (">");

While (gets (s) = NULL)

    {

printf (">");

//scanf ("%s", s);

printf ("i =%d:%s\n", I, s);

i++;

    }

 

printf ("End main\n");

return 0;

}

The parameter of the gets function does not contain the length control. When entering characters, you can enter a maximum of N-1(Terminator, which keeps a string in place ). If the input character exceeds N-1, the extra symbol is also stored in the character array, which causes the memory to be accessed out of bounds and the result is unpredictable. Therefore, when you use this function, there is a warning. When we use this function, we must pay attention to the length of the array.




This article is from the "Record the Study" blog, please be sure to keep this source http://sjgwyy.blog.51cto.com/3830494/1829324

Input and output of data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.