Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Similar documents
C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

Chapter 1 & 2 Introduction to C Language

C Language, Token, Keywords, Constant, variable

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

6.096 Introduction to C++ January (IAP) 2009

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Fundamentals of Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Fundamental of Programming (C)

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Introduction to the C Programming Language

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Programming and Data Structures

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

DEPARTMENT OF MATHS, MJ COLLEGE

Computers Programming Course 5. Iulian Năstac

Programming in C++ 4. The lexical basis of C++

Work relative to other classes

Data types, variables, constants

ANSI C Programming Simple Programs

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Lecture 02 C FUNDAMENTALS

Fundamental of C programming. - Ompal Singh

Presented By : Gaurav Juneja

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

IS 0020 Program Design and Software Tools

CMPE-013/L. Introduction to C Programming

Preview from Notesale.co.uk Page 6 of 52

Introduction to Computing Lecture 01: Introduction to C

bphanikrishna.wordpress.com

ET156 Introduction to C Programming

Lecture 2: C Programming Basic

Differentiate Between Keywords and Identifiers

A Fast Review of C Essentials Part I

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Computer programming & Data Structures Unit I Introduction to Computers

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Lecture 3. More About C

Introduction to C# Applications

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Chapter 2: Overview of C. Problem Solving & Program Design in C

Fundamentals of C. Structure of a C Program

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Programming. C++ Basics

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to C Language

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Fundamentals of C Programming CS Introduction to Programming

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Full file at C How to Program, 6/e Multiple Choice Test Bank

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

UEE1302 (1102) F10: Introduction to Computers and Programming

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Creating a C++ Program

edunepal_info

Syntax and Variables

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Programming Fundamentals (CS-302 )

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSCI 171 Chapter Outlines

Data Types and Variables in C language

C Programming Language Training. This instruction relies on the C language described in C++: The Complete Reference Third Edition By Herbert Schildt

CS102: Variables and Expressions

Chapter 1 Introduction to Computers and C++ Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

VARIABLES AND CONSTANTS

General Announcements

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 2. Lexical Elements & Operators

Basic Elements of C. Staff Incharge: S.Sasirekha

ET156 Introduction to C Programming

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Department of Computer Applications

Computer Programming : C++

4.1. Structured program development Overview of C language

BLM2031 Structured Programming. Zeyneb KURT

A flow chart is a graphical or symbolic representation of a process.

CMSC 246 Systems Programming

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CHW 469 : Embedded Systems

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

LECTURE 02 INTRODUCTION TO C++

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Computer System and programming in C

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Class B.Com. III Sem.

C - Basic Introduction

Transcription:

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1

Outline p Introduction p Program development p C language and beginning with programming p Compilation steps of a C program p Variables and their data types p Format specifiers and escape sequences p C operators p Taking input p Comments 2

Computer Programming p A program is a set of ordered instructions that enables a computer to solve a problem p The process of developing and implementing these steps is called programming p Instructions must be provided to the computer in a systematic order 3

Computer Components p Hardware Physical parts of a computer CPU, RAM, etc. p Software A collection of computer programs and data Makes use of computer hardware Guides the computer at each and every step 4

Types of computer software p Application software To perform specific tasks of computer users Word processors, Spreadsheets, Payroll, Accounts p System software Control, operate and monitor the computer through interacting with the hardware An interface between the hardware and the application software Operating systems, device drivers, compilers, interpreters 5

Programming language p Enables instructing the computer to perform specific tasks p Based on rules of syntax and semantics p Evolution Efficient translation of human language p High-level syntax, procedural/structured languages Complexity management p Object oriented approach 6

Types of languages p Low level languages Machine oriented languages that interact with the machine at low level Detailed knowledge of computer hardware and its configuration Machine language p Strings of 0 s and 1 s p Fast but complex and difficult to debug Assembly language p 0 s and 1 s are replaced by mnemonic codes p Assembler translates the code into machine language 7

Types of languages p High level languages Use English-like instructions Abstracts over the target hardware Easy to learn and use Desirable if achievable COBOL, FORTRAN, Pascal, etc. Use compiler/interpreter to translate high level code instructions into machine language 8

Program development 1. Define the problem 2. Outline the solution 3. Develop the outline into an algorithm 4. Test the algorithm for correctness 5. Code the algorithm into a specific programming language 6. Run the program on the computer 7. Document and maintain the program 9

Define the problem p Input What we got? p Output What we want to get? p Processing How do we get the desired output from input? p Area of a rectangle (A=L x W) 10

Outline the solution p Identification of major steps to solve the problem p Subtasks involved p Major variables and data structures (L,W) p Major control structures p The underlying logic (A=L * W) 11

Develop the outline into an algorithm p An algorithm is a specification of precise and ordered steps that describe the tasks to be performed to solve a problem (pseudo code) Start Input L Input W Calculate Area, A=W * L Display output, A End 12

Test the algorithm for correctness p Evaluating the algorithm through test data p Comparing the obtained results to actual ones p Correcting logic errors 13

Code the algorithm p Coding the algorithm using some chosen programming language p Language-specific considerations are made here 14

Run the program on the computer p Translation of the high-level code into low level Correct compile time errors p Run the compiled code Correct runtime errors (incorrect inputs) Correct logic errors (incorrect results) 15

Document and maintain the program p Documentation of steps involved in developing algorithm and code p Maintenance and updating of program 16

Outline p Introduction p Program development p C language p Compilation steps of a C program p Variables and their data types p Format specifiers and escape sequences p C operators p Taking input p Comments 17

C Language p Dennis Ritchie, 1972, Bell Labs. p Successor of B formerly BCPL (Basic Combined Programming Language) p Strongly associated with UNIX p Incorporate feathers of high level and assembly languages p C programs are efficient and fast p C programs are fairly portable p C language has a simple and well-structured syntax 18

A simple C program #include <stdio.h> void main(void) { instructing the preprocessor to include header file stdio.h for the built function printf() main function declaration. One main () function is mandatory for a program to get control from OS } printf( Welcome to C language"); Use of the built in function, printf(), to print a simple string on the screen 19

Compilation of a C program Editor MyProgram.c, Source file Preprocessor Modification in memory Compiler MyProgram.o, Object code file Linker MyProgram.o, Object code file 20

Variables p Variable is a space in memory identified through a given name p Can represent a numerical value, character or string of characters p Variables must be declared at the beginning of a program with their proper types p A variable name can contain alphabets, digits and underscore p A name cannot start with a digit p Keywords cannot be used as variable names 21

Variable data types A variable can hold a specific type of data which must be defined Data Type Range of Values Space in memory char or signed char -128 to 127 1 byte unsigned char 0 to 255 1 byte int -32768 to 32767 2 bytes long int -2,147483,648 to 2,147483,647 4 bytes float double 3.4e-38 to 3.4e+38 1.7e-308 to 1.7e+308 The above-given sizes are the minimum for these variables. In practice, they depend upon the implemented compiler 22

C Keywords auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while 23

Format specifiers Format specifiers are used to format the printed output Format specifier Variable type %d int %c char %f float %lf double %s string 24

Escape Sequences Special characters reserved for specific tasks such as changing the line or printing some symbol which perform other tasks when used directly Escape Code Use \n New line \ Double quote \t Tab space \\ Backslash \b backspace \f Form feed 25

Example: Format specifiers & escape sequences # include <stdio.h> void main(void) { int a=34; float b=4.61; printf( The value of \ integer\ a=%d \n,a); printf( The value of \ float\ b=%f,b); } 26

Arithmetic operators p Addition, + p Subtraction, - p Division, / p Multiplication, * p Modulus (remainder), % (9%2=1) p Increment, ++ p Decrement, -- p Example A+B => A and B are operands and + is an operator 27

Assignment operators p Simple assignment, = (a=4) p Addition & assignment, += (a+=5 => a=a+5) p Subtraction & assignment, -+ p Multiplication & assignment, *= p Division & assignment, /= p Modulus & assignment, %= 28

Relational operators p Equal, == p Not-equal,!= p Less than, < p Greater than, > p Less than or equal, <= p Greater than or equal, >= 29

Logical operators p And (&&) operator returns true if both of its operands are true p Or ( ) operator, returns true if at least one of its operands are true p Not (!) operator reverses the logical state of its operand (from true to false and from false to true) 30

Operators precedence Name Operator Logical NOT! Arithmetic * / % Arithmetic +- Relational < > <= >= Relational ==!= Logical AND && Logical OR Assignment = += -= *= /= %= Parenthesis are evaluated first. This order can be changed by using parenthesis 31

Example operators # include <stdio.h> void main (void) { int a=6; int b=3; int c=4; int d; d=a+b*c-a/b; printf("d=%d",d); d++; printf("d=%d",d); } 32

Taking input # include <stdio.h> void main (void) { float length, width, area; printf( please enter length: ); scanf( %f, &length); printf( please enter width: ); scanf( %f,&width); area=length*width; printf( area=%f,area); } 33

Lab task p Write a program that inputs the radius of a circle and estimates its area A=Pi * r 2 34

Comments p Comments provide information about the program p Compiler does not read comments p Single line comments //.. comments.. p Multiline comments /*... comments */ 35

Summary Now you know, p What is programming and why we need programming languages? p Steps to develop a program p What is C language? p Writing simple programs Printing on display Using variables Formatting the output and using escape sequences Taking input Use of comments 36