Structured Programming Approach Module III - Expressing Algorithm Sequence Module IV - Concept of scalar Data Types Prof: Muhammed Salman Shamsi.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
© Oxford University Press All rights reserved. CSL-101 COMPUTER PROGRAMMING 2. BASICS OF C These slides are based on the book “Programming in C”
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Basics of C OBJECTIVE Understand the basic structure of a program in C. Learn the commands used in UNIX/LINUX and MS-DOS for compiling and.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Recap……Last Time [Variables, Data Types and Constants]
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter # 2 Part 2 Programs And data
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 3 Assignment and Interactive Input.
Basic Elements of C++.
C Language VIVA Questions with Answers
Introduction to C Programming Language
' C ' PROGRAMMING SRM-MCA.
Basic Elements of C++ Chapter 2.
Operators and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
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.
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
C – Programming Language
DATA TYPES There are four basic data types associated with variables:
Basic Programming Lab C.
Presentation transcript:

Structured Programming Approach Module III - Expressing Algorithm Sequence Module IV - Concept of scalar Data Types Prof: Muhammed Salman Shamsi

INTRODUCTION Ken Thompson at Bell Labs, USA, wrote his own variant over Martin Richards’s Basic Combined Programming Language and called it B. Dennis Ritchie, at Bell Labs, is credited for designing C in the early 1970s. Today C is a high-level language which also provides the capabilities that enable the programmers to ‘get in close’ with the hardware and allows them to interact with the computer on a much lower level.

Our First Program /* A Simple Program in C" #include //Preprocessor Directive int main(void) // can also be written as int main() { printf("\nHello World"); /* Statement to display output on screen ; are compulsory at the end of statement*/ return 0; // returns value to OS }

PROGRAM STATEMENTS A statement is a syntactic constructions that performs an action when a program is executed. All C program statements are terminated with a semi-colon (;).

Control statement is a statement whose execution results in a choice being made as to which of two or more paths should be followed. In other words, the control statements determine the ‘flow of control’ in a program.  Selection statements allow a program to select a particular execution path from a set of one or more alternatives. Various forms of the if..else statement belong to this category.  Iteration statements are used to execute a group of one or more statements repeatedly. “while, for, and do..while” statements falls under this group.  Jump statements cause an unconditional jump to some other place in the program. Goto statement falls in this group

Parts of C Program Revisited Header File The header files, usually incorporate data types, function declarations and macros, resolves this issue. The file with.h extension is called header file, because it’s usually included at the head of a program. Every C compiler that conforms to the international standard (ISO/IEC 9899) for the language will have a set of standard header files supplied with it. The header files primarily contain declarations relating to standard library functions and macros that are available with C.

Set of Standard Headers in C

Declaration Declaration means describing the type of a data object to the compiler but not allocating any space for it.  A declaration announces the properties of a data object or a function. If a variable or function is declared and then later make reference to it with data objects that do not match the types in the declaration, the compiler will complain.  Syntax: data_type variable_name_1,  Example: int number1; int myfunction(int n1,int n2);

Definition finition means declaration of a data object and also allocating space to hold the data object.  A definition, on the other hand, actually sets aside storage space (in the case of a data object) or indicates the sequence of statements to be carried out (in the case of a function).  Example: int myfunction(int a,int b) { int c=a+b; return c; }

VARIABLES: ATTRIBUTES All variables have three important attributes:  A data type that is established when the variable is defined, e.g., integer, real, character. Once defined, the type of a C variable cannot be changed.  A name of the variable.  A value that can be changed by assigning a new value to the variable. The kind of values a variable can assume depends on its type. For example, an integer variable can only take integer values, e.g., 2, 100, –12.

Classification of Data Types

BASIC DATA TYPES:SIZE & RANGE 16-bit Computer

BASIC DATA TYPES:SIZE & RANGE 32-bit Computer

SPECIFIERS / MODIFIERS : DATA TYPES The specifiers and qualifiers for the data types can be broadly classified into three types:  Size specifiers— short and long  Sign specifiers— signed and unsigned  Type qualifiers— const, volatile and restrict The type void does not have these modifiers.

KEYWORDS

CLASSIFICATION:OPERATORS IN C

ARITHMETIC OPERATOR

UNARY OPERATION Unary operators: The unary ‘–’ operator negates the value of its operand (clearly, a signed number). A numeric constant is assumed positive unless it is preceded by the negative operator. That is, there is no unary ‘+’. It is implicit. Remember that -x does not change the value of x at the location where it permanently resides in memory. Unary increment and decrement operators ‘++’ and ‘-- ’ operators increment or decrement the value in a variable by 1.

RELATIONAL OPERATORS

LOGICAL OPERATORS

Example

BITWISE OPERATORS

CONDITIONAL OPERATOR The conditional operator has three expressions.  It has the general form expression1 ? expression2 : expression3  First, expression1 is evaluated; it is treated as a logical condition.  If the result is non-zero, then expression2 is evaluated and its value is the final result. Otherwise, expression3 is evaluated and its value is the final result. For example,int m = 1, n = 2, min; min = (m < n ? m : n); /* min is assigned a value 1 */ In the above example, because m is less than n, m<n expression evaluates to be true, therefore, min is assigned the value m, i.e., 1.

Comma Operator This operator allows the evaluation of multiple expressions, separated by the comma, from left to right in order and the evaluated value of the rightmost expression is accepted as the final result. The general form of an expression using a comma operator is Expression M = (expression1, expression2, …,expression N);

SIZEOF OPERATOR C provides a useful operator, sizeof, for calculating the size of any data item or type. It takes a single operand that may be a type name (e.g., int) or an expression (e.g.,100) and returns the size of the specified entity in bytes.The outcome is totally machine-dependent.

Write a program to calculate the size in bytes required by data items and in- built data types of C using the “sizeof” operator.

Output

EXPRESSION EVALUATION: PRECEDENCE & ASSOCIATIVITY Precedence :The precedence of operators determines the order in which different operators are evaluated when they occur in the same expression. Operators of higher precedence are applied before operators of lower precedence. Associativity :The associativity of operators determines the order in which operators of equal precedence are evaluated when they occur in the same expression. Most operators have a left-to-right associativity, but some have right-to-left associativity.

Write a program to demonstrate the evaluation of expression according to the precedence rule.

LVALUES AND RVALUES An lvalue is an expression to which a value can be assigned. An rvalue can be defined as an expression that can be assigned to an lvalue. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. The address associated with a program variable in C is called its lvalue; the contents of that location are its rvalue, the quantity that is supposed to be the value of the variable. The rvalue of a variable may change as program execution proceeds; but never its lvalue.

Example

Rules for constructing Variables 1.A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to your typing effort 2.The first character of the variable name must either be alphabet or underscore. It should not start with the digit 3.No commas and blanks are allowed in the variable name 4.No special symbols other than underscore ( _ ) are allowed in the variable name

Type Conversion

RULES OF TYPE CONVERSION  float operands are converted to double.  char or short (signed or unsigned) are converted to int (signed or unsigned).  If any one operand is double, the other operand is also converted to double, and that is the type of the result  If any one operand is long, the other operand is treated as long, and that is the type of the result;  If any one operand is of type unsigned, the other operand is converted to unsigned and that is also the type of the result.

Following Assignment are Invalid int = long; int = float; long = float float = double Following Assignment are valid long = int double = float int = char

COMPLEX NUMBERS A complex number is a number with a real part and an imaginary part. It is of the form a + bi Three complex types are supported by C:  float complex  double complex  long double complex C also support three imaginary types also :  float imaginary  double imaginary  long double imaginary

complex.h To use the complex types, the complex.h header file must be included. The complex.h header file defines some macros and several functions that accept complex numbers and return complex numbers. In particular, the macro I represents the square root of –1. It enables to do the following:  double complex c1 = * I;  float imaginary c2= -5.0 * I;

Formatted Input and Output Statement Formatted Input Function: scanf(“control string”, arg1, arg2, …); Formatted Output Function: printf(“control string”, var1, var2, …,var n);

Sr no.FormatMeaning 1%wdFormat for integer output 2%w.cfFormat for float numbers output 3%w.csFormat for string output Format for various inputs & outputs