Chapter 3: The C in C++ Creating functions

1.Introduction to data types

Data types define the way you use storage(memory) in the programs you write. By specifying a data type, you tell the compiler how to create a particular piece of storage, and also how to manipulate that storage.


2.Specifiers

Specifiers modify the meanings of basic built-in types and expand then to a much larger set. There are four specifierslong,short,signed, and unsigned.

sign and unsigned specifiers tell the compiler how the sign bit with integral types and characters(floating-point numbers always contain a sign).int has 8 bytes.But it has a sign bit so its minimum is -2^31 and maximum is 2^31 - 1. Howerer,unsigned int doesn't have the sign bit, so its minimus is 0 and maximum is 2 ^ 32 - 1.

short and long specifiers tell the compiler how many bits the type has.

#include<iostream>
using namespace std;

int main()
{
    cout << "short int: " << sizeof(short int) << endl;
    cout << "int: " << sizeof(int) << endl;
    cout << "unsigned int: " << sizeof(unsigned int) << endl;
    cout << "long int: " << sizeof(long int) << endl;
    cout << "long long int: " << sizeof(long long int) << endl;
    cout << endl;
    cout << "float: " << sizeof(float) << endl;
    cout << "double: " << sizeof(double) << endl;
    cout << "long double: " << sizeof(long double) << endl;
    cout << endl;
    cout << "char: " << sizeof(char) << endl;
    cout << "unsigned char: " << sizeof(unsigned char) << endl;
    return 0;
}
/*
 *
short int: 2
int: 4
unsigned int: 4
long int: 4
long long int: 8

float: 4
double: 8
long double: 12

char: 1
unsigned char: 1
 *
 */

However, I don't know the differences between int and long int.

3.Understand pointer through operator &

Operator & will produce the address of identifier.See the program below:

#include<iostream>
using namespace std;

int main()
{
	int a = 4;
	int* pa;
	double b = 8;
	double*pb;
	pa = &a,pb = &b;
	cout << pa << endl;
	cout << pb << endl;

	cout << &pa << endl;
	cout << &pb << endl;
	return 0;
}
/*
address of a: 0x22fedc
address of pbb: 0x22fed0

value of pa: 0x22fedc
value of pb: 0x22fed0

address of pa: 0x22fed8
address of pb: 0x22fecc

address of *pa: 0x22fedc
address of *pb: 0x22fed0
 */

From the results, we can see that the address of a is equal to the value of pa and also equal to the address of *pa.  So we can regard the *pa as a type which has 4 bytes(int 32b system) . It has its own memory and its value is address of the identifier.
4.void*

If you state that a pointer is a void*,it means that any type of address at all can be assigned to that pointer.

5.Specifying storage allocation

(1). Global variables

Global variables are defined outside all function bodies and are available to all parts of the program(even code in other files).Its lifetime lasts until the program ends.

(2)Local variables

Local variables are "local" to a function.

(3)static

Normally, the local variables defined local to a function disappear at the end of the function scrope. But if you want a value to be extant throughout the life of a program,you can define a function's local variable to be static and give it an initial value. The initialization is performed only the first time the function is called.See the program below:

#include<iostream>
using namespace std;

void f()
{
	static int t = 0;
	t++;
	printf("t:%d\n",t);
}

int main()
{
	for(int i = 0;i < 5;i++)
		f();
	return 0;
}
/*
t:1
t:2
t:3
t:4
t:5
 */
When static is applied to a function name or to a variable that is outside of all functions, it means "This name is unavailable outside of this file".We say it has file scope.

(4)extern

It tells the compiler hasn't yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.

6. Constants

In C++,use const to define a constant.One a constant is defined, it can't be modified. Pay attention,a const must alwayse have an initialization value.const has a scope just like a regular variable.

7. volatile

the qualifier volatile tells the compiler "You never know when this will change" and prevents the compiler from performing any optimizations based on the stability of that variable.

It will be further illuminated.

8.atoi(), atol(), atof()

These three functions are declared in <cstdlib>. They can convert an ASCII character array to an int,long and double respectively.

9.Defining a function pointer

Once a function is compiled and loaded into the computer to be executed, it occupies a chunk of memory. That memory, and thus the function, has an address.

To define a pinter to a function that has no arguments and no return value, like this:

void (*fun)();
Let's see another format:

int (*fun)(int,float);
This function pointer should be point to a function that has int and float parameters and returns int.
We can use it like below:

#include<iostream>
using namespace std;

int (*fun)(int,float);

int t(int m)
{
	cout << 12;
	return 0;
}
int main()
{
	fun = t;
	fun(3);
	return 0;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值