1. Cèšèªãšã¯ïŒ
Cèšèªã¯ã1970幎代åé ã«AT&Tãã«ç 究æã§ããã¹ã»ãªãããŒã«ãã£ãŠéçºãããããã°ã©ãã³ã°èšèªã§ãããã®ååã¯ãå身ãšãªãèšèªãBèšèªãããé²åããããšã瀺ããŠããŸãã
1.1 Cèšèªã®æŽå²ãšç¹åŸŽ
Cèšèªã¯ãUNIXãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®éçºã®ããã«çãŸããŸãããUNIXãšãšãã«ãCèšèªã¯æ¥éã«æ®åããå€ãã®ãã©ãããã©ãŒã ãã¢ããªã±ãŒã·ã§ã³ã®éçºã«äœ¿çšãããããã«ãªããŸãããCèšèªã®äž»ãªç¹åŸŽãšããŠã以äžã®ç¹ãæããããŸãã
- ã·ã³ãã«ã§ç°¡æœãªæ§æ
- é«ãããŒã¿ããªãã£
- çŽæ¥çãªã¡ã¢ãªã¢ã¯ã»ã¹
- é«ãå®è¡é床
ã·ã³ãã«ãªæ§æã¯ãåå¿è ã«ãšã£ãŠåŠã³ããããšããã¡ãªããããããŸãããŸããé«ãããŒã¿ããªãã£ã¯ãããŸããŸãªããŒããŠã§ã¢ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã®åäœãä¿èšŒããããã®ç¹åŸŽã§ããçŽæ¥çãªã¡ã¢ãªã¢ã¯ã»ã¹ã¯ãéçºè ãããã°ã©ã ã®åäœã现ããå¶åŸ¡ããããšãå¯èœã«ããŸããããã«ãããé«ãå®è¡é床ãå®çŸããããšãã§ããŸãã
1.2 Cèšèªãéžã°ããçç±
Cèšèªãå€ãã®éçºè ã«éžã°ããçç±ã¯ããã®åŒ·åãªæ©èœãšæè»æ§ã«ãããŸããç¹ã«ãã·ã¹ãã ããã°ã©ãã³ã°ãçµã¿èŸŒã¿ã·ã¹ãã ã®éçºã«ãããŠãCèšèªã®èœåã¯éåžžã«äŸ¡å€ããããŸãã
å©ç¹ | 説æ |
---|---|
å¹ççãªã³ãŒã | Cèšèªã¯ãæé©åãããã³ã³ãã€ã©ã«ãã£ãŠé«éãªå®è¡ã³ãŒããçæããããšãã§ããŸãã |
ããŒã¿ããªã㣠| å€ãã®ãã©ãããã©ãŒã ã§åäœããã³ãŒããæžãããšãã§ããŸãã |
çŽæ¥çãªããŒããŠã§ã¢ã¢ã¯ã»ã¹ | ããŒããŠã§ã¢ã®ç¹å®ã®æ©èœãçŽæ¥å©çšããããšãã§ããŸãã |
ãããã®å©ç¹ãèæ ®ãããšãCèšèªã¯å€ãã®ã¢ããªã±ãŒã·ã§ã³ãã·ã¹ãã ã®éçºã«ãããŠãæé©ãªéžæãšãªãããšãããããŸãã
2. Cèšèªã®åºæ¬çãªæ§æ
Cèšèªã¯ãã®ã·ã³ãã«ããšå¹çæ§ããå€ãã®ããã°ã©ããŒã«æãããŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãCèšèªã®åºæ¬çãªæ§æã«çŠç¹ãåœãŠãå€æ°ãããŒã¿åãæŒç®åããããŠäž»èŠãªå¶åŸ¡æ§æã«ã€ããŠè§£èª¬ããŸãã
2.1 å€æ°ãšããŒã¿å
Cèšèªã«ãããå€æ°ã¯ãããŒã¿ãæ ŒçŽããããã®ååä»ãã®ã¡ã¢ãªé åãæããŸããå€æ°ã䜿çšããåã«ããã®åã宣èšããå¿ èŠããããŸãããã®åã¯ãå€æ°ãä¿æã§ããããŒã¿ã®çš®é¡ãæå®ããŸãã
äŸãã°ãæŽæ°ãæ ŒçŽããããã®å€æ°ã¯ãintãåãšããŠå®£èšãããå®æ°ãæ ŒçŽããããã®å€æ°ã¯ãfloatãåãšããŠå®£èšãããŸãã以äžã¯ãCèšèªã§ã®äž»èŠãªããŒã¿åã®äžéšã§ãã
ããŒã¿å | 説æ |
---|---|
int | æŽæ°ãæ ŒçŽããããã®å |
float | å粟床浮åå°æ°ç¹æ°ãæ ŒçŽããããã®å |
double | å粟床浮åå°æ°ç¹æ°ãæ ŒçŽããããã®å |
char | 1æåãæ ŒçŽããããã®å |
2.2 æŒç®åãšåŒ
Cèšèªã«ã¯ãæ°åŠçãªèšç®ãå€æ°ã®æäœãè¡ãããã®å€ãã®æŒç®åãçšæãããŠããŸãããããã®æŒç®åã¯ãç®è¡ãé¢ä¿ãè«çãä»£å ¥ãªã©ã®ã«ããŽãªã«åããããŸãã
äŸãšããŠãç®è¡æŒç®åã«ã¯å ç®(+), æžç®(-), ä¹ç®(*), é€ç®(/)ãªã©ããããŸãããããã®æŒç®åã䜿çšããŠãæ°å€ãå€æ°ãçµã¿åãããŠåŒãäœæããããšãã§ããŸãã
2.3 å¶åŸ¡æ§æïŒifæãforæãwhileæ
ããã°ã©ã ã®æµããå¶åŸ¡ããããã®æ§æã¯ãCèšèªã«ãããŠéåžžã«éèŠã§ããããã«ã¯ãæ¡ä»¶åå²ãç¹°ãè¿ãåŠçãè¡ãããã®æ§æãå«ãŸããŸãã
ifæã¯ãæå®ãããæ¡ä»¶ãçã®å Žåã«ç¹å®ã®ã³ãŒããããã¯ãå®è¡ããŸããäžæ¹ãforæãwhileæã¯ãæå®ãããæ¡ä»¶ãçã®éãç¹å®ã®ã³ãŒããããã¯ãç¹°ãè¿ãå®è¡ããŸãã
ãããã®å¶åŸ¡æ§æãé©åã«äœ¿çšããããšã§ãããã°ã©ã ã®åäœãæè»ã«å¶åŸ¡ããããšãã§ããŸãã
3. Cèšèªã§ã®é¢æ°ã®å©çš
é¢æ°ã¯ãããã°ã©ã ã®äžã§ç¹å®ã®ã¿ã¹ã¯ãå®è¡ããããã®ã³ãŒãã®ãŸãšãŸããæããŸããCèšèªã«ãããé¢æ°ã®å©çšã¯ãã³ãŒãã®åå©çšæ§ãé«ããããã°ã©ã ã®æ§é ãæŽçããäžã§éåžžã«éèŠã§ãã
3.1 é¢æ°ã®å®çŸ©ãšåŒã³åºã
é¢æ°ã¯ãç¹å®ã®ã¿ã¹ã¯ãå®è¡ããããã®ã³ãŒãã®ãããã¯ãšããŠå®çŸ©ãããŸããé¢æ°ã®å®çŸ©ã«ã¯ãé¢æ°åãæ»ãå€ã®åãåŒæ°ã®ãªã¹ããå«ãŸããŸããé¢æ°ãå®çŸ©ããããšãããã°ã©ã ã®ä»ã®éšåãããã®é¢æ°ãåŒã³åºãããšãã§ããŸãã
äŸãã°ã2ã€ã®æ°å€ãå ç®ããé¢æ°ãèããŠã¿ãŸãããããã®é¢æ°ã¯ã2ã€ã®æŽæ°ãåŒæ°ãšããŠåãåãããã®åãè¿ããã®ãšããŸãã
int add(int a, int b) { return a + b; }
äžèšã®é¢æ°ã¯ãaddãšããååã§å®çŸ©ãããŠããã2ã€ã®æŽæ°ãåŒæ°ãšããŠåãåããæŽæ°ãæ»ãå€ãšããŠè¿ããŸãã
3.2 åŒæ°ãšæ»ãå€
é¢æ°ã¯ã0å以äžã®åŒæ°ãåãåãããšãã§ããŸãããããã®åŒæ°ã¯ãé¢æ°ãå®è¡ãããéã«å€éšããæž¡ãããããŒã¿ãæããŸããé¢æ°ã¯ãèšç®ãåŠçã®çµæãšããŠ1ã€ã®å€ãè¿ãããšãã§ããŸãããã®è¿ãããå€ãæ»ãå€ãšåŒã³ãŸãã
é¢æ°ã®æ»ãå€ã®åã¯ãé¢æ°ã®å®çŸ©æã«æå®ãããŸããæ»ãå€ããªãå Žåãé¢æ°ã®åã¯ãvoidããšãªããŸãã
3.3 ååž°é¢æ°ã®åºç€
ååž°é¢æ°ã¯ãèªåèªèº«ãåŒã³åºãé¢æ°ãæããŸããååž°ã¯ãç¹å®ã®åé¡ã解決ããããã®åŒ·åãªææ³ãšããŠç¥ãããŠããŸããããããååž°é¢æ°ã®äœ¿çšã«ã¯æ³šæãå¿ èŠã§ããç¡éã«èªåèªèº«ãåŒã³åºããšãããã°ã©ã ã¯çµäºããªããªãå¯èœæ§ããããŸãã
ååž°ã®å žåçãªäŸãšããŠãéä¹ã®èšç®ãæããããŸããnã®éä¹ã¯ã1ããnãŸã§ã®å šãŠã®æŽæ°ã®ç©ãšããŠå®çŸ©ãããŸãããã®èšç®ã¯ãååž°ã䜿çšããŠå¹ççã«è¡ãããšãã§ããŸãã
int factorial(int n) { if (n <= 1) { return 1; } return n * factorial(n - 1); }
äžèšã®é¢æ°ã¯ãåŒæ°ãšããŠåãåã£ãæŽæ°ã®éä¹ãèšç®ããŠè¿ããŸããé¢æ°å ã§ãé¢æ°èªèº«ãååž°çã«åŒã³åºãããŠããŸãã
4. Cèšèªã«ãããé åãšãã€ã³ã¿
Cèšèªã«ãããŠãããŒã¿ã®éåãã¡ã¢ãªã®çŽæ¥çãªæäœã¯ãé åãšãã€ã³ã¿ã䜿çšããŠè¡ãããŸãããããã®æŠå¿µã¯ãCèšèªã®åŒ·åããšæè»æ§ã®æºæ³ãšãªã£ãŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ããããã®åºæ¬çãªæŠå¿µãšãã®æŽ»çšæ¹æ³ã«ã€ããŠè§£èª¬ããŸãã
4.1 é åã®åºæ¬
é åã¯ãåãåã®è€æ°ã®ããŒã¿ãé£ç¶çãªã¡ã¢ãªé åã«æ ŒçŽããããã®ããŒã¿æ§é ã§ããé åã®åèŠçŽ ã¯ãã€ã³ããã¯ã¹ã䜿çšããŠã¢ã¯ã»ã¹ãããŸããCèšèªã«ãããé åã®ã€ã³ããã¯ã¹ã¯ã0ããå§ãŸããŸãã
äŸãã°ã5ã€ã®æŽæ°ãæ ŒçŽããããã®é åã¯ä»¥äžã®ããã«å®çŸ©ãããŸãã
int numbers[5];
ãã®é åã«ã¯ãnumbers[0]ããnumbers[4]ãŸã§ã®5ã€ã®èŠçŽ ãå«ãŸããŸããé åã®èŠçŽ ã«ã¯ãéåžžã®å€æ°ãšåæ§ã«ããŒã¿ãä»£å ¥ãããããã®å€ãååŸãããããããšãã§ããŸãã
4.2 ãã€ã³ã¿ã®ç解ãšæŽ»çš
ãã€ã³ã¿ã¯ãã¡ã¢ãªã®ã¢ãã¬ã¹ãæ ŒçŽããããã®å€æ°ã§ãããã€ã³ã¿ã䜿çšããããšã§ãã¡ã¢ãªã®ç¹å®ã®äœçœ®ã«çŽæ¥ã¢ã¯ã»ã¹ããããããŒã¿ã®åç §ãæž¡ãããããããšãã§ããŸãã
ãã€ã³ã¿ã®å®£èšã«ã¯ãã¢ã¹ã¿ãªã¹ã¯(*)ã䜿çšããŸãã以äžã¯ãæŽæ°ã®ã¢ãã¬ã¹ãæ ŒçŽããããã®ãã€ã³ã¿ã®äŸã§ãã
int *p;
ãã®ãã€ã³ã¿ã¯ãæŽæ°å€æ°ã®ã¢ãã¬ã¹ãæãããšãã§ããŸããã¢ãã¬ã¹ã®ååŸã«ã¯ãã¢ã³ããµã³ã(&)ã䜿çšããŸãããŸãããã€ã³ã¿ãéããŠããŒã¿ã«ã¢ã¯ã»ã¹ããããã«ã¯ãã¢ã¹ã¿ãªã¹ã¯ã䜿çšããŸãã
4.3 æååã®æäœ
Cèšèªã«ãããæååã¯ãæåã®é åãšããŠè¡šçŸãããŸããæååã®çµç«¯ã¯ããã«æå(' ')ã«ãã£ãŠç€ºãããŸãããã®ãããæååãæ±ãéã«ã¯ããã®çµç«¯æåãèæ ®ããå¿ èŠããããŸãã
äŸãã°ã"Hello"ãšããæååã¯ã'H', 'e', 'l', 'l', 'o', ' 'ãšãã6ã€ã®æåãããªãé åãšããŠæ ŒçŽãããŸããCèšèªã«ã¯ãstring.hãšããããããã¡ã€ã«ã«ãæååã®æäœãè¡ãããã®å€ãã®é¢æ°ãçšæãããŠããŸãããããã®é¢æ°ã䜿çšããããšã§ãæååã®æ¯èŒãã³ããŒãé£çµãªã©ã®æäœãå¹ççã«è¡ãããšãã§ããŸãã
5. Cèšèªã§ã®ãã¡ã€ã«æäœ
Cèšèªã䜿çšããŠãå€éšã®ãã¡ã€ã«ã«ã¢ã¯ã»ã¹ããããŒã¿ã®èªã¿æžããè¡ãããšãã§ããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãCèšèªã«ãããåºæ¬çãªãã¡ã€ã«æäœã®æ¹æ³ãšããšã©ãŒåŠçã«ã€ããŠè§£èª¬ããŸãã
5.1 ãã¡ã€ã«ã®èªã¿æžã
Cèšèªã«ããããã¡ã€ã«ã®æäœã¯ãFILEåã®ãã€ã³ã¿ã䜿çšããŠè¡ãããŸãããã¡ã€ã«ãéãã«ã¯ãfopené¢æ°ã䜿çšããŸãããã®é¢æ°ã¯ããã¡ã€ã«åãšã¢ãŒããåŒæ°ãšããŠåãåãããã¡ã€ã«ãžã®ãã€ã³ã¿ãè¿ããŸãã
äŸãã°ãããã¹ããã¡ã€ã«ãèªã¿åãã¢ãŒãã§éãå Žåã以äžã®ããã«ã³ãŒããèšè¿°ããŸãã
FILE *fp; fp = fopen("sample.txt", "r");
ãã¡ã€ã«ã®èªã¿æžãã«ã¯ãfscanfãfprintfãªã©ã®é¢æ°ã䜿çšãããŸãããããã®é¢æ°ã¯ãscanfãprintfãšéåžžã«äŒŒãåäœãããŸããããã¡ã€ã«ã®ãã€ã³ã¿ãè¿œå ã®åŒæ°ãšããŠåãåããŸãã
ãã¡ã€ã«æäœãå®äºããããfcloseé¢æ°ã䜿çšããŠãã¡ã€ã«ãéããããšãéèŠã§ããããã«ããã䜿çšããŠãããªãœãŒã¹ã解æŸãããä»ã®ããã°ã©ã ãæäœããã¡ã€ã«ã«ã¢ã¯ã»ã¹ã§ããããã«ãªããŸãã
5.2 ãã¡ã€ã«ã®ãšã©ãŒåŠç
ãã¡ã€ã«æäœäžã«ã¯ãããŸããŸãªãšã©ãŒãçºçããå¯èœæ§ããããŸããäŸãã°ããã¡ã€ã«ãååšããªããã¢ã¯ã»ã¹æš©éããªãããã£ã¹ã¯ã®ç©ºã容éãäžè¶³ããŠãããªã©ã®çç±ã§ãšã©ãŒãçºçããããšãèããããŸãã
Cèšèªã«ã¯ããããã®ãšã©ãŒãæ€åºããé©åã«åŠçããããã®æ©èœãæäŸãããŠããŸããå ·äœçã«ã¯ãferroré¢æ°ã䜿çšããŠããã¡ã€ã«æäœäžã«ãšã©ãŒãçºçãããã©ããã確èªããããšãã§ããŸãã
ãšã©ãŒãçºçããå Žåãperroré¢æ°ã䜿çšããŠããšã©ãŒã¡ãã»ãŒãžã衚瀺ããããšãæšå¥šãããŸããããã«ããããšã©ãŒã®åå ãç¹å®ããé©åãªå¯ŸåŠãè¡ãããšãã§ããŸãã
ãã¡ã€ã«æäœã«é¢ãããšã©ãŒåŠçã¯ãããŒã¿ã®æ倱ãäžæ£ç¢ºãªçµæãé²ãããã«éåžžã«éèŠã§ãããããã£ãŠããã¡ã€ã«ãæäœããéã«ã¯ãåžžã«ãšã©ãŒåŠçãé©åã«å®è£ ããããšãå¿æããã¹ãã§ãã
6. å®è·µçãªCèšèªããã°ã©ãã³ã°
CèšèªãåŠã¶äžã§ãçè«ã ãã§ãªãå®éã®ããã°ã©ã äœæããããã°ã®æè¡ãéåžžã«éèŠã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ããµã³ãã«ããã°ã©ã ã®è§£èª¬ãšããããã°ã®åºæ¬çãªæ¹æ³ã«ã€ããŠçŽ¹ä»ããŸãã
6.1 ãµã³ãã«ããã°ã©ã ã®è§£èª¬
ããã§ã¯ãç°¡åãªCèšèªã®ããã°ã©ã ãäŸã«ããã®åäœãšã³ãŒãã®æ§é ã«ã€ããŠè§£èª¬ããŸãã以äžã¯ããŠãŒã¶ãŒãã2ã€ã®æ°å€ãå ¥åããŠåãåãããã®åã衚瀺ããããã°ã©ã ã§ãã
#include <stdio.h> int main() { int a, b, sum; printf("2ã€ã®æ°å€ãå ¥åããŠãã ãã: "); scanf("%d %d", &a, &b); sum = a + b; printf("åèš: %dn", sum); return 0; }
ãã®ããã°ã©ã ã¯ãprintfé¢æ°ã䜿çšããŠãŠãŒã¶ãŒã«ã¡ãã»ãŒãžã衚瀺ããscanfé¢æ°ã䜿çšããŠ2ã€ã®æŽæ°ãå ¥åãšããŠåãåããŸãããã®åŸããããã®æ°å€ã®åãèšç®ããå床printfé¢æ°ã䜿çšããŠçµæã衚瀺ããŸãã
6.2 ãããã°ã®åºæ¬
ããã°ã©ã ã®äœæäžãå®è¡åŸã«ã¯ãäºæããªããšã©ãŒãäžå ·åãçºçããããšããããŸãããããã®åé¡ãç¹å®ããä¿®æ£ããããã»ã¹ããããã°ãšåŒã³ãŸãã
ãããã°ã®ç¬¬äžæ©ã¯ããšã©ãŒã¡ãã»ãŒãžãããã°ã©ã ã®åºåã泚ææ·±ã確èªããããšã§ããããã«ãããåé¡ãçºçããŠããç®æãåå ãç¹å®ããæããããåŸãããšãã§ããŸãã
ãŸããCèšèªã«ã¯ãããã°ã®ããã®ããŒã«ãé¢æ°ãå€æ°æäŸãããŠããŸããäŸãã°ãasserté¢æ°ã䜿çšãããšãããã°ã©ã ã®ç¹å®ã®éšåã§æ¡ä»¶ããã§ãã¯ãããã®æ¡ä»¶ãæºããããªãå Žåã«ãšã©ãŒã¡ãã»ãŒãžã衚瀺ããããšãã§ããŸãã
ãããã°ã¯ãããã°ã©ãã³ã°ã®ã¹ãã«ãåäžãããäžã§éåžžã«æçãªããã»ã¹ã§ããåé¡ã®åå ãç¹å®ããé©åãªä¿®æ£ãè¡ãããšã§ãããå ç¢ã§å¹ççãªããã°ã©ã ãäœæããããšãã§ããŸãã
7. CèšèªåŠç¿ã®æ¬¡ã®ã¹ããã
Cèšèªã®åºæ¬ãåŠã¶ããšã¯ãããã°ã©ãã³ã°ã®äžçãžã®ç¬¬äžæ©ã§ããããããCèšèªã«ã¯ãŸã æ¢æ±ãã¹ãå€ãã®é«åºŠãªãããã¯ãæ©èœãååšããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãCèšèªã®åŠç¿ãããã«æ·±ããããã®æ¹æ³ããªãœãŒã¹ã«ã€ããŠçŽ¹ä»ããŸãã
7.1 é«åºŠãªãããã¯ãžã®å°å ¥
Cèšèªã®åºæ¬çãªæŠå¿µãç解ããåŸã次ã«ææŠãããã®ã¯é«åºŠãªãããã¯ã§ããäŸãã°ãæ§é äœãå ±çšäœãåçã¡ã¢ãªã®ç¢ºä¿ãªã©ã®æŠå¿µã¯ãããè€éãªããã°ã©ã ãäœæããéã«éåžžã«åœ¹ç«ã¡ãŸãã
ãŸããCèšèªã«ã¯å€ãã®ã©ã€ãã©ãªãååšãããããã®ã©ã€ãã©ãªãå©çšããããšã§ãã°ã©ãã£ãã¯ã¹ã®æç»ããããã¯ãŒã¯éä¿¡ãªã©ãããŸããŸãªé«åºŠãªæ©èœãå®è£ ããããšãã§ããŸãããããã®ãããã¯ãåŠã¶ããšã§ãCèšèªã®å¯èœæ§ãããã«åºããããšãã§ããŸãã
7.2 ãããªãåŠç¿ãªãœãŒã¹
CèšèªãåŠã¶ããã®ãªãœãŒã¹ã¯ãã€ã³ã¿ãŒãããäžã«æ°å€ãååšããŸãããªã³ã©ã€ã³ã®ãã¥ãŒããªã¢ã«ãæžç±ãåç»è¬åº§ãªã©ãããŸããŸãªåœ¢åŒã§æ å ±ãæäŸãããŠããŸãã
ç¹ã«ããªãŒãã³ãœãŒã¹ã®ãããžã§ã¯ããåç §ããããšã¯ãå®éã®ããã°ã©ã ã®ã³ãŒããèªãè¯ãæ©äŒãšãªããŸããGitHubãBitbucketãªã©ã®ãã©ãããã©ãŒã ã«ã¯ãå€ãã®Cèšèªã®ãããžã§ã¯ãããã¹ãã£ã³ã°ãããŠããããããã®ã³ãŒããèªãããšã§ãå®éã®éçºã®çŸå Žã§ã®ã³ãŒãã£ã³ã°ã®æ¹æ³ãæè¡ãåŠã¶ããšãã§ããŸãã
ãŸããCèšèªã«é¢ããã³ãã¥ããã£ããã©ãŒã©ã ãå€æ°ååšããŸãããããã®ã³ãã¥ããã£ã«åå ããããšã§ãä»ã®åŠç¿è ãçµéšè±å¯ãªããã°ã©ããŒãšã®äº€æµãæã€ããšãã§ããçåãåé¡ãå ±æãã解決ã®æå©ããåããããšãã§ããŸãã