程式語言:C語言 第三堂課


字串輸入
scanf("%s",字串變數);

陣列名稱=陣列第一個元素(elemenl)的位址

char str1[10]; //宣告字串變數
printf("請輸入一個字串");
scanf("%s", str1);
putchar('\n');
printf("您輸入的內容為:%s\n", str1);

fflush(stdin); //清除buffer緩衝區

運算元與運算子
sum++; //sum=sum+1的簡寫
sum+=1; //也可以這樣寫(四則運算都能這樣用)

◎運算符號優先權

↑ ( ) 小括號運算子
│ [ ] 中括號運算子
│ ! + - 反向與正負號
│ ++ -- 遞增、遞減
│ * / % + - 數學運算
↓ > >= < <= == != && || = 邏輯運算子


printf("%d", ++num); //會先++再列印(前置)
printf("%d", num++); //先列印再做++動作(後置)

要在字串秀出「%」,需打上「%%」

#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
/*******************************
* 宣告變數
*******************************/
char crlf = '\n';
int num1,num2;
int pro,sum,sub,num;
float quo;
/*******************************
* 輸入資料
*******************************/
printf("請輸入二個整數:");
scanf("%d %d",&num1,&num2);
putchar(crlf);
/*******************************
* 資料運算
*******************************/
pro = num1 * num2;
quo = (float)num1 / (float)num2;
sum = num1 + num2;
sub = num1 - num2;
num = num1 % num2;
/*******************************
* 資料輸出
*******************************/
printf("%d * %d = %d",num1,num2,pro);
putchar(crlf);
printf("%d / %d = %.2f",num1,num2,quo);
putchar(crlf);
printf("%d + %d = %d",num1,num2,sum);
putchar(crlf);
printf("%d - %d = %d",num1,num2,sub);
putchar(crlf);
printf("%d %% %d = %d",num1,num2,num);
putchar(crlf);
system("pause");
return 0;
}


提升的原則:必須從表示範圍小的→表示範圍大的

型態轉換
(要轉換的型態)變數名稱

查詢資料所佔的位元組(Byte)
整數變數=sizeof(資料型態或變數或變數的值)

system("cls"); //清除畫面資料

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

int main (vold)
{
/* 變數宣告 */
char crlf = '\n';
char ch = 'a';
short s = -2;
int i = 3;
float f = 5.3f;
double d = 6.28;

/* 資料計算與輸出,包括查詢其資料型態數值 */
printf("(ch/s)-(d/f)-(s+i) = %f", (ch/s)-(d/f)-(s+i));
putchar(crlf);
printf("運算結果的資料型態值為:%d", sizeof((ch/s)-(d/f)-(s+i)));
putchar(crlf);
system("pause");
system("cls");
system("pause");
return 0;
}


流程控制:
1. if(條件式)
{當條件為真時執行;}

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

int main (vold)
{
char crlf = '\n';
int score;
int StandardScore = 60;
char word[255] = "請輸入你的分數:";
printf("%s" , word);
scanf("%d" , &score);
if (score < StandardScore) { printf("%d分,不及格!", score); } putchar(crlf); system("pause"); return 0; }


1 則留言:

◎感謝來訪,歡迎分享你的想法!
◎勾選「通知我」可以避免遺漏後續回覆的訊息
◎別忘了留下大名、連結等資料,我才有機會多認識你!