473,884 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Identifying variables: what process do you follow?

I'm trying to learn C. I have a fairly extensive background with C-based
scripting languages (Python,Tcl, others), so a great deal of C is
proving easy to pick up (control structures, functions, etc.). However,
what is *really* tripping me up is the entire process of identifying and
declaring variables at the beginning of your program, i.e.:

short int short_max = SHRT_MAX;
int integer_min = INT_MIN;
double double_max = DBL_MAX;

Generally, when developing a program in a scripting language, I follow
an interative development process, starting with some core
functionality, adding functions and variables as the program evolves. In
other words, I don't know in advance what variables I will need to declare.

With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
So, I'm wondering what approach seasoned C programmers use for larger
programs, i.e. ones larger than the simple single-function ones I'm
learning from my C textbook. Is there a design process that identifies
such things?

I hope this question about "mental models," getting into the C mindset,
isn't off-topic. TIA.

--Kevin
Oct 16 '07 #1
12 1978
Kevin Walzer said:
I'm trying to learn C. I have a fairly extensive background with C-based
scripting languages (Python,Tcl, others), so a great deal of C is
proving easy to pick up (control structures, functions, etc.). However,
what is *really* tripping me up is the entire process of identifying and
declaring variables at the beginning of your program, i.e.:

short int short_max = SHRT_MAX;
int integer_min = INT_MIN;
double double_max = DBL_MAX;
s/program/function/

Whilst it is possible and legal and all that to define objects at the
beginning of the *program* (at "file scope", outside any function), it is
better practice to define each object at the smallest scope that is
practical. Almost invariably, this means no wider than "function scope" -
and this makes functional decomposition and top-down development much
easier.
Generally, when developing a program in a scripting language, I follow
an interative development process, starting with some core
functionality, adding functions and variables as the program evolves. In
other words, I don't know in advance what variables I will need to
declare.
That's all right - neither do I. :-)
With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
So, I'm wondering what approach seasoned C programmers use for larger
programs, i.e. ones larger than the simple single-function ones I'm
learning from my C textbook. Is there a design process that identifies
such things?
I want to write a good answer to this bit for you, but it will probably be
rather long, and I don't have time right now. I will have time in a day or
two (or, perhaps, tonight). I'll "watch" this thread. If nobody else has
given you a decent explanation before then, I'll do my best to fill the
gap.
I hope this question about "mental models," getting into the C mindset,
isn't off-topic.
So do I, or we'll both get lynched by an angry mob. :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 16 '07 #2
Kevin Walzer <kw@codebykevin .comwrites:
Generally, when developing a program in a scripting language, I follow
an interative development process, starting with some core
functionality, adding functions and variables as the program
evolves. In other words, I don't know in advance what variables I will
need to declare.

With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
I usually write code and the corresponding variable declarations
in tandem. Sometimes it is obvious in advance what variables
will be needed ("I know I'll need a loop index"), so I'll declare
those. Then I go and write the code that uses them. Usually
there's a variable or two that I forgot to declare, so I go back
and insert that declaration. Then I look over the whole function
and try to informally prove to myself that it works. Often, it
obviously doesn't, so I adjust the code and the variable
declarations to fix it.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Oct 16 '07 #3
In article <53************ *************** @FUSE.NET>,
Kevin Walzer <kw@codebykevin .comwrote:
>
With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
So, I'm wondering what approach seasoned C programmers use for larger
programs, i.e. ones larger than the simple single-function ones I'm
learning from my C textbook. Is there a design process that identifies
such things?
Most of us don't type the complete program in a single pass from beginning to
end. If you use an editor instead, you'll have the ability to go back and add
variable declarations. The Standard Text Editor (ed) is sufficient, but there
are even more helpful editors available today, which actually make use of the
cursor positioning abilities of the terminal, enabling you to see the code as
you edit it!

With this modern wonder, the full-screen text editor, you may begin writing a
C function without including any local variable declarations. When you come
to a point where you need a local variable, instruct the editor to relocate
the cursor to the beginning of the function. This is often done by the
repeated use of a key labeled with an arrow pointing in an upwardly
direction, each press of which will cause the editor to respond by moving the
cursor onto the preceding line of your source code.

Having arrived at the beginning of the function, insert the declaration.
Locate the cursor movement key of opposing orientation to the one previously
employed. Press repeatedly until the return of the cursor to its starting
position is actualized.

Advanced users may find editor shortcuts for moving the cursor to the desired
locations with fewer keypresses.

.... Seriously, what are you editing with, "cat file.c" ?

--
Alan Curry
pa****@world.st d.com
Oct 16 '07 #4
On Oct 16, 9:23 am, Kevin Walzer <k...@codebykev in.comwrote:
With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
C99 (which is the latest standard) allows mixed declarations and code,
but is not widely implemented in full; however, in C90, you can also
do something like the following:

int myfunction (void) {
int a = 5;
/* do stuff with 'a' here */
{
int b = 6;
/* do stuff with 'a' and 'b' here */
}

/* do more stuff with 'a' here */
return a;
}

The scope of the variable 'b' begins at the inner opening brace and
ends at the inner closing brace, and as long as you keep its
declaration at the very beginning of that scope, it's legal in both
C90 and C99. Any amount of nested braces is allowed as well.

Oct 16 '07 #5
Alan Curry wrote:
In article <53************ *************** @FUSE.NET>,
Kevin Walzer <kw@codebykevin .comwrote:
>With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
So, I'm wondering what approach seasoned C programmers use for larger
programs, i.e. ones larger than the simple single-function ones I'm
learning from my C textbook. Is there a design process that identifies
such things?

Most of us don't type the complete program in a single pass from beginning to
end. If you use an editor instead, you'll have the ability to go back and add
variable declarations. The Standard Text Editor (ed) is sufficient, but there
are even more helpful editors available today, which actually make use of the
cursor positioning abilities of the terminal, enabling you to see the code as
you edit it!

With this modern wonder, the full-screen text editor, you may begin writing a
C function without including any local variable declarations. When you come
to a point where you need a local variable, instruct the editor to relocate
the cursor to the beginning of the function. This is often done by the
repeated use of a key labeled with an arrow pointing in an upwardly
direction, each press of which will cause the editor to respond by moving the
cursor onto the preceding line of your source code.

Having arrived at the beginning of the function, insert the declaration.
Locate the cursor movement key of opposing orientation to the one previously
employed. Press repeatedly until the return of the cursor to its starting
position is actualized.

Advanced users may find editor shortcuts for moving the cursor to the desired
locations with fewer keypresses.

... Seriously, what are you editing with, "cat file.c" ?
Emacs, actually.
Oct 16 '07 #6
Kevin Walzer wrote:
You need to identify
some variables ahead of time just to have a program that will compile!
Easy. Declare all possible variables you can think of upfront and when
you've finished writing your function, let the compiler tell you which
ones have not been used and delete them. (A similar principle to
counting sheep in a flock: count the legs and divide by four.)

Just kidding, Ben and Alan gave you the real answer.
Oct 16 '07 #7
On Oct 17, 12:30 am, jaysome <jays...@hotmai l.comwrote:
On Tue, 16 Oct 2007 20:24:26 -0000, Justin Spahr-Summers
<Justin.SpahrSu mm...@gmail.com wrote:
On Oct 16, 9:23 am, Kevin Walzer <k...@codebykev in.comwrote:
With C, such an approach doesn't seem practical. You need to identify
some variables ahead of time just to have a program that will compile!
C99 (which is the latest standard) allows mixed declarations and code,
but is not widely implemented in full; however, in C90, you can also
do something like the following:
int myfunction (void) {
int a = 5;
/* do stuff with 'a' here */
{
int b = 6;
/* do stuff with 'a' and 'b' here */
}
/* do more stuff with 'a' here */
return a;
}
The scope of the variable 'b' begins at the inner opening brace and
ends at the inner closing brace, and as long as you keep its
declaration at the very beginning of that scope, it's legal in both
C90 and C99. Any amount of nested braces is allowed as well.

If the scope of the variable 'b' begins at the inner opening brace,
shouldn't I be allowed to do something like this?

int myfunction (void) {
int a = 5;
/* do stuff with 'a' here */
{
int b0 = b;
int b = 6;
/* do stuff with 'a' and 'b' here */
}

/* do more stuff with 'a' here */
return a;

}

When I do this, I get a compiler error:

error C2065: 'b' : undeclared identifier

What gives?
I was thinking in a hurry after a long day. It obviously begins where
the declaration is. I'm fairly certain you realize that, but the OP
might not, so thanks for bringing it up.

Oct 17 '07 #8
Justin Spahr-Summers wrote:
Kevin Walzer <k...@codebykev in.comwrote:
.... snip ...
>
int myfunction (void) {
int a = 5;
/* do stuff with 'a' here */
{
int b = 6;
/* do stuff with 'a' and 'b' here */
}
/* do more stuff with 'a' here */
return a;
}

The scope of the variable 'b' begins at the inner opening brace
and ends at the inner closing brace, and as long as you keep its
declaration at the very beginning of that scope, it's legal in
both C90 and C99. Any amount of nested braces is allowed as well.
Err'm. From the C standard:

5.2.4.1 Translation limits

[#1] The implementation shall be able to translate and
execute at least one program that contains at least one
instance of every one of the following limits:12)

-- 127 nesting levels of blocks

which seems to me to define 'any amount' as 127. :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 19 '07 #9
jaysome wrote:
Justin Spahr-Summers <Ju************ *****@gmail.com wrote:
.... snip ...
>
>The scope of the variable 'b' begins at the inner opening brace
and ends at the inner closing brace, and as long as you keep its
declaration at the very beginning of that scope, it's legal in
both C90 and C99. Any amount of nested braces is allowed as well.

If the scope of the variable 'b' begins at the inner opening
brace, shouldn't I be allowed to do something like this?

int myfunction (void) {
int a = 5;
/* do stuff with 'a' here */
{
int b0 = b;
int b = 6;
It _can_ begin at the brace. It does begin when b is declared.

--
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/ (taming google)
<http://members.fortune city.com/nnqweb/ (newusers)

--
Posted via a free Usenet account from http://www.teranews.com

Oct 19 '07 #10

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

Similar topics

18
3722
by: lylefair | last post by:
Can you post code, or a reference to a md? or ad? file on a website, where object variables are not released when they go out of scope and cause a problem, (but causing a problem is extraneous to this question), other than these two: DAO.Database DAO.Recordset ? To rephrase: Can you demonstrate any situation where failure to
18
1447
by: Jeff S | last post by:
It seems that anyone can put anything on their resume and refer to themselves as a "senior Web developer." What are some clues that can be used by an IT manager during a hiring process to differentiate truly proficient Web developers from the "less-than" proficient Web developers? Thank You!
11
1932
by: Rolf Rosenquist | last post by:
I have a website with several input forms over a number of pages. The information that the user gives is following through the pages as session variables. It has been working good for two years. Now I am redoing the whole site and have all the different pages put into an Iframe on the first page. The form pages are the same as before, but now I seem to loose some of the session variables. But the strange thing is that if I first go...
4
3042
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including memory management and connection pooling constructs) as static variables which were intended to scoped to the process. 2. Managed C++ assemblies (.NET 1.1) that wrap the unmanaged DLLs as nice neat classes with managed interfaces.
18
3453
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that File-New-Window creates an instance of IE in the same process with the same SessionID as the parent window is in big trouble. This fundamentally restricts the usefullness of using session state management. I probably missed it somewhere - can...
58
4704
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
7
1962
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize, StringBuilder etc.. }
10
4521
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more or less, a taskId. In this case, the userSuppliedState {really taskId} is of the object type,...
0
9953
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10868
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7984
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.