數值轉字串:
字串轉數值:
#include <string.h>
size_t strlen ( const char * str );
#include <stdio.h>
#include <string.h>
#define STR "I am fine."
int main(void) {
// strlen()
printf("%zd", strlen(STR));
// output 10
// sizeof
printf("%zd", sizeof STR);
// output 11 (contain '\0' means null character.)
sizeof
is an operator.
// example
printf("split \
first line.");
//output
split first line.
gcc sample.c -lthis_dll_sample
// print octal and hexadecimal
printf("%#o, %#x\n", octal, hexadecimal);
// "short int" or short
short int a_1;
// or
short a_2;
// "long int" or "long"
long int b_1;
// or
long b_2
// "long long int" or "long long", at least 64-bit cpu.
long long int c_1;
// or
long long c_2;
printf("%f\n", -1.32e-11);
// example
_Bool b_1;
// C99, stdbool.h
bool b_2;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define SWAP(a, b, tmp) ((tmp) = (a), (a) = (b), (b) = (a))
#ifndef SOME_H
#define SOME_H
/* Declare some data types and public functions. */
#endif
// 測測編譯環境
#if defined(_WIN32)
#define PLATFORM "windows"
#elif defined(__linux__)
#define PLATFORM "Linux"
#elif defined(__APPLE__)
#define PLATFORM "mac"
#else
#define PLATFORM "other os"
#endif
#include <stdio.h>
#define pow(a) (#a)
int main(int argc, char *argv[]) {
printf("%s\n", pow(sdkfjsdkf));
}
#define test(...) (printf("%d\n", __VA_ARGS__))
...
test(5000);
// output: 5000
#define test(a,b) (printf("%d\n", a##b))
...
test(50,6);
// output: 506
#define test(5,6) ( \
printf("%d\n", a##b))
#define cmp(a,b,tmp)({ \
if((a)>(b)) { \
tmp = -1; \
} else if(((a)<(b))) { \
tmp = 1; \
} \
else { \
tmp = 0; \
} \
})
#pragma once
#ifndef UTILS_H
#define UTILS_H
/* ... */
#endif
參考
更多巨集
#include <assert.h>
/* ... */
int a;
scanf("%d", &a);
assert(a>10);
printf("a == %d", a);
int width, percision;
scanf("%d, %d", &width, &percision);
printf("%*.*f", width, percision, 3.2)
int status;
int code;
while(status=scanf("%d", code)){
if(status != 1)
scanf("%*s"); // dispose non-integer
printf("Enter an integer, please.\n");
}
#include <stdio.h>
#define FREEZING 4.0
int main(int argc, char *argv[]) {
float temperature;
int all_day = 0;
int cold_day = 0;
printf("Input temperature value. (any character to quit)\n");
while(scanf("%f", &temperature)) {
all_day++;
if(temperature < FREEZING)
cold_day++;
}
if(all_day==0)
printf("No data!\n");
else
printf("%d cold day. Total of %d day(s).\n", cold_day, all_day);
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
char ch;
/* move characters. */
/* getchar() be a statement. */
/* while( (ch=getchar() ) != '\n'), don't need "ch = getchar()" in end. */
/* The operator '!=' is higher priority than '='. Therefore, It need to bracket on "ch = getchar()". */
ch = getchar();
while(ch != '\n') {
if(ch == ' ') {
putchar(ch);
} else {
putchar(ch+1); // encryption
}
ch = getchar();
}
putchar(ch); // print the newline.
return 0;
}
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
char ch;
/* move characters. */
while( (ch=getchar()) != '\n') {
if(ch == isalpha()) {
putchar(ch+1); // encryption
} else {
putchar(ch);
}
}
putchar(ch); // print the newline.
return 0;
}
cplusplus - cctype
while( (ch=getchar()) != EOF);
#include <stdio.h>
#define SIZE 10
int total(int *start, int *end)
{
int sum = 0;
while(start < end) {
sum += *start;
start++;
}
return sum;
}
int main(int argc, char *argv[]) {
int a[SIZE];
int sum;
for(int i=0; i<SIZE; i++)
a[i] = 10;
sum = total(a, a+SIZE);
printf("%d\n", sum);
return 0;
}
*(array_a + 2) == array_a[2];
*(*(array_b + 4) + 5) == array_b[4][5];
int a[3][2] = {{1,2}, {3,4}, {5,6}};
int (*p)[2]; // The 'p' is an address that point to two ints' address.
/* Alternatively, p[][2] */
p = a+1;
printf("%d\n", (*p)[1]);
/* output: 4 */
#define COLS 4
int Sum(int ar[][COLS], int rows)
{
int total = 0;
for(int r=0; r<rows; r++)
for(int c=0; c<COLS; c++)
total += ar[r][c];
return total;
}
假設要傳2維array, 其rows和columns也是傳送參數,則rows和columns要先傳(寫在左邊)
int Sum(int rows, int cols, int ar[rows][cols]);
C99/C11 可以捨棄變數名稱int Sum(int, int, int ar[*][*]);
int div[2] = {10, 20};
/* equal */
/* nameless */
(int [2]){10,20};
/* initialize */
(int []){50, 20, 100}; // a compound literal with 3 elements.
/* example */
int *pt1;
pt1 = (int [2]) {10, 20};
/* example */
/* pass parameters */
int sum(const int1r[], int n);
...
int total;
total = sum((int []{4, 2, 2, 4, 5}, 6);
/* example */
int (*pt)[4]; // int pt[][4] can not initialize.
pt = (int [2][4]){ {1,2,3,4}, {5,6,7,8} };
puts();
char str[] = "First" " Second"
" Third.";
/* Output: First Second Third.*/
#include <stdio.h>
void put1(const char *str)
{
while(*str!='\0') // while(*str)
putchar(*str++);
}
int main(int argc, char *argv[]) {
put1("I want to fly!\n");
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
char a1[40];
int count;
count = sprintf(a1, "Number is %d.", 10);
puts(a1);
printf("%d\n", count);
return 0;
}
double *pt
pt = (double *) malloc(30 * sizeof(double));
...
free(pt);
int *restruct pt = (int*) malloc(10 sizeof(int));
Y CP的部落格
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int ch;
FILE *fp;
fp = fopen("SomeTextFile.txt", "r");
while((ch=getc(fp)) != EOF)
putc(ch, stdout); // putchar
fclose(fp);
return 0;
}
getc(fp);
putc(ch, fp);
rewind(fp);
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int ch;
char word[50];
FILE *fp;
fp = fopen("SomeTextFile.txt", "r");
while(fscanf(fp, "%s", word)==1);
fclose(fp);
printf("%s", word);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int ch;
char word[50];
FILE *fp;
fp = fopen("SomeTextFile.txt", "r");
fgets(word, 7, fp);
fclose(fp);
printf("%s", word);
return 0;
}
fputs(word, fp);
#include <stdio.h>
struct book {
char linear_algebra[50];
char Discrete[50];
};
int main(int argc, char *argv[]) {
struct book bookcase[2] = {
{"book_a", "book_b"},
{"book_c", "book_d"}
};
struct book *pt;
pt = bookcase;
puts(pt->linear_algebra);
// output: book_a
pt++; // point to next index.
puts(pt->linear_algebra);
// output: book_c
return 0;
}
fwrite(&primer, sizeof(struct book), 1, fp);
ref - 程式人生 enum DAY {
MON=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
};
int main(void)
{
enum DAY day_var;
day_var = WED;
printf("%d\n", day_var);
// output: 3
return 0;
}
ref - runoob
int *returnArr = (int *)malloc(100*sizeof(int));
int *size = 10;
returnArr = realloc(returnArr, (*size)*sizeof(int));
int t['z'+1] = {
['a'] = 10,
['c'] = 30,
['d'] = 40,
};
printf("%d\n", t['f']); // t['f'] == 0
printf("%d\n", t['d']); // t['d'] == 40
cucerdariancatalin - [1LINER][Fastest Solution Explained] O(n)time complexity O(n)space complexity
int a= 10
for(int n=31; n>=0; n--) {
printf("%d", a>>n & 1);
}
int n = 10;
// set the 5th bit from right to 1
int mask = 1<<4;
n |= mask;
// set the 3rd bit from right to 0
int mask = ~(1<<2);
n &= mask;
// toggle the 4rd bit from right
int mask = 1<<3;
n ^= mask;
getch()
display version
getche()
// COORD pos = { 10,5 };
COORD pos;
pos.X = (short)5;
pos.Y = (short)3;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
/* set text color */
SetConsoleTextAttribute(hOut, 0x01 | 0x05);
printf("Something Something");
UINT32 EXA;
UINT32 EXB;
UINT32 EXC;
UINT32 EXD;
CHAR8 ID[(32/8)*4+1] = {0};
*(UINT32 *)ID = EXA;
*(UINT32 *)ID = EXB;
*(UINT32 *)ID = EXC;
*(UINT32 *)ID = EXD;
printf("%s\n", ID); // 前提是整個字串要是滿的