41223215 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • week5
  • week6
  • week7
  • week13
  • week15
  • week16
  • C_EX
  • ANSIC
  • 期末總結
  • Brython
week16 << Previous Next >> ANSIC

C_EX

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

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

3.

#include <stdio.h>
void main()
{
printf("%d\n", 1 || 0 );
printf("%d\n", 0 || 0 );
printf("%d\n", 2 || 2 );
}
4.
#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) );
}
5.
#include <stdio.h>
 
int main() {
    char ch;
    printf("input a char: ");
    scanf("%c", &ch);
    if (ch == 'a') {
        printf("You pressed 'a'\n");
    }
    return 0;
}
6.
#include <stdio.h>

int main() {
char ch; // 宣告一個字符型變數 ch

// 提示用戶輸入一個字符
printf("輸入一個字符:");

// 檢查 scanf 的返回值,確保成功讀取了一個字符
if (scanf("%c", &ch) != 1) {
fprintf(stderr, "錯誤:無法讀取字符。\n");
return 1; // 返回錯誤碼
}

// 檢查輸入的字符是否為 'a'
if (ch == 'a') {
printf("你輸入的是 'a'\n"); // 如果是 'a',則印出相應的消息
} else {
printf("你輸入的不是 'a'\n"); // 如果不是 'a',則印出相應的消息
}

return 0;
}
7.
#include <stdio.h>

int main() {
int i; // 宣告一個整數變數 i

// 提示用戶輸入一個整數
printf("輸入一個整數:");

// 檢查 scanf 的返回值,確保成功讀取了一個整數
if (scanf("%d", &i) != 1) {
fprintf(stderr, "錯誤:無法讀取整數。\n");
return 1; // 返回錯誤碼
}

// 根據 i 的值進行不同的判斷
if (i < 100) {
printf("i < 100\n");
} else {
if ((i >= 100) && (i < 200)) {
printf("i >= 100 and i < 200\n");
} else {
printf("i >= 200\n");
}
}

return 0;
}

8.

#include <stdio.h>

int main() {
char c; // 宣告一個字符型變數 c

// 提示用戶輸入一個字符
printf("輸入一個字符:");

// 檢查 scanf 的返回值,確保成功讀取了一個字符
if (scanf(" %c", &c) != 1) {
fprintf(stderr, "錯誤:無法讀取字符。\n");
return 1; // 返回錯誤碼
}

// 使用 switch 陳述式根據讀取的字符執行相應的分支
switch(c) {
case 'a':
printf("你按下了 'a'\n");
break;
case 'b':
printf("你按下了 'b'\n");
break;
case 'c':
printf("你按下了 'c'\n");
break;
default:
printf("不是 'a'、'b' 或 'c'\n");
break;
}

return 0;
}

9.

#include <stdio.h>

int main() {
char c; // 宣告一個字符型變數 c

// 提示用戶輸入一個字符
printf("輸入一個字符:");

// 檢查 scanf 的返回值,確保成功讀取了一個字符
if (scanf(" %c", &c) != 1) {
fprintf(stderr, "錯誤:無法讀取字符。\n");
return 1; // 返回錯誤碼
}

// 使用 switch 陳述式根據讀取的字符執行相應的分支
switch(c) {
case 'a':
printf("你按下了 'a'\n");
break;
case 'b':
printf("你按下了 'b'\n");
break;
case 'c':
printf("你按下了 'c'\n");
break;
default:
printf("不是 'a'、'b' 或 'c'\n");
break;
}

return 0;
}

10.

#include<stdio.h>

void main() {
int i;
for (i = 0; i < 6; i++) {
printf("%d\n", i); /* 印出 i */
}
}


week16 << Previous Next >> ANSIC

Copyright © All rights reserved | This template is made with by Colorlib