随笔分类 - C语言实例
摘要:1.#include <stdio.h>int func(int x){ if( x > 1 ) { return x * func(x - 1); } else { return 1; }}int main(){ printf("x! = %d\n", func(4)); return 0;}说明
阅读全文
摘要:1.#include <stdio.h>#include <malloc.h>#define MALLOC(type, n) (type*)malloc(n * sizeof(type))int main(){ int* p = MALLOC(int, 5); int i = 0; for(i=0;
阅读全文
摘要:1.#include <stdio.h>int main(){ int k = 2; int a = 1; k = k++ + k++; printf("k = %d\n", k); if( a-- && a ) { printf("a = %d\n", a); } return 0;} 2.#in
阅读全文
摘要:1.#include <stdio.h>#include <malloc.h>void f(int* p, int size){ int i = 0; for(i=0; i<size; i++) { printf("%d\n", p[i]); } free(p);}int main(){ int*
阅读全文
摘要:1.栈,堆和静态存储区是 堆和静态存储区是C语言程序常涉及的三个基本内语言程序常涉及的三个基本内存区2.栈区主要用于函数调用的使用3.堆区主要是用于内存的动态申请和归还4.静态存储区用于保存全局变量和静态变量
阅读全文
摘要:1.#include <stdio.h>#include <malloc.h>int main(){ int i = 0; int* pI = (int*)malloc(5 * sizeof(int)); short* pS = (short*)calloc(5, sizeof(short)); f
阅读全文
摘要:1.#include <stdio.h>typedef int(FUNC)(int);int test(int i){ return i * i;}void f(){ printf("Call f()...\n");}int main(){ FUNC* pt = test; void(*pf)()
阅读全文
摘要:1.#include <stdio.h>void access(int a[][3], int row){ int col = sizeof(*a) / sizeof(int); int i = 0; int j = 0; printf("sizeof(a) = %d\n", sizeof(a));
阅读全文
摘要:1.#include <stdio.h>int main(int argc, char* argv[], char* env[]){ int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; int i = 0; int j = 0; for(i=0; i<3
阅读全文
摘要:1.#include <stdio.h>typedef int(AINT5)[5];typedef float(AFLOAT10)[10];typedef char(ACHAR9)[9];int main(){ AINT5 a1; float fArray[10]; AFLOAT10* pf = &
阅读全文
摘要:1.#include <stdio.h>#include <assert.h>size_t strlen(const char* s){ return ( assert(s), (*s ? (strlen(s+1) + 1) : 0) );}int main(){ printf("%d\n", st
阅读全文
摘要:1.#include <stdio.h>int main(){ int a[5] = {1, 2, 3, 4, 5}; int* p1 = (int*)(&a + 1); int* p2 = (int*)((int)a + 1); int* p3 = (int*)(a + 1); printf("%
阅读全文
摘要:1. #include <stdio.h>// another file// char* p = "Hello World!";extern char p[];int main(){ printf("%s\n", p); return 0;} 说明: 数组是一片连续的内存空间数组的地址和数组首元素
阅读全文
摘要:1. #include <stdio.h>int main(){ int i = 5; int* p = &i; printf("%d, %08X\n", i, p); *p = 10; printf("%d, %08X\n", i, p); return 0;} 说明: const int* p;
阅读全文
摘要:1.#include <stdio.h>#define NAME(n) name##nint main(){ int NAME(1); int NAME(2); NAME(1) = 1; NAME(2) = 2; printf("%d\n", NAME(1)); printf("%d\n", NAM
阅读全文
摘要:1.#include <stdio.h>#if defined(ANDROID20) #pragma message("Compile Android SDK 2.0...") #define VERSION "Android 2.0"#elif defined(ANDROID23) #pragma
阅读全文
摘要:1.#include <stdio.h>#define CONST_NAME1 "CONST_NAME1"#define CONST_NAME2 "CONST_NAME2"int main(){ #ifndef COMMAND #warning Compilation will be stoped
阅读全文
摘要:#include <stdio.h>#define C 1int main(){ #if( C == 1 ) printf("This is first printf...\n"); #else printf("This is second printf...\n"); #endif return
阅读全文
摘要:#include <stdio.h>int f1(int a, int b){ #define _MIN_(a,b) ((a)<(b) ? a : b) return _MIN_(a, b);}int f2(int a, int b, int c){ return _MIN_(_MIN_(a,b),
阅读全文
摘要:1.#include <stdio.h>int main(){ int i = -2; unsigned int j = 1; if( (i + j) >= 0 ) { printf("i+j>=0\n"); } else { printf("i+j<0\n"); } printf("i+j=%d\
阅读全文