第二章的程序

什么是变量(variable )

//program 2.1 What is a variable?
                 #include<stdio.h>

                 int  main(void)
                 {
                     printf("My salary is $10000");
                     return 0;
                 }

试试看:使用变量

  • 变量声明语句以分号结束

将上一个程序改为使用int型变量(用来存放变量的声明语句,变量声明,变量声明以分号结尾)

                     // 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;
                }

试试看:除法和取模运算符

假设你有一罐饼干(其中有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块时
用一个浮点数除以另一个浮点数,然后显示其结果

  //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;
}

试试看:算术运算
利用输入的直径计算一个圆桌的周长和面积。计算圆的周长及面积时,其数学公式
要使用PI(周长=2R.,面积=R*R >>使用函数scanf()时,要在变量前加上寻址运算符&,而使用printf()函数时不用添加
在scanf()的控制字符串后面有多少个参数,控制字符串就有多少个格式说明符

//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语句中的标识符都是大写

            //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转换说明符将变量的内容解释为单个字符
%d转换说明符将变量的内容解释为整数

//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;
 }

试试看:用字符的对应整数值进行算术运算
标准库 ctype.h头文件提供的toupper()和tolower()函数可以把字符转换为大写和小写

//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);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,548评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,069评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,985评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,305评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,324评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,030评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,639评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,552评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,081评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,194评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,327评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,004评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,688评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,188评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,307评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,667评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,337评论 2 358

推荐阅读更多精彩内容