MSB = Most Significant Bit
簡單來說等於最左邊的 Bit,第 2^{n-1} 位的 Bit
LSB = Least Significant Bit
簡單來說等於最右邊的 Bit,第 0 位的 Bit
MS Byte、LS Byte
Bit 換成 Byte 的概念。
int (*pfi)();
int *pfi();
extern int f1();
typedef int (*funcptr)();
funcptr pfi;
pfi = &f1;
實際上你可以省略 &,因為 function 若不呼叫,他就只能回傳 pointer ( 原文 : When you mention the name of a function but are not calling it, there's nothing else you could possibly be trying to do except generate a pointer to it. ) pfi = f1;
int (*pfi)(arg1, arg2);
int *pfi(arg1, arg2);
int pfi(arg1, arg2);
typedef unsigned int size_t // 通常定義在 stddef.h
int8 : -128 ~ 127
int16 : -32768 ~ 32767
int32 : -2147483648 ~ 2147483647
int64 : -9223372036854775808 ~ 9223372036854775807
uint8 : 0 ~ 255
uint16 : 0 ~ 65535
uint32 : 0 ~ 4294967295
uint64 : 0 ~ 18446744073709551615
uint8_t : 0xFF
uint16_t : 0xFFFF
uint32_t : 0xFFFFFFFF
uint64_t : 0xFFFFFFFFFFFFFFFF
void
showMalloc(int pointerValue)
{
int *p = malloc(sizeof(int));
printf("Address : %p\n", p);
printf("Value : %d\n", *p);
*p = pointerValue;
printf("Address : %p\n", p);
printf("Value : %d\n", *p);
free(p);
}
Address : 00682B90
Value : 6826496
Address : 00682B90
Value : 100
void
showMalloc(int pointerValue)
{
int *p = calloc(1, sizeof(int));
printf("Address : %p\n", p);
printf("Value : %d\n", *p);
*p = pointerValue;
printf("Address : %p\n", p);
printf("Value : %d\n", *p);
free(p);
}
Address : 00722B90
Value : 0
Address : 00722B90
Value : 100
#ifndef HELLO_H // 有些人會定義成 _HELLO_H_
#define HELLO_H // 但目的就是不會被重複編譯
#include <stdlib.h>
int helloIntro(char** str);
int main() __attribute__((weak));
#endif
#ifdef DEBUG
int main ()
{
return 0;
}
#endif
gcc -DEBUG other.c -o other.x
int main() __attribute__((weak));
int main ()
{
return 0;
}
int main()
{
char str[5];
sprintf(str, "ABC");
printf("%s\n", str); // output : ABC
return 0;
}
int main()
{
char str[5];
sprintf(str, "ABCDEF");
printf("%s\n", str); // output : ABCDEF
return 0;
}
int main()
{
int ret;
char str[5];
ret = snprintf(str, 5, "ABC");
printf("%s\n", str); // output : ABC
printf("%d\n", ret); // output : 3
return 0;
}/code>
int main()
{
int ret;
char str[5];
ret = snprintf(str, 5, "ABCDEF");
printf("%s\n", str); // output : ABCD
printf("%d\n", ret); // output : 6
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<syslog.h>
syslog(LOG_INFO, "hello %s", "woring");
syslog(LOG_ERR, "hello %s", "test");
底下為參數 int priorityLOG_USER | : A miscellaneous user process |
LOG_MAIL | |
LOG_DAEMON | : A miscellaneous system daemon |
LOG_AUTH | : Security (authorization) |
LOG_SYSLOG | : Syslog |
LOG_LPR | : Central printer |
LOG_NEWS | : Network news (e.g. Usenet) |
LOG_UUCP | : UUCP |
LOG_CRON | : Cron and At |
LOG_AUTHPRIV | : Private security (authorization) |
LOG_FTP | : Ftp server |
LOG_LOCAL(0~7) | : Locally defined |
enum direction
{
North, // 0
South, // 1
East, // 2
West // 3
};
typedef enum direction Direction;
enum direction
{
North,
South,
East,
West
};
int main(void)
{
Direction dest = East;
return 0;
}
#define Max 10
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MINUS(a,b) a - b // 注意這個 a-b 沒加括號很容易出錯
#define MINUS(a,b) (a - b) // Good.
#define MACRO(arg1, arg2) \
do \
{ \
test1; \
test2; \
} while (0)
extern int func(void);
int __attribute__((weak)) func(void)
{
return 0;
}
void real_func()
{
printf("strong func\n");
}
weak.so void real_func() __attribute__((weak))
{
printf("weak func\n");
}
main.c extern void real_func();
void main()
{
real_func();
}
結果 :
// 動態連結無效,只看順序
gcc main.c -lstrong -lweak // 輸出 : strong func
gcc main.c -lweak -lstrong // 輸出 : weak func
// 正常 .o 檔
gcc main.c weak.o strong.o // 輸出 : strong func
gcc main.c strong.o weak.o // 輸出 : strong func
void unsignedcharTest(unsigned char value)
{
char c = value;
unsigned char uc = value;
int char_to_int = c;
int unchar_to_int = uc;
unsigned int char_to_unint = c;
unsigned int unchar_to_unint = uc;
printf("%%c: %c, %c\n", c, uc);
printf("%%X: %X, %X\n", c, uc);
printf("%%d: %d, %d\n", char_to_int, unchar_to_int);
printf("%%u: %u, %u\n", char_to_unint, unchar_to_unint);
}
int main()
{
unsignedcharTest(0x7F); // 0111 1111 符號 bit 為 0 不會擴展
unsignedcharTest(0x80); // 1000 0000 符號 bit 為 1 會擴展,補 1
return 0;
}
結果 :
%c: ,
%X: 7F, 7F
%d: 127, 127
%u: 127, 127
%c: ,
%X: FFFFFF80, 80
%d: -128, 128
%u: 4294967168, 128
#include "hello.h"
int count(){
static int count = 0;
++count;
return count;
}
(2) 宣告在 function 外 ( 連結模式 )