當前位置

首頁 > 商務英語 > 計算機英語 > c語言中putchar的用法

c語言中putchar的用法

推薦人: 來源: 閱讀: 3.08W 次

c語言中putchar的用法的用法你知道嗎?下面小編就跟你們詳細介紹下c語言中putchar的用法的用法,希望對你們有用。

ing-bottom: 100%;">c語言中putchar的用法

  c語言中putchar的用法的用法如下:

單個字符輸入輸出函數

putchar函數(單個字符輸出函數)

例 putchar函數的格式和使用方法。

1 #include "stdio.h"

2 void main()

3 {

4 char ch1='N', ch2='E', ch3='W';

5 putchar(ch1); putchar(ch2); putchar(ch3);

6 putchar('n');

7 putchar(ch1); putchar('n');

8 putchar('E'); putchar('n');

9 putchar(ch3); putchar('n');

10 }

程序運行結果如下:

NEW

N

E

W

1. putchar函數的格式:putchar(ch)

其中ch可以是一個字符變量或常量,也可以是一個轉義字符。

2.putchar函數的作用:向終端輸出一個字符。

(1)putchar函數只能用於單個字符的輸出,且一次只能輸出一個字符。

(2)在程序中使用putchar函數,務必牢記:在程序(或文件)的開頭加上編譯預處理命令(也稱包含命令),即:#include "stdio.h"。

getchar函數(單個字符的輸入函數)

例 說明getchar函數的格式和作用。

1 #include "stdio.h"

2 void main()

3 {

4 char ch;

5 printf("Please input two character: ");

6 ch=getchar();

7 putchar(ch);putchar('n');

8 putchar(getchar());

9 putchar('n');

10 }

程序運行情況如下:

Please input two characters: ab↙(注意輸入方法)

a

b

1. getchar函數的格式:c=getchar();

2.getchar函數的作用:從系統隱含的輸入設備(如鍵盤)輸入一個字符。

getchar函數只能用於單個字符的輸入,一次輸入一個字符。程序的功能是輸入一個字符,顯示一個字符,回車換行,再輸入並顯示一個字符。而運行時字符是連續輸入的,運行結果卻是正確的,這是因爲輸入字符後,它們暫存於鍵盤的緩衝區中,然後由getchar函數從鍵盤緩衝區中一個一個的取出來。使用getchar函數時,在程序(或文件)的開頭也必須加上編譯預處理命令:#include "stdio.h"

getchar輸入字符,一直到按下回車才結束,然後執行代碼

getch不用按回車鍵

#include "stdio.h"

main()

{

int i=5;

void palin(int n);

printf("40:");

palin(i);

printf("n");

}

void palin(int n)

{

char next;

if(n<=1)

{

next=getchar();

printf("n