code.club

 找回密碼
 立即註冊
搜索
查看: 19555|回復: 22
打印 上一主題 下一主題

基本重要觀念筆記

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2015-5-20 19:51:48 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
一變數的值要由其它變數決定時,必須在其它變數的值都已確定後,再導出該變數。
如下例:

int x ,y,z;
x=1;
y=2;
z=x+y; //則z==3

如果 int x,y,z;
z=x+y
x=1;
y=2;
z==? //此時會跑出亂數。因為 z 已在 x和y的值確定前就已被 x + y 賦值了。除非之後再另行給z賦值,例如 z = 123 強迫它變為 123 。
回復

使用道具 舉報

沙發
發表於 2015-6-3 00:00:05 | 只看該作者
本帖最後由 return 於 2015-6-3 00:31 編輯

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

int main(void){
        printf("%d\n",sizeof(char));// 1
        printf("%d\n",sizeof('k'));// 4, 因為將'k'先變為ASKII碼的數字,故視為 int
        printf("%d\n",sizeof(2)); // 4
        printf("%d\n",sizeof(int)); // 4
        printf("%d\n",sizeof(long)); // 8
        printf("%d\n",sizeof(2L)); // 8, 2是常數但給予 long 型態
        printf("%d\n",sizeof(float)); // 4
        printf("%d\n",sizeof(2.3f)); // 4
        printf("%d\n",sizeof(2.3));        // 8, 後面不特別標明是float則有小數點一律給予 double 型態
        printf("%4.2f\n",23.456);// 五捨六入
        printf("%4.2f\n",23.455);// 五捨六入
        printf("%3.2f, %3.2f, %3.2f\n",1.234, 1.235, 1.236); // 四捨五入
        printf("%3.2f, %3.2f, %3.2f\n",1.244, 1.245, 1.246); // 四捨五入
        printf("%3.2f, %3.2f, %3.2f\n",1.254, 1.255, 1.256); // 五捨六入

                return 0;
        }
回復 支持 反對

使用道具 舉報

板凳
 樓主| 發表於 2015-6-5 01:08:50 | 只看該作者
#include <stdio.h>
#include <stdlib.h>

int main(void){
       
        char ch;
        printf("input a character:");
        scanf(" %c",&ch); //在%c之前留一空格,則scanf會略過輸入的空白格而取第一個出現的字符。否則若按空白鍵就會被直接scanf去。
        printf("%c",ch);
       
        return 0;
       
                 }
回復 支持 反對

使用道具 舉報

地板
 樓主| 發表於 2015-7-2 18:56:25 | 只看該作者
#include <stdio.h>
#include <stdlib.h>

void test(int *);

int main(void){

int x=123;
int *p=&x;

test(p); //這裡是傳入x的位址,p=&x

        return 0;
       
       
}

void test(int *j) //這裡*j表示要傳入指標所代表的位址
{
       
        printf("%d",*j); //這裡*j則是表示指標代表位址的那個值
       
}
回復 支持 反對

使用道具 舉報

5#
 樓主| 發表於 2015-7-22 15:26:38 | 只看該作者
#include <stdio.h>
#include <stdlib.h>

struct book {
        char name[20];
        int price;
        } *q1;
       
int main(void){

int x,y,*p;
struct book *q2;
p=&x;
q1=(struct book*)malloc(sizeof(struct book)); //結構的指標要先配置一塊記憶體,無論在哪宣都一樣
q2=(struct book*)malloc(sizeof(struct book));
printf("enter a number:\n");
scanf("%d",p); //一般指標就是位址,不用前面加上&
printf("you enter %d.\n",x);

printf("enter the name and price of book.\n");
gets(q1->name);//在字串前就不用&
scanf("%d",&q1->price); //但結構指標不知為何,在數字上前面還是要加上&
printf("the price for %s is %d.\n",q1->name,q1->price);

return 0;
}
回復 支持 反對

使用道具 舉報

6#
 樓主| 發表於 2015-7-30 22:11:39 | 只看該作者
char str1[5] = "hello";
char str2[5]= {'h','e','l','l','o'};

二者是不同的。前者是字串,最後要加上一個結尾的'\0',所以陣列子要是 str1[6],否則在C++編譯器中會出錯,在部份C編譯器中雖可通過,但有的也會出錯。
而str2[5]則是字符陣列,五個就是五個,無需最後加一。
回復 支持 反對

使用道具 舉報

7#
 樓主| 發表於 2015-8-14 11:59:47 | 只看該作者
#include <iostream>
using namespace std;

int main(void){
       
        int a=0,b=0,c=2,d=0,e=2,f=2;
        cout << a << " " << a++ << " " << endl; //1,0
        cout << ++b <<" " << b++ << " " << endl;//2,0
        cout << c << " " << (c++)+(++c) << " " << endl;//4,6
        cout << (d=f++)+(e=f) << endl;//4
        cout << f << " " << d << " " << e << endl;//3,2,2
       
        return 0;
}
回復 支持 反對

使用道具 舉報

8#
 樓主| 發表於 2015-8-17 23:32:28 | 只看該作者
#include <stdio.h>
#include <Stdlib.h>

int main(void){
       
        int a,b;
       
        a=0;
        b=10;
        a = 5>3&&2||8<4-(b=!0);
       
        printf("%d\n%d\n",a,b); // a=0, b=10,因為邏輯運算中,只要可以確定答案,則後面的b電腦就不會再去算,而直接印出它的原值。
       
        return 0;
}
回復 支持 反對

使用道具 舉報

9#
 樓主| 發表於 2015-8-19 13:59:54 | 只看該作者
本帖最後由 enter 於 2015-8-19 14:04 編輯





回復 支持 反對

使用道具 舉報

10#
 樓主| 發表於 2015-8-29 01:40:37 | 只看該作者
#include <iostream>
using namespace std;

int main(){
       
        char s[10]="123456789";
       
        cout << sizeof(s) <<endl << strlen(s);//10 & 9
       
        return 0;
}

sizeof()是算出陣列所佔記憶體的大小,所以是得到10。而strlen()則是求字串的長度,所以不會去計算最後一個\0,所以只得出9而已。
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-4-26 01:10 , Processed in 0.069958 second(s), 16 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表