គឺជា Standard Type ដែលត្រូវបានគេប្រើសំរាប់សំណត់ដែនតំលៃរបស់អថេរ ប្ញ ចំនួនថេរនោះនៅពេលដែលយើងប្រកាស់ដើម្បីប្រើប្រាស់ ។ Standard Type សំខាន់ៗនៅក្នុងភាសា C++ មានដូចជា
ប្រភេទ | ការប្រើប្រាស់ | ទំហំ | ដែនកំណត់ |
---|---|---|---|
char | តាងអោយតួអក្សរ1តួអក្សរ | 1byte | (signed)-128 to 127 |
short int | តាងអោយចំនួនគត់ | 2byte | (signed) -32768 to 32767 (unsigned) 0 to 65535 |
int | តាងអោយចំនួនគត់ | 4byte | (signed)-2147483648 to 2147483647 (unsigned) 0 to 4294967295 |
long int | តាងអោយចំនួនគត់ | 4byte | (signed)-2147483648 to 2147483647 (unsigned) 0 to 4294967295 |
bool | តាងអោយតំលៃពី គឺ True/False | 1byte | true or false |
float | តាងអោយចំនួសទស្សភាគ | 4byte | +/-3.4e +/-38(~7 digits) |
double | តាងអោយចំនួសទស្សភាគ | 8byte | +/-1.7e +/-308(~15 digits) |
long double | តាងអោយចំនួសទស្សភាគ | 8byte | +/-1.7e +/-308(~15 digits) |
wchar_t | តាងអោយតួរអក្សរច្រើនតួ | 2 or 4byte | 1 wild charater |
2.Variable
អថេរគឺជាទំហំដែលអាចប្រែប្រួលតំលៃ អថេររបស់កម្មវិធីគឺជាឈ្មោះនៃតំបន់ Memory រក្សារទិន្នន័យ។បណ្តាលអថេរត្រូវបានប្រកាស់ដោយរបៀបដាក់ឈ្មោះអថេរនៅផ្នែកVariable Declarition ។
ឈ្មោះអថេរ / Identifiers(Variable Name) : អថេរដែលត្រូវបានប្រកាស់សំរាប់ប្រើប្រាស់ក្នុងភាសាសរសេរកម្មវិធី ។ យើងអាចកំណត់ឈ្មោះអោយអថេរ បានតាមសេចក្តីត្រូវការហើយឈ្មោះដែលប្រកាស់រួចត្រូវបានរក្សាទុកក្នុងMemory ដែលមានទំហំអាស្រ័យទៅតាមប្រភេទទិន្នន័យរបស់អថេរនោះ។
Identifiers ឈ្មោះដែលប្រើសំរាប់តាងអោយអញ្ញាតិណាមួយ ហើយការតាងឈ្មោះនេះមានលក្ខខ័ណ្ឌដូចតទៅ
-A,B,C....a,b,c...ជាតួអក្សរអង់គ្លេស រឺ underscore " _ " នាំមុខជានិច្ច -ហាមដកឃ្លា
-1,2,3,4,...9 អាចមានតួលេខតែមិនអាចនៅខាងមុខគេទេ
-ហាមប្រើឈ្មោះជាន់នឹង Keyword
-ហាមប្រើ Symbol as & % + - / ^ . \ | ......
-អាចប្រើ underscore " _ " ចំនួសការដកឃ្លា
ឧទាហរណ៍ int score_st1; ត្រឹមត្រូវ
char 1s; មិនត្រឹមត្រូវ
float love-ss; មិនត្រឹមត្រូវ
int ss ee; មិនត្រឹមត្រូវ
Keyword គឺជាពាក្យគន្លឹះដែលគេប្រើប្រាស់ញឹកញាប់ក្នុងការសរសេរកូដ ហើយមានអត្ថន័យ នៃការប្រើប្រាស់ខុសប្លែកពីគ្នា ម្យ៉ាងទៀត Keyword ដែលមាននៅក្នុងភាសា C / C++ សុទ្ធសឹងតែជាអក្សរតូចទាំងអស់ ដូចជា asm , auto , bool ,break , case , catch , char , class , const , const_cast , continue , default , delete , do , double , dynamic_cast , else , enum , explicit , export , extern , false , float , for , friend , goto , reinterpret_cast , return , short , signed , sizeof , static , static_cast , struct , switch , template , this , throw , true , try , typedef , typeid , typename , union , unsigned , using , virtual , void , volatile , wchar_t , while .....
Declare Variable : DataType Name_Variable ;
ឧទាហរណ៍
int score ; // ការប្រកាសអថេរដោយមិនកំណត់តំលៃតំបូង
int score = 0; // កំណត់តំលៃតំបូងរបស់អថេរ ជាតួលេខចំនួនគត់ int
char charater;
char charater = 'A'; // កំណត់តួអក្សរ១តួតំបូង ដោយប្រើ ''
string name;
string name = "Jack"; // កំណត់អក្សរតំបូង ដោយប្រើ ""
int score1,score2; // ការប្រកាសអថេរលើសពីរក្នុងពេលតែមួយ
Variable Declaration គឺជាការប្រកាសអអថេរ
ការប្រកាសអថេរមួយត្រូវមាន DataType Identifier ដែលបព្ចាប់ដោយ ; ។
អថេរមាន 2 ប្រភេទធំៗ គឹ Global variable និង Local variable .
Global Variable :
គឺជាអថេរដែលគេប្រកាសនៅក្រៅ Function សំរាប់អោយ Functions នានាយកទៅប្រើ ។
Local Variable :
គឺជាអថេរដែលគេប្រកាសនៅក្នុង Function សំរាប់អោយបានតែ Function
នោះយកទៅប្រើប៉ុណ្ណោះ ។
Initialization គឺជាការប្រកាសអថេរដោយផ្តល់តំលៃមួយទៅអោយអថេរដោយផ្ទាល់ នៅពេលគេប្រកាស់អថេរនោះ ។
ឧ.int x = 100;
3.Operator
Operator ក្នុងភាសា C / C++ គេបែងជាច្រើនផ្នែកដូចជា :Arithmetic Operators ពិជណិត
សញ្ញា | អត្តន័យ | ឧទាហរណ៍ |
---|---|---|
+ | បូក | c = a + b ; |
- | ដក | d = e - d ; |
* | គុណ | a = b * c ; |
/ | ចែក | a = b / c ; |
% | សំណល់នៃការចែក | k = m % n ; |
Compound Assignment :
សញ្ញា | ឧទាហរណ៍ | អត្តន័យ |
---|---|---|
+= | c += b; | c = c + b ; |
-= | d -= e; | d = d -e ; |
*= | a *= c; | a = a * c ; |
/= | a /= c; | a = a / c ; |
%= | k %= n; | k = k % n ; |
Increase and decrease (++ , --)
ឧទាហរណ៍ | អត្តន័យ |
---|---|
n++; | ដំណើរការនូវតំលៃ n រួចហើយទើបបានបូក 1 បន្ថែមទៅលើតំលៃ n |
++n; | បូក 1 បន្ថែមទៅលើតំលៃ n រួចហើយទើបដំណើរការនូវតំលៃ n |
n--; | ដំណើរការនូវតំលៃ n រួចហើយទើបបានដក 1 ពីលើតំលៃ n |
--n; | ដក 1 ពីលើតំលៃ nរួចហើយទើបដំណើរការនូវតំលៃ n |
Relational and Equality Operators (==,!=,>,<,>=,<=)
ប្រើក្នុងលក្ខខ័ណ្ឌពិចារណា conditional
សញ្ញា | អត្ថន័យ | ឧទាហរណ៍ |
---|---|---|
== | ស្មើ | if(a==5).. |
!= | មិនស្មើ | if(a!=5).. |
> | ធំជាង | if(a>5).. |
< | តូចជាង | if(a<5).. |
>= | ធំជាងរឺស្មើ | if(a>=5).. |
<= | តូចជាងរឺស្មើ | if(a<=5).. |
Logical Operators (!,&&,||)
a | b | a&&b | a||b | !a |
---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
ឧទាហរណ៍ |
---|
!(5==5) // evaluates to false because the expression at its right (5==5) is true. !(6<=4) // evaluates to true because (6<=4) is false. !true // evaluates to false. !false // evaluates to true. |
Conditional Operator (?)
a = (7==5?4:3); // a = 3 cause 7==5 is false |
Code | លទ្ធផល |
---|---|
//conditional operator
#include <iostream> using namespace std;int main(){ int a,b,c; a=2; b=7; c=(a>b)?a:b; cout<<c; return 0; } | 7 |
Comma Operator (,)
a = ( b = 3 , b+2 ); |
4.Comment
int number; // declare variable number as integer char name; /* declare variable name as charactor */ |
//............. ប្រើបានតែ១បន្ទាប់ /*............ ប្រើបានច្រើនបន្ទាប់ ................ .........*/ |
5.Input / Output
Key word | Meaning | Example |
cin | បញ្ចូលទិន្នន័យ | cin>>n; |
cout | បង្ហាញទិន្នន័យ | cout<<n; |
Key word | Meaning | Example |
scanf | បញ្ចូលទិន្នន័យ | scanf("%d",n); |
printf | បង្ហាញទិន្នន័យ | printf("%d",n); |
No comments:
Post a Comment