C 語言中的變數



變數是某一值的一個佔位符。所有變數都有一些與其關聯的型別,該型別表示可以為其分配的“型別”的值。C 語言提供了一組豐富的變數 −

型別 格式字串 說明
char %c 字元型別變數(ASCII 值)
int %d 機器中最自然的整數大小。
float %f 單精度浮點數。
double %e 雙精度浮點數。
void − N/A − 表示不存在型別。

C 語言中的字元 (char) 變數

字元 (char) 變數儲存一個單個字元。

#include <stdio.h>

int main() {
   char c;        // char variable declaration
   c = 'A';       // defining a char variable
   
   printf("value of c is %c", c);
   
   return 0;
}

該程式的輸出應為 −

value of c is A

C 語言中的整數 (int) 變數

int 變數儲存單個字元的有符號整數值。

#include <stdio.h>

int main() {
   int i;         // integer variable declaration
   i = 123;       // defining integer variable
   
   printf("value of i is %d", i);
   
   return 0;
}

該程式的輸出應為 −

value of i is 123

C 語言中的浮點數 (float) 變數

float 變數儲存單精度浮點數。

#include <stdio.h>

int main() {
   float f;             // floating point variable declaration
   f = 12.001234;       // defining float variable
   
   printf("value of f is %f", f);
   
   return 0;
}

該程式的輸出應為 −

value of f is 12.001234

C 語言中的雙精度 (double) 浮點數變數

double 變數儲存雙精度浮點數。

#include <stdio.h>

int main() {
   double d;            // double precision variable declaration
   d = 12.001234;       // defining double precision variable
   
   printf("value of d is %e", d);
   
   return 0;
}

該程式的輸出應為 −

value of d is 1.200123e+01

C 語言中的 void (void) 資料型別

C 語言中的 void 表示“無”或“無值”。它與指標宣告或函式宣告一同使用。

// declares function which takes no arguments but returns an integer value
int status(void)  

// declares function which takes an integer value but returns nothing
void status(int)

// declares a pointer p which points to some unknown type
void * p
simple_programs_in_c.htm
廣告
© . All rights reserved.