C Primer Plus È«ÊéÔ´´úÂë(Ò»)

ÉÏһƪ / ÏÂһƪ  2012-03-19 09:25:03 / ¸öÈË·ÖÀࣺѧϰÈÕÖ¾

// 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; ctal = %o; hex = %x\n", x, x, x);

   printf("dec = %d; ctal = %#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


TAG:

 

ÆÀ·Ö£º0

ÎÒÀ´ËµÁ½¾ä

Open Toolbar