# Extremeely basic programming question.

**URL:** <https://boards.straightdope.com/t/extremeely-basic-programming-question/624300>\
**Category:** Factual Questions\
**Created:** [June 6, 2012, 6:48pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300 "2012-06-06T18:48:37Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![RaftPeople](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@RaftPeople](https://boards.straightdope.com/u/RaftPeople)\
**Post date:** [June 7, 2012, 5:39am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/41 "2012-06-07T05:39:00Z")

</div>

> [@Senegoid](#):
>
> The pair of statements:  
> int i ;  
> i = 5 ;  
> is much more UN-ambiguous, and it is quite clear what result this causes. I think modern languages typically _attempt_ to define the single statement: int i = 5 ; to produce the same result in all cases – but it gets confusing, I think.

You know what the compiler does with that?

It notices that “i” is only used for a loop, unrolls the loop for optimization and never even creates or initializes the variable “i” 🙂

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [June 7, 2012, 5:41am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/42 "2012-06-07T05:41:08Z")

</div>

> [@Kinthalis](#):
>
> It is present in what are called strongly typed languages. Languages that require the compiler to check for valid data types before running.

That isn’t what strongly-typed means. For example, C requires the compiler to check for valid data types, but it isn’t strongly typed. Neither is C++. They’re weakly typed, meaning there are ways around the type system, so you can break invariants the type system is mean to ensure. They’re also statically typed, which is the term you should have used there; statically typed means that variables have types attached to them, not values, so a variable can only hold data of one type.

Strongly typed means that certain interfaces cannot be breached, certain invariants must always hold. Python is strongly typed for its built-in types, in that you cannot (for example) introspect into a floating-point value and view it as a sequence of bytes, as you can in C and C++. Python is also dynamically typed, which means values, not variables, have types attached to them, so a variable can hold data of multiple types.

For the record, Haskell is strongly and statically typed. I don’t really know of any modern languages that are dynamically and weakly typed.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 7, 2012, 6:06am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/43 "2012-06-07T06:06:09Z")

</div>

> [@Xema](#):
>
> Indeed, the details can vary. I was attempting to describe what is typically happening “behind the scenes”.

Well, in fact depending on what the program does, the compiler/optimizer might go ahead and decide either to:

1. Not reserve any new memory and use a previously declared variables space. This is tied to the concept of a “live range”, if there are two variables i and j and i’s live range expires before j’s begins, the compiler may merge variables such that i and j are identifiers for the same internal variable.

2. Never even put it in memory, if a variable is sufficiently short lived the value(s) assigned to it might just sit in a register somewhere, or in extreme cases the values, if sufficiently predictable, might become hard-coded into the generated executable. For instance

Load X into $1  
add $1 and 5 into $3

Might become

set $3 to 7

If X can only be 2 (though for various reasons in some languages this may not be done in case the programmer needs to be able to directly alter the space of memory X is assigned).

But now we’re getting into nitpicky land and you’re right that what you said is probably **generally** true.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [June 7, 2012, 6:10am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/44 "2012-06-07T06:10:35Z")

</div>

> [@Derleth](#):
>
> Python is strongly typed for its built-in types, in that you cannot (for example) introspect into a floating-point value and view it as a sequence of bytes, as you can in C and C++.

Sure you can, although it requires a function call:

> **[struct — Interpret bytes as packed binary data](https://docs.python.org/3/library/struct.html)**
>
> Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...

I could accept an argument that C is weakly typed, but in my experience, strong- vs. statically- typed is nearly synonymous. I’ve never heard anyone besides you claim that C is weakly typed. Besides, the notion of strong typing is really a collection of properties that nobody will completely agree on. It’s better to just describe what kind of type safety mechanisms a given language implements.

> [@Derleth](#):
>
> I don’t really know of any modern languages that are dynamically and weakly typed.

Most scripting languages are both dynamic and weakly typed. Perl, for instance.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 7, 2012, 6:11am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/45 "2012-06-07T06:11:50Z")

</div>

> [@Derleth](#):
>
> That isn’t what strongly-typed means. For example, C requires the compiler to check for valid data types, but it isn’t strongly typed. Neither is C++. They’re weakly typed, meaning there are ways around the type system, so you can break invariants the type system is mean to ensure. They’re also statically typed, which is the term you should have used there; statically typed means that variables have types attached to them, not values, so a variable can only hold data of one type.
> 
> Strongly typed means that certain interfaces cannot be breached, certain invariants must always hold. Python is strongly typed for its built-in types, in that you cannot (for example) introspect into a floating-point value and view it as a sequence of bytes, as you can in C and C++. Python is also dynamically typed, which means values, not variables, have types attached to them, so a variable can hold data of multiple types.
> 
> For the record, Haskell is strongly and statically typed. I don’t really know of any modern languages that are dynamically and weakly typed.

It’s actually not so clear cut. Strong vs Weak typing aren’t very well defined terms, some people say that any out of the blue, ad-hoc polymorphism or implicit conversion makes a language weakly typed, other people say that that’s a completely silly definition and say that there’s sort of a pecking order of strongness. This usually goes something like Haskell \> Java/C# \> C … (to use a really incomplete list).

As for a weak, dynamically typed language, I **think** Lua is.

---

<div class="post-metadata">

**Author:** ![Sofis](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Sofis](https://boards.straightdope.com/u/Sofis)\
**Post date:** [June 7, 2012, 6:18am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/46 "2012-06-07T06:18:30Z")

</div>

> [@BarryB](#):
>
> Here are the models they considered (lines in figure 1 being the answers)
> 
> 1. Value moves from right to left (a := b; b := 0 – third line in ﬁgure 1).
> 2. Value copied from right to left (a:=b–fourth line of ﬁgure 1, and the ‘correct’ answer).
> 3. Value moves from left to right (b := a; a := 0 – eighth line of ﬁgure 1).
> 4. Value copied from left to right (b := a – ﬁfth line of ﬁgure 1, and a reversed version of  
> the ‘correct’ answer).
> 5. Right-hand value added to left (a := a+b – second line of ﬁgure 1).
> 6. Right-hand value extracted and added to left (a := a+b; b := 0 – ﬁrst line of ﬁgure 1).
> 7. Left-hand value added to right (b := a+b – omitted in error).
> 8. Left-hand value extracted and added to right (b := a+b; a:=0 – omitted in error).
> 9. Nothing happens (sixth line of ﬁgure 1).
> 10. A test of equality: nothing happens (fourth and ﬁfth lines of ﬁgure 1).
> 11. Variables swap values (seventh line in ﬁgure 1).
> 
> What other models are there?

“a is tied to the value of b, and will change as b changes.” Admittedly, it looks like their tests are insufficient for distinguishing this from the standard C-style model, so they’d still mark it as consistent.

---

<div class="post-metadata">

**Author:** ![Dervorin](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dervorin](https://boards.straightdope.com/u/Dervorin)\
**Post date:** [June 7, 2012, 7:27am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/47 "2012-06-07T07:27:22Z")

</div>

> [@Senegoid](#):
>
> Here is a link that I promised a few posts above.
> 
> [http://www.textfiles.com/100/famous.bug](http://www.textfiles.com/100/famous.bug)
> 
> This is the classic famous “Famous Bugs” document. It is a plain-text file from an earlier era (I first saw it in the mid-1980’s), consisting of a loosely collected compendium of disastrous programming bug reports. And yes, it includes some mentions of bugs that killed people, for example by fouling up high-tech medical equipment. There is one story of a space probe that was lost because somebody tried to send it a software update that caused the directional antenna to cease pointing back at Earth, thus losing all communication with it forever. It’s a kinda fun read.

I’m a bit late to the party, but here’s a slightly more up-to-date list:  
[http://www.wired.com/software/coolapps/news/2005/11/69355?currentPage=all](http://www.wired.com/software/coolapps/news/2005/11/69355?currentPage=all)

Still makes interesting reading, and also makes you realise how much we depend on software to control our everyday lives, and just how hard it is to make truly reliable software. It does make me a little more tolerant of crashes in commercial software, because even simple programs that I’ve written have had subtle and not-so-subtle bugs which were very hard to diagnose and fix.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [June 7, 2012, 7:41am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/48 "2012-06-07T07:41:41Z")

</div>

> [@Dr.Strangelove](#):
>
> I’ve never heard anyone besides you claim that C is weakly typed.

Really? That is bizarre. C allows you to, for example, create a floating-point value bit-by-bit and feed in into the floating-point hardware on your CPU.

> [@](#):
>
> Most scripting languages are both dynamic and weakly typed. Perl, for instance.

OK, then show me how to typecast a Perl integer value into a Perl floating-point value in the same way you can in C.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [June 7, 2012, 8:03am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/49 "2012-06-07T08:03:29Z")

</div>

> [@Derleth](#):
>
> Really? That is bizarre. C allows you to, for example, create a floating-point value bit-by-bit and feed in into the floating-point hardware on your CPU.

Yes, but you have to be explicit about it. You can’t do it by accident. C# is sililar, except that it requires the entire block to be surrounded by “unsafe”.

> [@](#):
>
> OK, then show me how to typecast a Perl integer value into a Perl floating-point value in the same way you can in C.

You can’t do it via cast. But you can do this:  
printf ‘0x%x’, unpack ‘L’, pack ‘f’, 3.14159;

result: 0x40490fd0

And then:  
print unpack ‘f’, pack ‘L’, 0x40490fd0;

result: 3.1415901184082 (the slightly different result is because of limited floating point precision)

Since I know that the ones bit of the exponent is at 0x00800000, I can do this if I want:  
print unpack ‘f’, pack ‘L’, 0x40490fd0 | 0x00800000;

result: 6.28318023681641

This doesn’t work for the true internal types, though, like hash tables.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [June 7, 2012, 8:07am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/50 "2012-06-07T08:07:48Z")

</div>

> [@Derleth](#):
>
> OK, then show me how to typecast a Perl integer value into a Perl floating-point value in the same way you can in C.

To expand, though: that’s not the only way something can be weakly typed. Perl allows mixed types to work in a “do what I mean” fashion. For instance:  
print 1.0 + ‘1.0’;

result: 2

Even though the second operand of the addition is a string, Perl decided that I was probably using it as a floating-point number, and converted it for me without so much as a warning.

---

<div class="post-metadata">

**Author:** ![Xema](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@Xema](https://boards.straightdope.com/u/Xema)\
**Post date:** [June 7, 2012, 11:26am UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/51 "2012-06-07T11:26:41Z")

</div>

> [@Jragon](#):
>
> … depending on what the program does, the compiler/optimizer might go ahead and decide either to…

Right - you are describing certain optimizations that a compiler indeed might perform.

My point was more along the lines that it’s part of a (good) compiler’s job to complain when the programmer does things such as attempt more than once to allocate memory to the same symbol name. As was noted upthread, a compiler whose mission is “Let’s see if we can make some sort of sense of this program” is likely to be less useful than one that insists on precision.

---

<div class="post-metadata">

**Author:** ![Quercus](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@Quercus](https://boards.straightdope.com/u/Quercus)\
**Post date:** [June 7, 2012, 12:23pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/52 "2012-06-07T12:23:24Z")

</div>

> [@Napier](#):
>
> a = b could also be interpreted as the claim that a and b are already equal. It would evaluate to 1 if that were true and 0 if it weren’t. For example 5 + (a = b) could be 5 or 6, depending.

Do I recall correctly that in some languages Booleans evaluate to -1 or 0?

---

<div class="post-metadata">

**Author:** ![Sicks\_Ate](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sicks\_Ate](https://boards.straightdope.com/u/Sicks_Ate)\
**Post date:** [June 7, 2012, 12:45pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/53 "2012-06-07T12:45:20Z")

</div>

Well, it started out extremely basic…

---

<div class="post-metadata">

**Author:** ![Mijin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mijin/32/9369_2.png) [@Mijin](https://boards.straightdope.com/u/Mijin)\
**Post date:** [June 7, 2012, 12:45pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/54 "2012-06-07T12:45:24Z")

</div>

> [@Quercus](#):
>
> Do I recall correctly that in some languages Booleans evaluate to -1 or 0?

In C there is (or was) no bool type. So programmers would use integers for data that actually just represented a boolean. But what integer values map to true or false? The most common definitions were to define FALSE as 0 and TRUE as either 1, or !FALSE (i.e. so any value that is non-zero would be considered TRUE).

However, as it was up to the programmer, different teams would have different standards. And yes, there were many problems caused by not every member of the team knowing what the standard was, or not being consistent, or plugging software together that had different standards etc.

I’ve seen FALSE defined as -1 and TRUE as 0. There was even a rationale for it, but I can’t remember what it was…

---

<div class="post-metadata">

**Author:** ![Thudlow\_Boink](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/thudlow_boink/32/320_2.png) [@Thudlow\_Boink](https://boards.straightdope.com/u/Thudlow_Boink)\
**Post date:** [June 7, 2012, 1:08pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/55 "2012-06-07T13:08:53Z")

</div>

> [@Mijin](#):
>
> I’ve seen FALSE defined as -1 and TRUE as 0. There was even a rationale for it, but I can’t remember what it was…

Maybe because the bitwise NOT of 0 is -1 (all 1s in binary)?

---

<div class="post-metadata">

**Author:** ![robardin](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@robardin](https://boards.straightdope.com/u/robardin)\
**Post date:** [June 7, 2012, 1:26pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/56 "2012-06-07T13:26:47Z")

</div>

Yeah, when I learned C programming it was common for people to have a header file like “consts.h” to macro define

#define TRUE 1  
#define FALSE 0  
#define NULL 0

So code would read more like similar implementations in other languages. One prank of the “don’t leave your terminal logged in at the undergrad CS lab” was to surreptitiously change the value of a NULL #define there in someone’s private code to a non null value to cause memory pointer errors 🙂

As for 0 being true, consider that most system process programs effectively do that. A successful invocation of a command returns 0, but any failure or warning has a unique error code to tell you what went wrong. If you view the idea of a function return of Boolean state as a special case of “integer as error code (0=no error)”, and want all the code to read the same (0=good, no problems, what the code expected), it’s a pretty natural convention.

That’s how the standard C string function strcmp() behaves, as an obvious example.

---

<div class="post-metadata">

**Author:** ![Mijin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mijin/32/9369_2.png) [@Mijin](https://boards.straightdope.com/u/Mijin)\
**Post date:** [June 7, 2012, 2:30pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/57 "2012-06-07T14:30:20Z")

</div>

> [@Thudlow\_Boink](#):
>
> Maybe because the bitwise NOT of 0 is -1 (all 1s in binary)?

Ah, yes that was probably it. So it wouldn’t matter (here) if you mixed up bitwise and logical NOT.

---

<div class="post-metadata">

**Author:** ![Sicks\_Ate](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sicks\_Ate](https://boards.straightdope.com/u/Sicks_Ate)\
**Post date:** [June 7, 2012, 3:39pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/58 "2012-06-07T15:39:17Z")

</div>

Mk, I get career-change whims now and then (which I don’t act on) and I think this will be my next.

What would the process look like for someone to go from zero programming knowledge, to knowing a language well enough to gain employment? No degree involved; just pick a language, learn it, and start applying for jobs? Is that even possible? Which language would be a good done to choose?

---

<div class="post-metadata">

**Author:** ![Mijin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mijin/32/9369_2.png) [@Mijin](https://boards.straightdope.com/u/Mijin)\
**Post date:** [June 7, 2012, 4:02pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/59 "2012-06-07T16:02:45Z")

</div>

> [@Sicks\_Ate](#):
>
> Mk, I get career-change whims now and then (which I don’t act on) and I think this will be my next.
> 
> What would the process look like for someone to go from zero programming knowledge, to knowing a language well enough to gain employment? No degree involved; just pick a language, learn it, and start applying for jobs? Is that even possible? Which language would be a good done to choose?

Yes absolutely this is possible IMO. If you’re committed, I’d say about 3 months intensively studying evenings and weekends, followed by a further three months creating one or more of your own projects for a portfolio. If you can’t work intensively, adjust the timespans accordingly.

I mention a portfolio, as because you won’t have any formal or commercial experience, having some coding examples is basically essential. But programmers are very much in demand (yes, even now); so I don’t think that not having a degree, say, is that big a problem in itself. If anything, the danger is that you get hired for a job that you end up finding too challenging.

I personally would recommend Java or C#. Purists may be horrified by this as both of these languages have “managed memory” and so abstract you a little further from the system than, say, C++. I personally don’t think that’s a huge issue – abstraction is what programming is all about – and there are less confusing old libraries to avoid in these newer languages.

Java is multi-platform whereas C# is (basically) Windows-only.

Oh, and if you just want to play around and first discover if programming is for you, take a look at [Processing](http://processing.org/). You can create an application to draw a circle with a single line of code, and it contains the editor and everything you need so there’s no faffing about.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [June 7, 2012, 4:42pm UTC](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300/60 "2012-06-07T16:42:09Z")

</div>

It also depends on whether you’re looking for a job as a programmer or just a job programming, and what other relevant skills you have. There are, for instance, physicists, most of whose job consists of writing computer programs. Most of these physicists aren’t nearly good enough at programming to be able to get a job as a programmer per se, but the relatively ugly, suboptimized programs they write are good enough for their purposes. Example: Programs written and used by scientists often don’t have proper IO routines, but take their “input” by editing the source code and recompiling. Which is fine if the programmer and people he’s directly trained are the only ones who ever use the program, but you’d never be able to sell it.

[Previous page](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300.md?page=2)

[Next page](https://boards.straightdope.com/t/extremeely-basic-programming-question/624300.md?page=4)
