C+Primer+Plus学习笔记-第二章

1.如何称呼c程序的基本模块

main函数是c程序的基本模块

2.什么是数据类型

例如:整数、字符、浮点数

3.实例说明

#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;

}

a)#include指示和头文件

这是程序的第一行,用于将头文件的内容复制黏贴到源代码文件中,#include指令是c预处理指令的一个例子,通常c编译器在编译前要做一些准备工作,称为预处理

stdio.h文件作为所有c编译包的一部分提供,包含有关输入和输出的函数的信息以供编译器使用,头文件包括了建立最终可执行程序时编译器需要用到的信息。例如,可定义常量,或者说明函数名以及该函数如何使用。函数的实际代码包含在一个预编译代码的库文件中。编译器的链接部分负责找到所需要的库代码,简言之,头文件指引编译器把程序正确的组合到一起

注:#include并不是c语言的语句,#符号表明这一行是在编译器接手前由c预处理器处理的语句

b)main()函数

一个c程序,一般是从main开始执行的

 

4.多个函数

#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");

}

Butler()函数在程序中出现3次。第一次出现在原型中,通知编译器要用到该函数,第二次在main()函数中以函数调用的形式出现的。最后,程序给出了函数的定义,即函数本身的源代码

原型是一种声明形式,告诉编译器正在使用一个特殊的函数,也指明函数属性,例如butler第一个void表明butler()没有返回值,第二个void表明butler(void)没有参数。因此,当编译器到达main()函数中的butler()的调用处会检查butler()的使用是否正确。

通过简单的调用语句,在main()中就可以调用该函数了,当butler()执行完毕后,程序会继续执行butler()之后的语句。

注:butler()函数的执行时间由main()函数调用它的位置决定

 

5.10种打印hello的方法

#include<stdio.h>

 

void hello1()

{

printf("helloworld");

}

 

void hello2()

{

printf("A%sB", "helloworld");//"helloworld"按照%s格式化为字符串

}

void hello3()

{

//printf("%c%c", ' ','B');

printf("%c%c%c%c%c%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd');

}

 

void hello4()

{

putchar('h');

putchar('e');

putchar('l');

putchar('l');

putchar('o');

putchar('w');

putchar('o');

putchar('r');

putchar('l');

putchar('d');

 

}

 

void hello5()

{

//putchar(65);

//putchar(66);

putchar(104);

putchar(101);

putchar(108);

putchar(108);

putchar(111);

putchar(119);

putchar(111);

putchar(114);

putchar(108);

putchar(100);

 

}

void hello6()

{

//putchar(65);

//putchar(66);

putchar(0150);//前面带0是八进制,八进制代表字符的编号

putchar(0145);

putchar(0154);

putchar(0154);

putchar(0157);

putchar(0167);

putchar(0157);

putchar(0162);

putchar(0154);

putchar(0144);

 

}

void hello7()

{

//putchar(65);

//putchar(66);

putchar(0x68);//0x是十六进制,

putchar(0x65);

putchar(0x6c);

putchar(0x6c);

putchar(0x6f);

putchar(0x77);

putchar(0x6f);

putchar(0x72);

putchar(0x6c);

putchar(0x64);

 

}

void hello8()

{

//putchar(65);

//putchar(66);

putchar('\150');//前面带0是八进制,八进制代表字符的编号

putchar('\145');// '\ddd'.3位八进制代表的字符

putchar('\154');

putchar('\154');

putchar('\157');

putchar('\167');

putchar('\157');

putchar('\162');

putchar('\154');

putchar('\144');

 

}

void hello9()

{

//putchar(65);

//putchar(66);

putchar('\x68');// '\xhh'16进制数据转换成编号,查找字符

putchar('\x65');

putchar('\x6c');

putchar('\x6c');

putchar('\x6f');

putchar('\x77');

putchar('\x6f');

putchar('\x72');

putchar('\x6c');

putchar('\x64');

 

}

 

 

void hello10()

{

puts("helloworld");//传入字符串,打印字符串

}

 

 

void mainhello()

{

hello10();

 

 

getchar();

}



以上所述有部分来自原作,部分来自本人理解,如有错误的地方,欢迎提出指正和批评

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值