C + + Note 13: C + + extension--c++ all variables and functions must have a type

Source: Internet
Author: User

C + + all variables and functions must have a type;

C The default type in the language is not legal in C + +!

In C language can write some very strange function! and can run successfully!

What type is the return value of the F (i) function? What is the parameter type? We have a value of 10 to print out!

How many parameters can a G () function accept? We have no parameters, but we can add parameters when the function is called and run successfully!

F (i)

{

printf ("i=%d\n", I);

}

G ()

{

return 5;

}

int main ()

{

f (10);

printf ("G () =%d\n", G (1,2,3,4,5));

return 0;

}

Operation Result:

i = 10

G () = 5

Press any key to Continu

So this is the C language bad place, this bad place C + + has abandoned it!

The same code we put in C + + to see!

#include <iostream>

using namespace Std;

F (i)

{

printf ("i=%d\n", I);

}

G ()

{

return 5;

}

int main ()

{

f (10);

printf ("G () =%d\n", G (1,2,3,4,5));

System ("pause");

return 0;

}

Found compile error.

In C + +, whether it is a function return value type or variable type, or the number of function parameters, we can not be sloppy, C + + compiler will be able to find the error! The following program compiles before you can pass!

#include <iostream>

using namespace Std;

int f (int i)

{

printf ("i=%d\n", I);

return 0;

}

int g ()

{

return 5;

}

int main ()

{

f (10);

printf ("G () =%d\n", g ());

System ("pause");

return 0;

}

So C + + is more rigorous in the type checking of variables and functions!

Summary:

in C in Language

an int f () is a function that returns a value of type int and accepts arbitrary arguments;

an int f (void) represents an parameterless function that returns a value of type int.

in C + + in

int f () and int f (void) have the same meaning, all of which represent an parameterless function that returns a value of type int.

C + + emphasizes the type, and any program element must indicate the type.

Long Press to unlock

Unlock more Insider Stories

Legal programming

: Lightspeed-tech

Technology-driven Life

C + + Note 13: C + + extension--c++ all variables and functions must have a type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.