第二章的程序

什么是變量(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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市昧辽,隨后出現(xiàn)的幾起案子衙熔,更是在濱河造成了極大的恐慌,老刑警劉巖搅荞,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件红氯,死亡現(xiàn)場離奇詭異,居然都是意外死亡咕痛,警方通過查閱死者的電腦和手機(jī)痢甘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茉贡,“玉大人塞栅,你說我怎么就攤上這事∏簧ィ” “怎么了放椰?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長愉粤。 經(jīng)常有香客問我砾医,道長,這世上最難降的妖魔是什么科汗? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任藻烤,我火速辦了婚禮,結(jié)果婚禮上头滔,老公的妹妹穿的比我還像新娘怖亭。我一直安慰自己,他們只是感情好坤检,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布兴猩。 她就那樣靜靜地躺著,像睡著了一般早歇。 火紅的嫁衣襯著肌膚如雪倾芝。 梳的紋絲不亂的頭發(fā)上讨勤,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音晨另,去河邊找鬼潭千。 笑死,一個胖子當(dāng)著我的面吹牛借尿,可吹牛的內(nèi)容都是我干的刨晴。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼路翻,長吁一口氣:“原來是場噩夢啊……” “哼狈癞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起茂契,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤蝶桶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后掉冶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體真竖,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年郭蕉,在試婚紗的時候發(fā)現(xiàn)自己被綠了疼邀。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡召锈,死狀恐怖旁振,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情涨岁,我是刑警寧澤拐袜,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站梢薪,受9級特大地震影響蹬铺,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜秉撇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一甜攀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧琐馆,春花似錦规阀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至滋饲,卻和暖如春厉碟,著一層夾襖步出監(jiān)牢的瞬間喊巍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工箍鼓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留崭参,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓款咖,卻偏偏與公主長得像阵翎,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子之剧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容