Scanf Reads in information from the keyboard Examples –scanf(“%d”, &number); –scanf(“%d%d”, &value1, &value2); WARNINGS! –Don’t forget the & (address of)

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Computer Programming w/ Eng. Applications
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Introduction to C Programming
Introduction to C Programming
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
Software Development Method & C Language Elements H&K Chapter 1-2
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Introduction to C Programming CE Lecture 2 Basics of C programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
The Java Programming Language
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?
Computer Science 101 Introduction to Programming.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
Introduction to Programming
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
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.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
C++ First Steps.
CSE 220 – C Programming C Fundamentals.
Input and Output: I/O Finish reading chapters 1 and 2 of the text
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Revision Lecture
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 – Getting Started
Chapter 2 Elementary Programming
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.
Introduction to CS Your First C Programs
Introduction to C++ Programming
Documentation and Style
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
CSCE 206 Lab Structured Programming in C
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

scanf Reads in information from the keyboard Examples –scanf(“%d”, &number); –scanf(“%d%d”, &value1, &value2); WARNINGS! –Don’t forget the & (address of) in front of each variable! –Use the correct percent code for the type of the variable you are using!

/* Arup Guha My Second C Program, edited 9/2/03 Computes the number of feet user ran. */ #include #define YARDS_IN_MILE1760 #define FEET_IN_YARD3 int main(void) { int feet_in_mile, num_miles; feet_in_mile = YARDS_IN_MILE*FEET_IN_YARD; printf("How many miles did you run?\n"); scanf("%d", &num_miles); printf("You ran %d feet.\n", feet_in_mile*num_miles); // system(“PAUSE”); Necessary to see screen in DevC++ return 0; }

Programming Style Include ample white space Indent code in between matching {} Always write a header comment Comment each major block of code Use meaningful variable names Define constants when appropriate Be consistent with your style

Types of Errors Compiler Error –Your program has incorrect syntax –Compiler outputs some message pertaining to the problem Run-Time Error –Some statement in your program forces your computer to crash on an individual execution –Not all executions may create a run-time error Logic Error –Program runs, but produces incorrect output sometimes.

Common Errors Forgetting a matching double quote Forgetting a semicolon Using the incorrect character code in a printf or scanf Forgetting & (address of) when reading into a variable Not initializing variables Incorrect order of assignment statements

Rules for Variable Names Comprised of letters, digits, underscores Can NOT start with a digit Usually, variable names are case sensitive Can NOT be a keyword (such as double) Although it’s not a rule, variable names should reveal the purpose of the variable.

Why use constants? Symbolic name (eg. WATER_DENSITY) has more meaning that the value (1). If you ever have to change its value, you can change it in one place. The compiler does NOT allow you to change its value DURING execution. Two Different Schools of Thought –Some think constants should ONLY be used for things that never change (PI, E, etc.) –Others think constants can be used for values that might change sometime, but WON’T change within the execution of a program.

Increment/Decrement Operators variable++ or ++variable –Equivalent to variable = variable+1; –There’s a subtle difference between the two forms. (We won’t go into it in this class.) variable-- or –variable –Equivalent to variable = variable-1;

Other Shorthand Assignments General Form –Short hand for = Examples –x += 10; –money -= 20; –value *= (rate+2); –portion /= (students+staff);

Pesky Detail about = Can be used multiply –x = y = z – 4; is valid –Not good style. –If z were 7 before this statement executed y would first get set to 3, and this operation would return 3 Then, x would ALSO get set to 3, the return value of the original operation. –Thus, the associativity is right to left.

/* Arup Guha 8/29/07 Circle Program – edited to read in the radius of a circle and the cost for one square foot of it and print out the total cost of the whole circle. */ #include #define PI int main(void) { double radius, area; double cost, totalcost; printf(“Enter the radius&cost per sqft of your circle.”); scanf(“%lf%lf”, &radius, &cost); area = PI*radius*radius; totalcost = cost*area; printf(“The total cost of the circle is $.2lf\n”, totalcost); // system(“PAUSE”); Necessary to see screen in DevC++ return 0; }