ANSIC <<
Previous Next >> 教學影片
c_ex
#include <stdio.h>
int main() {
char ch;
printf("Input a char: ");
scanf("%c", &ch);
if (ch == 'a') {
printf("You pressed 'a'\n");
}
return 0;
}

#include <stdio.h>
void main()
{
int i, j;
j = 10; /* 迴圈外先設定初值 */
for ( i = 0; i < 6; i++ )
{
printf( "i = %d, ", i );
printf( "j = %d \n", j );
j++; /* 迴圈後加上變更運算式 */
}
}

#include <stdio.h>
int main()
{
/* 印出 Hello World! Bye Bye */
printf("Hello World! "); printf("Bye "); printf("Bye");
return 0;
}

#include <stdio.h>
int main()
{
int a = 1;
int A = 8;
int b = 2, c;
c = A - a + b;
/* 輸出 a, A, b, c 到螢幕 */
printf( "a = %d, A = %d, b = %d, c = %d ", a, A, b, c );
return 0;
}

#include <stdio.h>
int main()
{
char x, y;
x = 'a';
y = (char)97;
/* 輸出 x, y, x, 最後一個是以 ASCII 值顯示 y */
printf( " x = %c, y = %c, ASCII of y = %d", x, y, y );
return 0;
}

#include<stdio.h>
int main()
{
int a,b;
a = 10; b = 3;
printf( "%d \n", a * b );
printf( "%d \n", a / b );
printf( "%d \n", a + b );
printf( "%d \n", a - b );
printf( "%d \n", a % b );
return 0;
}

#include <stdio.h>
int main()
{
int a = 10, b = 5;
printf( " a == b is %d \n", a == b );
printf( " a > b is %d \n", a > b );
printf( " a < b is %d \n", a < b );
printf( " a >= b is %d \n", a >= b );
printf( " a <= b is %d \n", a <= b );
printf( " a != b is %d \n", a != b );
printf( "\n" );
b = 10;
printf( " a == b is %d \n", a == b );
printf( " a > b is %d \n", a > b );
printf( " a < b is %d \n", a < b );
printf( " a >= b is %d \n", a >= b );
printf( " a <= b is %d \n", a <= b );
printf( " a != b is %d \n", a != b );
return 0;
}

#include<stdio.h>
void main()
{
int a,b;
a = 15;
b = 1;
printf("%d \n", a | b ); /* a OR b */
printf("%d \n", a & b ); /* a AND b */
printf("%d \n", a ^ b ); /* a XOR b */
printf("%d \n", a << 1 ); /* a 位元左移 1 位 */
printf("%d \n", a >> 1 ); /* a 位元右移一位 */
printf("%d \n", ~a ); /* A 的補數運算 */
}

#include <stdio.h>
void main()
{
int a;
a = 3;
printf("%d\n", !a );
a = 0;
printf("%d\n", !a );
}

#include <stdio.h>
void main()
{
char c;
printf( "Input a char:" );
scanf( "%c", &c );
switch( c )
{
case 'a':
printf(" you pressed a ");
break;
case 'b':
printf(" you pressed b ");
break;
case 'c':
printf(" you pressed c ");
break;
default:
printf(" not a, b, c ");
break;
}
}

#include <stdio.h>
/* 讀取並顯示一個數字 */
int main () {
char c;
printf("Enter character: ");
c = getchar(); /* 讀取其中的字符 */
printf("Character entered: ");
putchar(c); /* 寫出這個字符 */
return(0);
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 演示一個for循環 */
main()
{
float this_is_a_number, total;
int i;
total = 0;
/* forloop 迴圈 10 次 */
for (i = 0;i < 10;i++)
{
printf("Please enter a number:\n ");
scanf("%f", &this_is_a_number); /* 讀取數字 */
total = total + this_is_a_number;
}
printf("Total Sum is = %f\n", total);
}

#include <stdio.h>
/* if 運算範例 */
int main()
{
int this_is_a_number;
printf( "Please enter an integer between 1 and 10:\n " );
scanf( "%d", &this_is_a_number );
if (this_is_a_number <6)
printf( "This number is less than 6;\n " );
printf( "Please enter an integer between 10 and 20:\n " );
scanf( "%d", &this_is_a_number );
if (this_is_a_number <16)
printf( "This number is less than 16\n " );
else
printf( "This number is greater than 15\n " );
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/* if then else if 操作的範例 */
int main()
{
int this_is_a_number;
printf("Please enter an integer between 1 and 10:\n ");
scanf("%d", &this_is_a_number);
if (this_is_a_number < 6)
printf("This number is less than 6;\n ");
printf("Please enter an integer between 10 and 20:\n ");
scanf("%d", &this_is_a_number);
if (this_is_a_number < 16)
{
printf("This number is less than 16\n ");
}
else if (this_is_a_number == 20)
{
printf("This number is 20\n ");
}
else
{
printf("This number is greater than 15\n ");
}
return 0;
}

/* 返回答案的函數 */
/* 尋找學校一年中得分最高的學生 */
#include <stdio.h>
double getmarks(double pupils[]);
int main()
{
double pupil;
/* 帶有班級標記的數組已預設在主要部分中
程式 */
double marks[] = { 10.6, 23.7, 67.9, 93.0, 64.2, 33.8 ,57.5 ,82.2
,50.7 ,45.7 };
/* Call function getmarks. The function returns the max marks which
is then stored in pupil */
pupil = getmarks(marks);
printf("Max mark is = %f", pupil);
return 0;
}
double getmarks(double pupils[])
{
int i;
double highest;
highest = 0;
/* 依序檢查所有學生並儲存最高分 */
for (i = 0; i < 6; ++i)
{
if (highest < pupils[i])
highest = pupils[i];
}
return highest; /* 傳回函數所在位置的最高值
被稱為*/
}

/* 這是我的第一個C程序*/
int main()
{
printf("My first program\n");
return(0);
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int this_is_a_number1, this_is_a_number2, total;
printf("Please enter an integer number:\n ");
scanf("%d", &this_is_a_number1); /* read number in */
printf("You entered %d\n", this_is_a_number1);
printf("Please enter another number: \n");
scanf("%d", &this_is_a_number2); /* read number in */
printf("You entered %d\n", this_is_a_number2);
total = this_is_a_number1 + this_is_a_number2;/* add two numbers */
printf("total is %d\n", total);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/* 兩個浮點數相乘 */
int main()
{
float this_is_a_number1, this_is_a_number2, total;
printf("Please enter a number:\n ");
scanf("%f", &this_is_a_number1); /* 讀取數字 */
printf("You entered %f\n", this_is_a_number1);
printf("Please enter another number: \n");
scanf("%f", &this_is_a_number2); /* 讀取數字 */
printf("You entered %f\n", this_is_a_number2);
total = this_is_a_number1 * this_is_a_number2;/* 將數字相乘 */
printf("product is %f\n", total);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/* 兩個浮點數相除 */
int main()
{
float this_is_a_number1, this_is_a_number2, total;
printf("Please enter a number: \n");
scanf("%f", &this_is_a_number1); /* 讀取數字 */
printf("You entered %f\n", this_is_a_number1);
printf("Please enter another number:\n ");
scanf("%f", &this_is_a_number2); /* 讀取數字 */
printf("You entered %f\n", this_is_a_number2);
total = this_is_a_number1 / this_is_a_number2;/* 除以數字 */
printf("quotient is %f\n", total);
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 顯示數組使用的程序 */
int main()
{
int arr1[8];/* 定義一個 8 個整數的陣列 */
int i;
printf("enter 8 integer numbers\n");
for (i = 0;i < 8;i++)
{
scanf("%d", &arr1[i]);/* 讀入 arr1[i]*/
}
printf("Your 8 numbers are \n");
for (i = 0;i < 8;i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
}

/* ====================
Say Hello World!.
==================== */
#include <stdio.h>
void main()
{
/* 印出 Hello */
printf("Hello World!");
}

#include <stdio.h>
void main()
{
float a = 0.5;
double b = 1.2;
int c = 3;
b = b + a + c;
/* 輸出 a, b, c 到螢幕 */
printf( " a = %3.1f, b = %3.1f, c = %d ", a ,b, c );
}

/* =========================
輸入一個字元
========================= */
#include <stdio.h>
int main()
{
char ch;
printf("Input a char:");
scanf( "%c", &ch ); /* ch 前面加個 &(位址運算元) */
return 0;
}

/* =========================
輸入一個整數
========================= */
#include <stdio.h>
int main()
{
int i;
printf("Input an integer:");
scanf( "%d", &i ); /* ch 前面加個 &(位址運算元) */
printf( "the number is %d", i );
return 0;
}

/* ====================
Logical AND.
==================== */
#include <stdio.h>
void main()
{
printf("%d\n", 1 && 3 );
printf("%d\n", 0 && 0 );
printf("%d\n", 2 && 2 );
}

#include <stdio.h>
void main()
{
printf("%d\n", 1 || 0 );
printf("%d\n", 0 || 0 );
printf("%d\n", 2 || 2 );
}

#include <stdio.h>
void main()
{
char a;
printf( " The size of int is %d \n", sizeof(int) );
printf( " The size of char a is %d \n", sizeof(a) );
}

#include <stdio.h>
float circle( int r ); /* 宣告 circle 的 prototype */
void main()
{
float answer;
answer = circle(8);
printf( " 圓周長度是 %f", answer );
}
/* ====================
circle 函數, 計算 circle 的圓周長
==================== */
float circle( int r )
{
float result;
result = 3.14159 * (double)2 * r;
return ( result );
}

#include <stdio.h>
void main()
{
char c;
printf( "Input a char:" );
scanf( "%c", &c );
switch( c )
{
case 'a': printf(" you pressed a \n");
case 'b': printf(" you pressed b \n");
case 'c': printf(" you pressed c \n");
default: printf(" not a, b, c ");
}
}

int main()
{
/* 變數宣告 */
int a;
int A;
int b, c;
a = 1;
A = 8;
b = 2;
c = A - a + b; /* 先計算 A - a + b, 將結果傳會給 c */
printf( "%d", c ); /* 以 printf 印出 c 這個整數型態的變數 */
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 示範一個嵌套的for循環 */
main()
{
float this_is_a_number, total;
int i, j;
total = 0;
/* 外層for循環循環10次 */
for (i = 0;i < 10;i++)
{
/* 內部 forloop 迴圈兩次 */
for (j = 0;j < 2;j++)
{
printf("Please enter a number:\n ");
scanf("%f", &this_is_a_number); /* 讀取數字 */
total = total + this_is_a_number;
}
}
printf("Total Sum is = %f\n", total);
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 演示 do 循環 */
main()
{
float this_is_a_number, total;
int i;
total = 0;
i = 0;
/* do 循環循環直到 i 的值達到 10 */
do {
printf("Please enter a number:\n ");
scanf("%f", &this_is_a_number);
total = total + this_is_a_number;
i++;
}while( i < 10);
printf("Total Sum is = %f\n", total);
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/* 開關操作範例 */
int main()
{
int this_is_a_number;
printf("Please enter an integer between 1 and 5:\n ");
scanf("%d", &this_is_a_number);
switch (this_is_a_number)
{
case 1:
printf("Case1: Value is: %d", this_is_a_number);
break;
case 2:
printf("Case2: Value is: %d", this_is_a_number);
break;
case 3:
printf("Case3: Value is: %d", this_is_a_number);
break;
case 4:
printf("Case4: Value is: %d", this_is_a_number);
break;
case 5:
printf("Case5: Value is: %d", this_is_a_number);
break;
default:
printf("Error Value is: %d", this_is_a_number); /* 號碼
輸入的數字不在 1 到 5 之間 */
}
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/* 使用字元開關的範例*/
int main()
{
char this_is_a_character;
printf("Please enter character a,b,c,d or e:\n ");
scanf("%c", &this_is_a_character);
switch (this_is_a_character)
{
case 'a':
printf("a entered");
break;
case 'b':
printf("b entered");
break;
case 'c':
printf("c entered");
break;
case 'd':
printf("d entered");
break;
case 'e':
printf("e entered");
break;
default:
printf("Default ");
}
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 顯示數組使用的程序 */
int main()
{
int arr1[8];/* 定義一個 8 個整數的陣列*/
int i;
printf("enter 8 integer numbers\n");
for (i = 0;i < 8;i++)
{
scanf("%d", &arr1[i]);/* 讀入 arr1[i]*/
}
printf("Your 8 numbers are \n");
for (i = 0;i < 8;i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 顯示字元數組使用的程序 */
int main()
{
char arr2[10];/* 定義 10 個字元的數組 */
int i;
printf("enter 10 characters \n");
for (i = 0;i < 10;i++)
{
scanf("%c", &arr2[i]);
}
printf("Your 10 characters are \n");
for (i = 0;i < 10;i++)
{
printf("%c ", arr2[i]);
}
printf("\n");
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 二維數組測試範例*/
int main()
{
int arr1[7][8];/* 二維陣列 */
int i, j, k, l;
printf("enter number of rows and columns (max 7 rows max 8) \n");
scanf("%d %d", &k, &l);
if (k > 7 || l > 8)
{
printf("error — max of 8 for rows or 7 for columns \n");
}
else
{
printf("enter array\n");
for (i = 0;i < k;i++)
{for (j = 0;j < l;j++)
{
scanf("%d", &arr1[i][j]);
}
}
printf("Your array is \n");
for (i = 0;i < k;i++)
{
for (j = 0;j < l;j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}
}
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
/* 二維數組測試範例*/
int main(){
int arr1[7][8];
int i, j, k, l;
printf("enter number of rows and columns (max 7 rows max 8) \n");
scanf("%d %d", &k, &l);
if (k > 7 || l > 8)
{
printf("error — max of 8 for rows or 7 for columns \n");
}
else
{
printf("enter array\n");
for (i = 0;i < k;i++)
{
for (j = 0;j < l;j++)
{
scanf("%d", &arr1[i][j]);
}
}
printf("Your array is \n");
for (i = 0;i < k;i++)
{
for (j = 0;j < l;j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}
}
printf("first row of array\n");
for (j = 0;j < l;j++)
{
printf("%d ", arr1[0][j]);
}
printf("\n");
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/* 這段程式碼示範了函數的作用 */
/* 這裡的函數比較兩個數字並指出哪個較大 */
/* 使用者輸入三個數字並被告知哪個比哪個大!*/
void myfunction(int a,int b); /* d您的職能及其具體化
參數 */
int first , second, third;
main()
{
printf( "Please enter first integer number: " );
scanf( "%d", &first );
printf( "Please enter second integer number: " );
scanf( "%d", &second );
printf( "Please enter third integer number: " );
scanf( "%d", &third );
myfunction(first , second);
myfunction(first , third);
myfunction(second , third);
}
void myfunction(int a,int b)
/* 該函數位於程式的 main{} 部分之外 */
/* 函數只是比較兩個參數 a 和 b,並指出哪一個
更偉大*/
{
if(a>b)
printf("%d is greater than %d\n", a,b);
else if (a<b)
printf("%d is greater than %d\n", b,a);
else
printf("%d and %d are equal\n", a,b);
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
/* 示範字串操作的程式 strlen、strcpy、strcat、strcmp*/
int main() {
char borrow[7] = { 'b', 'o', 'r', 'r', 'o', 'w','\0' };
char string1[32] = "This is string1";
char string2[16] = "This is string2";
char string3[16];
int len;
/* 列印出字串的長度 */
len = strlen(string1);
printf("strlen(string1) : %d\n", len);
len = strlen(string2);
printf("strlen(string2) : %d\n", len);
len = strlen(string3);
printf("strlen(string3) : %d\n", len);
/* 將字串1複製到字串3中 */
strcpy(string3, string1);
printf("strcpy( string3, string1) : %s\n", string3);
len = strlen(string3);
printf("strlen(string3) after copy of string1 into string3 : %d\n", len);
/* 比較 string1 和 string3 (這些應該是相同的)*/
if (strcmp(string1, string3) == 0)
printf("strings are the same\n");
/* 連接 string1 和 string2*/
strcat(string1, string2);
printf("strcat( string1, string2): %s\n", string1);
/* 連接後 string1 的總長度 */
len = strlen(string1);
printf("strlen(string1) after cat of string2 onto string1 : %d\n", len);
printf("String as predefined quoted chars: %s\n", borrow);
return 0;
}

ANSIC <<
Previous Next >> 教學影片