puts

puts()函式用來向標準輸出設備(螢幕)輸出字元串並換行,具體為:把字元串輸出到標準輸出設備,將'\0'轉換為回車換行。其調用方式為,puts(s);其中s為字元串字元(字元串數組名或字元串指針)。

基本介紹

  • 外文名:puts
  • 功 能: 送一字元串到流stdout中
  • 用 法:int puts(const char *string);
  • 調用方式為:puts(s);
功 能,用 法,程式例,說明,

功 能

: 送一字元串到流stdout中

用 法

int puts(const char *string);

程式例

:
#include <stdio.h>int main(void){    char string[] = "This is an example output string\n";    puts(string);    return 0;}
初學者要注意以下例子
#include <stdio.h>#include <conio.h>int main(void){    int i;    char string[20];    for(i=0;i<10;i++)        string[i]='a';    puts(string);    getch();    return 0;}
從此例中可看到puts輸出字元串時要遇到'\0’也就是字元結束符才停止,如上面的程式加上一句 string[10]='\0';
#include <stdio.h>#include <conio.h>int main(void){    int i;    char string[20];    for(i=0;i<10;i++)        string[i]='a';    string[10]='\0';    puts(string);    getch();    return 0;}
運行就正確了

說明

:
(1). puts()函式只能輸出字元串, 不能輸出數值或進行格式變換。
(2). 可以將字元串直接寫入puts()函式中。如:
puts("Hello, world!");
(3). puts 和 printf的用法一樣,puts()函式的作用與語句“printf("%s\n",s);的作用相同。注意:puts在輸出字 符串後會自動輸出一個回車符。
puts()函式的一種實現方案如下:
int puts(const char * string) {     const char * t = string;     const char * v = string;     int i = 0;     while(*t!='\0')     {         i++;         t++;     }     int j = 0;     for(j;j<=i;j++)         putchar((v[j]));     putchar('\n');    return 0; }

相關詞條

熱門詞條

聯絡我們