Variables in C Amir Haider Lecturer.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Chapter 2: Introduction to C++.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
CPS120: Introduction to Computer Science Lecture 8.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
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.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
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.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science Variables and Constants.
1 Variables and Arithmetic Operators in JavaScript.
CMSC 1041 Variables in C Declaring, Naming, Using Variables.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables and Arithmetic Operators in JavaScript
Basics (Variables, Assignments, I/O)
CMSC 104, Section 4 Richard Chang
Introduction to C Programming
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
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.
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,
Variables and Arithmetic Operators in JavaScript
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Introduction to C Programming
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Variables in C Declaring , Naming, and Using Variables.
Chapter 2: Introduction to C++.
WEEK-2.
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
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 C Amir Haider Lecturer

Topics Naming Variables Declaring Variables Using Variables The Assignment Statement

What Are Variables in C? Variables in C have the same meaning as variables in algebra. That is, they represent some unknown, or variable, value. x = a + b z + 2 = 3(y - 5) Remember that variables in algebra are represented by a single alphabetic character.

Naming Variables Variables in C may be given representations containing multiple characters. But there are rules for these representations. Variable names (identifiers) in C May only consist of letters, digits, and underscores May be as long as you like, but only the first 31 characters are significant May not begin with a digit. 2, 4, 5 wrong but a, b, c write. May not be a C reserved word (keyword)

Reserved Words (Keywords) in C auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Naming Conventions C programmers generally agree on the following conventions for naming variables. Begin variable names with lowercase letters Use meaningful identifiers Separate “words” within identifiers with underscores or mixed upper and lower case. Examples: surfaceArea surface_Area surface_area Be consistent!

Naming Conventions (con’t) Use all uppercase for symbolic constants (used in #define preprocessor directives). Note: symbolic constants are not variables, but make the program easier to read. Examples: #define PI 3.14159 #define AGE 52

Case Sensitivity C is case sensitive It matters whether an identifier, such as a variable name, is uppercase or lowercase. Example: area Area AREA ArEa are all seen as different variables by the compiler.

Declaring Variables Before using a variable, you must give the compiler some information about the variable; i.e., you must declare it. The declaration statement includes the data type of the variable. Examples of variable declarations: int meatballs ; float area ;

Declaring Variables (con’t) When we declare a variable Space is set aside in memory to hold a value of the specified data type That space is associated with the variable name That space is associated with a unique address Unless we specify otherwise, the space has no known value. Visualization of the declaration int meatballs ; meatballs garbage FE07 int

More About Variables C has three basic predefined data types: Integers (whole numbers) Int. long int, short int, unsigned int Floating point (real numbers) Float. Float , double Characters char At this point, you need only be concerned with the data types that are bolded.

Notes About Variables You must not use a variable until you somehow give it a value. You can not assume that the variable will have a value before you give it one. Assume your compiler does not give it an initial value!

Using Variables: Initialization Variables may be given initial values, or initialized, when declared. Examples: int length = 7 ; float diameter = 5.9 ; char initial = ‘A’ ; length 7 diameter 5.9 initial ‘A’

Using Variables: Initialization (con’t) Do not “hide” the initialization put initialized variables on a separate line a comment is always a good idea Example: int height ; /* rectangle height */ int width = 6 ; /* rectangle width */ int area ; /* rectangle area */ NOT int height, width = 6, area ;

Using Variables: Assignment Variables may have values assigned to them through the use of an assignment statement. Such a statement uses the assignment operator = This operator does not denote equality. It assigns the value of the right-hand side of the statement (the expression) to the variable on the left-hand side. Examples: diameter = 5.9 ; area = length * width ; Note that only single variables may appear on the left-hand side of the assignment operator.

Functions It is necessary for us to use some functions to write our first programs, but we are not going to explain functions in great detail at this time. Functions are parts of programs that perform a certain task and we have to give them some information so the function can do the task. We will show you how to use the functions as we go through the course and later on will show you how to create your own.

Displaying Variables Variables hold values that we occasionally want to show the person using the program. We have a function called printf( ) that will allow us to do that. The function printf needs two pieces of information to display things. How to display it What to display printf( “%f\n”, diameter );

printf( “%f\n”, diameter ); The name of the function is “printf”. Inside the parentheses are: print specification, where we are going to display: a floating point value (“%f”) We want to have the next thing started on a new line (“\n”). We want to display the contents of the variable diameter. printf( ) has many other capabilities.

Taking Input From Users We use Scan function for input values. Different values can be taken from users. we write function as scanf(“ %d ””,a); This shows that a value is taken by user and stored in variable a, %d is telling us the typ of variable.

Address Types Decimal values %d Fraction values %f Character values %c Strings values %s (array of chars)

Example: Declarations and Assignments #include <stdio.h> int main( void ) { int inches, feet, fathoms ; fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; inches garbage feet garbage fathoms garbage fathoms 7 feet 42 inches 504

Example: Declarations and Assignments (cont’d) printf (“Its depth at sea: \n”) ; printf (“ %d fathoms \n”, fathoms) ; printf (“ %d feet \n”, feet) ; printf (“ %d inches \n”, inches) ; return 0 ; }

Enhancing Our Example What if the depth were really 5.75 fathoms? Our program, as it is, couldn’t handle it. Unlike integers, floating point numbers can contain decimal portions. So, let’s use floating point, rather than integer. Let’s also ask the user to enter the number of fathoms, rather than “hard-coding” it in by using the scanf( ) function.

Enhanced Program #include <stdio.h> int main ( void ) { float inches, feet, fathoms ; printf (“Enter the depth in fathoms : ”) ; scanf (“%f”, &fathoms) ; feet = 6 * fathoms ; inches = 12 * feet ; printf (“Its depth at sea: \n”) ; printf (“ %f fathoms \n”, fathoms) ; printf (“ %f feet \n”, feet) ; printf (“ %f inches \n”, inches) ; return 0 ;

scanf (“%f”, &fathoms) ; The scanf( ) function also needs two items: The input specification “%f”. (Never put a “\n” into the input specification.) The address of where to store the information. (We can input more than one item at a time if we wish, as long as we specify it correctly.) Notice the “&” in front of the variable name. It says to use the address of the variable to hold the information that the user enters.

Final “Clean” Program #include <stdio.h> #define FEET_PER_FATHOM 6 #define INCHES_PER_FOOT 12 int main( void ) { float inches ; /* number of inches deep */ float feet ; /* number of feet deep */ float fathoms ; /* number of fathoms deep */ /* Get the depth in fathoms from the user */ printf (“Enter the depth in fathoms : ”) ; scanf (“%f”, &fathoms) ;

Final “Clean” Program (con’t) feet = FEET_PER_FATHOM * fathoms ; inches = INCHES_PER_FOOT * feet ; /* Display the results */ printf (“Its depth at sea: \n”) ; printf (“ %f fathoms \n”, fathoms) ; printf (“ %f feet \n”, feet); printf (“ %f inches \n”, inches); return 0 ; }

Good Programming Practices Place each variable declaration on its own line with a descriptive comment. Place a comment before each logical “chunk” of code describing what it does. Do not place a comment on the same line as code (with the exception of variable declarations). Use spaces around all arithmetic and assignment operators. Use blank lines to enhance readability.

Another Sample Program #include <stdio.h> #define PI 3.14159 int main ( void ) { float radius = 3.0; float area; area = PI * radius * radius; printf( “The area is %f.\n”, area ); return 0 ; }

Some Examples Make a simple calculator which can perform basic functions of + - * / . Make Ohms Law Calculator. which can find R, V, I. Also find Power = V*I. Make a calculator for voltage divider rule? Make a calculator for Current divider rule?