Data Types and Arithmetic in C

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Outline 2.1 Introduction 2.2 Basics of C Programs
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Input/Output and Variables Ethan Cerami New York
Introduction to C++ Programming
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
© 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.
Week 1 Algorithmization and Programming Languages.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 JavaScript: Introduction to Scripting
User Interaction and Variables
Chapter 2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Multiple variables can be created in one declaration
Chapter 2 - Introduction to C Programming
By: Syed Shahrukh Haider
Basic Elements of C++ Chapter 2.
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
Introduction to C Programming
Variables & Data types Selection Statements Additional Operators
Structured Program Development in C
Chapter 2 - Introduction to C Programming
Engineering Problem Solving with C++ An Object Based Approach
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Structured Program Development in C
Introduction to C Programming
Presentation transcript:

Data Types and Arithmetic in C Nouf Aljaffan (C) 2018 Editedby : Nouf almunyif

Adding Two Integers Add Comments Insert pre-processor definitions 17/12/2019 Add Comments Insert pre-processor definitions Define main function Declare variables [DataType] [VariableName]; Variable names are case sensitive User scanf to read variables by a user at the keyboard scanf(“format control string,”, &variableName) computes the sum of two values Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018

17/12/2019 Adding Two Integers A variable name in C is any valid identifier. An identifier is a series of characters consisting of letters, digits and underscores ( _ ) that does not begin with a digit. • C is case sensitive uppercase and lowercase letters are different in C. All variables must be defined with a name and a data type before they can be used in a program Every variable has a name, a type and a value. Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018

17/12/2019 Adding Two Integers Standard Library function scanf can be used to obtain input from the standard input, which is usually the keyboard. • The %d conversion specifier indicates that the data should be an integer (the letter d stands for “decimal integer”). The % in this context is treated by scanf (and printf) as a special character that begins a conversion specifier. Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018

17/12/2019 Adding Two Integers ampersand (&)—called the address operator followed by a variable name. The ampersand, when combined with a variable name, tells scanf the location in memory at which the variable is located The computer then stores the value for the variable at that location. Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018

Constants The const qualifier is used to tell C that the variable value can not change after initialisation. Constants are literal/fixed values assigned to variables or used directly in expressions. Examples:

Constants Another way to define constant value is the following : Examples:

Type Conversion Implicit Explicit Data will get automatically converted from one type to another. Data may also be expressly converted, using the typecast operator Nouf Aljaffan (C) 2018

Implicit Type Conversion 1. When data is being stored in a variable, if the data being stored does not match the type of the variable. The data being stored will be converted to match the type of the storage variable. Input : 11.5 What will be printed ? Nouf Aljaffan (C) 2018

Implicit Type Conversion 2. When an operation is being performed on data of two different types. The "smaller" data type will be converted to match the "larger" type. For example, when an int is added to a double, the computer uses a double version of the int and the result is a double. The following example converts the value of nTotal to a double precision value before performing the division.  Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the result. (try it ) int nTotal ; double average ; average = nTotal / 3.0; 3. When data is passed to or returned from functions. Nouf Aljaffan (C) 2018

Explicit Type Conversion The following example converts the value of nTotal to a double precision value before performing the division. ( nStudents will then be implicitly promoted, following the guidelines listed above. ) average = ( double ) nTotal / nStudents; Note that nTotal itself is unaffected by this conversion. Nouf Aljaffan (C) 2018

are reserved words of the language Nouf Aljaffan (C) 2018

Arithmetic in C Nouf Aljaffan (C) 2018

Arithmetic in C Nouf Aljaffan (C) 2018

Integer division yields an integer result Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. C provides the remainder operator, %, which yields the remainder after integer division. The remainder operator is an integer operator that can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. Arithmetic in C Nouf Aljaffan (C) 2018

Arithmetic in C Nouf Aljaffan (C) 2018

Rules of Operator Precedence Nouf Aljaffan (C) 2018

Rules of Operator Precedence Nouf Aljaffan (C) 2018

Rules of Operator Precedence y = ( a * x * x ) + ( b * x ) + c; redundant parentheses Rules of Operator Precedence Nouf Aljaffan (C) 2018