1 / 56

Programming Fundamentals

Programming Fundamentals. Overview of Previous Lecture. Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments. Today’s Lecture. CPU and Memory Variable Types Integer Variables Character Variables Floating Point Types Input with cin. CPU and Memory.

robertalee
Download Presentation

Programming Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ProgrammingFundamentals

  2. Overview of Previous Lecture • Phases of C++ Environment • Program statement Vs Preprocessor directive • Whitespaces • Comments

  3. Today’s Lecture • CPU and Memory • Variable Types • Integer Variables • Character Variables • Floating Point Types • Input with cin

  4. CPU and Memory • A computer acts on data according to the instructions specified in a program (like our previous C++ program) and produces the output. • This computation is done by Central Processing Unit(CPU) of the computer. • A CPU of computer performs arithmetic operations such as addition, subtraction, multiplication and division and also performs logical operation on the data such as comparing two numbers.

  5. CPU and Memory • Memory of a computer is the area where data and programs are stored. • A computer memory is made up of cells that is capable of holding information in the form of binary digits 0 and 1 (bits). • This is the way information is stored in a memory just like we memorize numbers using 0 to 9.

  6. CPU and Memory • Collection of 8 bits is called a byte. • Normally the CPU does not accessed memory by individual bits. • Instead, it accesses data in a collection of bits, typically 8 bits, 16 bit, 32 bit or 64 bit. • The amount if bits on which it can operate simultaneously is known as the word length of the computer.

  7. CPU and Memory • When we say that Pentium 4 is a 32 bit machine, it means that it simultaneously operates on 32 bit of data.

  8. Program memory • When the program is compiled and linked, different parts of the program is organized in separate segments. • Our code will be in one segment. • Code means the instructions to be executed • That segment is called as code segment or program memory.

  9. Data segment and Stack memory • Then there is data on which the code operates, this data get stored in a segment called Data segment. • Stack memory is a part of programs memory which will be used in case of function calls

  10. Program Memory • The three types of memory specified above are owned by the corresponding process or program. • The linker will give information about where to store which data to the loader • Based on these information loader will load the corresponding executable(.exe in our case)in the memory.

  11. Heap memory • Heap memory is the memory which is owned by the process when a process requests for extra memory. • These requests are done by the malloc and calloc function calls.

  12. Program vs Process • A program needs memory when it gets executed and a program under execution is a process.

  13. CPU and Memory • Data is stored in the memory at physical memory locations. • These locations are known as the memory address.  • It is very difficult for humans to remember the memory addresses in numbers like 46735, so instead we name a memory address by a variable.

  14. Variables • Variables are the most fundamental part of any language. • A variable has a symbolic name and can be given a variety of values. • Variables are located in particular places in the computer’s memory. • When a variable is given a value, that value is actually placed in the memory space assigned to the variable.

  15. Variable types • Most popular languages use the same general variable types, such as integers, floating-point numbers, and characters

  16. Integer variables • Integer variables represent integer numbers like 1, 30,000, and –27. • Such numbers are used for counting discrete numbers of objects, like 11 pencils or 99 bottles of beer. • Unlike floating point numbers, integers have no fractional part; you can express the idea of fourusing integers, but not four and one-half.

  17. Contdd . . . . • The amount of memory occupied by the integer types is system dependent. • On a 32-bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory. • This allows an int to hold numbers in the range from –2,147,483,648 to 2,147,483,647.

  18. OUTPUT

  19. Compile time error

  20. Defining Integer Variables

  21. Here’s a program that defines and uses several variables of type int:

  22. Explanation • The statements int var1; int var2; define two integer variables, var1 and var2 • The keyword int signals the type of variable. • These statements, which are called declarations, must terminate with a semicolon, like other program statements.

  23. Contdd . . . . • You must declare a variable before using it. • However, you can place variable declarations anywhere in a program.

  24. Declarations and Definitions • A declaration introduces a variable’s name (such as var1) into a program and specifies its type (such as int). • However, if a declaration also sets aside memory for the variable, it is also called a definition.

  25. Variable Names • The names given to variables (and • other program features) are called identifiers. What are the rules for writing identifiers? • You can use upper- and lowercase letters, and the digits from 1 to 9. • You can also use the underscore (_). • The first character must be a letter or underscore.

  26. Contdd . . . . • Identifiers can be as long as you like, but most compilers will only recognize the first few hundred characters. • The compiler distinguishes between upper- and lowercase letters, so Var is not the same as var or VAR. • You can’t use a C++ keyword as a variable name.

  27. Keyword • A keywordis a predefined word with a special meaning. • int, class, if, and while are examples of keywords.

  28. Example program

  29. Assignment Statements • The statements assign values to the two variables. • The equal sign (=) causes the value on the right to be assigned to the variable on the left.

  30. Integer Constants • The number 20 is an integer constant. • Constants don’t change during the course of the program. • An integer constant consists of numerical digits. • There must be no decimal point in an integer constant, and it must lie within the range of integers.

  31. Example program

  32. Output Variations • The statement displays a string constant. The next statement displays the value of the variable var2. OUTPUT???

  33. The endl Manipulator • This causes a linefeed to be inserted into the stream, so that subsequent text is displayed on the next line. • It has the same effect as sending the ‘\n’ character. • Its an example of a manipulator.

  34. Manipulators • Manipulators are instructions to the output stream that modify the output in various ways; we’ll see more of them as we go along.

  35. Other Integer Types • There are several numerical integer types besides type int. • The two most common types are long and short. • Type char is an integer type as well.

  36. Character Variables • Type char stores integers that range in value from –128 to 127. • Variables of this type occupy only 1 byte (eight bits) of memory. • Character variables are sometimes used to store numbers that confine themselves to this limited range, but they are much more commonly used to store ASCII characters.

  37. As you may already know, the ASCII character set is a way of representing characters such as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers. • These numbers range from 0 to 127.

  38. Character Constants • Character constants use single quotation marks around a character, like ‘a’ and ‘b’. • Note: This differ from string constants, which use double quotation marks. • When the C++ compiler encounters such a character constant, it translates it into the corresponding ASCII code. The constant ‘a’ appearing in a program, for example, will be translated into 97

  39. Character variables can be assigned character constants as values.

  40. Initialization • Variables can be initialized at the same time they are defined. In this program two variables of type char—charvar1 and charvar2—are initialized to the character constants ‘A’ and ‘\t’.

  41. Escape Sequences

  42. Escape Sequences • Since the backslash, the single quotation marks, and the double quotation marks all have specialized meanings when used in constants, they must be represented by escape sequences when we want to display them as characters.

  43. Example • Here’s an example of a quoted phrase in a string constant:

  44. Floating Point Types • Floating-point variables represent numbers with a decimal place—like 3.1415927, 0.0000625,and –10.2. • They have both an integer part, to the left of the decimal point, and a fractional part, to the right.

  45. Kinds of floating-point variables • There are three kinds of floating-point variables in C++: type float, type double, and type long double.

  46. Type float • Type float stores numbers in the range of about 3.4x10–38 to 3.4x1038. • It occupies 4 bytes (32 bits) in memory.

  47. Here’s a sample interaction with the program:

  48. Type double and long double • The larger floating point types, double and long double, are similar to float except that they require more memory space and provide a wider range of values and more precision. • Type double requires 8 bytes of storage and handles numbers in the range from 1.7x10–308 to 1.7x10308. Type long double is compiler-dependent but is often the same as double.

More Related