Difference between revisions of "C"

From ElectroDragon
Jump to navigation Jump to search
(Complier)
(Redirected page to Category:C)
(Tag: New redirect)
 
Line 1: Line 1:
== Language ==
+
#redirect [[category: C]]
* [https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Operators in C]
 
== Compiler ==
 
* Linux GCC
 
* HI-TECH PICC for PIC
 
 
 
=== Declaration ===
 
* [http://en.cppreference.com/w/cpp/language/enum enum]
 
* [http://c.biancheng.net/cpp/html/88.html struct]
 
=== Data type ===
 
* [https://en.wikipedia.org/wiki/Primitive_data_type Wiki Primitive data type]
 
* data-types are (from [http://sdcc.sourceforge.net/doc/sdccman.html/node3.html SDCC Manual]):
 
Classic basic primitive types may include:
 
* Character (character, char);
 
* Integer (integer, int, short, long, byte) with a variety of precisions;
 
* Floating-point number (float, double, real, double precision);
 
* Fixed-point number (fixed) with a variety of precisions and a programmer-selected scale.
 
* Boolean, logical values true and false.
 
* Reference (also called a pointer or handle), a small value referring to another object's address in memory, possibly a much larger one.
 
More sophisticated types which can be built-in include:
 
* Tuple in Standard ML, Python, Scala
 
* List in Common Lisp, Scheme, Haskell
 
* Complex number in C99, Fortran, Common Lisp, Python, D, Go
 
* Rational number in Common Lisp, Haskell
 
* Associative array in Perl, Python, Ruby, Javascript, Lua, D, Go
 
* First-class function, in all functional languages, Javascript, Lua, D, Go, and in newer standards of C++, Java, C#, Perl
 
{| border="1" cellspacing="0" cellpadding="5"
 
!  type !! width !! default !! signed range !! unsigned range
 
|-
 
| bool || 1 bit || unsigned || - || 0, 1
 
|-
 
| char || 8 bits, 1 byte || signed || -128 to +127 || 0 to +255
 
|-
 
| short || 16 bits, 2 bytes || signed || -32,768 to +32,767 || 0 to +65,535
 
|-
 
| int || 16 bits, 2 bytes || signed || -32,768 to +32,767 || 0 to +65,535
 
|-
 
| long || 32 bits, 4 bytes || signed || -2,147,483,648 to +2,147,483,647  || 0 to +4,294,967,295
 
|-
 
| float || 4 bytes IEEE 754 || signed || || 1.175494351E-38 to 3.402823466E+38
 
|-
 
| pointer || 1, 2, 3 or 4 bytes || generic || ||
 
 
 
|}
 
 
 
 
 
Easy to remember integer data types :
 
 
 
{| border="1" cellspacing="0" cellpadding="5"
 
! data type !! width !! signed/<br>unsigned !!range
 
|-
 
|align="center" | s8 || 8 bits (1 byte) || signed || -128 to +127
 
|-
 
|align="center" | s16 || 16 bits (2 bytes) || signed || -32,768 to +32,767
 
|-
 
|align="center" | s32 || 32 bits (4 bytes) || signed || -2,147,483,648 to +2,147,483,647
 
|-
 
|align="center" style="border-bottom:2px solid gray;" | s64*
 
|style="border-bottom:2px solid gray;" | 64 bits (8 bytes)
 
|style="border-bottom:2px solid gray;" | signed
 
|style="border-bottom:2px solid gray;" |  −9,223,372,036,854,775,808 <br> to 9,223,372,036,854,775,807
 
|-
 
|align="center" | u8 || 8 bits (1 byte) || unsigned || 0 to +255
 
|-
 
|align="center" | u16 || 16 bits (2 bytes) || unsigned || 0 to +65,535
 
|-
 
|align="center" | u32 || 32 bits (4 bytes) || unsigned || 0 to +4,294,967,295
 
|-
 
|align="center" | u64* || 64 bits (8 bytes) || unsigned || 0 to +18,446,744,073,709,551,615
 
|}
 
 
 
=== Data Format ===
 
{| class="wikitable sortable"
 
|-
 
! From !! to !! function !! example !! description !! reference
 
|-
 
| String || char || toCharArray || sensorVal.toCharArray(sensorPrintout, 4); ||  ||TFT
 
|-
 
| float || string || dtostrf || dtostrf(f_volt, 6, 2, volt_char);    || f_volt is float value
 
|-
 
| Example || Example || Example
 
|}
 
 
 
*
 
* printf
 
* String aaa = String(bbb) // bbb is char array
 
 
 
=== Data Type ===
 
{| class="wikitable sortable"
 
|-
 
! - !! - !! -
 
|-
 
| Byte || 8 bit || Example
 
|-
 
| Int || Example || Example
 
|-
 
| Char || Example || Example
 
|-
 
| Hex || Example || Example
 
|}
 
 
 
== Operations ==
 
=== Relations ===
 
* == 检查两个操作数的值是否相等,如果相等则条件为真。 (A == B) 不为真。
 
* != 检查两个操作数的值是否相等,如果不相等则条件为真。 (A != B) 为真。
 
* > 检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (A > B) 不为真。
 
* < 检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (A < B) 为真。
 
* >= 检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (A >= B) 不为真。
 
* <= 检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (A <= B) 为真。
 
=== Logic Operations ===
 
* && 称为逻辑与运算符。如果两个操作数都非零,则条件为真。 (A && B) 为假。
 
* || 称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (A || B) 为真。
 
* ! 称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 !(A && B) 为真。
 
=== Bit Opeartions ===
 
* & 如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (A & B) 将得到 12,即为 0000 1100
 
* | 如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (A | B) 将得到 61,即为 0011 1101
 
* ^ 如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (A ^ B) 将得到 49,即为 0011 0001
 
* ~ 二进制补码运算符是一元运算符,具有"翻转"位效果,即0变成1,1变成0。 (~A ) 将得到 -61,即为 1100 0011,一个有符号二进制数的补码形式。
 
* << 二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 A << 2 将得到 240,即为 1111 0000
 
* >> 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111
 
=== Assignment Operations ===
 
* = 简单的赋值运算符,把右边操作数的值赋给左边操作数 C = A + B 将把 A + B 的值赋给 C
 
* += 加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数 C += A 相当于 C = C + A
 
* -= 减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数 C -= A 相当于 C = C - A
 
* *= 乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数 C *= A 相当于 C = C * A
 
* /= 除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数 C /= A 相当于 C = C / A
 
* %= 求模且赋值运算符,求两个操作数的模赋值给左边操作数 C %= A 相当于 C = C % A
 
* <<= 左移且赋值运算符 C <<= 2 等同于 C = C << 2
 
* >>= 右移且赋值运算符 C >>= 2 等同于 C = C >> 2
 
* &= 按位与且赋值运算符 C &= 2 等同于 C = C & 2
 
* ^= 按位异或且赋值运算符 C ^= 2 等同于 C = C ^ 2
 
* |= 按位或且赋值运算符 C |= 2 等同于 C = C | 2
 
=== MISC ===
 
* sizeof() 返回变量的大小。 sizeof(a) 将返回 4,其中 a 是整数。
 
* & 返回变量的地址。 &a; 将给出变量的实际地址。
 
* * 指向一个变量。 *a; 将指向一个变量。
 
* ? : 条件表达式 如果条件为真 ? 则值为 X : 否则值为 Y
 
 
 
== Header file Definition ==
 
=== Define macro ===
 
For examples set pins for segment display, file seg.h
 
* Further refer to SFR_Macro.h in BSP of nuvoton
 
<source lang="c">
 
#define set_a      P05 = 1
 
#define set_b      P03 = 1
 
#define set_c      P10 = 1
 
#define set_d      P01 = 1
 
#define set_e      P07 = 1
 
#define set_f      P02 = 1
 
#define set_g      P14 = 1
 
#define set_dot    P00 = 1
 
</source>
 
 
 
=== Define type ===
 
<source lang="c">
 
typedef bit                  BIT;
 
typedef unsigned char        UINT8;
 
typedef unsigned int          UINT16;
 
typedef unsigned long        UINT32;
 
 
 
typedef unsigned char        uint8_t;
 
typedef unsigned int          uint16_t;
 
typedef unsigned long        uint32_t;
 
</source>
 
 
 
== Complier ==
 
* [[Mingw]] for windows
 
== Common Header ==
 
* [[string_h]]
 
[[category: Topics]]
 

Latest revision as of 04:04, 8 November 2019

Redirect to: