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


基礎課程的最後一堂課。

while(條件式)
{
if()
{
}
}

例:列印10個*號
#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
int a=0;
while(a<10)
{
a++;
printf("*");
}
putchar('\n');

system("pause");
return 0;
}


for(條件初值設定;條件式;條件值遞增)
{
}

例:列印10個*號
#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
int a=0;
for(a=0;a<10;a++)
{
printf("*");
}
putchar('\n');

system("pause");
return 0;
}


例:列印10x10個*號
#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
int a=0;
int y=0;
for(y=0;y<10;y++)
{
for(a=0;a<10;a++)
{
printf("*");
}
putchar('\n');
}

system("pause");
return 0;
}


傳回值資料型態 函數名稱(資料型態 參數名稱)
#include <stdio.h>
#include <stdlib.h>

void PrintStar(int total,char ch)
{
int x;
for(x=0;x<total;x++)
{
printf("%c",ch);
}
}

int main(void)
{
PrintStar(10,'&');
putchar('\n');

system("pause);
return 0;
}


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

void PrintStar(void)
{
int x;
int total;
char ch;

printf("請輸入欲列印的字元與個數:");
scanf("%c %d",&ch,&total);
putchar('\n');

for(x=0,x<total;x++)
{
print("%c",ch);
}
}


字串小寫轉大寫
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int cnt=0;
char str[255];
printf("請輸入一個字串:");
scanf("%s",&str);
while(str[cnt] != '\0')
{
if(str[cnt]>='a' && str[cnt]<='z')
str[cnt]=str[cnt]-32;
cnt++;
}
printf("%s",str);
putchar('\n');

system("pause);
return 0;
}


指標:是特殊的變數,存放某個變數的記憶體位址
pointer(指位器)

char ch;
char *ptr;
ptr=&ch;
%d (用10進位印出位址)
%p (用16進位印出位址)

& 取位址
* 依址取值

0 回應:

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