UPDATED:

How To Make A Simple Calculator By C Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... This blog is under construction!!!!
Namecheap.com

Friday, December 23, 2016

Variables and Constants

Data must be stored continuously during the program sequence or data must be read in. When the user makes an input, this input must be stored somewhere in order to be able to evaluate it later. To store data, there are basically two possibilities: storage in memory or in a file. A database is ultimately only one file.

The main memory has two main problems: it is volatile, so the data is lost at the latest when you turn off the PC. And its capacity is usually too limited to accommodate larger amounts of data. The latter is increasingly relativized by falling prices for memory modules, but compared to hard disk sizes it is still "small". In addition, the requirements are always increasing with the calculation work. 

But the memory has an advantage: It is fast! Much faster than any hard drive. How fast depends on the type of memory and its construction. In principle, the closer to the processor, the faster the memory module should be. So L1 cache before L2 cache before RAM modules. This topic is not intended to deal with this issue.

To process data, you will always load it into the working memory. At least part of it, depending on the size of the data and the available memory. If data are also available after the end of the program, they are stored in a file or transferred to a database management system. I will treat the file access in a later post.

Now to the actual topic:  To store data in the working memory, one needs in the programming variables. 
Variables are certainly known to you from mathematics. Here they stand for placeholders. This is similar in programming. A variable reserves a certain amount of memory in memory. Thus, a variable can reserve about two bytes, in which data can then be stored.

Variables can be of different types (data types). Depending on the type, the reserved memory space is of a different size and different data can be stored. For example, variables of type int have a size of 2 or 4 bytes. How much accurately depends on the word length of the processor register and the compiler used. A register is an internal memory space of a processor. This is the fastest available memory for the CPU. For this purpose, the number of registers and thus the memory size is strongly limited. Say: There are only a few.

You can determine how much memory a variable actually needs on your system using the size of operator (more on this later). Depending on the type, however, not only a differently large memory space is reserved, but different values can also be stored. For example, int can only store integers (about -10, 12, or 20007, and you will see that characters such as "a" can be stored as a number); floating-type variables record floating-point numbers (about 1.3, 40.345313) can.

Constants must be distinguished from variables. A constant corresponds to a variable, but with a fixed, non-variable value. This means that a constant must be initialized (defined) at its declaration. So if you want a constant, you must assign a value to it. And it keeps this as long as it exists (which does not necessarily have to be the end of the program).

C compilers do not complain about this (no error, no warning) if you do not perform the initialization, but you can not begin with the constant at a later time. For example, if you try to assign a value to the constant a, you get the error message from GCC: Error: assignment of the read-only variable »a«. You only have the "opportunity" at the declaration.

A declaration is the announcement to the compiler that something (here: the variable) exists. If a is declared as int, the compiler knows: The variable a exists and has the type int. If a variable is assigned a value before its use, especially at the declaration (ie an initial value), it is referred to as an initialization.

If a value is assigned, it is called a definition. Initialization is a special form of definition. As far as the linguistic subtleties that had to be mentioned. As shown in Fig.

Declaration
Notification, e.g. Tell the compiler that a variable exists, which identifier (name) it carries, and what type it is.
Definition
value assignment, e.g. A previously declared variable is now assigned a value.

To declare a variable, you need to know two things:

How do I call my variable? Each variable requires a name (identifier), which can be used to address them. What is my variable to be? Depending on which values are to be stored, a different type must be selected. Sometimes it is also necessary to consider the storage space consumption. When you choose the name (identifier), you are bound by some rules. The name can only consist of alphanumeric characters (such as "a", "X" or "3", BUT NOT "§" or "$"), and the underscore "_". The name must start with a letter or the underline. There must therefore be no number at the beginning of the name.

By default, the identifier should not contain more than 32 characters. In addition, there are some other things to consider that make sense, but are not a duty. The identifier should be as meaningful as possible so that you can know later what the variable is good for. This is difficult for variables such as a, k1, j, and y3.

The identifier should therefore indicate the purpose of the variable. The purpose of the data storage is, of course, clear, but the question arises, which data should be stored and when and why the variable is used. However, try to keep the length of the identifier as short as possible. So as short as possible, as long as necessary.

So better count than This_is_my_newvariable as an identifier. Count variables are often also designated with the letters i, j, k, etc. Here, therefore, only i (like index) would be used.

Let's look at a variable declaration:

Int i;

You already know the integer type int. From this, the variable i exists after this line. The declaration is indicated by a semicolon; completed. In general it can be said:

Type identifier;

Several variables can be easily declared in one line. Example:

Int a, b, c;

As you can see, each identifier must be separated by commas. In the example above, the variables a, b, and c were declared int (each type int). If you also need one or more variables of a different type, this declaration must be placed in an extra line. Such as:

Int a, b, c;
Char z;

Here the variable z of type char was also declared. Because char is a type other than int, the declaration must be on its own line. In principle, you could also write the second declaration in the same line if it is after the semicolon. There is the semicolon.

IMPORTANT: In ANSI-C (C89 / C90), it is not allowed to simply place the variable declaration anywhere in the statement block. Declarations must ALWAYS be at the beginning of an instruction block. 
Example:

Int main ()
{
  Int a, b, c;
  Char z;

  / * Only now follow further instructions ... * /

  Return 0;
}


Attention: The next two programs contain an error!
These examples will not work with real (older) C compilers.

Example 1:

#include <stdio.h>

Int main ()
{
  / * ERROR: Program code BEFORE the variable declaration! * /
  Printf ("The program has started ...");

  Int a, b, c; / * The declaration is only made here ... /
  Char z;

  / * Only now follow further instructions ... * /

  Return 0;
}

Example 2:

Int main ()
{
  ; Int a, b, c; / * Pay attention to the semicolon at the beginning! * /
  Char z;

  / * Only now follow further instructions ... * /

  Return 0;
}

In Example 1, you will probably find the error easier than in Example 2. In the first example, there is an entire "meaningful" statement before the variable declaration. In the second example, the programmer is much more difficult. The only thing that is wrong here is a "lost" semicolon. It is before the variable declaration and is therefore considered by the compiler as an entire statement. Therefore, a mistake.

By the way: The line / * Only now follow further statements * / is marked with / * and * / as comment. Comments are ignored by the compiler. I'll go into later.

Now to practice: 

All upper "wrong" examples work with GCC - without error messages, without warnings. This is because GCC is also a C ++ compiler and also supports the newer C standards. As of C99, variable declarations are no longer required at the beginning of the statement block. In C ++ anyway not. Even with the command-line parameter -std = c89, the source code can be easily translated. GCC should still allow newer extensions as long as they do not conflict with the old C standard. The "error simulation" becomes perfect only if you also add the parameter -pedantic.

If you want to test an old, ancient C compiler, you can try Borland Turbo C. But please do not make any complaints to me, something should not work as desired (or Turbo C do not run). ;-) Turbo C comes in version 2 from the year 1989. Turbo C also knows no new standards guaranteed.

Although this is not the case, in this tutorial, variable declarations are always at the beginning of an instruction block. If you want to write pure C code, you should do that too.

A constant is declared by placing the const keyword before the type. Keywords are reserved identifiers. These are names that you can not use as identifiers. They may, for example, Do not declare a variable with the name "const". An example of a constant:

Const float PI = 3.14159;

Here the constant PI was declared and initialized (defined). Otherwise, the same applies to the declaration of variables.

No comments:

Post a Comment

Namecheap.com

Popular Posts