c_ex <<
Previous Next >> introduce to c:
jsliu_c_programming:
1.
/* ====================
Say Hello World! Bye Bye.
==================== */
#include <stdio.h>
int main()
{
/* 印出 Hello World! Bye Bye */
printf("Hello World! "); printf("Bye "); printf("Bye");
return 0;
}

2.
/* ====================
變數宣告的例子 3.
==================== */
#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;
}

3.
#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 );
}

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

5.
/* ====================
基本運算範例.
==================== */
#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;
}

6.
/* ====================
關係運算元的範例.
==================== */
#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;
}

7.
/* ====================
位元運算元的範例.
==================== */
#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 的補數運算 */
}

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

9.
/* ====================
sizeof 的範例.
==================== */
#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) );
}

10.
/* ====================
Function (2)
==================== */
#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 );
}

c_ex <<
Previous Next >> introduce to c: