以c primer plus(第六版)为大纲的C语言初学手记,含示例代码及编程练习

 

//!!!example3.7 ??? 我编程后的输出结果对不上 
//!!!3.4.2.3为什么int溢出从2147483647开始?它不是 32位long的范围吗 

//#include <stdio.h>
//#include <stdio.h>
//#include <stdlib.h>
//int main()
//{
//    puts("C语言中文网");
//    system("pause");
//    return 0 ;
//
//}

//this is a comment
//in C++,in the first line '#include <iostream>'should be typed,it is'File Inclusion'
//in the second line,'using namepace std'should be typed,it is 'Namespace',so we can use cout directly.
//in the third line,'int main(){}'should be typed,it is 'Main Function',
//we use 'cout' to print a sentence in C++.


//To write a programm successfully,we should observe the following steps:
//specialize your goal-design programms-write-transmit-run-test-improve

//exercise1.13
//goal:when users input a datum in inches,the programm is able totranslate it into a data in cm.
//design:main-input-translate-print

example2.1 
this is a simple example of C
//#include <stdio.h>
//int main(vaid)
//{
//    int num; //to define a variable(indentifier) which is a integer(declaration)
//    num=1;   //to assignment
//    printf("I am a simple"); //try to use fuction'printf()'
//    printf(" computer.\n");  //'\n' is used to newline
//    printf("My favourite number is %d becasue it is first.\n",num);
//    return 0;
//}
explaination: 'void' means there is no param included;'int' means integer
explaination:escape sequence is useed to express words that may be misunderstood
///*another way to write comment
//it is avaiable to write lines
//just like this 
//\t = Tab
//\b = Backpace
//*/
in old systems,the declarations must write at the front

example2.5
//#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",feet);
//    return 0;
//}
///*1,%d,it works as a pron.,it refers to the variables in the end*/

example2.6
//#include <stdio.h>
//void butler(void);  //prototype:I will use the function
//int main(void)
//{
//    printf("I will summon the bulter fuction.\n");
//    butler();  //fuction call
//    printf("Yes.Bring me some tea and writeable DVDs.\n");
//    return 0;
//}
//void butler(void)  //fuction definition
//{
//    printf("You rang,sir?\n");
//}
///*else explains:void expresses no feedback to butler
//                the position of fuction definition is not essiential */

/*exercise2.11
1.
# include <stdio.h>
int main(void)
{
    
    return 0;
}

2.
English: I am learn C language.
C:printf(I am learniing C language.)

3.
English:I like to eat pencials.
C:
int m,n
m=3 //m refers to the number of pen
n=5 //n refers to the number of pencial
printf("OH,%d pencials",m)

4.
#include <stdio.h>
int main(void)
{
int s;
s = 56;
print("There are %d weeks in a year.",s);
return 0;
}

5.
Baa Baa Black Sheep.
Have you any wool?

Begone
O creature of lard!

What?
No
fish?

2+2=4

6.
main int char =

7.
#include <stdio.h>
int main(void)
{
int words,lines;
words=3020;
lines=350;
printf("There were %d words and %d lines.",words,lines);
return 0
}

8.
a=5 b=2
a=5 b=5
a=5 b=5

9.
x=10 y=5
x=10 y=15
x=150 y=15

*/

//exercise2.12
1
//#include <stdio.h>
//int main(void)
//{
//    printf("Gustav Mahler\n");
//    printf("Gustav\nMahler\n");
//    printf("Gustav ");
//    printf("Mahler");
//    return 0;
//}
if there is no '\n',all the context will be print in the same line

2
//#include <stdio.h>
//int main(void)
//{
//    printf("My name is lpo.\n");
//    printf("I live in China. ");
//    return 0;
//}

3
//#include <stdio.h>
//int main(void)
//{
//    int age,days;
//    age=18;
//    days=age*365;
//    printf("My age is %d.It equals to %d days.",age,days);
//    return 0;
// }

4
//#include <stdio.h>
//void jolly(void);
//void deny(void);
//int main(void)
//{
//    jolly();
//    jolly();
//    jolly();
//    deny();
//    return 0;
//}
//
//void jolly(void)
//{
//    printf("For he's a jolly good feelow!\n\n");
//}
//
//void deny(void)
//{
//    printf("Which nobody can deny!");
//}

5.1
//#include <stdio.h>
//void br(void);
//void ic(void);
//int main(void)
//{
//    br();
//    printf(",");
//    ic();
//    printf("\n\nIndia,China");
//    printf("\n\nBrazil,Russia");
//    return 0;
//}
//
//void br(void)
//{
//    printf("Brazil,Russia");
//}
//
//void ic(void)
//{
//    printf("India,China");
//}

5.2
//#include <stdio.h>
//void br(void);
//void ic(void);
//int main(void)
//{
//    br();
//    printf(",");
//    ic();
//    printf("\n\n");
//    ic();
//    printf("\n\n");
//    br();
//    return 0;
//}
//
//void br(void)
//{
//    printf("Brazil,Russia");
//}
//
//void ic(void)
//{
//    printf("India,China");
//}

6
//#include <stdio.h>
//int main(void)
//{
//    int toes,toes2,toes3;
//    toes=10;
//    toes2=toes*toes;
//    toes3=toes2*toes;
//    printf("The number of toes is %d.\n\n",toes);
//    printf("The number of toes squares.The result is %d.\n\n ",toes2);
//    printf("The number of toes cubes.The result is %d.",toes3);
//    return 0;
//}

7
//#include <stdio.h>
//void s(void);
//int main(void)
//{
//    s();
//    s();
//    s();
//    printf("\n\n");
//    s();
//    s();
//    printf("\n\n");
//    s();
//    return 0;
//    
//}
//
//void s(void)
//{
//    printf("Smile!");
//}

8
//#include <stdio.h>
//void one_three(void);
//void two(void);
//int main(void)
//{
//    printf("starting now:");
//    one_three();
//    two();
//    printf("\n\nthree");
//    printf("\n\ndone!");
//    return 0;
//}
//
//void one_three(void)
//{
//    printf("\n\none");
//}
//
//void two(void)
//{
//    printf("\n\ntwo");
//}

//example3.1
//#include <stdio.h>
//int main(void)
//{
//    float weight;
//    float value;
//    printf("Are you worth your weight in platinum?\n");
//    printf("Let's check it out.\n'");
//    printf("Please enter your weight in kgs:");
//    scanf("%f",&weight);
//    value=weight*1000*31/1.28*14.5833;
//    printf("Your weight in platinum is worth $%.2f.\n",value);
//    printf("You are easily worth that!If platinum prices drop,\n");
//    printf("eat more to maintain your value.\n");
//    return 0;
//}

example 3.3
//#include <stdio.h>
//int main(void)
//{
//    int x=100;
//    printf("dec=%d;ocal=%o;hex=%x\n",x,x,x);
//    printf("dec=%d;ocal=%#o;hex=%#X\n",x,x,x);
//    return 0;
//}
hexadecimal:(0x or 0X)%x -%#x or %#X;octal(0):%o-%#o;decimal:%d

//skiped 3.4.2
//can't understand 3.4.3.3

example 3.4.3.4
//#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;
//    
//}

example3.6
//#include <stdio.h>
//#include <inttypes.h>
//int main(void)
//{
//    int32_t me32;
//    me32 = 45933945;
//    printf("First,assume int32_t is int:  ");
//    printf("me32 = %d\n",me32);
//    printf("Next,let's not make ant assumptions.\n");
//    printf("Instead,use a \"macro\" from inttypes.h:  ");
//    printf("me32 = %" PRId32 "\n",me32);  //in inttypes.h,PRId32="d";when several character strings apear,they will be spliced
//    return 0;
//}

//3.4.6.2
/* 0xa.1fp10 
in hexadecimal,a=10;f=15(so .1f=1/16+15/256);p10=1024(2^10); */

//3.4.6.3
/* to print float(%f);for exponential notation,it is %e;for hexadecimal,a/A to replace e/E.
for long double,%Lf or %Le or %La */

//example 3.7
//#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("And it's %a in hexadecimal,powers of 2 notation\n",aboat);
//    printf("%f can be written %e\n",abet,abet);
//    printf("%Lf can be written %Le\n",dip,dip);
//    return 0;
//}

//3.4.9
//example3.8--to know the size of the type
//#include <stdio.h>
//int main(void)
//{
//    printf("Type int ha a size of %zd bypes.\n",sizeof(int));
//    printf("Tpye char has a size of %zd bypes.\n",sizeof(char));
//    printf("Type long has a size of %zd bytes.\n", sizeof(long));
//    printf("Type long long has a size of %zd bytes.\n",
//sizeof(long long));
//    printf("Type double has a size of %zd bytes.\n",
//sizeof(double));
//    printf("Type long double has a size of %zd bytes.\n",
//sizeof(long double));
//    return 0;
//}
//%zd is the return type of sizeof
//sizeof(int);sizeofvariable

//example3.10
//#include <stdio.h>
//int main(void)
//{
//    float salary;
//    printf("\aEnter your desired monthly salary:");
//    printf("$_______\b\b\b\b\b\b\b");
//    scanf("%f",&salary);
//    printf("\n\t$%.2f a month is $%.2f a year.",salary,salary*12.0);
//    printf("\rGee!\n");
//    return 0;
//}

//about \r
//#include <stdio.h>
//int main(void)
//{
//    printf("nihao\r");
//    printf("haha");
//    return 0;
//}


//exercise 3.10<keys>
/*1.
a. int;short;unsigned short
b.float;double
c.char
d.int;unsigned

2.too large

*/

//#include <stdio.h>
//int main(void)
//{
//    int imate = 2;
//    long shot = 53456;
//    char grade = 'A';
//    float log = 2.71828;
//    printf("The odds against the %d were %ld to 1.\n",
//imate, shot);
//    printf("A score of %f is not an %c grade.\n", log,
//grade);
//    return 0;
//}

//exercise3.11
/*1.? */
//2
//#include <stdio.h> 
//int main(void)
//{
//    char ascll;
//    printf("please input a number:");
//    scanf("%d",&ascll);
//    printf("%c\n",ascll);
//    return 0;
//}
 
//3
//#include <stdio.h>
//int main(void)
//{
//    printf("\a");
//    printf("Startled by the sudden sound,Sally shouted,\n");
//    printf("\"By the Great Pumpin,what was that!\"");
//    return 0;
//}

//4
//#include <stdio.h>
//int main(void)
//{
//    float n;
//    printf("Enter a floating-point value:");
//    scanf("%f",&n);
//    printf("fixed-point notation:%f",n);
//    printf("\nexponential notation:%e",n);
    printf("p notation: %#0xp",n);
//    return 0;
//}

//5
//#include <stdio.h>
//int main(void)
//{
//    int age;
//    double s;
//    printf("please input your age:");
//    scanf("%d",&age);
//    s=age * 3.156E7;
//    printf("it's equal to %f seconds.",s);
//    return 0;
//}

//6
//#include <stdio.h>
//int main(void)
//{
//    double quart;
//    double num;
//    printf("how much quarts does the water?");
//    scanf("%lf",&quart);
//    num=quart * 950 /(3.0E-23);
//    printf("the number of the water is %e",num);
//    return;
//    
//}

//#include <stdio.h>
//int main(void)
//{
//    float mass_mol = 3.0e-23;
//    float mass_qt = 950;
//    float quarts, molecules;
//
//    // 提示用户输入水的夸脱数
//    printf("Enter the number of quarts of water:");
//    scanf("%f", &quarts);
//    // 计算水分子的数量
//    molecules = quarts * mass_qt / mass_mol;
//    printf("%g quarts of water contain %e molecules.\n", quarts, molecules);
//    return 0;
//}

//7
//#include <stdio.h>
//int main(void)
//{
//    float height;
//    float h_cm;
//    printf("please input your height in inch:");
//    scanf("%f",&height);
//    h_cm = height * 2.54;
//    printf("the height in cm is equal to %f. cm",h_cm);
//    return 0;
//}

//8
//#include <stdio.h>
//int main(void)
//{
//    float cups;
//    float pint;
//    float ounce;
//    float soup;
//    float tea;
//    printf("please input the numbers os cups:");
//    scanf("%f",&cups);
//    pint = cups * 2;
//    ounce = cups / 8;
//    soup = ounce / 2;
//    tea = soup / 3;
//    printf("\nit is equal to %f pints,%f ounces,%f soup ladles,%f tea spoons.",
//    pint,ounce,soup,tea);
//    return 0;
//}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值