C_Primer_Plus(第五版)全书源代码

// chapter 01

/*

#include <stdio.h>

int main(void)

{

    int dogs;

 

    printf("How many dogs do you have?/n");

    scanf("%d", &dogs);

    printf("So you have %d dog(s)!/n", dogs);

 

    return 0;

}

*/

//

//

//chapter 02

// fathm_ft.c -- converts 2 fathoms to feet

/*

#include <stdio.h>

int main(void)

{

    int feet, fathoms;

 

    fathoms = 2;

    feet = 6 * fathoms;

    printf("There are %d feet in %d fathoms!/n", feet, fathoms);

    printf("Yes, I said %d feet!/n", 6 * fathoms);

 

    return 0;

}

*/

//

/*

#include <stdio.h>

int main(void)                // a simple program           

{

    int num;                  // define a variable called num

    num = 1;                  // assign a value to num       

 

    printf("I am a simple "); // use the printf() function   

    printf("computer./n");

    printf("My favorite number is %d because it is first./n",num);

  

    return 0;

}*/

//

// two_func.c -- a program using two functions in one file

/*

#include <stdio.h>

void butler(void);      // ISO/ANSI C function prototyping

int main(void)

{

    printf("I will summon the butler function./n");

    butler();

    printf("Yes. Bring me some tea and writeable CD-ROMS./n");

  

    return 0;

}

 

void butler(void)          // start of function definition

{

    printf("You rang, sir?/n");

}

*/

//

//

//chapter 03  数据和C

/*

// altnames.c -- portable names for integer types

#include <stdio.h>

#include <inttypes.h> // supports portable types       the system doesn't contain the header file

int main(void)

{

    int16_t me16;     // me16 a 16-bit signed variable

  

    me16 = 4593;

    printf("First, assume int16_t is short: ");

    printf("me16 = %hd/n", me16);

    printf("Next, let's not make any assumptions./n");

    printf("Instead, use a /"macro/" from inttypes.h: ");

    printf("me16 = %" PRId16 "/n", me16); 

   

    return 0;

}

*/

//

/*

// bases.c--prints 100 in decimal, octal, and hex

#include <stdio.h>

int main(void)

{

    int x = 100;

   

    printf("dec = %d; octal = %o; hex = %x/n", x, x, x);

    printf("dec = %d; octal = %#o; hex = %#x/n", x, x, x);

    //%# 十六进制前显示 Ox   // 八进制数前显示o

    return 0;

}

 

*/

//

/*

// charcode.c-displays code number for a character

#include <stdio.h>

int main(void)

{

    char ch;

   

    printf("Please enter a character./n");

    scanf("%c", &ch);  

    printf("The code for %c is %d./n", ch, ch);

   

    return 0;

}

*/

//

/*

//print1.c-displays some properties of printf()

#include <stdio.h>

int main(void)

{

    int ten = 10;

    int two = 2;

   

    printf("Doing it right: ");

    printf("%d minus %d is %d/n", ten, 2, ten - two );

    printf("Doing it wrong: ");

    printf("%d minus %d is %d/n", ten );  // forgot 2 arguments

 

    return 0;

}

*/

//

/* print2.c-more printf() properties */

/*

#include <stdio.h>

int main(void)

{

    unsigned int un = 3000000000; // system with 32-bit int

    short end = 200;              // and 16-bit short     

    long big = 65537;

     long verybig = 12345678908642;

   

         //C 也可以使用前缀h 来表示short 类型。

         // 因此%hd 显示一个十进制的short 整型。%ho 为八进制形式。

    printf("un = %u and not %d/n", un, un);

    printf("end = %hd and %d/n", end, end);

    printf("big = %ld and not %hd/n", big, big);

    printf("verybig= %lld and not %ld/n", verybig, verybig);

   

    return 0;

}

*/

//

 

/* showf_pt.c -- displays float value in two ways */

/*

#include <stdio.h>

int main(void)

{

    float aboat = 32000.0;

    double abet = 2.14e9;

    long double dip = 5.32e-5;

  

    printf("%f can be written %e/n", aboat, aboat);

    printf("%f can be written %e/n", abet, abet);

    printf("%f can be written %e/n", dip, dip);

  

    return 0;

}

*/

//

/* toobig.c-exceeds maximum int size on our system */

/*

#include <stdio.h>

int main(void)

{

    int i = 2147483647;

    unsigned int j = 4294967295;

 

    printf("%d %d %d/n", i, i+1, i+2);

    printf("%u %u %u/n", j, j+1, j+2);

  

    return 0;

}

*/

//

/* typesize.c -- prints out type sizes */

/*

#include <stdio.h>

int main(void)

{

// c99 provides a %zd specifier for sizes

    printf("Type int has a size of %u bytes./n", sizeof(int)); //4 bytes

    printf("Type char has a size of %u bytes./n", sizeof(char));// 1 bytes

    printf("Type long has a size of %u bytes./n", sizeof(long));//4 bytes

    printf("Type double has a size of %u bytes./n", sizeof(double));//8 bytes

    return 0;

}

*/

//

//

// chapter04  字符串的格式化输入/ 输出

// defines.c -- uses defined constants from limit.h and float.              // this is useful ,you can know some limits of the system

/*

#include <stdio.h>

#include <limits.h>    // integer limits

#include <float.h>     // floating-point limits

int main(void)

{

    printf("Some number limits for this system:/n");

    printf("Biggest int: %d/n", INT_MAX);

    printf("Smallest long long: %lld/n", LONG_MIN);

    printf("One byte = %d bits on this system./n", CHAR_BIT);

    printf("Largest double: %e/n", DBL_MAX);

    printf("Smallest normal float: %e/n", FLT_MIN);

    printf("float precision = %d digits/n", FLT_DIG);

    printf("float epsilon = %e/n", FLT_EPSILON);

   

    return 0;

}

 

*/

//

/* flags.c -- illustrates some formatting flags */

/*

#include <stdio.h>

int main(void)

{

    printf("%x %X %#x/n", 31, 31, 31);

    printf("**%d**% d**% d**/n", 42, 42, -42);

    printf("**%5d**%5.3d**%05d**%05.3d**/n", 6, 6, 6, 6);

 

    return 0;

}

*/

//

 

/* floatcnv.c -- mismatched floating-point conversions */

/*

#include <stdio.h>

int main(void)

{

    float n1 = 3.0;

    double n2 = 3.0;

    long n3 = 2000000000;

    long n4 = 1234567890;

 

    printf("%.1e %.1e %.1e %.1e/n", n1, n2, n3, n4);

    printf("%ld %ld/n", n3, n4);

    printf("%ld %ld %ld %ld/n", n1, n2, n3, n4);

  

    return 0;

}

 

*/

//

// floats.c -- some floating-point combinations

/*

#include <stdio.h>

 

int main(void)

{

    const double RENT = 3852.99;  // const-style constant

 

    printf("*%f*/n", RENT);

    printf("*%e*/n", RENT);

    printf("*%4.2f*/n", RENT);

    printf("*%3.1f*/n", RENT);                         //take care here

    printf("*%10.3f*/n", RENT);

    printf("*%10.3e*/n", RENT);

    printf("*%+4.2f*/n", RENT);

    printf("*%010.2f*/n", RENT);

 

    return 0;

}

 

*/

//

/* intconv.c -- some mismatched integer conversions */

/*

#include <stdio.h>

#define PAGES 336

#define WORDS 65618

int main(void)

{

    short num = PAGES;

    short mnum = -PAGES;

 

    printf("num as short and unsigned short:  %hd %hu/n", num,

            num);

    printf("-num as short and unsigned short: %hd %hu/n", mnum,

            mnum);

    printf("num as int and char: %d %c/n", num, num);

    printf("WORDS as int, short, and char: %d %hd %c/n",

            WORDS, WORDS, WORDS);

   return 0;

}

 

*/

/

/* longstrg.c -- printing long strings */

/*

#include <stdio.h>

int main(void)                                                 //take care of this program

{

    printf("Here's one way to print a ");

    printf("long string./n");

    printf("Here's another way to print a /

long string./n");

    printf("Here's the newest way to print a "

          "long string./n");      // ANSI C

    return 0;

}

*/

/

/* praise1.c -- uses an assortment of strings */

/*

#include <stdio.h>

#define PRAISE "What a super marvelous name!"

int main(void)

{

    char name[40];

 

    printf("What's your name?/n");

    scanf("%s", name);

    printf("Hello, %s. %s/n", name, PRAISE);

   printf("Hello, %s. %s/n", name, "very good");                    //take care of here

    return 0;

}

 

*/

/

/* praise2.c */

/*

#include <stdio.h>

#include <string.h>      // provides strlen() prototype

#define PRAISE "What a super marvelous name!"

int main(void)

{

    char name[40];

 

    printf("What's your name?/n");

    scanf("%s", name);

    printf("Hello, %s. %s/n", name, PRAISE);

    printf("Your name of %d letters occupies %d memory cells./n",

          strlen(name), sizeof name);

    printf("The phrase of praise has %d letters ",

         strlen(PRAISE));

    printf("and occupies %d memory cells./n", sizeof PRAISE);

 

    return 0;

}

*/

/

/* printout.c -- uses conversion specifiers */

/*

#include <stdio.h>

#define PI 3.141593

int main(void)

{

    int number = 5;

    float espresso = 13.5;

    int cost = 3100;

 

    printf("The %d CEOs drank %f cups of espresso./n", number,

         espresso);

    printf("The value of pi is %f./n", PI);

    printf("Farewell! thou art too dear for my possessing,/n");

    printf("%c%d/n", '$', 2 * cost);

 

    return 0;

}

*/

/

/* prntval.c -- finding printf()'s return value */

/*

#include <stdio.h>

int main(void)

{

    int bph2o = 212;

    int rv;

 

    rv = printf("%d F is water's boiling point./n", bph2o);

    printf("The printf() function printed %d characters./n",               // finding printf()'s return value== how many characters printed

             rv);

    return 0;

}

*/

/

/* skip2.c -- skips over first two integers of input */

/*

#include <stdio.h>

int main(void)

{

    int n;

 

    printf("Please enter three integers:/n");

    scanf("%*d %*d %d", &n);                                                  //* means skip

    printf("The last integer was %d/n", n);

  

    return 0;

}

*/

/

/* strings.c -- string formatting */

/*

#include <stdio.h>

#define BLURB "Authentic imitation!"

int main(void)

{

    printf("/%2s//n", BLURB);                                         // still don't understand clearly

    printf("/%24s//n", BLURB);

    printf("/%24.5s//n", BLURB);

    printf("/%-24.5s//n", BLURB);

  

    return 0;

}

*/

//

/* varwid.c -- uses variable-width output field */

/*

#include <stdio.h>

int main(void)

{

    unsigned width, precision;

     int number = 256;

    double weight = 242.5;

 

    printf("What field width?/n");

    scanf("%d", &width);

    printf("The number is :%*d:/n", width, number);

    printf("Now enter a width and a precision:/n");                 // still don't understand clearly

    scanf("%d %d", &width, &precision);

    printf("Weight = %*.*f/n", width, precision, weight);                //%*.*f  %24.5s what does is mean?

    printf("Done!/n");

   

    return 0;

}

*/

//

/* width.c -- field widths */

/*

#include <stdio.h>

#define PAGES 931

int main(void)

{

    printf("*%d*/n", PAGES);           // still will print 931

    printf("*%2d*/n", PAGES);            // still will print 931

    printf("*%10d*/n", PAGES);

    printf("*%-10d*/n", PAGES);

 

    return 0;

}

 

*/

//

/

//chapter 05         运算符、表达式和语句

/* add_one.c -- incrementing: prefix and postfix */

/*

#include <stdio.h>

int main(void)

{

    int ultra = 0, super = 0;

 

    while (super < 5)

    {

        super++;

        ++ultra;

        printf("super = %d, ultra = %d /n", super, ultra);

     }

   

    return 0;

}

*/

///

/* addemup.c -- four kinds of statements */

/*

#include <stdio.h>

int main(void)                // finds sum of first 20 integers

{

    int count, sum;          

    int num=0;

    count = 0;               

    sum = 0;                 

    while (count++ < 20)                  // you should take care of

    {

              num++;

              sum = sum + count;         

              printf("count= %d sum = %d/n",count, sum);

    }

       printf(" the time of loop is %d/n",num);

    return 0;

}

*/

//

 

/* bottles.c -- counting down */

/*

#include <stdio.h>

#define MAX 10

int main(void)

{

    int count = MAX + 1;

 

    while (--count > 0) {

        printf("%d bottles of spring water on the wall, "

               "%d bottles of spring water!/n", count, count);

        printf("Take one down and pass it around,/n");

        printf("%d bottles of spring water!/n/n", count - 1);

    }

 

    return 0;

}

 

*/

//

 

 

/* shoes1.c -- converts a shoe size to inches */

/*

#include <stdio.h>

#define ADJUST 7.64

#define SCALE 0.325

int main(void)

{

    double shoe, foot;

 

    shoe = 9.0;

    foot = SCALE * shoe + ADJUST;

    printf("Shoe size (men's)    foot length/n");

    printf("%10.1f %15.2f inches/n", shoe, foot);

 

    return 0;

}

*/

///

/* shoes2.c -- calculates foot lengths for several sizes */

/*

#include <stdio.h>

#define ADJUST 7.64

#define SCALE 0.325

int main(void)

{

    double shoe, foot;

 

    printf("Shoe size (men's)    foot length/n");

    shoe = 3.0;

    while (shoe < 18.5)     

    {

        foot = SCALE*shoe + ADJUST;

        printf("%10.1f %15.2f inches/n", shoe, foot);

        shoe = shoe + 1.0;

    }                    

    printf("If the shoe fits, wear it./n");

 

    return 0;

}*/

//

/* golf.c -- golf tournament scorecard */

/*

#include <stdio.h>

int main(void)

{

    int jane, tarzan, cheeta;

 

    cheeta = tarzan = jane = 68;

    printf("                  cheeta   tarzan    jane/n");

    printf("First round score %4d %8d %8d/n",cheeta,tarzan,jane);

 

    return 0;

}

*/

///

/* squares.c -- produces a table of first 20 squares */

/*                                                 // have fun ,you can learn much

#include <stdio.h>

int main(void)

{

    int num = 1;

 

    while (num < 21)

    {

 

        printf("%4d %6d/n", num, num * num);

        num = num + 1;

    }

 

    return 0;

}

*/

///

/* wheat.c -- exponential growth */

/*                                                       // 棋盘赠大米,国王傻眼啦

#include <stdio.h>

#define SQUARES 64    // squares on a checkerboard  

#define CROP 1E15     // US wheat crop in grains    

int main(void)

{

    double current, total;

    int count = 1;

 

    printf("square     grains       total     ");

    printf("fraction of /n");

    printf("           added        grains      ");

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值