Sign in to follow this  
Followers 0
bluepig.man

about keyword and include

4 posts in this topic

Hey,everyone. Today, l try write some code but without include any header file, but this code run well. In my opinion, if we want use something in c++ we need to include it's header file into our program. But why these keyword no need to include any header file ?

If they aren't in header file, where they are? l tried to google ,but just can't find the answer. so please help. Thanks.

this the code:

class a
{
public:
	int d;
	int b;
private:
};

struct B
{
	double c;
};

int A( const int &a, const double &b )
{
	int justTest = 5;
	int aa = a;
	int bb = b;
	return bb;
}

union MyUnion
{
	int a;
	int b;
	struct c
	{
		double ad;
	};
};

int main()
{
	a in;
	in.d = 0;
	in.d -= 5;
	in.b = 6;
	in.d -= in.b;

	return 0;
}

Edited by bluepig.man
0

Share this post


Link to post
Share on other sites

in this example you only use language native elements, without referring to any external function, type or class. That's why you d'ont have to include anything there. Just add somewhere

 

std::string s = "foo";

 

and you'll get an error because of missing headers.

0

Share this post


Link to post
Share on other sites

If I understand your question correctly, you're asking why the keywords of C++ do not need a header file.  

 

Simply, they are a part of the language.  A compiler / parser is essentially hard-coded with the rules it needs to understand what keywords are, and what it is supposed to do with them.  Several international C++ standards exist which give precise definitions, depending on the version of C++ you're using.  

 

While it might be possible for a tool to construct a language from a set of rules, such as the keywords, you would have to require that tool to have certain rules to understand those definitions.  At some point a tool must have some set of basic definitions for it to do its job.  And that's what keywords partially represent -- an internal skeleton that guides the parsing / compiling processes to choose the appropriate rules when translating your code.

Nice answer!  Before ask this question, l think if we want use something from language we must include some header file,but yesterdayl try to remove any header in my source file,then no error is reported.  This break my cognitive.  Your answer is very nice for me, thanks.

Edited by bluepig.man
0

Share this post


Link to post
Share on other sites

You may wonder why / how the language designers decide what goes in the language as a "keyword" and / or "part of the core language", and what goes in the language as part of the "standard library" / aka requiring "#includes".

 

The normal short answer is - the language core is usually the smallest part reasonable, that can be used to write / reference the rest - they put nearly everything they can into the "standard library" instead of the language core ... but obviously if nothing is in the language itself, you couldn't do anything at all - so a certain subset of features needs to exist at a basic level, to even support the concept of writing basic code, and referencing and using already written code.

 

And as in Cosmic's answer ... it also includes whatever set of "rules" are needed so that the compiler / parser can just divide and understand the program's structure and content.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0