What typing system does BASIC use? - typing

I've noticed that nowhere I can find can give me a definitive answer to the question above. I first wondered about this when I noticed that you never had to state the type of variable in QBasic when declaring them, although you could add a suffix to the name of a variable to make sure it was of a particular type.
Also, as some dialects of BASIC are interpreted and others compiled, does this affect the typing system?

There are so many flavors of BASIC, some only historical and some still in use, that it's impossible to give one true answer.
Some of the old BASICs (the line numbered BASICs) had two data types: String, or Integer. The original BASIC that shipped with Apple-II computers was an "Integer BASIC." Later BASICs introduced Floating Point, which was often single precision FP. The BASIC that shipped with the TI-99/4a was an example of an early-80's floating point BASIC. "Way back when", you would make a string literal with quotes, and a string variable with a $ sigil following the identifier name. Variables that didn't have the $ sigil would usually default to the type of numeric variable that the given flavor of basic supported (Integer or Floating Point). GWBasic, for example, would default to floating point unless you specified the % sigil, which meant "Integer". TI Extended Basic didn't have an integer type, but the floating point numeric type had something like 15 significant digits, if I recall (floating point math errors not withstanding).
These early basics were essentially statically typed, though the distinction was far less useful than in more powerful languages. The choices for data types were few: String, Number (sometimes Int, sometimes FP), and sometimes with the ability to specify whether a number as Int or FP. Behind the scenes some even freely converted between ints and floating point as necessary. Often such behind the scenes conversions were not well documented.
But that was the state of affairs in the '80s, when everyone with a home computer was a hobbiest, and standards were loose. Every hardware manufacturer seemed to have their own take on how BASIC should work.
More modern BASICs are more powerful, and allow for tighter control over variable types (when needed).

Earlier dialects of BASIC were always statically typed. Numeric variables, string variables and arrays each required different syntax. Also length of names was often limited to just one symbol. Most often used syntax was just V for numeric, V$ for string and arrays were separately declared with DIM.
Since I didn't program in BASIC for good 15 years, I can't say for sure what is going on in modern dialects.

The enhanced version of BASIC used in MultiValue Database systems uses dynamic typing. This means that the compiler decides how to treat your variable based on the logic and context of the statements.
Anything in double quotes is a string and any numeric value not in double quotes is a number. For writing numeric data away in the form of doubles or floats there are various format expressions you can use to achieve this, which you apply to your variables.
Ultimately everything is saved at database level as an ASCII string. So the developer enforces the type at business logic level, as opposed to the database enforcing it.

Related

Why do we need to specify the data type of variable before assigning a value?

Let’s say we want to define a variable that stores the age of a certain individual;
int main(void) {
int age=5;
printf(“%d\n”,age);
}
Here, before assigning the variable, we specify the type of data it would store. Does we define it just to tell the compiler/interpreter how many bytes the value will occupy in memory. No other reason?
Variables usually change their value (that's why they are called variables). The initial value of a variable usually doesn't say much about how the variable is used, and what it's for.
If you want to implement an algorithm which calculates an average age, you might need your variable which holds the age to be non-integer, like float or double. You usually want to initialize it to 0, which is an integer, so the compiler can't come up with a correct suggestion for its type.
int main(void)
{
int age=5;
printf("%d\n",age);
// Now print the age in decades (units of 10 years)
printf("%d\n", age / 10); // prints 0 - incorrect answer
double correct_age = 5;
printf("%f\n", correct_age);
// Now print the age in decades (units of 10 years)
printf("%f\n", correct_age / 10); // prints 0.5 - correct answer
}
C++ has support for type inference: auto age = 5 declares age as an integer, while auto age = 5.0 declares it as double. This support was not deemed important when C was created, and it's still not as important in C as in C++, because in C++ types sometimes have long names.
Here, before assigning the variable, we specify the type of data it would store.
No, that's a mischaracterization. You present a declaration of variable age that happens to include an initialization. Initialization is not assignment. Although in many cases you could separate that into a declaration without initialization and a separate assignment, there are cases where that would not be valid. Overall, the declaration is the primary element in the code presented, not the initialization.
Does we define it just to tell the compiler/interpreter how many bytes the value will occupy in memory. No other reason?
We specify the data type because modern C requires variables to be declared before use, and the data type is a required part of the C syntax for variable declarations. At the language level, that's why, end of story.
As for what benefits that yields, yes, it does convey how much storage to reserve. More importantly, however, it relieves the language implementation from any need to store, track, or dynamically determine objects' types at runtime. The resulting bare-bytes object representations are, generally speaking, congruent with the underlying machine data model. This is memory efficient (which was an order of magnitude more important when C was designed than it is now), and CPU efficient.
C code is also amenable to type-based static analysis, which helps detect (some) bugs early, and it supports a wider variety of basic data types than dynamically typed languages such as Python do.
And speaking of Python, do note that even this poster child for dynamically-typed languages now supports data type annotations to provide a few of the advantages that static typing has to offer.
Consider this function:
int add(int a, int b)
{
return a+b;
}
This function can be typically compiled as an instruction to add two registers and an instruction to return (plus possibly some other instructions needed to implement proper routine interfaces on the platform).
If we did not know what types the arguments were, the compiler would have to generate code to add any possible types of arguments: One-byte integers, two-byte integers, four-byte integers, wider integers, floating-point numbers, strings (for which “adding” them might concatenate the strings), and other support types. Further, it would have to generate code to handle different types of arguments: One might be an integer while the other is a string.
Specifying the types of the arguments makes it possible to generate programs that are tailored to specific tasks with specific data types, and this yields programs that are much smaller and faster.
A variable's type determines the values that the variable can have and the operations that can be performed on it.
Programming languages like C, C++, and Java are statically-typed programming languages. The word "static" here means we need to determine the types at compile time. One way to do that is to just specify the type directly in the code. It makes it easier to implement compilers and for developers to read the code. Another way I know is type inference. So instead of specifying the types in the code, we let the compiler infers the types based on the context. The any type in C++ does exactly that.
Different from statically typed languages, dynamically typed languages like Python and Javascript determine the types at runtime. The benefit is that the code is usually more concise and you can do crazy things like conditionally returning different types of data in a function. The downside is that it makes code maintenance significantly harder and easier to produce bugs. That's why we now have Typescript and type annotations in Python to mitigate these problems.

How would I modify the C compiler to work with a different base system other than decimal (heximal, dozenal, etc)?

What functions and features need to be changed in the C compiler and standard library, to be able to write and compile code in a separate base system? Obviously, there's trouble that comes from using base systems greater than decimal, where you must represent digits using non-numeric symbols (assume for now they are simply the capital letters A-Z). The underlying features of integer and floating point math do not need to be changed, only the portions when binary components are converted into decimal.
Ultimately, I'd like to know the feasibility of such a task and the edge cases of binary to decimal translation, so a detailed explanation is best. If you could also indicate the modifications necessary for a system with less digits than decimal (e.g., heximal) in a separate category from extra modifications that are also necessary for a system with more digits (e.g., dozenal), it would be appreciated.
Right now I believe that the parser which turns constant literals into binary numbers, as well as the underlying code for printf and scanf functions needs to be modified slightly. I'm curious if there's other details in an arbitrary portion of the standard library that would also need modification.

What does the following mean in context of C programming language?

From Modern C by Jens Gustedt,
Representations of values on a computer can vary “culturally” from architecture to architecture or are determined by the type the programmer gave to the value. Therefore, we should try to reason primarily about values and not about representations if we want to write portable code.
If you already have some experience in C and in manipulating bytes and bits, you will need to make an effort to actively “forget” your knowledge for most of this section. Thinking about concrete representations of values on your computer will inhibit you more
than it helps.
Takeaway - C programs primarily reason about values and not about their representation.
Question 1: What kind of 'representations' of values, is author talking about? Could I be given an example, where this 'representation' varies from architecture to architecture and also an example of how representations of values are determined by type programmer gave to value?
Question 2: What's the purpose of specifying a data type in C language, I mean that's the rule of the language but I have heard that's how a compiler knows how much memory to allocate to an object? Is that the only use, albeit crucial? I've heard there isn't a need to specify a data type in Python.
What kind of 'representations' of values, is author talking about?
https://en.wikipedia.org/wiki/Two%27s_complement vs https://en.wikipedia.org/wiki/Ones%27_complement vs https://en.wikipedia.org/wiki/Offset_binary. Generally https://en.wikipedia.org/wiki/Signed_number_representations.
But also the vast space of floating point number formats https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754:_floating_point_in_modern_computers - IEEE 745, minifloat, bfloat16, etc. etc. .
Could I be given an example, where this 'representation' varies from architecture to architecture
Your PC uses twos complement vs https://superuser.com/questions/1137182/is-there-any-existing-cpu-implementation-which-uses-ones-complement .
Ach - but of course, most notably https://en.wikipedia.org/wiki/Endianness .
also an example of how representations of values are determined by type programmer gave to value?
(float)1 is represented in IEEE 745 as 0b00111111100000000000000000000000 https://www.h-schmidt.net/FloatConverter/IEEE754.html .
(unsigned)1 with 32-bit int is represented as 0b00.....0001.
What's the purpose of specifying a data type in C language,
Use computer resources efficiently. There is no point in reserving 2 gigabytes to store 8-bits of data. Type determines the range of values that can be "contained" in a variable. You communicate that "upper/lower range" of allowed values to the compiler, and the compiler generates nice and fast code. (There is also ADA where you literally specify the range of types, like type Day_type is range 1 .. 31;).
Programs are written using https://en.wikipedia.org/wiki/Harvard_architecture . Variables at block scope are put on stack https://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Hardware_stack . The idea is that you have to know in advance how many bytes to reserve from the stack. Types communicate just that.
have heard that's how a compiler knows how much memory to allocate to an object?
Type communicates to the compiler how much memory to allocate for an object, but it also communicates the range of values, the representation (float vs _Float32 might be similar, but be different). Overflowing addition of two int's is invalid, overflowing addition of two unsigned is fine and wraps around. There are differences.
Is that the only use, albeit crucial?
The most important use of types is to clearly communicate the purpose of your code to other developers.
char character;
int numerical_variable;
uint_least8_t variable_with_8_bits_that_is_optimized_for_size;
uint_fast8_t variable_with_8_bits_that_is_optimized_for_speed;
wchar_t wide_character;
FILE *this_is_a_file;
I've heard there isn't a need to specify a data type in Python.
This is literally the difference between statically typed programming languages and dynamically typed programming languages. https://en.wikipedia.org/wiki/Type_system#Type_checking

Good Practice to use integer array to store characters in C

I am currently learning to program in C and am referring the book by Kernighan and Ritchie. In order to better grasp arrays especially when inputting characters I have searched the internet and noticed that a lot of solutions use an integer array instead of a character array during input. I know that characters are in a sense integers and have lower precedence, so my question is very simple, are there any major cons to declaring an integer array and using it to store characters? In addition, is it good practice to do this as the title suggests?
No, it would be pretty bad practice, in my opinion, since:
Integers are not characters, in general. That is "an array of integer" doesn't communicate as well to the reader what is going on, as "an array of character" does.
You can't use the standard library's string functions, which is sad since an array of characters can often be treated as a string.
It wastes memory.
It would be interesting if you had posted some code which you feel is representable of doing this. I don't think it happens in K&R.
C has various types that can store integers. (Types don't have precedence - perhaps you meant size.) Sometimes, an integer of one byte is enough to store one character of a specific text (if it's known ahead of time that the text will contain a limited range of characters).
In modern systems and applications, support for more than 256 characters is expected or required, so wider character types are often employed. For these types, there are usually library functions that perform operations comparable to those of the char processing functions.
The int type, however, is not one of these types and is generally not intended to be used like that.

Why use constants in programming? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I've just been going back over a bit of C studying using Ivor Horton's Beginning C book. I got to the bit about declaring constants which seems to get mixed up with variables in the same sentence.
Just to clarify, what is the difference in specifying constants and variables in C, and really, when do you need to use a constant instead of a variable? I know folks say to use a constant when the information doesn't change during program execution but I can't really think of a time when a variable couldn't be used instead.
A variable, as you can guess from the name, varies over time. If it doesn't vary, there is "no loss". When you tell the compiler that the value will not change, the compiler can do a whole bunch of optimizations, like directly inlining the value and never allocating any space for the constant on the stack.
However, you cannot always count on your compiler to be smart enough to be able to correctly determine if a value will change once set. In any situation where the compiler is incapable of determining this with 100% confidence, the compiler will err on the side of safety and assume it could change. This can result in various performance impacts like avoiding inlining, not optimizing certain loops, creating object code that is not as parallelism-friendly.
Because of this, and since readability is also important, you should strive to use an explicit constant whenever possible and leave variables for things that can actually change.
As to why constants are used instead of literal numbers:
1) It makes code more readable. Everyone knows what 3.14 is (hopefully), not everyone knows that 3.07 is the income tax rate in PA. This is an example of domain-specific knowledge, and not everyone maintaining your code in the future (e.g., a tax software) will know it.
2) It saves work when you make a change. Going and changing every 3.07 to 3.18 if the tax rate changes in the future will be annoying. You always want to minimize changes and ideally make a single change. The more concurrent changes you have to make, the higher the risk that you will forget something, leading to errors.
3) You avoid risky errors. Imagine that there were two states with an income tax rate of 3.05, and then one of them changes to 3.18 while the other stays at 3.07. By just going and replacing, you could end up with severe errors. Of course, many integer or string constant values are more common than "3.07". For example, the number 7 could represent the number of days in the week, and something else. In large programs, it is very difficult to determine what each literal value means.
4) In the case of string text, it is common to use symbolic names for strings to allow the string pools to change quickly in the case of supporting multiple languages.
Note that in addition to variables and "constant variables", there are also some languages with enumerations. An enumeration actually allows you to defines a type for a small group of constants (e.g., return values), so using them will provide type safety.
For example, if I have an enumeration for the days of the weeks and for the months, I will be warned if I assign a month into a day. If I just use integer constants, there will be no warning when day 3 is assigned to month 3. You always want type safety, and it improves readability. Enumerations are also better for defining order. Imagine that you have constants for the days of the week, and now you want your week to start on Monday rather than Sunday.
Using constants is more a way of defensive programming, to protect yourself from yourself, from accidentally changing the value somewhere in the code when you're coding at 2 a.m. or before having drunk your coffee.
Technically, yes, you can use a variable instead.
Constants have several advantages over variables.
Constants provide some level of guarantee that code can't change the underlying value. This is not of much importance for a smaller project, but matters on a larger project with multiple components written by multiple authors.
Constants also provide a strong hint to the compiler for optimization. Since the compiler knows the value can't change, it doesn't need to load the value from memory and can optimize the code to work for only the exact value of the constant (for instance, the compiler can use shifts for multiplication/division if the const is a power of 2.)
Constants are also inherently static - you can declare the constant and its value in a header file, and not have to worry about defining it exactly one place.
For one, performance optimization.
More importantly, this is for human readers. Remember that your target audience is not only the compiler. It helps to express yourself in code, and avoid comments.
const int spaceTimeDimensions = 4;
if(gpsSattelitesAvailable >= spaceTimeDimensions)
Good();
For a low-level language like C, constants allow for several compilation optimizations.
For a programming language in general, you don't really need them. High level dynamic languages such as Ruby and JavaScript doesn't have them (or at least not in a true constant sense). Variables are used instead, just like you suggested.
Constant is when you just want to share the memory, and it doesn't change.
The const keyword is often used for function parameters, particularly pointers, to suggest the memory the pointer points to will not be modified by the function. Look at the decleration for strcpy for instance:
char *strcpy(char *dest, const char *src);
Otherwise, for example, an declaration such as
const int my_magic_no = 54321;
might be preferred over:
#define MY_MAGIC_NO 54321
for type safety reasons.
It's a really easy way to trap a certain class of errors. If you declare a variable const, and accidentally try to modify it, the compiler will call you on it.
Constants are very necessary in regards to declaration and intialization of variable for any purpose such as at the starting of the loop, to check the condition within the if -else statement, etc.
For more reference, feel free to read either of the following articles:
Constants in C Programming Language
Variables in C Language
Not using const can mean someone in a team project could declare where int FORTY_TWO = 42 and make it equal FORTY_TWO = 41 somewhere else by another team member. Therefore the end of the world happens and you also loose the answer to life. with const although none of this will ever happen. Plus const is stored elsewhere in memory, when compared to the storage of normal variables, and is more efficient.

Resources