Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 2 Introduction to C Programming
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 2 - Introduction to Java Applications
Introduction to C Programming
Introduction to C++ Programming
Chapter 2 Introduction to C Programming Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
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?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 02 (Part II) Introduction to C++ Programming.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Java Applications
Introduction to C Programming
Chapter 6 JavaScript: Introduction to Scripting
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Lecture 2: Introduction to C Programming

OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data types.  Computer memory concepts.  To use arithmetic operators.  The precedence of arithmetic operators.  To write simple decision-making statements.

Simple Input and Output in C Adding two integers Definitions of variables scanf obtains a value from the user and assigns it to integer1 scanf obtains a value from the user and assigns it to integer2 Assigns a value to sum

Adding two integers Comments: Used to describe program #include : allows standard input/output operations int main() C programs contain one or more functions, exactly one of which must be main int means that main "returns" an integer value The execution of any C program starts from main

Adding two integers int integer1, integer2, sum; Definition of variables: locations in memory where a value can be stored. int means the variables can hold integers Variable names (identifiers) integer1, integer2, sum Identifiers: consist of letters, digits (cannot begin with a digit) and underscores( _ ) Case sensitive Definitions appear before executable statements

Adding two integers scanf( "%d", &integer1 ); Obtains a value from the user -- uses standard input (usually keyboard) Two arguments %d - data should be a decimal integer (“%d” is the format control string) &integer1 - location in memory to store variable When executing the program the user responds to the scanf statement by typing in a number, then pressing the enter (return) key

Adding two integers Assignment statement = - assignment operator Assigns a value to a variable A binary operator (having two operands) sum = integer1 + integer2 sum gets integer1+integer2 Variable receiving value on left

Adding two integers printf( “Sum is %d\n”, sum ); Similar to scanf %d means decimal integer will be printed Sum specifies that integer will be printed Calculations can be performed inside printf statements printf( "Sum is %d\n", integer1 + integer2 );

Good Programming Practice Identifier definition Choosing meaningful variable names helps make a program self- documenting, i.e., fewer comments are needed. The first letter of an identifier used as a simple variable name should be a lowercase letter. Later in the text we will assign special significance to identifiers that begin with a capital letter and to identifiers that use all capital letters. Multiple-word variable names can help make a program more readable. Avoid run­ning the separate words together as in totalcommissions. Rather, separate the words with underscores as in total_commissions, or, if you do wish to run the words together, begin each word after the first with a capital letter as in totalCommissions. The latter style is preferred. Separate the definitions and executable statements in a function with one blank line to emphasize where the definitions end and the executable statements begin. Place a space after each comma (, ) to make programs more readable. Place spaces on either side of a binary operator. This makes the operator stand out and makes the program more readable.

Memory Concepts Variable Variable names correspond to locations in the computer's memory Every variable has a name, a type, and a value Whenever a new value is placed into a variable (through scanf, for example), it replaces (and destroys) the previous value Reading variables from memory does not change them … … … integer1 integer2 sum RAM

Arithmetic Arithmetic calculations Use * for multiplication and / for division Integer division truncates remainder e.g. 9 / 5 evaluates to 1. Modulus operator (%) returns the remainder e.g. 9 % 5 evaluates to 4. Operator precedence Used to decide which of two operators should be processed first.  Parentheses ()  Multiplication/Division/Remainder  Addition/Subtraction Use parenthesis when needed E.g.: Find the average of three variables a, b, and c using (a + b + c)/3, not a + b + c/3

Arithmetic Operator associativity used to decide which of two operators should be processed when both operators have same precedence.  Exponentiation: Right to Left  Multiplication/Division: Left to Right  Addition/Subtraction: Left to Right Examples: a - b + c = ((a - b) + c) a * b % c = ((a * b) % c) a^b^c = (a^(b^c))

Some Arithmetic Operators

Decision Making: if control statement Simple version, more detail next lecture If a condition is true, then the body of the if statement executed 0 is false, non-zero is true. Control always resumes after the if structure Example: find out the relationship of two user-entered numbers.

Example: Comparing Two Numbers Checks if num1 is equal to num2 Checks if num1 is not equal to num2 Checks if num1 is less than num2 Definitions of variables

Example: Comparing Two Numbers Checks if num1 is greater than num2 Checks if num1 is less than or equal to num2 Checks if num1 is greater than equal to num2

Good Programming Practice Indent the statement(s) in the body of an if statement. Place a blank line before and after every if statement in a program for readability. Although it is allowed, there should be no more than one statement per line in a program. A lengthy statement may be spread over several lines. If a statement must be split across lines, choose breaking points that make sense (such as after a comma in a comma-separated list). If a statement is split across two or more lines, indent all subsequent lines. If you are uncertain about the order of evaluation in a complex expression, use parentheses to group expressions or break the statement into several simpler statements. Be sure to observe that some of C’s operators such as the assignment operator ( = ) associate from right to left rather than from left to right.

Equality and Relational Operators

Keywords Special words reserved for C Cannot be used as identifiers or variable names