2013年11月21日 星期四

[觀點][C語言]字串是不能比較的

#include <stdio.h>

int compare_string(char* a, char* b)
{
    if (a == b)
        return 0;
    else
        return 1;
}
int main()
{
    char str1[] = "abc";
    char str2[] = "abc";
    int answer;

    answer = compare_string(str1, str2);
    printf("%d\n", answer);

    return 0;
}
[Answer] 1

[觀點][C語言]在while(負數)判斷是TRUE,在if(負數)判斷也是TRUE

#include <stdio.h>

int foo(int x, int y)
{
    while(x)
    {
        ++x;
        ++y;
    }
    if(y)
    {
        --y;
    }
    return x+y;
}

int main()
{
    int answer;

    answer = foo(-1, -1);
    printf("%d\n", answer);

    answer = foo(0, -1);
    printf("%d\n", answer);

    return 0;
}

[answer]
0
-2

[觀點][C語言](0x7700 && 0x8800) || 0xff00 用%d印出來是1

#include <stdio.h>

int main()
{
    int a = 0x7700;
    int b = 0x8800;
    int c = 0xff00;
    int d, e;

    d = (a && b) || c;
    e = (a | b) & c;

    printf("d = %d\n", d);
    printf("e = %d\n", e);

    return 0;
}

[Answer]
1
65280

[觀點][C語言]sizeof各類型別,struct > union == array > char*

#include <stdio.h>

union
{
    char chVals[16];
}uu;

struct
{
    char chVals[16];
    int a;
}ss;

int main()
{
    char chVals[16];
    char* pVals = chVals;

    printf("%lu\n", sizeof(ss));
    printf("%lu\n", sizeof(uu));
    printf("%lu\n", sizeof(chVals));
    printf("%lu\n", sizeof(pVals));

    return 0;
}
[answer]
20
16
16
4

[觀點][C語言]即使函式內的變數和local變數名稱一樣,但跳出函式後就無關 

#include <stdio.h>
#include <stdlib.h>

void foo(int* x)
{
    x = (int*)malloc(sizeof(int));
    *x = 12;
}

int main()
{
    int x = 10;
    foo(&x);
    printf("x = %d\n", x);
    return 0;
}
[answer]
x = 10

[觀點][C語言]重新定義一樣的變數名稱,觀察其值的改變

#include <stdio.h>
#include <stdlib.h>

void foo(int* x)
{
    x = (int*)malloc(sizeof(int));
    *x = 12;
}

int main()
{
    int x = 10;
    foo(&x);
    printf("x = %d\n", x);
    return 0;
}
[Answer]
a = 7 b = 13
a = 7

[觀點][C語言]x變數在做完logic判斷後,它的值會變成1

#include <stdio.h>

int main()
{
    int x = 2;
    int y = 2;
    int z;

    if (x = 2 || y == 2) //x = 1
        z = x + y;
    else
        z = x - y;

    printf("z = %d\n", z); 
    return 0;
}
[Answer]
z = 3

[觀點][C語言]變數沒有initialize,則會有run-time error,硬是要印出來的話,會印出其位址

#include <stdio.h>

int main()
{
    int x;
    while(x < 100)
        printf("x = %d\n", x);
    return 0;
}
[Answer]
run-time error
x = -858993460

[觀點][C語言]n會先--之後才去做<=的比較

#include <stdio.h>

void Test(int n)
{
    int i;
    for (i = 1; i<= --n; i++)
    printf("i = %d n = %d\n", i, n);
}

int main()
{
    int x;
    Test(5);

    return 0;
}
[Answer]
i = 1 n = 4
i = 2 n = 3

[觀點][C語言]multi array

#include <stdio.h>

int main()
{
    int sum = 0;
    int i, j;
    int ary[][4] = {1, 3, 5, 7, 9, 11};

    for (i = 0; i <=6; i++)
        for (j = 0; j <=6; j++)
        printf("ary[%d][%d] = %d\n", i, j, ary[i][j]);

    int (*ptr)[4] = ary;
    sum = (*ptr)[1] + ary [0][3] + ary[1][2];
    printf("sum = %d", sum);

    return 0;
}
[Answer]
ary[0][0] = 1
ary[0][1] = 3
ary[0][2] = 5
ary[0][3] = 7
ary[0][4] = 9
ary[0][5] = 11
ary[0][6] = 0
ary[1][0] = 9
ary[1][1] = 11
ary[1][2] = 0
ary[1][3] = 0
ary[1][4] = 15773951
ary[1][5] = 0
ary[1][6] = 4195824
ary[2][0] = 15773951
ary[2][1] = 0
ary[2][2] = 4195824
ary[2][3] = 0
ary[2][4] = 0
ary[2][5] = 5
ary[2][6] = 2
ary[3][0] = 0
ary[3][1] = 1
ary[3][2] = 3
ary[3][3] = 0
ary[3][4] = -2050383040
ary[3][5] = 32767
ary[3][6] = 0
ary[4][0] = -2050383040
ary[4][1] = 32767
ary[4][2] = 0
ary[4][3] = 0
ary[4][4] = 0
ary[4][5] = 0
ary[4][6] = -662104211
ary[5][0] = 0
ary[5][1] = 0
ary[5][2] = -662104211
ary[5][3] = 32702
ary[5][4] = 0
ary[5][5] = 0
ary[5][6] = -2050383032
ary[6][0] = 0
ary[6][1] = 0
ary[6][2] = -2050383032
ary[6][3] = 32767
ary[6][4] = 0
ary[6][5] = 1
ary[6][6] = 4195572
sum = 10

[觀點][C語言]#define之後代入的數要小心

#include <stdio.h>

#define S(z) (z*z)

void foo(int x, int *py)
{
    int n = S(x+1);
    x = *py;
    *py = n;
}

int main()
{
    int a = 2;
    int b = 10;
    foo(a, &b);
    ++b;
    printf("a = %d b= %d\n", a, b);
    return 0;
}
[Answer]
a = 2 b= 6

a = 2代入S(2+1)則2+1*2+1=5,不是3*3=9

[觀點][C語言]array搭配強制型別轉換的pointer, 當轉換小型別轉成大型別的時候, 位址會跟著變, 還有int的加減法和char不同, 單位會是4

#include <stdio.h>

char ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
char *p1 = ary;
char *p2;
int *pn;

void test()
{
    p1+=2;                  //p1[0] = 2     
    pn = (int*)&p1[2];     //p1[2] = 4, char->int之後,pn[0] = 7
    pn+=1;                  //加完之後pn[0] = 8
    p1 = (char*)pn;        //pn從int->char,還是維持在原位,所以p1[0]=8
    --pn;                   //因為pn是int型別,做加減法會位移4個byte而不是1個byte,所以pn[0]=4
    p2 = (char*)pn;        //int->char不會縮減,所以p2[0]=4
    --p2;                   //p2是char,所以只會減1個byte,p2[0]=3
}

int main()
{
    test();
    printf("*p1 = %d *p2= %d\n", *p1, *p2);
    return 0;
}
[answer]
*p1 = 8 *p2= 3