Chapter IV Data Types

Posted in: Chapter IV Data Types by tommyphen on December 16, 2016

Data types in any of the language means that what are the various type of data the variables can have in that particular language. Information is stored in a computer memory with different data types. Whenever a variable is declared it becomes necessary to define data type that what will be the type of data that variable can hold.

pic 6

There are two data types available in C++:

  1. Primary Data Type character, integer, floating point, boolean, double floating point, void, wide character.
  2. Additional Data Types typedef, enumerated.

Character Data Types

Data Type (Keywords) Description Size Typical Range
char Any single character. It may include a letter, a digit, a punctuation mark, or a space. 1 byte -128 to 127 or 0 to 255
signed char Signed character. 1 byte -128 to 127
unsigned char Unsigned character. 1 byte 0 to 255
wchar_t Wide character. 2 or 4 bytes 1 wide character

 

Integer Data Types

Data Type (Keywords) Description Size Typical Range
int Integer. 4 bytes -2147483648 to 2147483647
signed int Signed integer. Values may be negative, positive, or zero. 4 bytes -2147483648 to 2147483647
unsigned int Unsigned integer. Values are always positive or zero. Never negative. 4 bytes 0 to 4294967295
short Short integer. 2  bytes -32768 to 32767
signed short Signed short integer. Values may be negative, positive, or zero. 2 bytes -32768 to 32767
unsigned short Unsigned short integer. Values are always positive or zero. Never negative. 2 bytes 0 to 65535
long Long integer. 4 bytes -2147483648 to 2147483647
signed long Signed long integer. Values may be negative, positive, or zero. 4 bytes -2147483648 to 2147483647
unsigned long Unsigned long integer. Values are always positive or zero. Never negative. 4 bytes 0 to 4294967295

Floating-point Data Types

Data Type (Keywords) Description Size Typical Range
float Floating point number. There is no fixed number of digits before or after the decimal point. 4 bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. More accurate compared to float. 8 bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits)

 

Boolean Data Type

Data Type (Keywords) Description Size Typical Range
bool Boolean value. It can only take one of two values: true or false. 1 byte true or false

Note: Variables sizes might be different in your pc from those shown in the above table, depending on the compiler you are using.

Below example will produce correct size of various data type, on your computer.

Example:

#include <iostream>using namespace std;

 

int main()

{

cout << “Size of char is ” << sizeof(char) << endl;

cout << “Size of int is ” << sizeof(int) << endl;

cout << “Size of float is ” << sizeof(float) << endl;

cout << “Size of short int is ” << sizeof(short int) << endl;

cout << “Size of long int is ” << sizeof(long int) << endl;

cout << “Size of double is ” << sizeof(double) << endl;

cout << “Size of wchar_t is ” << sizeof(wchar_t) << endl;

return 0;

}

 

 

 

Program Output:

Size of char is 1

Size of int is 4

Size of float is 4

Size of short int is 2

Size of long int is 4

Size of double is 8

Size of wchar_t is 4

 

Enum Data Type

This is an user defined data type having finite set of enumeration constants. The keyword ‘enum‘ is used to create enumerated data type.  Syntax:  enum enum-name {list of names}var-list;enum mca(software, internet, seo);

 

Typedef

It is used to create new data type. But it is commonly used to change existing data type with another name.

Syntax:  typedef [data_type]

synonym;or

typedef [data_type] new_data_type;

Example:

typedef int integer;

integer rollno;

Tags:

Chapter III Names, Binding, and Scopes

Posted in: Chapter III Names, Binding, Scopes by tommyphen on December 16, 2016

A. Naming Convention

There are various things that have to be paid attention to when giving name for a variable, class, and such in C++, such as the following:

  • Naming should be unique and a variable should not have the same name with other variables in the same scope.
  • Reserved words and keywords may not be used for a variable name.
  • Naming can only consist of alphabets (A-Z, a-z), numbers (-0-9), dollar symbol ($), and underscore (_).
  • The first character of the name should be either an alphabet or an underscore.
  • Names in C++ are case-sensitive, so, for example, the variable index and Index are different from each other. This may cause a problem in both readibility and writability as it considers similar names as different.
  • While there are no limit in character length, during execution the program will only recognize the first 32 characters of the name.
  • It is advised to use relevant, short, and clear names for variables, classes, etc., so when the coder or other people read the codes again, they would not be confused of what are the use of several variables and the variables would be easier to use.

 

Pay attention to the following snippet of codes:

int main() {

int 2nama, nim, _alamat, for;

cin>> 2nama >> NIM >> _alamat >> for;

}

There are a few mistakes that can be seen from the codes above:

  • During the declaration (line 1), the variable 2nama violated the rule that a name can only be started with alphabets or underscore, not numbers. The variable for can’t be used too, as for is already a keyword used for looping function.
  • During the value input of the variables (line 2), same as line 1, variables 2nama and for violate the rule. In addition, athe variable NIM would cause an error too as the compiler does not recognize that name, because C++ is a casesensitive language, so it considers the variable nim declared on the first line different from the variable NIM used on the second line.

 

B. Binding

Binding is the association between an entity and an attribute, such as the association of a variable and its attributes like name, address, data type, value, lifetime, and scope. Binding time is the time at which binding takes place.

The examples of binding time: o

Language design time o Language implementation time o Program translation / compile time o Link edit time o Load time

  • Run time

For a variable to be able to be referenced in a program, first it need to be bound to a type, which is called type binding. It can be differentiated into two kinds of type binding:

  • Static type binding, binding that took place during compile time. The data type that the variable is bound to is decided either by directly specifying a data type to the variable name (explicit) or by default associating a variable with a type during compile time (implicit). In C++, Explicit static type binding is usually used.
  • Dynamic type binding, where a variable is bound to a type when it is assigned a value in an assignment statement (at run time, take the type of the value on the right-hand side). By using dynamic type binding, it’s more flexible compared to static type binding, but error detection is harder and prone to typing errors. In C++, dynamic type binding can be used with the help of template by using generics.

C. Scope

The scope of a variable is the range of statements over which it is visible. The scope rules of a language determine how references to names are associated with variables. In C++, the scope of a variable depends on the block where the variable is declared or called. There are two kinds of scope, static and dynamic scope.

Depending on the scope it is in, a variable can be divided into two kinds:

  • Local Variable is variable declared in a block of program and can only be called in said block.
  • Global Variable is variable declared outside of all block of programs and can be called from anywhere after its declaration. In the case when a global variable and a local variable share a name, the local variable will be prioritized if the variable name is called inside the same block of that local variable.

 –  Static Scope

Static scope is based on program text and connect a name reference to a variable by finding the declaration. The search process of the declaration starts locally, then in increasingly larger enclosing scopes, until one is found for given name.

 –  Dynamic Scope

Dynamic scope is based on calling sequences of program units, not their textual layout. References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.

Tags:

A.Definition

The word syntax comes from Ancient Greek: σύνταξις “coordination”, which consists of σύν syn, “together,” and τάξις táxis, “an ordering”.

In computer science, the syntax of a computer language is the set of grammatical rules that defines the combinations of words and symbols that are considered to be a correctly structured document or fragment in that language.

Semantic focuses on the relationship between signifiers—like words, phrases, signs, and symbols—and what they stand for, their denotation. In computer science, semantics is the meaning of the expressions,  statements, and program units

B.C++ Syntax

The syntax of textual programming languages is usually defined using a combination of regular expressions (for lexical structure) and Backus–Naur Form (for grammatical structure) to inductively specify syntactic categories (nonterminals) and terminal symbols. Syntactic categories are defined by rules called productions, which specify the values that belong to a particular syntactic category. Terminal symbols are the concrete characters or strings of characters (for example keywords such as define, if, main, or void) from which syntactically valid programs are constructed.

 

The following are the examples of basic C++ keywords syntax:

  1. #include, which is used to insert the library of functions we are going to use in our program. For example, #include <iostream>, which includes the standard input and output codes.
  2. int main(), which is called first by the program after the initialization. A statement return 0; should be written at the end of the codes inside int main().
  3. cout, which is used to print output on the screen.
  4. cin, which is used to input data by user and store it as a variable.
  5. int, char, etc., which are used to declare a variable, according to the data types. (int for integer, char for a character, string for string of characters, etc.)
  6. if, which is used in selection and will execute the codes if the condition is true.
  7. while, for, are used in looping/reiteration.

There are also various points that should be paid attention to: a. The semicolon (;) is used to separate each statements.

  1. The curly brackets ({ }) must be used on several functions such as if, for, and while if there are more than one statements that will be executed in the function.
  2. C++ is a case-sensitive programming language, so if you use capitalization incorrectly, the compiler will give an error message.
  3. To set the value of char and string data types or to print text on a program, double quotation mark (“ “) is necessary.
  4. For comments, we use double slash (//) to mark the following text in a line as comment or type them between /* and */ to mark several lines of code or text as comments.

Here are an example of a program with correct syntax in C++:

#include <iostream> using

namespace std; int main()

{

int x,y,z;

cout<<“Masukkan nilai x dan y : “;

cin>> x >> y;  fflush(stdin);

 

z=x+y;

 

if (z>50) cout<<“Jumlah dari x dan y lebih dari 50”;

else cout<<“Jumlah dari x dan y kurang dari 50”;

 

getchar();

return 0;

}

 

C.Semantics

The requirements for a methodology and notation for semantics are as follows:

  • Programmers need to know what statements mean
  • Compiler writers must know exactly what language constructs do
  • Correctness proofs would be possible
  • Compiler generators would be possible
  • Designers could detect ambiguities and inconsistencies

 

There are some kinds of semantics:

  • Operational semantics, which describes the meaning of a program directly by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement.
  • Denotational semantics, where each phrase in the language is interpreted as a denotation, i.e. a conceptual meaning that can be thought of abstractly.
  • Axiomatic semantics,  where axioms or inference rules are defined for each statement type in the language (to allow transformations of logic expressions into more formal logic expressions).
Tags:

Chapter 1 Introduction

Posted in: Chapter I Introduction by tommyphen on December 16, 2016

A.Reasons for Studying Concepts of Programming Languages

➢ Increased ability to express ideas.

  • It is believed that the depth at which we think is influenced by the expressive power of the language in which we communicate our thoughts. It is difficult for people to conceptualize structures they can’t describe, verbally or in writing.
  • Language in which they develop software places limits on the kinds of control structures, data structures, and abstractions they can use.

➢ Improved background for choosing appropriate languages.

  • Many programmers, when given a choice of languages for a new project, continue to use the language with which they are most familiar, even if it is poorly suited to new projects.
  • If these programmers were familiar with other languages available, they would be in a better position to make informed language choices.

➢ Greater ability to learn new languages.

  • Programming languages are still in a state of continuous evolution, which means continuous learning is essential.
  • Programmers who understand the concept of OO programming will have easier time learning Java.
  • Once a thorough understanding of the fundamental concepts of languages is acquired, it becomes easier to see how concepts are incorporated into the design of the language being learned.

➢ Understand significance of implementation.

  • Understanding of implementation issues leads to an understanding of why languages are designed the way they are.
  • This in turn leads to the ability to use a language more intelligently, as it was designed to be used.

➢ Ability to design new languages.

  • The more languages you gain knowledge of, the better understanding of programming languages concepts you understand.

➢ Overall advancement of computing.

  • Many believe that ALGOL 60 was a better language than Fortran; however, Fortran was most widely used. It is attributed to the fact that the programmers and managers didn’t understand the conceptual design of ALGOL 60.

B. Programming Domains

  • Scientific applications
    • In the early 40s computers were invented for scientific applications.
    • The applications require large number of floating point computations.
    • Fortran was the first language developed scientific applications.
    • ALGOL 60 was intended for the same use.

 

  • Business applications
    • The first successful language for business was COBOL.
    • Produce reports, use decimal arithmetic numbers and characters.
    • The arrival of PCs started new ways for businesses to use computers.
    • Spreadsheets and database systems were developed for business.

 

  • Artificial intelligence
    • Symbolic rather than numeric computations are manipulated.
    • Symbolic computation is more suitably done with linked lists than arrays.
    • LISP was the first widely used AI programming language.

 

  • Systems programming
    • The operation system and all the programming supports tools are collectively known as its system software.
    • Need efficiency because of continuous use.

 

  • Scripting languages
    • Put a list of commands, called a script, in a file to be executed.
    • PHP is a scripting language used on Web server systems. Its code is embedded in HTML documents.  The code is interpreted on the server before the document is sent to a requesting browser.

C. Language Evaluation Criteria

  • Readability
    • Language constructs were designed more from the point of view of the computer than the users.
    • Because ease of maintenance is determined in large part by the readability of programs, readability became an important measure of the quality of programs and programming languages. The result is a crossover from focus on machine orientation to focus on human orientation.
    • The most important criteria “ease of use”
    • Orthogonality
      • Makes the language easy to learn and read.
      • Meaning is context independent. Pointers should be able to point to any type of variable or data structure.  The lack of orthogonality leads to exceptions to the rules of the language.
      • A relatively small set of primitive constructs can be combined in a relatively small number of ways to build the control and data structures of the language.
      • Every possible combination is legal and meaningful.
      • The more orthogonal the design of a language, the fewer exceptions the language rules require.
      • The most orthogonal programming language is ALGOL 68. Every language construct has a type, and there are no restrictions on those types.
      • This form of orthogonality leads to unnecessary complexity.

 

  • Writability
    • It is a measure of how easily a language can be used to create programs for a chosen problem domain.
    • Most of the language characteristics that affect readability also affect writability.
  • Simplicity and orthogonality
    • A smaller number of primitive constructs and a consistent set of rules for combining them is much better than simply having a large number of primitives.
  • Support for abstraction
    • Abstraction means the ability to define and then use complicated structures or operations in ways that allow many of the details to be ignored.
    • A process abstraction is the use of a subprogram to implement a sort algorithm that is required several times in a program instead of replicating it in all places where it is needed.
  • Expressivity
    • It means that a language has relatively convenient, rather than cumbersome, ways of specifying computations.
  • Reliability
    • A program is said to be reliable if it performs to its specifications under all conditions.
  • Type checking: is simply testing for type errors in a given program, either by the compiler or during program execution.
    • The earlier errors are detected, the less expensive it is to make the required repairs. Java requires type checking of nearly all variables and expressions at compile time.
  • Exception handling: the ability to intercept run-time errors, take corrective measures, and then continue is a great aid to reliability.
  • Aliasing: it is having two or more distinct referencing methods, or names, for the same memory cell.
    • It is now widely accepted that aliasing is a dangerous feature in a language.
  • Readability and writability: Both readability and writability influence reliability.
  • Cost
  • Categories
    • Training programmers to use language
    • Writing programs “Writability”
    • Compiling programs
    • Executing programs
    • Language implementation system “Free compilers is the key, success of Java”
    • Maintaining programs: Maintenance costs can be as high as two to four times as much as development costs.
    • Portability “standardization of the language”
    • Generality (the applicability to a wide range of application

 D.Influences on Language Design

Computer architecture: Von Neumann

  • We use imperative languages, at least in part, because we use von Neumann machines
    • Data and programs stored in same memory
    • Memory is separate from CPU
    • Instructions and data are piped from memory to CPU
    • Results of operations in the CPU must be moved back to memory
    • Basis for imperative languages
      • Variables model memory cells
      • Assignment statements model piping ✓Iteration is efficient

pic 1

  • Program Design Methodologies
    • New software development methodologies (e.g., object-oriented software development) led to new programming paradigms and by extension, new programming languages
  • Programming methodologies
    • 1950s and early 1960s: Simple applications; worry about machine efficiency
    • Late 1960s: People efficiency became important; readability, better control structures
      • Structured programming
      • Top-down design and step-wise refinement
    • Late 1970s: Process-oriented to data-oriented
      • data abstraction
    • Middle 1980s: Object-oriented programming
      • Data abstraction + inheritance + polymorphism

 

E. Language Categories

  • Imperative
    • Central features are variables, assignment statements, and iteration
    • C, Pascal
  • Functional
    • Main means of making computations is by applying functions to given parameters
    • LISP, Scheme
  • Logic
    • Rule-based
    • Rules are specified in no special order
    • Prolog
  • Object-oriented
    • Encapsulate data objects with processing
    • Inheritance and dynamic type binding
    • Grew out of imperative languages
    • C++, Java

 

F. Implementation Methods

  • Compilation
    • Programs are translated into machine language; includes JIT systems
    • Use: Large commercial applications
  • Pure Interpretation
    • Programs are interpreted by another program known as an interpreter
    • Use: Small programs or when efficiency is not an issue

➢Hybrid Implementation Systems

  • A compromise between compilers and pure interpreters
  • Use: Small and medium systems when efficiency is not the first concern

 

Layered View of Computer

pic 2

Compilation

  • Translate high-level program (source language) into machine code (machine language)
  • Slow translation, fast execution
  • Compilation process has several phases:
    • lexical analysis: converts characters in the source program into lexical units
    • syntax analysis: transforms lexical units into parse trees which represent the syntactic structure of program
    • Semantics analysis: generate intermediate code
    • code generation: machine code is generated

 

 

The Compilation Process

pic 3

➢Pure Interpretation

  • No translation
  • Easier implementation of programs (run-time errors can easily and immediately be displayed)
  • Slower execution (10 to 100 times slower than compiled programs)
  • Often requires more space
  • Now rare for traditional high-level languages
  • Significant comeback with some Web scripting languages (e.g., JavaScript, PHP)

 

Pure Interpretation Process

pic 4

➢Hybrid Implementation Systems

  • A compromise between compilers and pure interpreters
  • A high-level language program is translated to an intermediate language that allows easy interpretation
  • Faster than pure interpretation
  • Examples
    • Perl programs are partially compiled to detect errors before interpretation
    • Initial implementations of Java were hybrid; the intermediate form, byte code, provides portability to any machine that has a byte code interpreter and a run-time system (together, these are called Java Virtual Machine)

Hybrid Implementation Process

pic 5

 

Tags:
« Older Posts