The first thing that comes to our mind when we think of a programming language is a computer program. But, there are many other types of programming languages as well which do not use computers. Some of these programming languages are the programming languages used in calculators, programming languages used for video games, and programming languages used for software applications.
In this article, we will discuss about the programming languages used for software applications. There are many different types of programming languages that can be used for software applications.
Programming Languages: The Structure of a Program
Programs can be written using any of the programming languages. All Programming languages have a particular syntax and structure. No matter what type of programming language you use, the structure is going to remain the same.
We will discuss here about the structure or syntax of a program which contains variables, constants, operators, functions and loops.
A program is basically a sequence of instructions given to a processor/computer. Each instruction consists of one or more variables, constants, operators and functions.
Programming Languages: The Structure of a Program
Here is an example:
int i = 10; // Declare and initialize a variable named i with value 10 int j = 20; // Declare and initialize a variable named j with value 20 int sum = i + j; // Calculate the sum of i and j and store it in sum printf(“%d %d
”, i, j); // print the values stored in i and j on screen (i.e display) }
Variable names follow certain conventions. They must begin with a letter, but can contain letters, numbers and underscore characters. Variable names cannot begin with a digit. A variable name can be of any length, but cannot exceed 255 characters. The following are examples of valid variable names:
i for loop count my_name qty_sold qty_sold_in_may qty_available qty_received qty_available_for_sale xyz abc123
UNDERSTANDING VARIABLE NAMES AND VALUES:
In the above example, integer variables i and j have been declared and initialized with values 10 and 20 respectively. Variable names can have any length, but they must begin with a letter. The underscore character (_) cannot be used as a first character in a variable name. However, underscores may be used in a variable name anywhere else in the name. Variable names follow the naming rules for C identifiers described in “Identifiers” on page 7-4. A variable’s value can change during execution of a program. In the above example, the value of i is changed from 10 to 20 after execution reaches the declaration statement for j. This is because each time an integer variable is declared, it is given automatic storage class data type int by default, which means its value cannot be changed except by using an assignment statement or a function call that returns a value. Variables declared with a data type other than integer must be initialized before they are used in expressions or statements. If no initial value is given to a variable when it is declared, it is automatically initialized to 0 (zero). When a variable is initialized with an initial value, the actual value stored in memory is never 0 (zero). If you want to initialize a variable with 0 (zero), you must explicitly assign 0 (zero) to it. For example: int i = 0; When you use variables in your programs, remember that they do not store data themselves; they only contain references to data stored somewhere else on disk or in memory. For this reason, you should always assign values to variables before you use them in expressions or statements. Otherwise, the value stored in the variable may be garbage or may point to uninitialized memory space. For example: float f = 1; x = f; // x gets garbage value of f because f had not been assigned a value at this time y = f+1; // y gets garbage value of f because f had not been assigned a value at this time When you declare variables at module level (outside any function), their values persist throughout the execution of your program and between calls to your program until you specifically change them (or until they go out of scope, as described below). If you declare multiple variables at module level with the same name, these variables refer to one another through aliases – they all point to the exact same value – and their values will be automatically changed when any one of them is changed. For example: int i = 1; int j = 2; printf(“%d %d
”, i++ , j++ ); // prints out 3 2 because now both variables refer to each other printf(“%d %d
”, i++ , ++j ); // prints out 4 3 because now both variables refer to each other printf(“%s %s %d
”, “i” , “j” ); // prints out 2 2 printf(“%s %s %d
”, “j” , “i” ); // prints out 2 2 When you declare variables at function level or within a function body, their values persist only within the body of the function until it returns control back to where it was called from. After that, their values are lost unless they are declared again at module level or within another function body. Therefore, it is usually not necessary to declare variables at function level or within function bodies unless they are needed within the body of the function for temporary purposes. An exception to this rule is when you want something outside of your function body to access those variables without having to pass references to them as parameters or return values. Because functions are discussed in more detail later in this chapter, see page 9-7 for more information about declaring variables within functions or within other functions bodies. Note that if you declare multiple variables at function level or within other function bodies with the same name, they still refer to one another through aliases but these aliases are lost after the function returns control back to where it was called from. So if you need these aliases after your function returns control back to where it was called from then you must pass references to them as parameters or return values so that something outside your function body can access them without having to create new aliases for them. Function parameters are discussed later in this chapter on page 9-10. Return values are discussed later in this chapter on page 9-13. Read more about aliasing here: http://www.boostpro-online.com/blog/c-aliasing-and-overriding-and-how-to-avoid-them/