460 likes | 606 Views
This article provides a comprehensive overview of data types in C programming, including integer, floating-point, and character types. It explains the characteristics and formats of each type, such as signed and unsigned integers, float, double, and long double for floating points. It also details how ASCII codes represent characters and discusses constants and variables, such as local, global, static, and register variables. This guide is essential for programmers looking to understand the structure and definition of data in C.
E N D
CProgramming Data Types & Variables
Data type in C program • Types of Data Type • เลขจำนวนเต็ม (IntegerNumber) • เลขทศนิยม (Floating-point Number) • ตัวอักษร (Character) • ข้อมูลไม่มีค่า (Valueless)
Integer Number • เลขจำนวนเต็มที่เป็นได้ทั้งค่าบวก ลบและศูนย์ • ในภาษาซี สามารถกำหนดรูปแบบของเลขจำนวนเต็มได้ 3 รูปแบบ คือ • ระบบฐานสิบ เช่น 395 -24 เป็นต้น • ระบบฐานแปด นำหน้าด้วยเลข 0 เช่น 045 -023 เป็นต้น • ระบบฐานสิบหก นำหน้าด้วย 0x หรือ 0X เช่น 0X4F0x23A เป็นต้น
Integer Number • รูปแบบของเลขจำนวนเต็ม จัดแบ่งตามขนาดของข้อมูล โดยใช้ Modifier (signed, unsigned, short, long)ได้ดังนี้- signed integer- unsigned integer- signed short integer- unsigned short integer- signed long integer- unsigned long integer
Floating-point Number • เป็นค่าตัวเลขทศนิยม ที่เป็นได้ทั้งค่าบวกและลบ • ในภาษาซี เลขทศนิยมกำหนดได้ 2 รูปแบบคือ- เลขทศนิยมปกติ เช่น 1.5 , -3.456 เป็นต้น- เลขทศนิยมแบบเลขยกกำลัง เช่น 1E+3 หมายถึง 1x103 2.45E-6 หมายถึง 2.45x10-6 0.3945E6 หมายถึง 0.3945x106 *** ตัวเลขยกกำลังต้องเป็นจำนวนเต็มเท่านั้น
Floating-point Number • ในภาษาซี เลขทศนิยมจัดแบ่งตามขนาดในการจัดเก็บข้อมูลได้ 3 แบบ คือ- float-point- double- long double * ขนาดข้อมูลขึ้นอยู่กับประเภทองเครื่องคอมพิวเตอร์ที่ใช้งาน
Character Data Type • เป็นข้อมูลประเภทตัวอักษร (single character) • รูปแบบในการจัดเก็บตัวอักษรในหน่วยความจำนั้น จะถูกแทนด้วยรหัสตัวเลขของ ASCII Codeขนาด 8 บิท • The single character is enclosed within two single quotation marks. • Digits stored by enclosing them within single quotes are considered as characters and cannot be used for calculations.
ASCII CODE • รหัสตัวเลขขนาด 8 บิท เพื่อแทนตัวอักษร ตัวเลข สัญลักษณ์ และเครื่องหมายต่างๆ รหัสทั้งสิ้น 256 ตัว(000–255) • รหัสลำดับที่ 1 – 32 (000 – 031) เป็นรหัสควบคุม (Escape Sequence) เช่น000 = (\0) = NULL Value007 = (\a) = Beep (Alert)008 = (\b) = Backspace009 = (\t) = tab010 = (\n) = newline (Line feed)013 = (\r) = carriage return. . . . . . . . . . . . . .
Data Typein C Language The various data types and their memory requirements
Types of Datain C Language • ค่าคงที่ (Constant)เป็นค่าที่กำหนดด้วย # Pre-processing Directive ตอนต้นของโปรแกรม • ตัวแปร (Variable)2.1 ตัวแปรโลคอล (Local Variable)2.2 ตัวแปรโกลบอล (Global Variable)2.3 ตัวแปรสแตติก (Static Variable)2.4 ตัวแปรรีจิสเตอร์ (Register Variable)
Constant Value • เป็นค่าที่กำหนดขึ้นเพื่อใช้งานในโปรแกรม • ไม่สามารถเปลี่ยนแปลงค่าได้ในโปรแกรม • ในภาษาซี จะต้องประกาศไว้ที่ตอนต้นของโปรแกรม ด้วย Pre-processing Directive ดังนี้#define ConstantName Value *** ตามกติกาในการเขียนโปรแกรมนั้น ชื่อของค่าคงที่ ควรเป็นตัวอักษรตัวพิมพ์ใหญ่
ตัวแปร (Variable) • เป็นชื่อที่กำหนดขึ้น เพื่อใช้อ้างถึงพื้นที่ในหน่วยความจำของคอมพิวเตอร์ ในการจัดเก็บข้อมูลที่ประกาศไว้ในโปรแกรม • ข้อมูลของตัวแปรสามารถทำการเปลี่ยนแปลงค่าได้ • Variable refers to memory location where a particular value is stored. • Type of data decides the amount of memory allocated to variables. • Names assigned to variables to store a particular data help us in retrieving the data as and when required.
Variables Declaration in C • All variables must be declared before they can appear or referred in executable statements. • ต้องประกาศตัวแปรก่อนมีการเรียกใช้ทุกครั้ง • การประกาศตัวแปร = การจองพื้นที่ในหน่วยความจำ เพื่อใช้จัดเก็บข้อมูลที่กำหนด และอ้างอิงถึงพื้นที่ดังกล่าวด้วยชื่อของตัวแปรที่กำหนดไว้ • การประกาศตัวแปรจะถูกประกาศไว้ที่ตอนต้นของโปรแกรมหรือฟังก์ชั่น ตามลักษณะการใช้งานของตัวแปรนั้นๆ
Variables Declaration in C • รูปแบบของการประกาศตัวแปร dataType varName; dataType varName = initValue; dataType varName1; dataType varNmae2; dataType varName3, varName4, … ; dataType varName1 = initVal1, varName2 = initVal2, . . . . ;
Variables Declaration in C • การประกาศตัวแปรชนิดตัวเลขจำนวนเต็ม(Integer) int num; int num1 = 100; int num2, num3, num4; int x = -10, y = 20, z = -30; int a, b; a = 5; b = 10; int a = 5, b = 10;
Variables Declaration in C • การประกาศตัวแปรชนิดตัวเลขทศนิยม (floating-pointและdouble) float num; float pi = 3.141593; float x, y, z; float a = 1.234, b = -1.25e3, c = 0. ; double p, q, r; double i = -2.3, j = 0.123e-6;
Variables Declaration in C • การประกาศตัวแปรชนิดตัวอักษร (Character) char ch; char c1, c2, c3; char x = ‘a’; char y = ‘1’ , z = ‘2’; char star = ‘*’;
Local or Automatic Variable in C • การประกาศตัวแปรภายในบล็อกหรือภายในฟังก์ชั่นใดๆ ตัวแปรนั้นเรียกว่าตัวแปรแบบโลคอล (Local) • ตัวแปรแบบโลคอลจะคงอยู่หรือถูกเรียกใช้งาน เมื่อมีการทำงานอยู่ภายในบล็อกหรือฟังก์ชั่นที่มีการประกาศตัวแปรนั้นๆ และเมื่อจบการทำงานของบล็อกหรือฟังก์ชั่นนั้นๆ แล้ว ตัวแปรแบบโลคอลนี้จะถูกยกเลิกทันที • ตัวแปรแบบโลคอลจะถูกเรียกใช้งานได้เฉพาะภายในบล็อกหรือฟังก์ชั่นที่มีการประกาศตัวแปรนี้ไว้เท่านั้น
Global Variable in C • การประกาศตัวแปรไว้ตอนต้นของโปรแกรม หรือภายนอกฟังก์ชั่น main( ) ตัวแปรนั้นเรียกว่าตัวแปรแบบโกลบอล (Global) • ตัวแปรแบบโกลบอลจะถูกเรียกใช้งานได้จากทุกที่ในโปรแกรม • ตัวแปรแบบโกลบอลจะถูกยกเลิกก็ต่อเมื่อจบการทำงานของโปรแกรม
Local & Global Variable in C int num1; void main( ) { int num2; . . . . . . . . . } func( ) { int num3; . . . . . . . . . } Global Variable Local Variable
Static Variables in C • การประกาศตัวแปรประเภท static static dataTypevarName; static dataTypevarName =initVal; • การใช้งานต่างๆ จะเหมือนกับตัวแปรทั่วไป แตกต่างกันที่ค่าของตัวแปรประเภท static จะยังคงอยู่ โดยจะไม่ถูกยกเลิกไปเมื่อจบการทำงานภายในภายในบล็อกหรือฟังก์ชั่นที่ประกาศนี้ไว้ใช้งาน • หากไม่กำหนดค่าเริ่มต้นให้ ค่าของตัวแปรจะมีค่าเป็น 0
Static Variables in C void main( ) { sum( ); sum( ); sum( ); } sum( ) { int num = 10 , nt = 0; static int ns; nt = nt + n1; ns = ns + n1; printf(“nt = %d\tns = %d\n”,nt,ns); } Result : nt = 10 ns = 10 nt = 10 ns = 20 nt = 10 ns = 30
Register Variable in C • การประกาศตัวแปรประเภทรีจิสเตอร์ (Register) registerdataType varName; registerdataType varName = initVal; • คุณลักษณะและการใช้งานเช่นเดียวกับตัวแปรทั่วไป แตกต่างตรงที่เป็นการจัดเก็บข้อมูลในหน่วยความจำรีจิสเตอร์ • รีจิสเตอร์เป็นหน่วยความจำของ Processor ซึ่งมีความเร็วในการอ่านและเขียนข้อมูลได้รวดเร็วกว่าหน่วยความจำ RAM ของระบบ • รีจิสเตอร์ภายใน Processor มีขนาดจำกัด ไม่สามารถใช้จัดเก็บข้อมูลที่มีปริมาณมากๆ ได้
Expression , Operand & Operator A + B A , B Operand + Operator A + B Expression
Operators in C • Arithmetic Operators • Unary Operators • Relational Operators • Logical Operators • Assignment Operators • Conditional Operator
Arithmetic Operators • There are 5 Arithmetic operators + Addition - Subtraction * Multiplication / division % modulus(% = การหารที่ผลลัพธ์คือเศษจากการหาร)
Unary Operators There are 5 Unary operators • - Unary Minus operator • ++ Increment operator • - - Decrement operator • sizeof sizeof operator • (dataType) Data TypeCasting
Unary Operators • Unary minus operatorใช้กำหนดค่าลบให้กับข้อมูลตัวเลขเช่น -5 , -12.5 เป็นต้น • Increment operatorใช้สำหรับเพิ่มค่าของข้อมูลตัวเลขขึ้นอีก 1 • Decrement operatorใช้สำหรับลดค่าของข้อมูลตัวเลขลงอีก 1 • sizeof operatorใช้สำหรับหาขนาดของข้อมูล • Data Type castingใช้สำหรับเปลี่ยนชนิดของข้อมูล
Relational operators There are 6 relational operators in C • < less than • > greater than • <= less than or equal to • >= greater than or equal to • == equal to • != not equal to ผลลัพธ์ที่ได้จากการเปรียบเทียบ จะมีค่าเป็นตัวเลข คือ 0= ไม่เป็นจริง (False) 1 = เป็นจริง (True)
Logical operators There are 3 logical operators • && AND operator • || OR operator • ! NOT operator
Assignment operators There are 6 Assignment operators • = assignment operator • += Addition assignment • -= Subtraction assignment • *= Multiplication assignment • /= Division assignment • %= Modulus assignment
Conditional Operator • The form of conditional operator _exp1_ ? _exp2_ : _exp3_ • The meaning is :if (exp1 is true) then perform exp2 else perform exp3
Examples in Arithmetic Operators 4+3*6/2–5+5 4+[3*6/2]-5+5 4+[18/2]-5+5 [4+9]-5+5 [13-5]+5 13
Examples in Unary Operators • a = 1, b = ++a a = 2 , b = 2 • a = 1, b = a++ a = 2 , b = 1 • a = 10, a += 10 a = 20 • a = 10, a /= 2 a = 5 • int a = 10; sizeof a = 2 • float b = 1.5; sizeof b = 4 sizeof (float) = 4
Examples of Unary operators int a = 5, b = 2;a * b 10a / b 2 (int/int = int) (float) a / b 2.5 (float/int=float)a / (float) b 2.5 (int/float=float)
Examples of Relational Operators int i = 7; float f = 5.5; char c = ‘A’;
Examples of assignment operators int a = 10, b = 20;
Examples of conditional Operator int a = 10; b = 20; c = 0;