C languageRohit SinghThe document provides an introduction to the C programming language, detailing its history, functions, and essential components necessary for programming. It outlines basic functions, types of instructions, variable definitions, and special symbols used in C. Additionally, it includes common interview questions related to C programming concepts.
Programming in cankitjain851The document provides an overview of the C programming language, including its history, features, basic structure, and how to compile a C program. C was developed in the 1970s and became widely popular due to its reliability, simplicity, and ability to create efficient and fast programs. It combines high-level and low-level language features. The basic structure of a C program includes documentation, include, define, and main sections along with function definitions. Compiling a C program generates machine-readable binary code from the source code using a compiler.
Introduction to C ProgrammingAniket PatneThe document is an introduction to the C programming language, covering its history, significance, and basic concepts like constants, variables, and data types. It explains the syntax rules for defining variables, the use of keywords, and fundamental operations such as input and output through printf() and scanf(). It highlights the importance of C in various operating systems and programming paradigms, emphasizing its role as a foundation for learning other languages.
C programming language Reference NoteChetan Thapa MagarThe document provides information about C programming language. It was prepared by Chetan Thapa Magar of Brightland Higher Secondary College. C was created by Dennis Ritchie in 1972 and draws concepts from earlier languages like BCPL and B. C is portable, efficient, structured, and a middle-level language well-suited for system and application programming. The document discusses C's history, elements, data types, operators, control structures like loops and decisions, and provides some example programs.
8. โรงเรียนวิสุทธรังษี จังหวัดกาญจนบุรี
หน้า 8 เอกสารประกอบการสอน ภาษาซี
Text Formatting #2
ผลลัพธ์
Characters: a
Decimals: 1977 6500000000
Preceding with blanks: 1977
Preceding with zeros: 0001977
Base number: 427 1ab 653
Floats: 3.14 +3e+00 3.141600E+00
123 in this text is a String not a number
ข้อควรจา
ตัวอักษรหรืออักขระ1 ตัว (Character) ในภาษาC เราจะใช้เครื่องหมาย ' (Single qoute) คร่อม
ชุดตัวอักษร หรือสายอักขระ (String) ในภาษาC เราจะใช้เครื่องหมาย "(Double qoute) คร่อม
ตัวเลข สามารถใช้ได้เลยไม่ต้องมีตัวคร่อม
เพิ่มเติม
%7d หมายความว่า แสดงเลขฐานสิบโดยมีความกว้างอย่างน้อย7 ตัวอักษร
หากเลขนั้นมีจานวนหลักน้อยกว่า7 หลัก โปรแกรมจะใส่ช่องว่างด้านหน้าเลขนั้นจนครบ7 ตัว
%07d หมายความว่า แสดงเลขฐานสิบโดยมีจานวนหลักอย่างน้อย7 หลัก
หากเลขนั้นมีจานวนหลักน้อยกว่า7 หลัก โปรแกรมจะใส่เลขศูนย์ด้านหน้าเลขนั้นจบครบ7
หลัก
%+d หมายความว่า ปกติแล้วเลขจานวนบวกจะไม่มีเครื่องหมายนาหน้า ใช้โค๊ดนี้เพื่อสั่งให้แสดง
%.2f หมายความว่า แสดงเลขจานวนจริง โดยแสดงตาแหน่งทศนิยมเพียง2 หลัก
หลักการด้านบน สามารถใช้พร้อมกันได้ด้วยเช่น %07.3f %+3d เป็นต้น
printf ("Characters: %cn", 'a');
printf ("Decimals: %d %dn", 1977, 6500000000);
printf ("Preceding with blanks: %7d n", 1977);
printf ("Preceding with zeros: %07d n", 1977);
printf ("Base number: %d %x %on", 427, 427, 427);
printf ("Floats: %4.2f %+.0e %E n", 3.1416, 3.1416, 3.1416);
printf ("%s n", "123 in this text is a String not a number");
9. โรงเรียนวิสุทธรังษี จังหวัดกาญจนบุรี
หน้า 9 เอกสารประกอบการสอน ภาษาซี
แสดงผลให้เป็นระเบียบด้วยอักขระควบคุมการแสดงผล
นอกจากนี้เรายังสามารถจัดรูปแบบการแสดงผลให้ดูเป็นระเบียบมากขึ้น อย่างเช่นขึ้นบรรทัดใหม่ หลัง
แสดงข้อความหรือเว้นระยะแท็บระหว่างข้อความโดย ใช้อักขระควบคุมการแสดงผลร่วมกับคาสั่งprintf
อักขระควบคุมการ
แสดงผล
ความหมาย
n ขึ้นบรรทัดใหม่ (new line)
t
เว้นช่องว่างเป็นระยะ 1 แท็บ (6 ตัวอักษร) หรือ แท็บแนวนอน (horizontal
tab)
r กาหนดให้เคอร์เซอร์ไปอยู่ต้นบรรทัด (carriage return)
f เว้นช่องว่างเป็นระยะ 1 หน้าจอ หรือ ขึ้นหน้าใหม่ (form feed)
b ถอยหลัง 1 ช่องตัวอักษร(Backspace)
พิมพ์เครื่องหมาย (Literal backslash)
" พิมพ์เครื่องหมาย " (Double quote)
' พิมพ์เครื่องหมาย ' (Single quote)
a ส่งเสียง Alert 1ครั้ง (bell)
v แท็บแนวตั้ง (Vertical tab)
? พิมพ์เครื่องหมาย ? Question mark (used toescape trigraphs)
nnn Character with octal value nnn
xhh Character with hexadecimal value hh
การนาอักขระควบคุมการแสดงผลมาใช้ เราต้องเขียนอักขระควบคุมการแสดงผลไว้ภายในเครื่องหมาย” ”
ดังตัวอย่าง
printf("Hello ... n"); แสดงข้อความ Hello ... แล้วขึ้นบรรทัดใหม่
printf("Hello...nKanchanaburin"); แสดงข้อความ Hello ...แล้วขึ้นบรรทัดใหม่พร้อมกับแสดง
ข้อความ Kanchanaburi จากนั้นขึ้นบรรทัดใหม่อีกครั้ง
printf("Num1 = %dtNum2 = %fn",x,z); แสดงข้อความ Num1=45 ตามด้วยการเว้น ช่องว่าง 1 แท็บ
แล้วต่อด้วยข้อความNum2 = 20.153
10. โรงเรียนวิสุทธรังษี จังหวัดกาญจนบุรี
หน้า 10 เอกสารประกอบการสอน ภาษาซี
Comment
บางครั้ง เราอาจต้องการเขียนข้อความอธิบายการทางาน หรือคาสั่งต่างๆ (Statement) ของ
โปรแกรมเพื่อให้คนอื่น หรือแม้แต่ตัวเราเองซึ่งมาอ่านในภายหลัง สามารถเข้าใจได้ง่ายและรวดเร็วขึ้น
โดยข้อความเหล่านี้ เราไม่ต้องการให้คอมพิวเตอร์ประมวลผล จะทาอย่างไร?
เราเรียก ข้อความที่ไม่ต้องการให้คอมพิวเตอร์ประมวลผลนี้ว่า คอมเม้นท์ (Comment) มีอยู่สองแบบ
1. คอมเม้นท์แบบบรรทัดเดียวจบ (Single line comment)
ข้อความที่ตามหลังเครื่องหมาย // ในบรรทัดนั้นทั้งบรรทัดจะไม่ถูกประมวลผล
คอมพิวเตอร์จะเริ่มประมวลผลโค๊ดใหม่ในบรรทัดถัดไป
2. คอมเม้นท์แบบหลายบรรทัด (Multiple line comment)
เริ่มต้นด้วยเครื่องหมาย /* และจบด้วยเครื่องหมาย */
ข้อความที่อยู่ระหว่างเครื่องหมายทั้งสองนี้ จะถือว่าเป็นComment ทั้งหมด
// this is a comment and will not be shown in output.
printf( "This isn't a comment" ); // this is a single line comment
/*
This is a multiple line comment.
Comment #1
Comment #2
*/
โค้ด
This isn't a comment
ผลลัพธ์