##What novice programmers should know about C language The difference between C
When they first learned programming, did many people think that C language and C were the same? Today I will introduce in detail the differences between the following C language and C. Let us learn together.1. Keywords
Blue marks are C language keywords. C inherits all keywords of C language. The following red marks are C that contain but C Keywords that the language does not have (according to C 98, C contains 63 keywords)2. Source file differences C language file suffix is. c,c The original file name suffix is .cpp
If nothing is given when creating the source file, the default is .cpp
3. The return value is different
In C language, if a function does not specify a return value type , the default is int type, and returns a random number, usually 0XCCCCCCCC
In C, if the function has no return value, it must be specified as void type, otherwise the compilation cannot pass
4. Parameter list
In C In the language, when a function does not have a specified parameter list, it can receive any number of parameters by default
In C, there is strict parameter type detection. Functions without a parameter list default to void and do not receive any parameters.
Default parameters
Default parameters are function parameters that specify a default value when declared and defined. When calling this function, if no actual parameters are specified, the default values are used, otherwise the specified actual parameters are used.
The following code:
#include<iostream>using namespace std;void test(int a = 1) { cout << a << endl; }int main() { test(); test(10);//输出的结果是1 return 0;//函数输出结果是10}
The first is full default. All parameters of full default parameters have default values. If no parameters are passed manually, the compiler will use the parameters in the default parameter list. But it is worth noting here that if only some parameters are passed when passing parameters, the value will be matched from left to right.
Code example:
#include<iostream>using namespace std;void test(int a = 1,int b = 2, int c = 3) { cout << a << " " << b << " " << c << endl; }int main() { test();//1 2 3 test(10);//10 2 3 test(10, 20);//10 20 3 test(10, 20, 30);//10 20 30 return 0; }
Semi-default parameter code demonstration:
void test(int a ,int b = 2, int c = 3) { cout << a << " " << b << " " << c << endl; }void test1(int a, int b, int c = 3) { cout << a << " " << b << " " << c << endl; }
Note: Parameters with default values must be placed at the end of the parameter list. Because parameters are passed from right to left.
Default parameters cannot appear in both function declaration and definition, only one of them can be left.
The default value must be a constant or a global
variable.
C language does not support default.
5. C supports function overloading, but C language does not support it. In actual development, sometimes we need to implement several functions with similar functions, but some details are different. For example, if we want to exchange the values of two variables, which have multiple types, such as int, float, char, bool, etc., we need to pass the address of the variable into the function through parameters. In C language, programmers often need to design three functions with different names, and their function prototypes are similar to the following:
void swap1(int *a, int *b); //交换 int 变量的值 void swap2(float *a, float *b); //交换 float 变量的值 void swap3(char *a, char *b); //交换 char 变量的值 void swap4(bool *a, bool *b); //交换 bool 变量的值
The parameter list is also called parameter signature, including the type of parameter, the number of parameters and the order of parameters. As long as there is one difference, it is called a different parameter list.#include <iostream>using namespace std;//交换 int 变量的值void Swap(int *a, int *b){int temp = *a; *a = *b; *b = temp; }//交换 float 变量的值void Swap(float *a, float *b){float temp = *a; *a = *b; *b = temp; }//交换 char 变量的值void Swap(char *a, char *b){char temp = *a; *a = *b; *b = temp; }//交换 bool 变量的值void Swap(bool *a, bool *b){char temp = *a; *a = *b; *b = temp; }int main(){//交换 int 变量的值int n1 = 100, n2 = 200; Swap(&n1, &n2);cout<<n1<<", "<<n2<<endl;//交换 float 变量的值float f1 = 12.5, f2 = 56.93; Swap(&f1, &f2);cout<<f1<<", "<<f2<<endl;//交换 char 变量的值char c1 = 'A', c2 = 'B'; Swap(&c1, &c2);cout<<c1<<", "<<c2<<endl;//交换 bool 变量的值bool b1 = false, b2 = true; Swap(&b1, &b2);cout<<b1<<", "<<b2<<endl;return 0; }Copy after login
Run results:
200, 100 56.93, 12.5 B, A 1, 0
Overloading means within a scope (same class, same namespace, etc. ) has multiple functions with the same name but different parameters. The result of overloading is that a function name has multiple uses, making naming more convenient (in medium and large projects, naming variables, functions, and classes is a vexing problem) and calling more flexible. When using overloaded functions, the functions of the functions with the same name should be the same or similar. Do not use the same function name to implement completely unrelated functions. Although the program can run, the readability is not good and makes people confused. I feel baffled.Note that different parameter lists include different numbers, types, or orders of parameters. It is not acceptable to just have different parameter names. Function return values cannot be used as a basis for overloading.
Rules for overloading functions:
(1) The function names must be the same.
(2) The return types of functions can be the same or different.
(3)仅仅返回类型不同不足以成为函数的重载。
6、指针和引用
C语言中函数传参方式有两种:传值和传址
以传值方式,在函数调用过程中会生成一份临时变量用形参代替,最终把实参的值传递给新分配的临时形参。它的优点是避免了函数调用的副作用,却无法改变形参的值。如果要改变实参的值,只能通过指针传递。
指针可以解决问题,但是不安全,因此在C++中引入了引用。
引用:引用不是新定义的一个变量,他是原变量的一个别名,编译器不会为引用变量开辟空间,它和他引用的变量共用同一块内存空间。
类型& 变量(对象名)=引用变量
int &num1=num0;
引用特性;:
(1)引用定义时必须初始化
(2)一个变量可以有多个引用
(3)引用一旦绑定一个实体就不能改变为其他变量的引用
//指针和引用的区别
引用不可以为空,但指针可以为空
引用不可以改变指向,对一个对象”至死不渝”;但是指针可以改变指向,而指向其它对象
引用的大小是所指向的变量的大小,因为引用只是一个别名而已;指针是指针本身的大小,4个字节。
7、命名空间
在C++中,变量、函数和类都是大量存在的,这些变量、函数和类的名称将都存在于全局命名空间中,会导致很多冲突,使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或者名字污染,namespace关键字的出现就是解决这种问题。而C语言中没有。
8、输入与输出
cout代表c++的输出流
cin代表c++的输入流
它们都是在头文件“iostream”中定义。
“cout”必须与”<<”一起使用,“<<”起到插入的作用。
在一条语句中可以多次使用“<<”输出多个数据。
如:cout<
#include <iostream>using namespace std;int main() {int a,b;cout<<"请输入a,b的值"<<endl;cin>>a>>b;cout<<"输出a的值"<<a<<"输出b的值"<<b<<endl;return 0; }
感谢大家的阅读,大家现在知道C语言和C++的区别了吗?
本文转自:https://blog.csdn.net/qq_39539470/article/details/81268916
推荐教程:《C语言》
The above is the detailed content of What novice programmers should know about the difference between C language and C++. For more information, please follow other related articles on the PHP Chinese website!