什么是變量(variable )
//program 2.1 What is a variable?
#include<stdio.h>
int main(void)
{
printf("My salary is $10000");
return 0;
}
試試看:使用變量
- 變量聲明語句以分號結(jié)束
將上一個程序改為使用int型變量(用來存放變量的聲明語句枢泰,變量聲明翩肌,變量聲明以分號結(jié)尾)
// program 2.2 Using a variable
#include < stdio.h >
int main(void)
{
int salary;
salary =10000;
printf("My salary is %d.\n",salary);
return 0;
}
使用更多的變量
//program2.3 Using more variables
#include <stdio.h>
int main (void)
{
int brothers;
int brides;
brothers = 7;
brides=7;
printf("%d brides for %d brother\n",brothers,brides)
return 0;
}
試試看:減和乘
//program 2.5 calculations with cookies
#include <stdio.h>
int main ()
{
int cookies =5;
int cookie_calories =125;
int total_eaten =0;
int eaten =2;
cookies = cookies -eaten;
total_eaten =total_eaten +eaten
printf("\nI have eaten %d cookies. There are %d cookies left",eaten,cookies);
eaten = 3;
cookies =cookies -eaten;
total_eaten =total_eaten+eaten;
printf("\nI have eaten %d more.Now there are %d cookies left\n",eaten,cookies);
printf("\nTotal energy consumed is %d calories.\n",total_eaten*cookie_calories);
return 0;
}
試試看:除法和取模運(yùn)算符
假設(shè)你有一罐餅干(其中有45塊餅干)和7個孩子,要把餅干平分給每個孩子啄寡,計算每個孩子可得到幾塊餅干颖低,分完后剩下幾塊餅干拷窜。
//program 2.6 Cookies and kids
#include <stdio.h>
int main (void)
{
int cookies =45;
int children =7;
int cookies_per_child =0;
int cookies_left_over =0 ;
cookies_per_child = cookies/children;
printf("You have %d children and %d cookies\n", children, cookies);
printf("Give each child %d cookies.\n",cookies_left_child);
cookies_left_over = cookies%children;
printf("There are %d cookies left over.\n",cookies_left_child);
return 0;
}
試試看:使用float類型值的除法
將10尺長的厚板均分成4塊時
用一個浮點(diǎn)數(shù)除以另一個浮點(diǎn)數(shù)俭茧,然后顯示其結(jié)果
//program 2.7 Divisdion with float values
#include<stdio.h>
int main (void)
{
float plank_length =10.0f;
float piece_count =4.0f;
float piece_length = 0.0f;
piece_length = plank_length/piece_count;
printf("A plank %f feet long can be cut into %f feet long.\n",plank_length,piece_count,piece_length);
return 0;
}
試試看:算術(shù)運(yùn)算
利用輸入的直徑計算一個圓桌的周長和面積。計算圓的周長及面積時漓帚,其數(shù)學(xué)公式
要使用PI(周長=2R.,面積=R*R >>使用函數(shù)scanf()時母债,要在變量前加上尋址運(yùn)算符&,而使用printf()函數(shù)時不用添加
在scanf()的控制字符串后面有多少個參數(shù)尝抖,控制字符串就有多少個格式說明符
//program 2.8 calculations on the table
#include<stdio.h>
int main(void)
{
float radius =0.0f;
float diameter = 0.0f;
float circumference = 0.0f;
float area =0.0f;
float pi = 3.14159265f;
printf("Input the diameter of the table:");
scanf("%f", &diameter);
radius = diameter/2.0f;
circumference = 2.0f*pi*radius;
area = pi*radius*radius;
printf("\nThe circumference is %.2f", circumference);
printf("\nThe area is %.2f\n", area);
return 0;
}
試試看:定義一個常量
在C語言中有一個通用的約定的:#define語句中的標(biāo)識符都是大寫
//program 2.9 more round tables
#include <stdio.h>
#define PI 3.14159f
int main ()
{
float radius =0.0f;
float diameter = 0.0f;
float circumference = 0.0f;
float area =0.0f;
printf("Input the diameter of a table:");
scanf_s("%f", &diameter);
radius = diameter/2.0f;
circumference = 2.0f*PI*radius;
area =PI*radius*radius;
printf("\nThe circumference is %.2f. ", circumference);
printf("\nThe area is %.2f.\n",area);
return 0;
}
試試看:定義一個其值固定的變量
//program 2.10 Round tables again but shorter
#include <stdio.h>
int main(void)
{
float diameter =0.0f;
float radius = 0.0f;
const float pi = 3.14159f;
printf("Input the diameter of the table:");
scanf("%f", &diameter);
printf("\nThe circumference is %.2f.", 2.0f*pi*radius);
printf("\nThe area is %.2f.\n", pi*radius*radius);
return 0;
}
試試看:找出極限值
//program 2.11 Finding the limits
#include<stdio.h>
#include<limits.h>
#include<float.h>
int main (void)
{
printf("Variables of type char store values from %d to %d\n",CHAR_MIN,CHAR_MIAX);
printf("Variables of type unsigned char store values from 0 to %u\n", UCHAR_MAX);
printf("Variables of type short store values from %d to %d\n", SHRT_MIN,SHRT_MAX);
printf("Variables of type unsigned short store values from 0 to %u\n",USHRT_MAX);
printf("Variables of type long store values from %ld to %ld\n", INT_MIN, INT_MAX);
printf("Variables of type unsigned long store values from 0 to %lu\n",ULONG_MAX);
printf("Variables of type long long store values from %ld to %ld\n",LLONG_MIN,LLONG_MAX);
printf("Variables of type unsignd long long store values from 0 to %llu\n",ULLONG_MAX);
printf("\nThe size of the smalest positive non-zero value of type float is %.3e\n",FLT_MIN);
printf("The size of the largest value of type float is %.3e\n",FLT_MAX);
printf("The size of the smallest non-zero value of type double is %.3e\n",DBL_MIN);
printf("The size of the largest value of type double is %.3e\n",DBL_MAX);
printf("The size of the smalllest non-zero value of type long double is %.3Le\n", LDBL_MAX);
printf("The size of the largest value of type long double is %.3Le\n",LDBL_MAX);
printf("\n Variables of type float provide %u decimal digits precision.\n"; FLT_DIG);
printf("Variables of type double provide %u decimal digits precision.\n", DBL_DIG);
printf("Variables of type long double provide %u decimal digits precision.\n",LDBL_DIG);
return 0;
}
//program 2.12 Finding the size of a type
#include <stdio.h>
int main(void)
{
printf("Variables of type char occupy %u bytes\n", sizeof(char));
printf("Variables of type short occupy %u bytes\n", sizeof(short));
printf("Variables of type short occupy %u bytes\n", sizeof(int));
printf("Variables of type long occupy %u bytes\n",sizeof(long));
printf("Variables of type long long occupy %u bytes\n",sizeof(long,long));
printf("Variables of type float occupy %u bytes\n",sizeof(float));
printf("Variables of type double occupy %u bytes\n", sizeof(double));
printf("Variables of type long double occupy %u bytes\n",sizeof(long,double));
return 0;
}
試試看:字符的建立
%C轉(zhuǎn)換說明符將變量的內(nèi)容解釋為單個字符
%d轉(zhuǎn)換說明符將變量的內(nèi)容解釋為整數(shù)
//program 2.15 characters and number
#include <stdio.h>
int main(void)
{
char first = 'T';
char second = 63;
printf("The first example as a letter looks like this -%c\n", first);
printf("The first example as a number looks like this-%d\n",first);
printf("The second example as a letter looks like this-%c\n",second);
printf("The second example as a number looks like this-%d\n",second);
return 0;
}
試試看:用字符的對應(yīng)整數(shù)值進(jìn)行算術(shù)運(yùn)算
標(biāo)準(zhǔn)庫 ctype.h頭文件提供的toupper()和tolower()函數(shù)可以把字符轉(zhuǎn)換為大寫和小寫
//program 2.16 Using type char
#include <stdio.h>
int main(void)
{
char first = 'A';
char second = 'B';
char last ='Z'';
char number = 40;
char ex1 =first +2;
char ex2 =second -1;
char ex3 = last +2;
printf("Character values %-5c%-5c%-5c\n",ex1,ex2,ex3);
printf("Numerical equivalents %-5c%-5c%-5c\n",ex1,ex2,ex3);
printf("The number %d is the code for the character %c\n",number,number);
return 0;
}
//program 2.17 Calculating the height of a tree
#include <stdio.h>
int main(void)
{
long shorty = 0L;
long lofty = 0L;
long feet = 0L;
long inches =0L毡们;
long shorty_to_lofty =0 L;
long lofty_to_tree = 0L;
printf("Enter Lofty's height to the top of his/her head,in whole feet:");
scanf("%ld",&feet);