學C第5天(printf函數,scanf函數)

來源:互聯網
上載者:User

標籤:

一、格式

1.%d格式

 

    %[-]d      靠左對齊
    %[m]d      以m指定的欄位寬度輸出,資料的位元小於m,左端補空格
    %[0m]d     以m指定的欄位寬度輸出,資料的位元小於m,則左端補0
    %[l]d    輸出長整型整數
    %[-m]d  靠左對齊,忽視0或空格     int num = 1000;
    printf("%-d", num);//1000
    printf("\n%10d", num); //      1000
    printf("\n%010d", num);//0000001000
    printf("\n%ld", num);//1000
    printf("\n%-10d", num);//1000

 

 

2.%c格式     %[m]c以m指定的欄位寬度輸出,資料的位元小於m,左端補空格
    %[0m]c以m指定的欄位寬度輸出,資料的位元小於m,左端補0
    %[-m]c  靠左對齊,忽視0或空格    printf("\n%-c", ‘a‘);//a
    printf("\n%5c", ‘a‘);//    a
    printf("\n%05c", ‘a‘);//0000a
    printf("\n%-05c", ‘a‘);//a

 

 

3.%s格式

 

    %[-m]s    輸出字串佔m列,靠左對齊
    %[m]s    輸出字串佔m列,靠右對齊,左端補空格
    %[0m]s    輸出字串佔m列,靠右對齊,左端補0
    %[m.n]s    輸出字串前n個字元,佔m列,靠右對齊
    %[.n]s    輸出字串前n個字元,靠左對齊

 

    char str[50] = "hello";
    printf("\n%-s", str);//hello
    printf("\n%10s", str);//     hello
    printf("\n%010s", str);//00000hello
    printf("\n%-10s", str);//hello
    printf("\n%.3s", str);//hel
    printf("\n%10.3s", str);//       hel
    printf("\n%010.3s", str);//0000000hel

 

 

4.%f格式

 

    %[m][.n]f    輸出資料共佔m列,小數佔n位,靠右對齊
    %[-][m][.n]f    輸出資料共佔m列,小數佔n位,靠左對齊

 

 

    float num = 10.356;
    printf("\n%-8.2f", num);//10.36
    printf("\n%08.2f", num);//00010.36

 

5.egxc可以大寫

 

//EG 輸出的E大寫     X輸出的字母為大寫
    printf("%d", 10);//10
    printf("\n%D", 10);//D
    printf("\n%c", 65);//A
    printf("\n%C", ‘A‘);//A
    printf("\n%s", "abc");//abc
    printf("\n%S", "abc");//空
    printf("\n%f", 100000.0);//100000.000000
    printf("\n%F", 100000.0);//空
    printf("\n%e", 100000.0);//1.000000e+005
    printf("\n%E", 100000.0);//1.000000E+005
    //g怎麼精簡怎麼輸出
    printf("\n%g", 100000.0);//100000
    printf("\n%G", 100000.0);//100000
    printf("\n%G", 1000000000.5555);//1E+009
    printf("\n%o", 10);//12
    printf("\n%O", 10);//O
    printf("\n%x", 10);//a
    printf("\n%X", 10);//A

 6.printf函數注意事項

          1>.如需輸出",需用\"轉義

          2>.如需輸出%,需用%%

          3>.格式符少於輸出項時,多餘的輸出項不予輸出

          4>.格式符多於輸出項時,結果為不定值

    printf("%\"");//"
    printf("\n%%%");//%
    printf("\n%d,%d",10,20,30);//10,20
    printf("\n%d,%d,%d", 20, 30);//20,30,0

 7.scanf函數注意事項

        1>.附加格式符

           *輸入的值不賦給相應的變數

           l用於輸入長整形(32位以上平台輸入效果和%d一樣)和雙精確度實型資料

           m指定資料的寬度,自動截取所需長度的資料。

        2>.輸入資料時不能指定精度

二、檔案重新導向

從寫有指令的文本中讀取指令,將結果儲存在另一個文本中。

cmd——>路徑——>exe檔案<輸入有指令的檔案名稱.txt>輸出txt檔案名稱.txt


 

    int counts;//次數
    char instruct[50];//指令
    scanf("%d%s", &counts, instruct);
    char str[50];
    sprintf(str,"for /l %%i in (1,1,%d) do %s",counts,instruct);
    system(str);
    system("pause");

1.在記事本中輸入tasklist,儲存為input.txt

 在cmd中輸入路徑和exe檔案名稱<input.txt>output.txt

學C第5天(printf函數,scanf函數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.