Usage of puts () putc () getchar () gets () getch () getche () in C language, putcgetche

Source: Internet
Author: User

Usage of puts () putc () getchar () gets () getch () getche () in C language, putcgetche

1. puts () Output string, stops when '\ 0' is encountered, and converts' \ 0' into a carriage return line break

Eg:

#include
 
  int main(){    char a[] = "this is an example";    puts(a);    return 0;}
 

It is also very understandable that the character string ends with '\ 0', so the output result of the above example will end with a line break. Note the following example:
Eg:

# Include
 
  
Int main () {int I; char a [20]; for (I = 0; I <20; I ++) {a [I] = 'B ';} // stop when '\ 0' is encountered, and wrap a [10] =' \ 0'; puts (a); return 0 ;}
 

In the above example, only 10 B is output, and a line break occurs because '\ 0' is encountered'

2. putc () is also output. It can be used for character arrays. However, only one character is output.
Eg:

#include
 
  int main(){    char a[] = "hello world\n";    int i = 0;    putc(a[i],stdout);    printf("\n");    while(a[i]){        putc(a[i++],stdout);    }    return 0;}
 

In the above example, the first putc outputs the character h, and the second putc outputs the hello world line feed.

3. getchar () can be a string of characters, but only the first character is returned.
Eg:

# Include
 
  
Int main () {char ch; ch = getchar (); printf ("the input character is: % c", ch); return 0 ;}
 

After running the preceding example, enter abc and press Enter. The value of ch is only a, and only a is displayed on the screen.

4. gets () input a string to the character array from the terminal and obtain a function value. The function value is the starting address of the character array, the gets function is generally used to input a string to the character array, regardless of the function value.
Eg:

# Include
 
  
Int main () {char a [20]; gets (a); printf ("the input string is % s \ n", );}
 

* 5. The getch () and getche () functions are used to enter a single character. The input of the former is not displayed on the screen, and the characters entered by the latter are displayed on the screen.

Note: add the header file conio. h to the two functions.

Getch ()
Eg:

# Include
 
  
# Include
  
   
Int main () {char ch; ch = getch (); printf ("the input character is: % c", ch); return 0 ;}
  
 

Getche ()
Eg:

# Include
 
  
# Include
  
   
Int main () {char ch; ch = getche (); printf ("the input character is: % c", ch); return 0 ;}
  
 

Well, run the code on your own and you will understand it.

Related Article

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.