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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章