C Primer Plus匯總 (二)錯誤筆記

來源:互聯網
上載者:User

1、printf("%s! c is cool!\n","%s! c is cool!\n");          

#include <stdio.h> #define FORMAT "%s! C is cool!\n" int main(void) { int num=10; printf(FORMAT,FORMAT); return 0; }

輸出為

%s!C is cool! !C is cool!

將#define FORMAT "%s!C is cool!\n"
代入程式得到:printf(FORMAT,FORMAT);
變為:printf("%s!C is cool!\n","%s!C is cool!\n");
其中,後面的"%s!C is cool!\n"
整體作為一個字串被帶入最左邊的%s,
所以輸出%s!C is cool!(斷行符號)
//其實這是最右邊的那個字串!C is cool!
//其實這是最左邊的那個字串除了%s後剩下的內容

2、printf("%d\v",n);奇怪的輸出結果

 

#include <stdio.h>int main(void){int m,n;scanf("%d",&n);m=n+5;while(n++<m)printf("%d\v",n);}

輸出為什麼是亂碼?

http://tieba.baidu.com/p/1399157448

 

輸出

 

 

 

VS2010中

 

 

    TXT文檔:

 

 

 

WORD中

 

使用函數要先給變數初始化

#include <stdio.h>float sbwangyuxi(float x,float y);int main(void){ float a,b;  sbwangyuxi(a,b); return 0;}float sbwangyuxi(float x,float y){ float c; while(scanf("%f,%f",&x,&y)) {  c=(x-y)/(x*y);  printf("%f\n",c); }}

上面這個怎麼運行怎麼錯誤,最後給主函數裡的變數賦值
在主函數裡加入a=0,b=0;才能成功運行

#include <stdio.h>float sbwangyuxi(float x,float y);int main(void){ float a,b; a=0,b=0; sbwangyuxi(a,b); return 0;}float sbwangyuxi(float x,float y){ float c; while(scanf("%f,%f",&x,&y)) {  c=(x-y)/(x*y);  printf("%f\n",c); }}

if  '\"'==' "'

C PRIMER PLUS 第5版165頁例題chcount.c

#include<stdio.h>#define PERIOD '.'int main(void){    int ch;    int charcount=0;    while((ch=getchar())!=PERIOD)    {        if(ch!='"'&&ch!='\'')          charcount++;    }    printf("There are %d non-quote characters.\n",charcount);    return 0;}

注意到

if(ch!='"'&&ch!='\'')改為if(ch!='\"'&&ch!='\'')或if(ch!='\"'&&ch!=''')輸出結果相同 疑問else break;

#include<stdio.h>#include<ctype.h>int main(void){ char ch;   while(1) {   ch=getchar();  if(isalpha(ch))      putchar(ch);        else           break; } printf("你輸入的不是字母");  return 0; } 
為什麼只迴圈一次?  

\n被悲劇的抓住了 然後作為祭品召喚出了else break 擊敗了while大魔王

 

http://tieba.baidu.com/f?ct=335675392&tn=baiduPostBrowser&sc=17322832971&z=1418062378

 

#include<stdio.h>#include<ctype.h>int main(void){ char ch;   while(1) {   ch=getchar();  if(isalpha(ch)||ch=='\n')      putchar(ch);        else      break; } printf("你輸入的不是字母");  return 0; } 

//這之後犯了個毛病有問題不思考就提問,唉

小失誤if並不是結束

 

#include<stdio.h>int main(void){    int _a,_b,_c;    char ch;    _a=_b=_c=0;    while((ch=getchar())!='#')      {          if(ch==' ')             _a++;           if(ch=='\n')             _b++;          else            _c++;      }      printf("讀取的空白字元為 %d,換行字元為 %d,其它字元為 %d.",_a,_b,_c);      return 0;
}

輸出 為   讀取的空白字元為 1,換行字元為 1,其它字元為 4.

在第2個if前加上else變成如下後
#include<stdio.h>int main(void){    int _a,_b,_c;    char ch;    _a=_b=_c=0;    while((ch=getchar())!='#')      {          if(ch==' ')             _a++;           else           if(ch=='\n')             _b++;          else            _c++;      }      printf("讀取的空白字元為 %d,換行字元為 %d,其它字元為 %d.",_a,_b,_c);      return 0;}

讀取的空白字元為 1,換行字元為 1,其它字元為 3.

 由於對if分支語句的使用失誤造成在輸入第一個空格時,變數ch進入第二個if裡進行判斷屬於else於是_c++, 例題為C PRIMER PLUS 第五版第7章課後編程練習第1題

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.