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

Similar documents
EL2310 Scientific Programming

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

EL2310 Scientific Programming

PROGRAMMAZIONE I A.A. 2017/2018

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

C Language, Token, Keywords, Constant, variable

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

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

LECTURE/ STUDY NOTES ON C

Presented By : Gaurav Juneja

COMP1917: Computing 1 1. Introduction

CS Prof J.P.Morrison

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

Chapter 1 & 2 Introduction to C Language

Preview from Notesale.co.uk Page 6 of 52

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

COMP1917: Computing 1 1. Introduction

Introduction to Computing Lecture 01: Introduction to C

EC 413 Computer Organization

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C: How to Program. Week /Mar/05

CS 113: Introduction to

Chapter 2 - Introduction to C Programming

Chapter 1 Introduction to Computers and C++ Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming

by Pearson Education, Inc. All Rights Reserved.

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Chapter 2, Part I Introduction to C Programming

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Work relative to other classes

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

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

EL2310 Scientific Programming

Introduction to C Programming

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

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

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

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

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

Computer Programming

History of C Programming Language. Why Name C was given to Language? Summary of C Programming Language History. C Programming Language Timeline:

Programming and Data Structure

Programming in C and C++

CSCI 2132 Software Development. Lecture 8: Introduction to C

Structure of this course. C and C++ Past Exam Questions. Text books

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Programming in C and C++

Programming I Laboratory - lesson 01

CSE 303 Lecture 8. Intro to C programming

CMSC 246 Systems Programming

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

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

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

Tutorial No. 2 - Solution (Overview of C)

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Programming for Engineers Introduction to C

Spring 2018 NENG 202 Introduction to Computer Programming

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Lecture 2: C Programming Basic

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Fundamentals of Programming Session 4

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

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

Introduction to Basis and Practice in Programming

Programming. Data Structure

Programming in C. What is C?... What is C?

Course Outline Introduction to C-Programming

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

Welcome to... CS113: Introduction to C

Language Basics. Characteristics of C. This chapter describes the basic characteristics and elements of the C programming language.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CS240: Programming in C

CMPE-013/L. Introduction to C Programming

1 The Catholic University of Eastern Africa P.o Box , Nairobi Kenya Edward Kioko 2013

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

4.1. Structured program development Overview of C language

Computers Programming Course 5. Iulian Năstac

IS12 - Introduction to Programming. Lecture 7: Introduction to C

COMP s1 Lecture 1

CSI33 Data Structures

Programming Language Basics

from Appendix B: Some C Essentials

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

Software Lesson 1 Outline

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Lecture 2 Tao Wang 1

BLM2031 Structured Programming. Zeyneb KURT

A Fast Review of C Essentials Part I

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

printf( Please enter another number: ); scanf( %d, &num2);

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

edunepal_info

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Understanding main() function Input/Output Streams

Transcription:

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

Lecture 04: Introduction to C Readings: Chapter 1.5-1.7

What is C? C is a general-purpose, structured programming language that is powerful, efficient and compact Its instructions consists of terms that resemble algebraic expressions, supplemented by certain English keywords such as if, else, for, and do Features modern flow control and data structures, and a rich set of operators Contains additional features that allow it to be used at a lower level Used for writing system programs and application programs Bhaskar Shrestha 3

History of C (1/3) C was developed in the 1970s by Dennis Ritchie at Bell Labs (Murray Hill, New Jersey) in the process of implementing the Unix operating system on a DEC PDP-11 computer 1960s, CPL (Combined Programming Language), (Barron et al., 1963) Purpose: to create a language that was capable of both high level machine independent programming and would still allow the programmer to control the behavior of individual bits of data. Drawback: it was too large for use in many applications Bhaskar Shrestha 4

History of C (2/3) In 1967, BCPL (Basic CPL) a scaled down version of CPL while still retaining its basic features In 1970, B Ken Thompson developed the B language, which was a scaled down version of BCPL written specifically for use in systems programming In 1972, C Dennis Ritchie returned some of the features found in BCPL to the B language and developed C Bhaskar Shrestha 5

History of C (3/3) Limited to use within Bell Laboratories until 1978 In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C in their book titled The C Programming Language, now known as the K&R C ANSI formed a committee in 1983 to establish a standard definition of C, now known as ANSI C (1989) Updated in 1995 New features added in 1999, now known as C99 Bhaskar Shrestha 6

Features of C Small: C is a language of few words, containing only a handful of terms, called keywords, which serve as the base on which the language s functionality is built Portable: Portable means that a C program written for one computer system (an IBM PC, for example) can be compiled and run on another system (a DEC VAX system, perhaps) with little or no modification C provides a standard library of functions that work in the same way on all machines Bhaskar Shrestha 7

Features of C Middle-level language C is often called a middle-level computer language because it combines the best elements of high-level language with the control and flexibility of assembly language Structured Language C allows programmer to divide program into modules C provides all basic control structures Use of subroutines that employ local variables Use of code block No use of go-to statements Bhaskar Shrestha 8

C Is a Programmer s Language Not all computer programming languages are for programmers For example, COBOL was designed, in part, to enable nonprogrammers to read and presumably (however unlikely) to understand the program In contrast, C was created, influenced, and field-tested by working programmers C gives the programmer what the programmer wants: few restrictions, few complaints, block structure, stand-alone functions, and a compact set of keywords Bhaskar Shrestha 9

Preparing to Program The Programming Process Determine the objective of the program Design your solution Inputs, outputs and logical steps to achieve the outputs Code your solution Compile your program Handling errors Run and Test your program Bhaskar Shrestha 10

Structure of a C Program A program is a sequence of instructions Instructions of a C program are written as a statement A statement is terminated by a semicolon (;) One or more statements forms a block (compound) statement with the individual statements enclosed within a pair of braces, i.e., { } All executable statements must be inside a function Bhaskar Shrestha 11

Structure of a C Program A function is where all program activity occurs Every C program consists of one or more functions Every C program must contain a special function named main The statements within this function is the first one to be executed Comments are written within the delimiters /* and */ E.g., /* this is a comment */ Bhaskar Shrestha 12

First Program: hello.c /* A simple C program that outputs two lines of text */ #include <stdio.h> /* I/O header file */ main() /* main function heading */ { printf("hello, world\n"); /* call to printf */ printf("welcome to ITC213\n"); } Bhaskar Shrestha 13

Comments in C The first line /* A simple C program that outputs two lines of text */ starts with /* and ends with */ Anything written between /* and */ is called a comment Comments are not executable statements and they are ignored by the compiler They have no effect on the behavior of the resulting program Comment serves as documentation for the human reader of the program Bhaskar Shrestha 14

Preprocessor Directives (1/2) The line #include <stdio.h> is called the preprocessor directive Lines that begin with the # (read hash) sign are preprocessor directives It is mostly written at the beginning of the program They are not executable code line but indications for the C preprocessor Bhaskar Shrestha 15

Preprocessor Directives (2/2) The C preprocessor is a tool which filters your source code before it is compiled In this case, it tells the compiler s preprocessor that the contents of the file stdio.h should be included at the place where #include appears The file stdio.h is called a header file in C and it contains the declaration needed to perform standard input output operations Bhaskar Shrestha 16

The main Function (1/2) The next line main() is the first line of a function main The function main() is required in all C programs The main function is the starting point of a C program It is independent from whether it is at the beginning, at the end or by the middle of the code - its content is always the first to be executed when a program starts Bhaskar Shrestha 17

The main Function (2/2) main goes followed by a pair of parenthesis () because it is a function In C, all functions are followed by a pair of parenthesis () that, optionally, can include arguments within The content of the main function follows immediately to its header enclosed between braces {}, as in our example The code inside the braces {} are program statements that are to be executed Bhaskar Shrestha 18

Main Function Body (1/2) The first statement printf("hello, world\n"); causes the text Hello, world, enclosed in quotes, to be printed in the standard output device (often known as the console) Here, printf is a C function that outputs the text Anything written within the quotes is printed The printf function is defined in the file stdio.h Bhaskar Shrestha 19

Main Function Body (2/2) Here \n is a nonprinting character and is one of the escape sequence of C \n tells to print a new line which causes the next text to be printed on next line Hence, the next printf causes the text Welcome to ITC213 to be printed on next line The closing brace } at the last line program signifies the end of the main() function and hence the end of program Bhaskar Shrestha 20

The Build Process An editor is a specialized word processor used to prepare source modules in the language of choice (e.g. C, C++, Java, Fortran) The preprocessor adds in standard pre-written code (boilerplate) from include files you specify to produce a complete source module The compiler produces object code for the target computer/ operating system The linker ties multiple modules together into a complete program An executable file is a program that will run on the computer. The editor, preprocessor, compiler and linker are all executables. So is your program Bhaskar Shrestha 21

The Build Process Editor Source File Include Files Preprocessor Source File Other object Files Compiler Object File Library Files Linker Bhaskar Shrestha 22 Executable File

Compilation and Linker Errors A compilation error occurs when the compiler finds something in the source code that it can t compile A misspelling, typographical error, or any of a dozen other things can be a cause Linker errors are relatively rare and usually result from misspelling the name of a C library function In this case, you get an Error: undefined symbols: error message, followed by the misspelled name (preceded by an underscore) Bhaskar Shrestha 23

Another Example /* Program to find the area and perimeter of a rectangle given its width and height */ #include <stdio.h> main() { int width, length; /* variable declaration */ int area, perimeter; width = 5; /* assign the value 5 to the variable width */ length = 7; /* assign the value 7 to the variable length */ area = width*length; /* calculate the area */ perimeter = 2*(width+length); /* calculate the perimeter */ } /* Print the results */ printf("area is %d\n", area); printf("perimeter is %d\n", perimeter); Bhaskar Shrestha 24