Vi & Vim Tutorial
cite:
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
:set wrap
:set nowrap
Set tab spaces
1. vim /etc/vim/vimrc
2. set ts=4
Foreground and Background Switch
1) Ctrl + z
2) fg ....
http://arjanvandergaag.nl/blog/foreground-and-background-processes.html
hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white
hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white
※lightmagenta darkgray lightgrey darkgrey lightgreen lightgray darkmagenta gray white red grey darkred brown darkblue darkgreen lightblue yellow cyan
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
:set wrap
:set nowrap
Replace all string in document
- : %s / ______________ /\ ________________/ g
Set tab spaces
1. vim /etc/vim/vimrc
2. set ts=4
利用字串取代('^M' 要用 Ctrl+V 不要放開 Ctrl 再按 M 才能打出來喔!)
:%s/^M/\r/g
Display the line number in vim
- vim /etc/vim/vimrc
- :set number
Foreground and Background Switch
1) Ctrl + z
2) fg ....
http://arjanvandergaag.nl/blog/foreground-and-background-processes.html
Set current line highlight
set cursorlinehi CursorLine cterm=NONE ctermbg=darkred ctermfg=white
hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white
※lightmagenta darkgray lightgrey darkgrey lightgreen lightgray darkmagenta gray white red grey darkred brown darkblue darkgreen lightblue yellow cyan
Folding codes in vim
1. visual select the codes (v) (ctrl+v)
2. zf
3. za
相關文件
- Graphical vi-vim Cheat Sheet and Tutorial
- tab 切換
- Ctrl+PageUp | Ctrl+PageDown
- gt
- http://www.ptt.cc/bbs/Editor/M.1340702403.A.CFA.html
- Copy and Paste using Visual Selection
- Vim 懶人包 Janus
- Vim After 11 years
- Vim 套件 : PHP and javascript 檢查套件
- Vim Plugins
- Automatic Syntax Checking
- Vim 外掛管理工具 Vundle
- Vim 自動完成
- https://bitbucket.org/ns9tks/vim-autocomplpop/
- http://www.ptt.cc/bbs/CSIE_DBMS/M.1356354669.A.B0B.html
- Vim 設定檔
- **snipMate (自動完成 for, foreach, etc.)
- http://www.vim.org/scripts/script.php?script_id=2540
- http://www.catonmat.net/blog/vim-plugins-snipmate-vim/ (Also for Cygwin)
- http://net.tutsplus.com/tutorials/other/vim-essential-plugin-snipmate/ (**)
- **AutoComplPop (自動完成keywork)
- http://www.vim.org/scripts/script.php?script_id=1879
- http://www.ptt.cc/bbs/Linux/M.1260322500.A.CE3.html
- **Code folding
- http://smartic.us/2009/04/06/code-folding-in-vim/
- http://www.youtube.com/watch?v=2VQozxCH7D0
libcutils pthread
http://endroid.blogspot.com/2011/07/android-development-notes.html
------------------------------------------------------------------------------------------
http://www.cnblogs.com/ouling/archive/2011/09/08/2171337.html
------------------------------------------------------------------------------------------
http://www.cnblogs.com/ouling/archive/2011/09/08/2171337.html
The C Preprocessor
4. Conditionals
- A program may need to use different code depending on the machine or operating system it is to run on. In some cases the code for one operating system may be erroneous on another operating system; for example, it might refer to data types or constants that do not exist on the other system. When this happens, it is not enough to avoid executing the invalid code. Its mere presence will cause the compiler to reject the program. With a preprocessing conditional, the offending code can be effectively excised from the program when it is not valid.
- You may want to be able to compile the same source file into two different programs. One version might make frequent time-consuming consistency checks on its intermediate data, or print the values of those data for debugging, and the other not.
- A conditional whose condition is always false is one way to exclude code from the program but keep it as a sort of comment for future reference.
- e.g.
#ifdef#ifndef#endif- e.g.
#if (!defined(_DEBUG) && defined(USE_MYLIB))
- #endif
- 好處: 可同時檢測多個預編譯變數
- e.g.
- #if
- #else
- #endif
- e.g.
- #if
- #elif
- #elif
- #else
- #endif
- Delete codes
- #if 0
- #endif
5. Diagnostics (診斷)
- #error
- The directive ‘#error’ causes the preprocessor to report a fatal error ( enforce the compiler stopping compile ). The tokens forming the rest of the line following ‘#error’ are used as the error message.
- #ifdef A
- #error "ERROR A"
- #endif
- #warning
- The directive ‘#warning’ is like ‘#error’, but causes the preprocessor to issue a warning and continue preprocessing. The tokens following ‘#warning’ are used as the warning message.
6. #line
- The C preprocessor informs the C compiler of the location in your source code where each token came from.
- e.g.
#linelinenum#linelinenum filename#lineanything else
7. #pragma
- The method specified by the C standard for providing additional information to the compiler.
- e.g. #pragma token-sequence
- If token-sequence exist, then doing the correspond action.
8. #ident
9.#using
10. Operator in preprocessor
- #
- stringizing operator
- ##
- token pasting operator
- define()
- stringizing operator
- token pasting operator
11. Miscellaneous directive
- #pragma
- #line
- #error
gcc 編譯過程
一個編譯過程通常需要4道程序
- 預處理 (Pre-Processing) 先處理那些#ifdef #define這些東西並做一些巨集代換
- 編譯 (Compiling) 做語意分析,翻譯成組合語言
- 彙編 (Assembling) 翻成機器碼與OS有關的格式,做成relocatable obj檔
- 鏈接 (Linking) 找到symbol(函式,變數名)與程式庫(shared obj)中的副程式 ,做成可執行obj檔(executable obj)
- gcc -Wall -pedantic -ansi (常用)
e.g.
Syntax
#define identifier replacement-list(optional) | (1) | ||||||||
#define identifier( parameters ) replacement-list | (2) | ||||||||
#define identifier( parameters, ... ) replacement-list | (3) | (since C99) | |||||||
#define identifier( ... ) replacement-list | (4) | (since C99) | |||||||
#undef identifier | (5) | ||||||||
Explanation
#define directives
The
#define directives define the identifier as a macro, that is they instruct the compiler to replace all successive occurrences of identifier with replacement-list, which can be optionally additionally processed. If the identifier is already defined as any type of macro, the program is ill-formed unless the definitions are identical.Object-like macros
Object-like macros replace every occurrence of a defined identifier with replacement-list. Version (1) of the
#definedirective behaves exactly like that.Function-like macros
Function-like macros replace each occurrence of a defined identifier with replacement-list, additionally taking a number of arguments, which then replace corresponding occurrences of any of the parameters in the replacement-list. The number of arguments must be the same as the number of arguments in the macro definition (parameters) or the program is ill-formed. If the identifier is not in functional-notation, i.e. does not have parentheses after itself, it is not replaced at all.
Version (2) of the
#define directive defines a simple function-like macro.
Version (3) of the
#define directive defines a function-like macro with variable number of arguments. The additional arguments can be accessed using __VA_ARGS__ identifier, which is then replaced with arguments, supplied with the identifier to be replaced.
Version (4) of the
#define directive defines a function-like macro with variable number of arguments, but no regular arguments. The arguments can be accessed only with __VA_ARGS__ identifier, which is then replaced with arguments, supplied with identifier to be replaced.
# and ## operators
In function-like macros, a
# operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal. In addition, the preprocessor adds backslashes to escape the quotes surrounding embedded string literals, if any, and doubles the backslashes within the string as necessary. All leading and trailing whitespace is removed, and any sequence of whitespace in the middle of the text (but not inside embedded string literals) is collapsed to a single space. This operation is called "stringification". If the result of stringification is not a valid string literal, the behavior is undefined.
When
# appears before __VA_ARGS__, the entire expanded __VA_ARGS__ is enclosed in quotes:#define showlist(...) puts(#__VA_ARGS__) showlist(); // expands to puts("") showlist(1, "x", int); // expands to puts("1, \"x\", int") | (since C99) |
A
## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.
Note: some compilers offer an extension that allows ## to appear after a comma and before __VA_ARGS__, in which case the ## does nothing when __VA_ARGS__ is non-empty, but removes the comma when __VA_ARGS__ is empty: this makes it possible to define macros such as
fprintf (stderr, format, ##__VA_ARGS__)Macro offsetof
#include
#define offsetof(type, member) (size_t)&(((type*)0)->member)
Macro container_of
#include <linux/kernel.h>
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
相關文件
- The C Preprocessor
- C Preprocessor tricks
- https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
- GCC options
- GCC 簡介
- GNU Preprocessor
- http://tigcc.ticalc.org/doc/cpp.html#SEC46
- Others
- http://edisonx.pixnet.net/blog/post/42489122-%E9%A0%90%E8%99%95%E7%90%86%E5%99%A8(preprocessor)
- http://www.cquestions.com/2011/01/preprocessor-in-c.html
- http://www.cprogramming.com/tutorial/cpreprocessor.html
- http://qualitycoding.org/preprocessor/
Program Elapsed time
#include <stdio.h>
#include <time.h>
clock_t start = clock();
/* Code you want timed here */
printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
Reduce execution time of a C program?
1) Use the right algorithms. Look into the Big O efficiency of any algorithms you are using, and make sure there aren't more efficient algorithms available.
2) Think about trading using more memory for a faster algorithm. Perhaps pre-computing tables of intermediate results.
3) Use a profiler to see where the bottle necks are and try improving them.
4) Optimize the inner most loops using assembly language to get the last little bit faster.
5) Run it on a faster computer.
6) Make it run in parallel across many computers or CPUs. This is especially good on the newer Intel chips where you have access to multiple CPUs on one chip. Distributed network computing sometimes is a good idea. Cloud computing is another possibility these days.
7) Make sure that C really is the right answer. It might not be in all cases.
Read more: http://wiki.answers.com/Q/How_do_you_reduce_execution_time_of_a_C_program#ixzz24RwdgPVY
Subscribe to:
Posts (Atom)