c語言學習心得

 enum {jan=0,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
 int yearearn,monthearn;
 int n;
 for(yearearn=0,n=0;n<=11;n++)
 {
 switch(n)
 {
 case jan:printf("january:\n");break;
 case feb:printf("february:\n");break;/////使用枚舉類型。。

寫程序時要淡定,不要因爲小錯誤而慌張,認真檢查,找出錯誤,改正,完善。

c/c++中只能控制輸出的格式,而不能控制輸入的格式,         
  scanf("%6.3f")       cin>>setw(4)........都是錯的。

四捨五入標準函數式:float  b;int  a; a=(b+0.5)

break  —— 終止循環,不再執行該循環。
return  —— 終止函數
scanf  —— {,&n}除了字符串爲{,str}外
隨機數  —— rand()%n
continue  —— 用於跳過本次循環餘下的語句,轉向下一次循環

位運算
左移一位相當於乘以10倍(或2倍)-------11——》110
因此:110<<1==1100, 111000>>2==110
二進制中左移n位,相當於乘以2^n.


數組的輸入是:兩個元素之間空格

i=a>b?a:b
 

定義宏時 #define max x>y?x:y
使用是max;


fatal error C1004: unexpected end of file found 的錯誤原因爲少了花括號'}'。
  
注意數據類型不可搞錯。

最大公約數 int d(int a,int b){int r;while((r=a%b)!=0){a=b;b=r;}return b;}
最小公倍數int m(int a,int b){int x;x=d(a,b);return (a*b)/x;}
交換順序 {t=a;a=b;b=t;}


cannot open Debug 原因是有一個c語言程序在運行,沒有關閉。


error C2143: syntax error : missing ']' before '['
 error C2133: 'end' : unknown size
 error C2664: 'gets' : cannot convert parameter 1 from 'char [][80]' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
 error C2664: 'gets' : cannot convert parameter 1 from 'char [][80]' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
錯誤原因:end[[80]

error C2059: syntax error : '}'
錯誤原因:do{while(1);}

 

Compiling...
指針方式調用函數的程序.cpp
error C2440: '=' : cannot convert from 'float (__cdecl *)(float [],int)' to 'float (__cdecl *)(void)'This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2197: 'float (__cdecl *)(void)' : too many actual parameters
指針方式調用函數的程序.exe - 2 error(s), 0 warning(s)
--**&&**--錯誤原因:{float sumf,sump;
float a[M]={11,2,-3,4.5,69,7,80,780};
float (*p)();
float max(float a[],int n);
應改爲float sumf,sump;
float a[M]={11,2,-3,4.5,69,7,80,780};
float (*p)(float *,int);
float max(float a[],int n);


若沒有括號,則for僅包括靠近它的語句。
for (;*p!='\0';p++,q++){ *q=*p;    };*q='\0';等價於for(;*p!='\0';p++,q++)*q=*p;*q='\0';


動態數組解決了傳統靜態數組的缺陷。
結構體(將一些基本數據類型組合在一起)可以表示一些基本數據類型無法表示的複雜的事物
(*pt).x=pt->x;
pt=&p;
*pt=p;
定義時*pt=&p;


error C2448: '<Unknown>' : function-style initializer appears to be a function definition
問題出在:定義與引用時函數的參數應相同。如: 聲明時void list(struct stud_type student);
引用時若void list(student)則出現錯誤。應該void list(struct stud_type student).

void list(student)
struct stud_type student;//應去掉‘;’。
{
 printf("%-20s%8ld%6d%3c\n",student.name,student.num,student.age,student.sex);
}

fatal error C1004: unexpected end of file found

 

error C2039: 'im' : is not a member of 'complex'
原因是struct complex
{float re,m;
};少了‘i’。應改爲struct complex{float re,im;};


c++中也可以用<conio.h>getch();


setw(n)包含在#include<iomanip.h>頭文件中。

 

Linking...
LINK : fatal error LNK1168: cannot open Debug/用c++求平均成績的程序.exe for writing
執行 link.exe 時出錯.
原因是。。。。:有另外的c程序在運行,沒有關閉。


putchar('字符')-----------------puts("字符串")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章