473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Two basic questions

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!
Nov 14 '05 #1
7 2787

On Thu, 15 Jan 2004, Garma wrote:

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

Nov 14 '05 #2
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi**********************************@chloropr ene.ww.andrew.cmu.edu...

On Thu, 15 Jan 2004, Garma wrote:

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

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"?


Nov 14 '05 #3
j

"Garma" <Ga*****@nosysserv.com> wrote in message
news:VG********************@bgtnsc04-news.ops.worldnet.att.net...
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi**********************************@chloropr ene.ww.andrew.cmu.edu...

On Thu, 15 Jan 2004, Garma wrote:

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

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.


Nov 14 '05 #4
On Thu, 15 Jan 2004 03:21:25 GMT, "Garma" <Ga*****@nosysserv.com>
wrote in comp.lang.c:
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi**********************************@chloropr ene.ww.andrew.cmu.edu...

On Thu, 15 Jan 2004, Garma wrote:

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

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
j wrote:
"Garma" <Ga*****@nosysserv.com> wrote in message
news:VG********************@bgtnsc04-news.ops.worldnet.att.net...

<snip>
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


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

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

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #6
On Wed, 14 Jan 2004 21:42:49 -0500, Arthur J. O'Dwyer wrote:
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

Nov 14 '05 #7
j

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:bu**********@sparta.btinternet.com...
j wrote:
"Garma" <Ga*****@nosysserv.com> wrote in message
news:VG********************@bgtnsc04-news.ops.worldnet.att.net... <snip>
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


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

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


Right :)
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
8
by: Orange Free | last post by:
I want to create a program that will ask a user a series of questions and then generate a Microsoft Word document whose content is dictated by the answers. I am not a professional programmer, and...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
2
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
3
by: Jim H | last post by:
If there is a site someone can point me to that answers such basic questions, rather than taking up support's time posting answers, please let me know. I've developed C# .NET, unmanaged C++, and...
2
by: Fay Yocum | last post by:
BEWARE beginner questions!! I have some experience in Access but never as much as I want or need. I have decided to get in on VB.Net. I would only rate myself in Access as a...
0
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
4
by: Goran Djuranovic | last post by:
Hi all, I am experiencing a strange thing happening with a "designer.vb" page. Controls I manually declare in this page are automatically deleted after I drop another control on a ".aspx" page. -...
2
by: LayneMitch via WebmasterKB.com | last post by:
Hello. This is a basic quiz. Only about 3 questions. I'm having issue with the answer key which stays on a certain value regardless of acknowledging that you have the correct answer for the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.