Time of Update: 2018-12-07
所有基礎的資料類型, 最大的也不過 10 個位元組;我們可以自訂的資料類型 -- "結構", 通過把若干類型組合在一起, 讓一種類型可以大很多.我們知道, 一個位元組有八個 Bit 組成; 能否把一種類型縮小、縮小到 Bit 級? 結構中的 "位欄位" 是以 Bit 為單位的, 這已經是電腦的最小單位, 大小是 char 類型的 1/8.下面的例子中定義的位欄位, 分別有 1-4 Bit 大小; 1 Bit 的欄位只能放兩個數(0、1), 4 Bit 的欄位也只能放下 16 個數:1.
Time of Update: 2018-12-07
在結構中, 每個欄位的資料類型是唯一的; 使用聯合可以在一個欄位中儲存不同的資料類型.不同的資料類型共用一塊記憶體. 當然其記憶體大小應依著大的來.聯合中的資料, 非此即彼, 只有一個有效; 應該有說明在某個資料中此時有用的是什麼類型.除了共用記憶體以外, 聯合與結構一樣.1. union 的大小是其中最大資料成員的大小:#include <stdio.h>int main(void){ union MyUnion { short n1; int
Time of Update: 2018-12-07
自訂的類型名一般要大寫, 以提示這是自命名的類型.1. 把 unsigned long 更名為 UINT:#include <stdio.h>int main(void){ typedef unsigned long UINT; UINT num = 1234567890; printf("%lu\n", num); getchar(); return 0;}2. 給一個結構重新命名:#include <stdio.h>int
Time of Update: 2018-12-07
1. 如果函數沒有聲明, 應該在調用前定義:#include <stdio.h>/* 定義求大值函數 */int MAX(int x, int y) { if (x > y) return x; else return y;} /* 定義求小值函數 */int MIN(int x, int y) { return x 2. 可以在函數頭聲明:#include <stdio.h>int main(void){
Time of Update: 2018-12-07
C 語言的函數可以返回數組以外(不包括數組指標)的任何類型.不寫明傳回型別, 在以前的 C 語言版本中是預設返回 int, 現在 C99、C++ 已不支援.void 表示沒有傳回值, 也就無須 return 語句; 如果是其他語句必須有 return 語句.在 main 函數中(只是 main 函數)若忘掉 return, 有些編譯器會自動添加 return 0;1. 每執行 return 語句函數將會退出:#include <stdio.h>int main(void){
Time of Update: 2018-12-07
if (bool) { } else { }switch (v) { case v1: ... break; case v2: ... break; ... default: ... break; }do { } while (bool);while (bool) { }for (int i; i C# 的 switch 語句支援字串, 但好像只能用 const string 類型的變數.using System;class MyClass{ static void Main() {
Time of Update: 2018-12-07
顯示枚舉、枚舉值、枚舉名:using System;class MyClass{ enum MyEnum { AA, BB, CC }; /* 類型定義不能在函數體內 */ static void Main() { MyEnum e = MyEnum.CC; Console.WriteLine(e); // CC Console.WriteLine((byte)e); //
Time of Update: 2018-12-07
1. 傳值參數(非指標參數):#include <stdio.h>int inc(int x);int main(void){ int num = 1; printf("%d\n", inc(num)); /* 2 */ printf("%d\n", num); /* 1; num 並沒有改變, 用作函數參數時只是複製過去 */ getchar(); return 0;}int inc(int x) { x++;
Time of Update: 2018-12-07
1. 關於數組的首地址:#include <stdio.h>int main(void){ char cs[2][3] = { {'A','B','C'}, {'D','E','F'} }; char *p1,*p2,*p3,*p4; p1 = p2 = p3 = p4 = NULL; /* 下面四個指標都是指向了同一個地址 */
Time of Update: 2018-12-07
C 語言的記憶體配置很簡單: malloc、calloc、realloc、freemalloc(位元組數); 返回記憶體段的首地址, void 的.calloc(個數, 類型大小); 和 malloc 的區別就是它會初始化記憶體為空白.realloc(原指標, 位元組數); 重新分配由 malloc、calloc 分配的記憶體; 這裡有太多注意事項:1、如果縮小了, 會截掉一塊, 會保留前面的內容;2、如果擴大了, 仍會保留已有的內容, 但新加的記憶體不會初始化;3、在擴大時,
Time of Update: 2018-12-07
1. 局部變數:局部變數也叫自動變數, 它聲明在函數開始, 生存於棧, 它的生命隨著函數返回而結束.#include <stdio.h>int main(void){ auto int i = 9; /* 聲明局部變數的關鍵字是 auto; 因可以省略, 幾乎沒人使用 */ printf("%d\n", i); getchar(); return 0;}2. 全域變數:全域變數聲明在函數體外, 一般應在函數前; 每個函數都可以使用它,
Time of Update: 2018-12-07
1. 結構就是多個變數的集合:#include <stdio.h>int main(void){ struct Rec { int x; int y; }; struct Rec r1; r1.x = 111; r1.y = 222; printf("%d, %d", r1.x, r1.y); getchar(); return 0;}2. 定義時同時聲明變數:#include
Time of Update: 2018-12-07
數組參數屬於指標參數.指標參數即時傳址參數(或叫引用參數), 如果想在函數中修改參數的值, 這是唯一的途徑.如果把數組當作參數, 不管你願意與否, 它就是指標, 指向第一個值的指標.1. 數組參數就是指向第一個元素的指標:#include <stdio.h>void getArr(int p[], int si);int main(void){ int ns[] = {1,2,3,4,5}; getArr(ns, sizeof(ns)/sizeof(ns[0]));
Time of Update: 2018-12-07
1. 通過 &變數 可以擷取變數地址:#include <stdio.h>int main(void){ int num = 10; printf("變數值: %d\n", num); printf("變數址: %d\n", &num); getchar(); return 0;}2. 表示變數地址的變數就是指標:#include <stdio.h>int main(void){ int num = 10;
Time of Update: 2018-12-07
非指標參數(也就是傳值參數)不會被修改原始值, const 對它是沒有意義的.const 只用於指標.1. 第一種用法: const 類型 *變數:這種用法將限制修改指標指向的值.#include <stdio.h>int fun(const int *p) { *p += 1; /* 只有去掉 const 這句才可以執行 */ return *p;}int main(void){ int num = 3; printf("%d\n", fun(&
Time of Update: 2018-12-07
先複習函數的定義與函數的聲明://這是一個求和函數的定義:int add(int x, int y){ return(x + y);}//可以這樣聲明:int add(int x, int y);//也可以這樣聲明:int add(int, int);定義一個函數指標聲明一個函數差不多, 用 (* ) 包括函數即可://像這樣:int (*pfun)(int, int);//或這樣:int (*pfun)(int x, int y);//也可以:typedef int
Time of Update: 2018-12-07
本例:代碼:using System;class MyClass{ static void Main() { Console.WriteLine("sbyte 類型:"); sbyte sbyteVar = (sbyte.MaxValue + sbyte.MinValue) / 2; Console.WriteLine("{0}、{1}、{2}\n", sbyte.MinValue, sbyteVar, sbyte.MaxValue);
Time of Update: 2018-12-07
說實話, 有了 Delphi 真的很難喜歡 C++; 起碼對我是這樣.在我學點 C 與 C++ 的同時, 更加感到 Delphi 的優秀: 它就是 C 與 C++ 集合、與進步.但當 Delphi 過了入門關以後, 學習資料實在是太少了!很多好的技術是用 C/C++ 描述的... 因此我不得不硬著頭皮去瞭解一些.起初僅僅是為了更好地看懂那些書, 但也有意外收穫: 換個視角看 Delphi、能更好地理解 Delphi.但我不認為這是繼續深入 Delphi 的好方法, 實在是別無選擇;
Time of Update: 2018-12-07
基本: . () [] x++ x-- new typeof checked unchecked -> ::一元: + - ! ~ ++x --x (T)x True False & sizeof乘除: * / %加減: + -移位: >關係: = is as 相等: == !=邏輯: & ^ |條件: && ||賦值: = += -= *= /= %= &= |=
Time of Update: 2018-12-07
使用 Convert 類:ToBoolean -> boolToByte -> byteToChar -> charToDateTime -> DateTimeToDecimal -> decimalToDouble -> doubleToInt16 -> shortToInt32 -> intToInt64 -> longToSByte -> sbyteToSingle ->