Variables in C Declaring , Naming, and Using Variables.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Variables in C Amir Haider Lecturer.
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
Structure of a C program
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
1 Variables and Arithmetic Operators in JavaScript.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
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
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Data types Data types Basic types
LESSON 3 IO, Variables and Operators
Variables and Arithmetic Operators in JavaScript
C Short Overview Lembit Jürimägi.
مبانی کامپیوتر و برنامه سازی
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,
C++ Basics.
פרטים נוספים בסילבוס של הקורס
Variables and Arithmetic Operators in JavaScript
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Govt. Polytechnic,Dhangar
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Module 2 Variables, Data Types and Arithmetic
Building Blocks of C Programming 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 and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Variables in C Declaring , Naming, and Using Variables

Using Variables You may declare variables in C. The declaration includes the data type you need. Examples of variable declarations: int meatballs ; float area ;

Declaring Variables When we declare a variable: space in memory is set aside to hold that data type That space is associated with the variable name Visualization of the declaration int meatballs ; meatballs FE07

Legal Variable Names Variable names in C must be valid identifiers Consists 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 number May not be a C keyword

Naming Conventions Begin variable names with lowercase letters Use meaningful identifiers Separate “words” within identifiers with underscores or mixed upper and lower case. Example: surfaceArea surface_Area or surface_area Be consistent !!

Naming Conventions (continued) Use all uppercase for symbolic constants ( #define ) Example: PI (#define PI 3.14159 ) Function names follow the same rules as variables

Case Sensitive C is case sensitive It matters whether something is upper or lower case Example: area is different than Area which is different than AREA

More About Variables C has 3 basic predefined data types Integers int, long int, short int, unsigned int Floating point numbers float, double Characters char

Initializing Variables Variables may be initialized int x = 7; float y = 5.9; char c = ‘A’; Do not “hide” the initialization put initialized variables on a separate line a comment is probably a good idea int y = 6; /* feet per fathom */ NOT int x, y = 6, z;

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

Which Are Legal Identifiers ? AREA area_under_the_curve 3D num45 Last-Chance #values x_yt3 pi num$ %done lucky***

Declarations and assignments wreck.c inches #include <stdio.h> main ( ) { int inches, feet, fathoms ; fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; } feet fathoms fathoms 7 feet 42 inches 504

wreck.c (cont’d) main ( ) { printf (“Its depth at sea: \n”) ; printf (“ %d fathoms \n”, fathoms) ; printf (“ %d feet \n”, feet); printf (“ %d inches \n”, inches); } %d is a place holder - indicates that the value of the integer variable is to be printed in decimal form (rather than binary or hex) at that location.

Floating point numbers Are numbers that can contain decimal points. What if the depth were really 5.75 fathoms ? ... Our program, as it is, couldn’t handle it. We can declare floating point variables like this : float fathoms ; float feet ;

Floating point version of wreck.c (works for any depth shipwreck) #include <stdio.h> main ( ) { float fathoms, feet; printf (“Enter the depth in fathoms : ”); scanf (“%f”, &fathoms); feet = 6.0 * fathoms; printf (“She’s %f feet down.\n”, feet); }