ภาษาซี (Turbo C++ : Computer Language)
เว็บเพจสำรอง (Backup Webpages) : perlphpasp.com | thaiall.com
ปรับปรุง : 2551-10-03 (เพิ่มตัวอย่างที่ใช้ VC++)
    สารบัญหลัก
  1. กลุ่ม b01 : พื้นฐานการแสดงผลอย่างง่าย
  2. กลุ่ม b02 : รับ แสดงผล เลือก และการทำซ้ำ
  3. กลุ่ม b03 : ทบทวนการทำซ้ำซ้อนทำซ้ำ
  4. กลุ่ม b04 : function ในภาษา C++ แบบต่าง ๆ
  5. รูปแบบข้อมูล (Data Type)
What is C language?
The C programming language (often, just "C") is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is now one of the most widely used programming languages. C is distinguished for the efficiency of the code it produces, and is the most commonly used programming language for writing system software, though it is also widely used for writing applications. Though not originally designed as a language for teaching, and despite its somewhat unforgiving character, C is commonly used in computer science education, in part because the language is so pervasive.
    สารบัญ :: ตัวอย่างโปรแกรมภาษา C++
  1. b0101.cpp (CL)
    // include, void, printf, getch
    เริ่มต้นเขียนโปรแกรมอย่างง่าย ๆ
  2. b0102.cpp (CL)
    // printf, return(0)
    แสดงการใช้ return แทน void สำหรับโปรแกรมย่อย
  3. b0103.cpp (CL)
    // printf, d, i++, ++i, i+=2
    แสดงการคำนวณภายใต้คำสั่ง printf
  4. b0104.cpp (CL)
    // printf, d,f,e,s,d,o,x,u
    แสดงการกำนหนดการแสดงผลของตัวเลขแบบต่าง ๆ
  5. b0105.cpp (CL)
    // short, long, printf
    แสดงการพิมพ์เลข double ได้สูงถึง 2147483647
  6. b0106.cpp (CL)
    // cout
    แสดงการพิมพ์ด้วยคำสั่ง cout
  7. b0107.cpp (CL)
    // clrscr, cout
    แสดงการพิมพ์ด้วยคำสั่ง cout หลายค่า
  8. b0108.cpp (CL)
    // clrscr, cin, cout
    รับค่า นำมาคำนวน และแสดงผล
  9. b0109.cpp (CL)
    // define, cout, printf
    ประกาศ และพิมพ์ด้วย cout
  10. b0110.cpp (CL)
    // cout, do while
    การใช้ cout คู่กับ do while
  11. b0111.cpp (CL)
    // cout, for
    การใช้ cout คู่กับ for แบบคำสั่งเดียว
  12. b0201.cpp (CL)
    // int, scanf, d
    แสดงการรับ และแสดงผลค่าจากแป้นพิมพ์
  13. b0202.cpp (CL)
    // char, gets, s[30]
    รับตัวอักษรหลายตัวแล้วนำไปแสดงผล
  14. b0203.cpp (CL)
    // int, while
    แสดงการทำซ้ำเพื่อพิมพ์ตัวเลขด้วย while
  15. b0204.cpp (CL)
    // for
    แสดงการทำซ้ำเพื่อพิมพ์ตัวเลขด้วย for
  16. b0205.cpp (CL)
    // scanf, if, printf
    แสดงการเปรียบเทียบด้วย if กับตัวเลข
  17. b0206.cpp (CL)
    // clrscr, char, getch, if, printf
    แสดงการเปรียบเทียบด้วย if กับตัวอักษร
  18. b0207.cpp (CL)
    // clrscr, char[3], scanf, strcmp, getch, if
    แสดงการเปรียบเทียบด้วย if กับชุดตัวอักษร
  19. b0208.cpp (CL)
    // scanf, switch
    รับค่า แล้วเลือกกระทำด้วย switch
  20. b0209.cpp (CL)
    // int, cin, cout, if, else if, else
    ใช้คำสั่งเปรียบเทียบ int กับ else if
  21. b0301.cpp (CL)
    // for, for
    แสดงการทำงานของ for ซ้อน for
  22. b0302.cpp (CL)
    // scanf, for, for
    แสดงการทำงานของ for ซ้อน for โดยควบคุมด้วยค่าที่รับจากแป้นพิมพ์(*)
  23. b0303.cpp (CL)
    // scanf, for, for
    แสดงการทำงานของ for ซ้อน for โดยควบคุมด้วยค่าที่รับจากแป้นพิมพ์(j)
  24. b0304.cpp (CL)
    // scanf, for, for
    แสดงการทำงานของ for ซ้อน for โดยควบคุมด้วยค่าที่รับจากแป้นพิมพ์(i)
  25. b0401.cpp (CL)
    // function, cout
    การเรียก function 2 ครั้งอย่างง่าย
  26. b0402.cpp (CL)
    // function, char, int, cout
    การเรียก 2 function พร้อมส่งค่าผ่าน function
  27. b0403.cpp (CL)
    // function, return
    การส่งค่า และรับค่าคืนจาก function
  28. b0501.cpp (CL)
    // array int
    รับค่าลงอาเรย์แบบ int และนำมาแสดงอย่างง่าย
  29. b0502.cpp (CL)
    // array int, for
    รับค่าลงอาเรย์แบบ int และนำมาแสดง โดยใช้ for ช่วย
  30. b0503.cpp (CL)
    // array int, for
    รับค่าลงอาเรย์แบบ int และนำมาแสดง พร้อมหาค่า max
  31. b0504.cpp (CL)
    // array int, for
    กำหนดค่าให้อาเรย์ และนำมาแสดง พร้อมหาค่า max
  32. b0505.cpp (CL)
    // array char, 2 dimension
    การกำหนดอาเรย์ 2 มิติแบบตัวอักษร และการนำค่ามาแสดงผล
  33. b0506.cpp (CL)
    // array char, pointer of string
    ตัวชี้อาเรย์ ทำงานคล้าย string
  34. b0507.cpp (CL)
    // array char, pointer of string, 2 dimension
    นับความยาวของแต่ละ string พร้อมแยกตัวอักษรทีละตัว
  35. b0508.cpp (CL)
    // array char, double, function, calculation, max
    3 function : รับเข้าอาเรย์, คำนวณและแสดง, หาผลรวม
  36. b0510.cpp (CL)
    // array double, bubble sort, average
    function : จัดเรียงตัวเลขแบบ bubble sort
  37. b0511.cpp (CL)
    // array integer, bubble sort, average, max, min of 3 array
    function : จัดเรียงตัวเลขแบบ bubble sort ในตัวแปรอาเรย์ 3 ชุด พร้อมหา max, min, average โดยรวม
  38. cl0101.cpp (CL)
    #include
  39. cl0102.cpp (CL)
    #include
  40. cl0103.cpp (CL)
    #include
  41. cl0104.cpp (CL)
    #include
  42. cl0105.cpp (CL)
    #include
  43. cl0106.cpp (CL)
    #include // for getchar
  44. cl0107.cpp (CL)
    #include
  45. cl0108.cpp (CL)
    #include
  46. cl0109.cpp (CL)
    #include
  47. cl0110.cpp (CL)
    #include
  48. cl0111.cpp (CL)
    #include
  49. cl0201.cpp (CL)
    // DOS>cl /clr cl0201.cpp /D_CRT_SECURE_NO_DEPRECATE
  50. cl0202.cpp (CL)
    #include
  51. cl0203.cpp (CL)
    #include
  52. cl0204.cpp (CL)
    #include
  53. cl0205.cpp (CL)
    #include
  54. cl0206.cpp (CL)
    #include
  55. cl0207.cpp (CL)
    #include
  56. cl0208.cpp (CL)
    #include
  57. cl0209.cpp (CL)
    #include
  58. cl0301.cpp (CL)
    #include
  59. cl0302.cpp (CL)
    #include
  60. cl0303.cpp (CL)
    #include
  61. cl0304.cpp (CL)
    #include
  62. cl0401.cpp (CL)
    #include
  63. cl0402.cpp (CL)
    #include
  64. cl0403.cpp (CL)
    #include
  65. cl0501.cpp (CL)
    #include
  66. cl0502.cpp (CL)
    #include
  67. cl0503.cpp (CL)
    #include
  68. cl0504.cpp (CL)
    #include
  69. cl0505.cpp (CL)
    #include
  70. cl0506.cpp (CL)
    #include
  71. cl0507.cpp (CL)
    #include
  72. cl0508.cpp (CL)
    #include
  73. cl0510.cpp (CL)
    #include
  74. cl0511.cpp (CL)
    #include
Visual Studio 8	
http://msdn.microsoft.com/en-us/library/59zy697d(VS.80).aspx
start : Visual Studio 2005 Command Prompt
  DOS> cl cl0101.cpp
  DOS> cl /clr cl0101.cpp
  - ถ้า #include <iostream> ต้องใช้ /clr
  - /clr ทำให้มีการเรียกใช้ Incremental Linker
  - ให้ใช้ getchar() แทน getch()
  - ใช้ getchar() จะต้อง #include <stdio.h>
  - ใช้ system("PAUSE") จะต้อง #include <stdlib.h>
  - ถ้าใช้ cout ต้อง
    #include <iostream> และ using namespace std; // for cout 
    จึงจะใช้ cout << "a" << endl;
    แต่ถ้า #include  อย่างเดียว
    สามารถใช้ std::cout << "a";
  - จะใช้ system() ต้อง #include <iostream>
  - ใช้ scanf หรือ gets อาจพบ warning
    ให้ใช้ DOS>cl /clr x.cpp /D_CRT_SECURE_NO_DEPRECATE 

// Read from File with word split #include <stdio.h> int main() { FILE *fp; char userFile[40] = {"x.cpp"}; fp = fopen(userFile,"r"); char info[100]; while((fscanf(fp,"%s",info)) != EOF) printf("%s\n",info); fclose(fp); return 0; }
// Write to File #include <stdio.h> int main() { FILE *fp; char userFile[40] = {"y.txt"}; fp = fopen(userFile,"w"); fprintf(fp, "abc"); fclose(fp); return 0; }
// Assembly char my_getch() { char chr; _asm { mov ah, 07H INT 21H mov chr, al } return chr; }
ตัวแปลภาษา (Compiler) BCC 5.5.1
Download Site : 
Download Site : Borland C++ 5.5 9 MB ! . Configuration สำหรับ Compile ใน Editplus เมื่อใช้ BCC 5.5.1    - ให้กำหนด Command : C:\Borland\BCC55\Bin\bcc32.exe    - ให้กำหนด Argument : -IC:\Borland\BCC55\Include -LC:\Borland\BCC55\Lib -n$(FileDir) $(FilePath)    - ให้กำหนด Initial directory : C:\Borland\BCC55\Bin    - ให้ click ที่ Capture Output Configuration สำหรับ Run ใน Editplus เมื่อใช้ BCC 5.5.1 (กรณีไม่รับค่าจากแป้นพิมพ์)    - ให้กำหนด Command : $(FileNameNoExt)    - ให้กำหนด Initial directory : $(FileDir)    - ให้ click ที่ Capture Output มีปัญหาเรื่องการ run    - เพื่อนท่านหนึ่งโทรมาถามว่า run ใน Command line ไม่มีปัญหา แต่พบปัญหาการหยุดรับค่าจากแป้นพิมพ์ เมื่อใช้ EditPlus    - ให้กำหนด Command เป็น "c:\windows\system32\cmd.exe" "/k"    - ให้กำหนด Argument เป็น $(FileNameNoExt) หรือ E:\KEEPSRC\$(FileNameNoExt)    - ไม่ click ที่ Capture Output เพื่อไม่ให้ run ใน Output window    - ถ้าต้องการแปลผ่าน Command Line ควรสร้างแฟ้ม bcc32.cfg ในห้อง bin แล้วเพิ่ม 2 บรรทัดนี้ลงไป
-I"c:\Borland\Bcc55\include"
-L"c:\Borland\Bcc55\lib"    - ตัวอย่างการแปลผ่าน Command Line คือ DOS>bcc32 x.cpp    - ผลการแปลผ่าน Command Line
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
x.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

ตัวแปลภาษา (Compiler) TurboC 3.0
Download Site : TurboC 3.0 3 MB
วิธีแก้ปัญหา

1. ถ้า Compile ไม่ผ่าน เพราะหาห้อง include ไม่พบ เมื่อใช้ TCC ผ่าน command line หรือ Editplus แต่ไม่พบปัญหาใน TC
- ถ้าโปรแกรมทั้งหมดรวมทั้ง กองรวมกันอยู่ในห้อง c:\tc
- แก้แฟ้ม Turboc.cfg จาก -IC:\TC\INCLUDE เป็น -IC:\TC


บทเรียนพิเศษ (Extra Lesson)
โปรแกรมค้นหาตัวอักษรใน string
หัวหน้าผมใช้โปรแกรมลักษณะนี้ออกข้อสอบ จึงต้องทดสอบโปรแกรม เตรียมสอนนักศึกษา
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
main(void) {
  clrscr();
  char string[6];
  char *ptr, z=‘z‘;
  strcpy(string, "abcdef");
  ptr = strchr(string, z);
  if (strlen(ptr) == 0) cout << "not found z";
  ptr = strchr(string, ‘c‘);
  if (strlen(ptr) > 0) cout << "found c";
  getch();
}

ศึกษาเรื่อง date เพราะมีนักศึกษาคนหนึ่งโทรมาถาม
พบตัวอย่างที่ http://www.cs.duke.edu/~ola/ap/dates/
นำตัวแปร myMonth myDate และ myYear ไปใช้ต่อได้
#include <time.h>
#include <stdio.h>
#include <conio.h>
void main() {
  clrscr();
  static struct tm timeHolder;
  static struct tm *date = &timeHolder;
  time_t tloc;
  time(&tloc);
  date = localtime(&tloc);
  int myMonth = date->tm_mon + 1;
  int myDay   = date->tm_mday;
  int myYear  = date->tm_year + 1900;
  printf("%d%d%d",myMonth,myDay,myYear);
  getch(); // 9122007 (9 Dec 2007)
}

อีเมลจากเพื่อนชาวไทย ที่ให้คำแนะนำทีมงาน
1. คุณศร [sorn_kkc@hotmail.com] : ส่งโปรแกรมนี้มาให้ผมตอบคำถาม
คำถามของคุณศร : เขียนโปรแกรมส่งข้อมูลผ่าน port printer โดยควบคุมหลอด LED ปรากฎว่าไม่ได้ผลตามโปรแกรมที่เขียน
ผมก็ไม่รู้ จึงค้นคว้าเพิ่มเติม และไปเข้าใจที่ หนังสือภาษาซี ของ ดอนสัน ปงผาบ อ.คณะเทคโนโลยีอุตสาหกรรม ม.ราชภัฏลำปาง (donson@lpru.ac.th)
จึงเข้าใจว่า LED คืออะไร .. ผลการทดสอบต่อวงจรครั้งแรกในชีวิต ก็ได้มา 7 ภาพดังที่เห็น พร้อมแผลพุพองที่นิ้วกลางซ้ายอีกแผล

และนี่คือจุดเริ่มต้นที่จะใช้ภาษาอื่น ๆ คุมอุปกรณ์ผ่าน printer port
โปรแกรมนี้มีปัญหาใน WindowsXP แต่ใน WinMe กับ Win98 OK .. 2. คุณโอม [ohm10513@hotmail.com] : ส่ง e-mail มาแนะนำโปรแกรม userport.exe ใน etteam.com ที่หา download ได้จาก etteam.com สามารถแก้ปัญหาเรื่อง Printer Port ใน Windows XP ได้ [ UserPort.zip 33 KB ] กว่าจะทดสอบผ่าน ต้องหาวิธี compile ผ่าน command line ที่ทำงานควบคู่กับ tasm32 และ userport และแก้ IOPort.c มีรายละเอียดการทดสอบสร้าง IOPort.exe ด้วย BCC5.5 ที่ทำงานร่วมกับ UserPort.exe ใน http://www.thaiall.com/printer
ตัวอย่างการเขียนโปรแกรมภาษา C ที่แทรกด้วยโค้ดของภาษา Assembly
ตัวอย่างโปรแกรมที่แปลด้วย Turbo C 3.0
ทำหน้าที่พิมพ์  A ทางจอภาพ โดยใช้ Interrupt 21h
เช่น 
c:\tc>tcc  x.cpp
void main() {
  asm {
  mov ah,02h;
  mov dl,41h;
  int 21h;
  }
}
ตัวอย่างโปรแกรมที่แปลด้วย Borland C 5.5
ทำหน้าที่พิมพ์เลข 11 เพราะเป็นผลบวกที่ได้จากภาษา assembly
เช่น 
c:\borland\bcc55\bin>path=%path%;c:\tasm\bin
c:\borland\bcc55\bin>bcc32  x.cpp
#include <stdio.h>
void main() {
  int r;
  asm {
  mov eax, 5;
  mov ebx, 6;
  add eax,ebx;
  mov r,eax;
  }
  printf("%d",r);
}
    แนะนำเว็บ
    http://www.etteam.com/downloadf.html (มี compiler manual และ code แก้ปัญหา)
    http://www.beyondlogic.org/spp/parallel.htm (Have detail of pin number)
    http://www.machinegrid.com/content/view/30/84/ (แสดงการประยุกต์กับมอเตอร์)
    http://www.nelnickrobotics.com/printer_port.html (ภาพวงจรการเชื่อมต่ออุปกรณ์)
    http://my.tele2.ee/chipmcu/ing/prog705j.htm (อ่าน e-prom จาก printer port)

    ตัวอย่างภาษาซี กับส่งข้อมูลออกทาง Printer Port ที่ http://www.thaiall.com/printer