Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Similar documents
Fundamentals of Programming Session 4

Fundamentals of C Programming

Chapter 2, Part I Introduction to C Programming

Programming for Engineers Introduction to C

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

C Language, Token, Keywords, Constant, variable

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

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

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

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

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

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

Chapter 1 & 2 Introduction to C Language

IT 374 C# and Applications/ IT695 C# Data Structures

Chapter 1 Introduction to Computers and C++ Programming

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

CS102: Variables and Expressions

Fundamental of Programming (C)

Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Lecture 2 Tao Wang 1

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

ANSI C Programming Simple Programs

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

4. Inputting data or messages to a function is called passing data to the function.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Introduction to Programming

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

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Arithmetic Expressions in C

1.1 Introduction to C Language. Department of CSE

Preview from Notesale.co.uk Page 6 of 52

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Chapter 2. Lexical Elements & Operators

Data Types and Variables in C language

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

Data types, variables, constants

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

Basics of Programming

Chapter 2: Overview of C++

2 nd Week Lecture Notes

Objectives. In this chapter, you will:

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

2. Numbers In, Numbers Out

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

C - Basic Introduction

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

DEPARTMENT OF MATHS, MJ COLLEGE

Course Outline Introduction to C-Programming

Visual C# Instructor s Manual Table of Contents

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

Course Outline. Introduction to java

EC 413 Computer Organization

Full file at

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

2. Numbers In, Numbers Out

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

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

BCA-105 C Language What is C? History of C

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

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

Chapter 2: Introduction to C++

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

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Introduction to C# Applications

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

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

BITG 1233: Introduction to C++

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

Basics of Java Programming

بسم اهلل الرمحن الرحيم

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

Computer System and programming in C

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Work relative to other classes

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Some Computer Preliminaries

Lecture 3 Tao Wang 1

Fundamental of C programming. - Ompal Singh

Lesson 3 Introduction to Programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Maciej Sobieraj. Lecture 1

Programming and Data Structures

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Introduction to Java Applications; Input/Output and Operators

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Transcription:

Intro to Computer Programming (ICP) Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP)

What is C??? C is a programming language developed at AT & T s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. 2

Getting Started with C Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. 3

The C Character Set A character denotes any alphabet, digit or special symbol used to represent information. Below table shows the character set used in C lang. 4

Constants, variables and Keywords The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. A constant is an entity that doesn t change whereas a variable is an entity that may change. 5

In any program we typically do lots of calculations. The results of these calculations are stored in computers memory. Variable Like human memory the computer memory also consists of millions of cells. The calculated values are stored in these memory cells. these memory cells/locations are given names. Since the value stored in each location may change the names given to these locations are called variable names. 6

Veriable Since the location whose name is x can hold different values at different times x is known as a variable. As against this, 3 or 5 do not change, hence are known as constants. Any variable used in the program must be declared before using it. For example, int p, n; 7

Types of C Constants C constants can be divided into two major categories, sub categories are mentioned in the table, 8

Rules for Constructing Integer Constants An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is -32768 to 32767. For Example, 426, +724, -7866 etc. 9

Rules for Constructing Real Constants A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. For example, 426.0, +345.56, -34.789 etc 10

Rules for Constructing Character Constants A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, A is a valid character constant whereas A is not. The maximum length of a character constant can be 1 character. For Example A, I, 5, $ etc 11

Types of C variables An entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. In any language, the types of variables that it can support depend on the types of constants that it can handle. 12

Rules for Constructing Variable Names A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. The first character in the variable name must be an alphabet or underscore. No commas or blanks are allowed within a variable name. No special symbol other than an underscore (as in gross_sal) can be used in a variable name. For example si_int, m_hra, pop_e_89 etc These rules remain same for all the types of primary and secondary variables. 13

C Keywords Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called Reserved words. There are only 32 keywords available in C. 14

Keywords in C 15

Memory Requirement of Different Data Type char is 1 byte short int is 2 bytes int is 4 bytes long int is 4 bytes float is 4 bytes double is 8 bytes long double is 10 bytes You can get the size of the data type using sizeof(data type) function (e.g. sizeof(char)) Compiler to compiler these size varies. 16

Compilation and execution of C program Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor. Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler. Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. Many IDE s like, Turbo C, Trubo C++ and Microsoft C works under MS-DOS. Visual C++ and BorLand works under windows. gcc compiler works under Linux. 17

How to Run??? Start the compiler at C>prompt. The compiler (TC.EXE is usually present in C:\TC\BIN\). Select New from the File menu. Type the program. Save the program using F2 under a proper name (say Program1.c). Use Ctrl + F9 to compile and execute the program. Use Alt + F5 to view the output. 18

Typical C Program Development Environment 19

Printing a line of Text A simple C Program 20

Comments Line 1 and 2 begin with /* and end with */, called comments. Comments do not cause the computer to perform any action when the program is run. Comments are ignored by the C compiler and do not cause any machine-language object code to be generated. 21

Comments Comments cannot be nested. For example, /* Cal of SI /* Author sam date 01/01/2002 */ */ is invalid. A comment can be split over more than one line, as in, /* This is a jazzy comment */ Such a comment is often called a multi-line comment. 22

C Preprocessor #include<stdio.h> is a directive to the C preprocessor. Lines beginning with # are processed by the preprocessor before the program is compiled. Line 3 tells the preprocessor to include the contents of the standard input/output header(<stdio.h>) in the program. 23

Line 6, int main (void) is a part of every C program. Main Function The parentheses after main indicate that main is a program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. Functions can return information. The keyword int to the left of main indicates that main returns an integer (whole number) value. 24

Main function Functions also can receive information when they re called upon to execute. The void in parentheses here means that main does not receive any information. Line 7 A left brace, {, begins the body of every function (line 7). A corresponding right brace } ends each function (line 11). This pair of braces and the portion of the program between the braces is called a block. The block is an important program unit in C 25

Line 8 Program parts explanation Print on the screen the string of characters marked by the quotation marks. A string is sometimes called a character string, a message or a literal. The entire line, including printf, its argument within the parentheses and the semicolon(;), is called a statement. Every statement must end with a semicolon. When the preceding printf statement is executed, it prints the message Welcome to C! on the screen. The characters normally print exactly as they appear between the double quotes in the printf statement. 26

Printf() The general form of printf( ) function is, printf( "<format string>", <list of variables> ) ; <format string> can contain, %f for printing real values %d for printing integer values %c for printing character values In addition to format specifiers like %f, %d and %c the format string may also contain any other characters. These characters are printed as they are when the printf( )is executed. For example printf( the value of b is %d, b); 27

Receiving input using scanf() To make the program general the program itself should ask the user to supply the values of a,b through the keyboard during execution. This can be achieved using a function called scanf( ). This function is a counter-part of the printf() function. printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. For example scanf( %d %d, &a, &b); it will read value against a and b at run time. When we say &a, we are telling scanf( )at which memory location should it store the value supplied by the user from the keyboard. 28

Escape sequences The characters \n were not printed on the screen. The backslash (\) is called an escape character. It indicates that printf is supposed to do something out of the ordinary. When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. 29

Some common escape sequences 30

Program Parts Explanation Line 10 The keyword return is one of several means. We ll use to exit a function. When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully 31

Three types of C instruction Type Declaration Instruction C instruction To declare the type of variables used in a C program. Arithmetic Instruction To perform arithmetic operations between constants and variables. Control Instruction To control the sequence of execution of various statements in a C program. 32

Type Declaration Instruction Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function. Not right bc using a before Defining it 33

Arithmetic instruction A C arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /. 34

Arithmetic instruction A C arithmetic statement could be of three types. Integer mode arithmetic statement All integers Real mode arithmetic statement All reals Mixed mode arithmetic statement Mixed (real and integers) Note: C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal. 35

Modular division operator (%) The expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note that the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus 5 % 2 yields 1, whereas, 5 % -2 yields 1. 36

Arithmetic instruction An arithmetic instruction is often used for storing character constants in character variables. For example char x, y; int z; x = a' ; y = b' ; z=x+y; printf("the value of a and b is %d, %d", x, y); printf("the value of z =%d", z); Output: The value of a and b is 97, 98. the value of z= 195 37

exponentiation operation Thus following statements are invalid. a = 3 ** 2 ; b = 3 ^ 2 ; For example #include <math.h> main( ) { int a ; a = pow ( 3, 2 ) ; printf ( %d, a ) ; } 38

39