Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Structure of a C program
Data types and variables
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
CPS120: Introduction to Computer Science
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Choice of Programming Language 1 All the information is stored in the memory must be in the form of binary. As such any program to be executed by the CPU.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Numbers in ‘C’ Two general categories: Integers Floats
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Lesson #2 Introduction to C.
Basics (Variables, Assignments, I/O)
Lesson #2 Introduction to C.
ECE Application Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
TMF1414 Introduction to Programming
Data Types, Identifiers, and Expressions
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
By: Syed Shahrukh Haider
CMSC 104, Section 4 Richard Chang
Basic Elements of C++ Chapter 2.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Basics (Variables, Assignments, I/O)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
INPUT & OUTPUT scanf & printf.
2.1 Parts of a C++ Program.
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
elementary programming
Conversion Check your class notes and given examples at class.
Chapter 2: Introduction to C++.
Lesson #2 Introduction to C.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Unit 3: Variables in Java
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
DATA TYPES There are four basic data types associated with variables:
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change values. All variables must be declared at the top of the program. There are three basic types of variables in C: int: for integer (whole numbers). double (or float): for real (floating point numbers). char: for characters.

Declaring Variables in ‘C’ All variables in ‘C’ must be declared. Compiler should know The variable name Type of the variable Why? In order to allocate enough memory to it , before the variable can be used. What are data types? char int, long float, double, long double void

How to declare? /*declare variable of type character.*/  This Denotes a comment in C char a_character; char letter; /*declare variable of type integer.*/ int an_integer; int number; /*declare variable of type float.*/ float floating_point_number; float average; The following is also valid: int age, number, mark;

Where to Declare? Declaration MUST happen at the top of the program, that is, the very first thing that we do, MUST be declaring the variables.

Identifiers All variables must have names. There are strict rules for variable names. These rules will apply to function names later so we will call these names identifiers. A declaration is done with the type followed by the identifier ;. Ex: int lifespan; double mass; char letter;

Hard rules for identifiers Rule #1: An identifier must not be a reserved word. Reserved words are used by C exclusively. Here are a few: double, char, int, do, float, if, return, sizeof, void,while, typedef, struct, switch, for, else. See the complete list in the Documents section of the course website. Rule #2: An identifier must contain only letters, digits or underscores. Abc8 is valid, Abc-8 is not. _xyz is valid, atom number is not.

Hard rules for identifiers Rule #3: An identifier must never begin with a digit. U238 and _765 are valid, 7abc and 67_q are not.

Soft rules for identifiers Rule #4: An identifier should not be a standard identifier. A standard identifier is a name used by C but is not a reserved word. printf, scanf are examples of standard identifiers. Rule #5: All-capital names should be used only for constant macros. Variables and function should never use capital letters. Never mix upper-case and lower-case letters in a name.

How to select names for variables, functions, etc. in ‘C’? Use the set of rules for picking up VALID names in C Use meaningful and descriptive names so we need less comments. Rules For Picking up Names 1. Names in ‘C’ are Case Sensitive, i.e., ‘C’ makes distinction between upper and lower-case letters. e.g., int number; int Number; /* number and Number are two different integer variables.*/ 2. User defined names cannot be the same as C keywords. i.e., names such as for, while, if are NOT valid user defined names. int for; /* invalid variable name. for is C keyword. */

Rules For Picking up Names 3. Every name must start with A letter of alphabet (upper, lower) Underscore ( _ ) e.g., int age; float Salary; double _code; 4. The remainder of the name may be Upper or lower case letters of alphabets Decimal digits (0-9) Underscore

What is a ‘C’ Program? A collection of keywords, variables, operators, expressions, statements, different data types, etc. that together performs one or more than one task. Therefore, we need to know the followings: C Keywords If For While C Different data types Integer  int Floating Point  float Character  char Void  void

Valid C Variables int month; float average; double PI; char name; C Operators Arithmetic Assignment and Compound Assignment Sizeof Relational Logical Conditional Increment/Decrement C Statements C Expressions, A valid sequence of operands and operators to calculate a value, usually expressing a math, logical calculation, …

‘C’ Keywords Terms with pre-defined meaning in ‘C’. We must use the keywords within their intended meanings. Keyword definition cannot be changed.

Example: case used as part of switch sizeof gives the size of the variable in byte for used for looping purposes do used as part of do-while char a data type double a data type long a data type return used to return a value from a function static used in combination with data types void a data type int a data type float a data type if used for conditional purposes else used a part of if switch used for selection purposes struct used to define a data structure enum used to define user defined data type while used for looping purposes

Numbers in C in a table int integer 2 -32,768 to +32,767 long Data Type Purpose Bytes Range int integer 2 -32,768 to +32,767 long long integer 4 -2,147,483,648 to +2,147,483,647 unsigned unsigned integer 0 to 65,535 unsigned long unsigned long integer 0 to 4,294,967,295 float floating point 3.4E+/-38 double double float 8 1.7E+/-308 long double long double float 16 1.7E+/-4932

Are used to format the print out. Escape Sequences Are used to format the print out. (\) symbol is referred to as the escape character AND is used to signify a escape sequence. Sequence Purpose \n New line \t Tab \" To print a double quote \\ To print a backslash

Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on your machine, you may need 1 byte for characters, 2 bytes for integers and 4 bytes for float. You also know that integers are in fact a subset of float & as such you may decide to use float type for all numerical values. Although this is mathematically correct however you would not have an efficient memory management here. You could have used 2-byte for integers but you used 4-byte instead!

Placeholders in I/O Statements Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted.

Integer placeholders %d is the default integer placeholder. When used it will simply display the value as is without any padding. To add padding, to have columns for example, we need formatted placeholders. %nd will reserve n places to display the number. Justification will be to the right. The negative sign takes one place. If the value is 17 and %4d is used, then it will display 2 spaces followed by 17 on the screen. __17 These are spaces, not underscores.

Integer placeholders The number is always displayed in its entirety, even when the format is too narrow. With -1234 and a %3d placeholder, you would see -1234, therefore using 5 spaces instead of the 3 requested. A negative number change the justification to the left of the field. With a value of -1234 and a %-8d placeholder, you will get -1234___ . 3 trailing blanks

Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The formatted double placeholder has this format: %w.dlf, where w is the total width of the field (including sign and decimal point) and d the number of decimal digits. If the value is 4.56 and the placeholder is %6.3lf then the display will be _4.560 1 leading blank

Double placeholders With a double formatted, you always get the requested number of decimal digits (even if the field is not wide enough). You also always (like the integer placeholder) get all the significant numbers. However, if there are more decimal precision in the value than in the placeholder, the value is truncated and rounded-up if need be.

Double placeholders If the value is -32.4573 and the placeholder is %7.3lf, then you will get 3 decimal digits as requested plus the significant numbers: -32.457. If the value is -32.4578 and the placeholder is %8.3lf, then you will get 3 decimal digits as requested plus the significant numbers and padding: _-32.458. See the rounding-up effect. A %8.7lf for a value of 187.123 will produce a display of 187.1230000. Note: The internal value is unaffected by the placeholder, only its screen appearance.

Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The value is 5.87 and the placeholder is %6.3lf then the display will be b5.870 If the value is -54.6793 and the placeholder is %7.3lf ,then you will have 3 decimal digits and the display will be: -54.679 If the value is -54.6778 and the placeholder is %8.3lf ,then you will have: b-54.678 .ROUNDING-UP effect. Also you could have TRUNCATE. A %8.7lf will produce: -54.6793000 Note!!!!!! The internal value is UNAFFECTED by the placeholder, only its screen appearance.

PLEASE CHECK CLASS EXAMPLES FOR PLACEHOLDERS

Simple Assignment Operator (=) Assign a value to a variable Does not mean equality, it means assignment Var1='a'; /* Var1  a */ Var2=15; /* Var2 15 */ Var3=27.62; /* Var3  27.62 */

Initializing Variable Giving a value to the variable at the time that the variable is declared. char Var1='X'; int Var2=1095; float Var3=2277.25;

Scanf() Another way to fill a variable is to ask the user for its value. We do that with the scanf statement. printf(“Enter your score:”); Scanf(“%d”,&score);