识别变量:您遵循什么流程? [英] Identifying variables: what process do you follow?

查看:71
本文介绍了识别变量:您遵循什么流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习C.我有一个相当广泛的背景与基于C的

脚本语言(Python,Tcl,其他),所以很多C是

证明易于提取(控制结构,功能等)。然而,

*真正*绊倒我的是整个过程中识别和

在程序开始时声明变量,即:


short int short_max = SHRT_MAX;

int integer_min = INT_MIN;

double double_max = DBL_MAX;


通常,在使用脚本语言开发程序时,我会遵循一个交互式开发过程,从一些核心

功能开始,随着程序的发展添加函数和变量。用

换句话说,我事先并不知道我需要声明哪些变量。


使用C,这样的方法不会看似实用。你需要提前确定一些变量,以便有一个程序可以编译!

所以,我想知道经验丰富的C程序员用什么方法来做大br />
程序,即大于简单单功能程序的程序,我是从我的C教科书学习的b $ b。是否有一个设计过程可以确定这些东西是什么?


我希望这个问题关于心理模型,进入C心态,

不是偏离主题的。 TIA。


- 凯文

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

推荐答案

Kevin Walzer说:
Kevin Walzer said:

我正在努力学习C.我有一个相当广泛的背景,基于C的

脚本语言(Python,Tcl,其他),所以很多C是

证明易于拾取(控制结构,功能等)。然而,

*真正*绊倒我的是整个过程中识别和

在程序开始时声明变量,即:


short int short_max = SHRT_MAX;

int integer_min = INT_MIN;

double double_max = DBL_MAX;
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 /


虽然可行且合法且所有这些都是在

*程序开头*(在文件范围之外,任何函数之外),它是

更好的做法,在最小范围内定义每个对象

实用。几乎总是如此,这意味着不比功能范围更广泛。 -

这使得功能分解和自上而下的开发更容易

更容易。

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.


一般来说,开发时一个脚本语言的程序,我遵循一个交互式开发过程,从一些核心的

功能开始,随着程序的发展添加函数和变量。用

换句话说,我事先并不知道我需要什么变量来b / b
声明。
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. :-)


有了C,这样一种方法看起来并不实用。你需要提前确定一些变量,以便有一个程序可以编译!

所以,我想知道经验丰富的C程序员用什么方法来做大br />
程序,即大于简单单功能程序的程序,我是从我的C教科书学习的b $ b。是否有一个设计过程可以确定这些东西是什么?
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.


我希望这个关于心理模型的问题,进入C心态,

不是偏离主题的。
I hope this question about "mental models," getting into the C mindset,
isn''t off-topic.



我也是,或者我们都会被愤怒的暴徒私刑。 :-)


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

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


Kevin Walzer< kw@codebykevin.comwrites:
Kevin Walzer <kw@codebykevin.comwrites:

一般来说,开发时一个脚本语言的程序,我遵循

一个交互式开发过程,从一些核心

功能开始,添加函数和变量作为程序

进化。换句话说,我事先并不知道我将需要声明的变量是什么变量。


使用C,这样的方法不会看似实用。你需要提前确定一些变量,以便有一个可以编译的程序!
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!



我通常编写代码和相应的变量声明

串联。有时事先很明显需要变量

(我知道我需要一个循环索引),所以我会声明

那些。然后我去编写使用它们的代码。通常

有一个或两个我忘了申报的变量,所以我回去

并插入该声明。然后我查看整个函数

并尝试非正式地向自己证明它是有效的。通常,它显然不是
,所以我调整代码和变量

声明来修复它。

-

给我们所有人的教训:即使在琐事中也有陷阱。

--Eric Sosman

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


在文章< 53 *************************** @ FUSE.NET>,

Kevin Walzer< kw@codebykevin.comwrote:
In article <53***************************@FUSE.NET>,
Kevin Walzer <kw@codebykevin.comwrote:

>
对于C,这种方法看起来并不实用。你需要提前确定一些变量,只是为了得到一个可以编译的程序!
所以,我想知道经验丰富的C程序员用于更大的程序,即更大的程序而不是简单的单功能我将从我的C教科书中学习。是否有设计过程来确定这些事情?
>
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?



我们大多数人从一开始就没有输入完整的程序,直到

结束。如果您使用编辑器,您将能够返回并添加

变量声明。标准文本编辑器(ed)就足够了,但是

是今天更有用的编辑器,它们实际上利用了终端的

光标定位能力,你看到代码为

你编辑它!


有了这个现代奇迹,全屏文本编辑器,你可以开始写一个

C函数,不包括任何局部变量声明。当你来到
到需要局部变量的点时,指示编辑器将光标重新定位到函数的开头。这通常是通过

重复使用标有箭头指向上方

方向的键来完成的,每按一次将导致编辑通过移动

光标到源代码的前一行。


到达函数的开头,插入声明。

找到与之前使用的相反方向的光标移动键。反复按直到光标返回其开始的

位置为止。


高级用户可以找到将光标移动到所需光标的编辑器快捷方式$>
按键次数较少的位置。


....说真的,你在编辑什么,cat file.c。 ?


-

Alan Curry
pa ****@world.std.com


这篇关于识别变量:您遵循什么流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆