Two basic questions

G

Garma

How to implement encapsulation and scope like C++ in C?

What are differences between Declaration and Definition in C?

My understanding is:

/*this is declaration and definition*/
int a;

/*this is declaration*/
extern int a;/*this is declaration*/

/*this is declaration*/
int function(void);

/*this is definition*/
int function(void){
....
}

/*this is declaration*/
extern int function(void);

Definition IS a declaration.
Definition is having a storage.
Declaration just claims the existence of a variable or function.

Is there anything I am missing here?


Thanks for your comments!
 
A

Arthur J. O'Dwyer

How to implement encapsulation and scope like C++ in C?

Split each "module" into a different source file, and use header
files to define the "interface" of each module (structures and
functions). I don't know what you mean by "scope" in this context.
What are differences between Declaration and Definition in C?

My understanding is:

/*this is declaration and definition*/
int a;

Your understanding is basically correct, except to note that in
C, the above line is what's called a 'tentative definition.' That
is, in C (unlike C++) we can write

int a;
int a;
int a;
int a;

and that's perfectly acceptable to the compiler. When the compiler
reaches the end of the file ('translation unit'), it will make one
of those 'tentative definitions' into a "real" definition, and reserve
space for the variable. The other 'tentative' definitions will be
treated as if they were only declarations.
/*this is declaration*/
extern int a;/*this is declaration*/

/*this is declaration*/
int function(void);

/*this is definition*/
int function(void){
...
}

/*this is declaration*/
extern int function(void);

Definition IS a declaration.
Definition is having a storage.
Declaration just claims the existence of a variable or function.

Is there anything I am missing here?

Not really. That's certainly enough to get you programming at
a competent level. If you care about the minutiae, you should also
look up what is a function 'prototype,' and when a declaration is
also a prototype and when it's not. (All of your function declarations
above count as prototypes, and that's a good thing.)

HTH,
-Arthur
 
G

Garma

Arthur J. O'Dwyer said:
Split each "module" into a different source file, and use header
files to define the "interface" of each module (structures and
functions). I don't know what you mean by "scope" in this context.


Your understanding is basically correct, except to note that in
C, the above line is what's called a 'tentative definition.' That
is, in C (unlike C++) we can write

int a;
int a;
int a;
int a;

and that's perfectly acceptable to the compiler. When the compiler
reaches the end of the file ('translation unit'), it will make one
of those 'tentative definitions' into a "real" definition, and reserve
space for the variable. The other 'tentative' definitions will be
treated as if they were only declarations.


Not really. That's certainly enough to get you programming at
a competent level. If you care about the minutiae, you should also
look up what is a function 'prototype,' and when a declaration is
also a prototype and when it's not. (All of your function declarations
above count as prototypes, and that's a good thing.)

HTH,
-Arthur
Thanks for your post! Two more questions related:
What is exactly "Translation Unit"? Is there any difference of the concept
in C and C++?
When a declaration is not "prototype"?
 
J

Jack Klein

Thanks for your post! Two more questions related:
What is exactly "Translation Unit"? Is there any difference of the concept
in C and C++?
When a declaration is not "prototype"?

A translation unit is basically a source file and everything it
#includes.

This is a function prototype, and also a declaration:

double sqrt(double);

This is a function declaration that is NOT also a prototype:

double sqrt();

The minimum a function declaration must do is specify the function's
name and its return type. A prototype must also specify the number
and types of the function's arguments.
 
J

j

Garma said:
Thanks for your post! Two more questions related:
What is exactly "Translation Unit"? Is there any difference of the concept
in C and C++?

A ``translation unit'' is one of those self-explanatory terms.
Generally all C implementations are compilers and a compiler's job
is to perform a translation. A unit is the mechanism in which we
use to contain our C code to feed to the compiler.
When a declaration is not "prototype"?

You mean, ``when is a function declaration not a prototype?''
It isn't a prototype when the type of the arguments are not
specified.

e.g., int foo(); /* is a function declaration but isn't a function prototype
*/

Though this is considered obsolescent by both c89 and c99.

or iow, avoid it.
 
R

Richard Heathfield

j said:
A ``translation unit'' is one of those self-explanatory terms.
Generally all C implementations are compilers

....except the ones that aren't, of course. (C interpreters exist.)

(I know, I know - that's why you said "Generally", right?)
 
T

tinybyte

Split each "module" into a different source file, and use header
files to define the "interface" of each module (structures and
functions). I don't know what you mean by "scope" in this context.

I think he means how to declare symbols that aren't visible to other
modules.
If you want them to be accessible only inside the module where you have
declared them, make them static:

---module1.c---

static int one;

---module2.c---

static int two;

---------------

So, for what module1.c is concerned, one is a global variable, but
module2 can't see it. You can't access one from module2.
The same applies for the variable two (toward module1).

Section A11 of the K&R2 deals with this argument.

Please correct me if I'm wrong.

Bye
Daniele
 
M

Manohar S

Arthur J. O'Dwyer said:
Split each "module" into a different source file, and use header
files to define the "interface" of each module (structures and
functions). I don't know what you mean by "scope" in this context.


to limit the visibility of the global variables to a particular file
you should use static. This can act similar to your private variables
in C++. For this to happen you need to consider the file as a class
and build on this concept.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top