C Programming a Q & A Approach

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

Basic Elements of C. Staff Incharge: S.Sasirekha

Introduction to C Language

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

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

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

Chapter 2. Lexical Elements & Operators

C Language, Token, Keywords, Constant, variable

EL2310 Scientific Programming

INTRODUCTION 1 AND REVIEW

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Computers Programming Course 5. Iulian Năstac

Chapter 2, Part I Introduction to C Programming

Data Types and Variables in C language

Programming for Engineers Introduction to C

Work relative to other classes

6.096 Introduction to C++ January (IAP) 2009

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

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

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

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

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

1.1 Introduction to C Language. Department of CSE

LESSON 4. The DATA TYPE char

Fundamentals of C. Structure of a C Program

Preface. Intended Audience. Limits of This Dictionary. Acknowledgments

Fundamentals of Programming Session 4

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

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

CS Prof J.P.Morrison

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

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

Introduction to the C Programming Language

Chapter 1 Introduction to Computers and C++ Programming

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

Tutorial No. 2 - Solution (Overview of C)

Presented By : Gaurav Juneja

PROGRAMMAZIONE I A.A. 2017/2018

Number Systems, Scalar Types, and Input and Output

by Pearson Education, Inc. All Rights Reserved.

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

C: How to Program. Week /Mar/05

Chapter 1 & 2 Introduction to C Language

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

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

Chapter 2 - Introduction to C Programming

EL2310 Scientific Programming

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

The births of the generations are as follow. First generation, 1945 machine language Second generation, mid 1950s assembly language.

Computer Programming

SWEN-250 Personal SE. Introduction to C

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

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

Fundamental of C programming. - Ompal Singh

Basic Types and Formatted I/O

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Differentiate Between Keywords and Identifiers

2 nd Week Lecture Notes

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

Fundamentals of C Programming CS Introduction to Programming

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

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

Unit 4. Input/Output Functions

HP C/iX Reference Manual

Preview from Notesale.co.uk Page 6 of 52

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

C Programming a Q & A Approach

THE FUNDAMENTAL DATA TYPES

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

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

Lecture 2 Tao Wang 1

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

Introduction to Computing Lecture 01: Introduction to C

Typescript on LLVM Language Reference Manual

Lecture 2: C Programming Basic

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

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

EL2310 Scientific Programming

Introduction to C++ CS 1: Problem Solving & Program Design Using C++

edunepal_info

Data Types Literals, Variables & Constants

Variables and Literals

Computer programming & Data Structures Unit I Introduction to Computers

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

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

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

CS 113: Introduction to

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Fundamentals of Programming

ANSI C Programming Simple Programs

ECMA-404. The JSON Data Interchange Syntax. 2 nd Edition / December Reference number ECMA-123:2009

Fundamental of Programming (C)

Variables, Constants, and Data Types

COMP 2718: The OS, Shell, Terminal, and Text

Transcription:

C Programming a Q & A Approach by H.H. Tan, T.B. D Orazio, S.H. Or & Marian M.Y. Choy Chapter 1 Programming Fundamentals

Programming Languages We need to communicate with the computer Two favours Low level / Assembly Language High level language Machine Language Only (native) language that a computer can understand (0, 1) Very difficult for human to write Thus programs are written in other languages and translated into machine language 2

Assembly Language All instructions have a one-to-one mapping with machine language counterpart E.g. machine code 10010101 in assembly language will be ADD Assembly language instructions are not in binary code but in English words easier to memorise & use Problems Programmer needs complete understanding of computer hardware Substantial code needed just to do some simple tasks, e.g. print a message 3

High Level Languages Simplify the commands needed to be written by human, e.g., print ( a message ); Write programs with far less concern about the internal design of the machine Can be broken down into four types: Procedural (or imperative) Functional Declarative Object oriented C language is procedural type requires the programmer to lay out a procedure for solving a problem C is a subset of C++ : Everything you learn from this text about C can be applied to C++ 4

Summary of some high level languages Language Type Year developed Fortran Procedural Mid 1950s Basic Procedural Mid 1960s Lisp Functional Late 1950s Prolog Declarative Early 1970s Smalltalk Object oriented Mid 1970s Pascal Procedural Early 1970s C Procedural Mid 1970s C++ Object oriented Mid 1980s Java Object oriented Mid 1990s 5

Language Translators Programs that create machine language instructions (object code) from instructions written in assembly/high level language Three favours Assemblers: Convert programs (in assembly language) to object code Compilers: Taking an entire program (in high level language) and converting it to machine instructions Interpreters: Translate and execute (high level language) instructions one after another. 6

Software Engineering The process of software development Should be thoroughly thought out, planned, constructed, and tested Define function of the software Develop sketch of the layout Input from users, owners, programmers, etc. are solicited Design of individual components is addressed Planning modifications and assemble the software and test for functionality Comprehensively tested and modified as necessary Documentation about the software is maintained 7

CSC1510 Computer Principle & C Programming 8

Top-down Modular Design Begins by defining the main module of the software Then sub-modules are developed Each module is less complex than the whole (upper level one) Modules are called functions in C Can be divided into Library functions: Already included with C language User-defined functions: Custom-made by C programmer 9

C and ANSI C Developed in early 1970s at Bell Laboratories by Dennis Ritchie Highly portable, i.e., machine independent American National Standards Institute (Committee X3J11) approved a version of C ANSI C In 1999, a new version ISO/IEC 9899:1999 C is being introduced (C99) Another major revision is underway and the first draft proposed in 2009 In this text, we follow the ANSI C standard for broader supports 10

Program Development Objective: Create an executable file (.exe) A modern C programming development environment will: 1. Allowing user to edit text (create C source code) 2. Preprocessing source code 3. Compiling source code and indicating any possible errors 4. Linking object code from (3) with library (other object codes) 11

Basic Structure Topics to learn Writing a simple but complete C program Using the printf( ) function to display text on the screen Structure of a simple C program Basic rules for writing a C program 12

Line No Code Program 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } Output 13

Line No Code Program 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } Comment : Notes describing a particular portion of your program 14

Line No Code Program 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } Preprocessor directives: Essential to every program 15

Line No Program Code 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } Name of program body, actual program to be executed will be placed here 16

Line No Code Program 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } Braces: Mark the beginning & end of a program body 17

Line No Code Program 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } Statements that instruct computer to print a message 18

Line No Code Program 01 /*L1_1.C - In this book, the source for Lesson x_y is Lx_y.c */ 02 #include <stdio.h> /* This is an include directive */ 03 void main(void) 04 {/*The purpose of this program is to print one 05 statement to the screen */ 06 printf("this is C!"); 07 } All statement must ends with semicolon ; 19

Further Exploration Q: Can we use both uppercase and lowercase letters to write C code? A: Yes! C language distinguishes between lower- and uppercase letters, i.e. main is different from Main Both main and printf must be written in lowercase letters 20

Further Exploration Q: Where are blank space(s) permitted in C code? A: All C words should be written continuously, e.g., void ma in(void) is not legal But void main ( void ) is okay In general, it is acceptable to add blanks between words (we called it tokens) but not within 21

Further Exploration Q: Is it necessary to use different lines in writing code? A: You have the freedom to write C code at any row or column you like E.g. #include <stdio.h>void main(void){printf("this is C!");} or #include<stdio.h>void main( void ) { printf ( "This is C!" ) ; are both valid 22

1.7 Formatting Output 04 printf("welcome to"); 05 printf("london!"); 06 printf("\nhow do we\njump\n\ntwo lines?\n"); 07 printf("\n"); 08 printf("it will rain\ntomorrow\n"); Output 23

Basic Structure of C Program # preprocessing directives void main(void) { statement 1; statement 2; statement 3; statement 4;... } 24

1.8 More Escape Sequences 04 printf("listen to the beep now. \a"); 05 printf("\nwhere is the 't' in cat\b?\n\n"); 06 printf("i earned $50 \r Where is the money?\n); 07 printf("the rabbit jumps \t\t two tabs.\n\n"); 08 printf("welcome to \ 09 New York!\n\n"); 10 printf("from " "Russia \ 11 with " "Love.\n"); 12 printf("print 3 double quotes -\" \" \" \n"); Output 25

1.8 More Escape Sequences 04 printf("listen to the beep now. \a"); Doing back space 05 printf("\nwhere is the 't' in cat\b?\n\n"); 06 printf("i earned $50 \r Where is the money?\n); 07 printf("the rabbit jumps \t\t two tabs.\n\n"); 08 printf("welcome to \ 09 New York!\n\n"); 10 printf("from " "Russia \ 11 with " "Love.\n"); 12 printf("print 3 double quotes -\" \" \" \n"); 26

1.8 More Escape Sequences Line 01 02 Moving cursor to start column 03 { 04 printf("listen to the beep now. \a"); 05 printf("\nwhere is the 't' in cat\b?\n\n"); 06 printf("i earned $50 \r Where is the money?\n); 07 printf("the rabbit jumps \t\t two tabs.\n\n"); 08 printf("welcome to \ 09 New York!\n\n"); 10 printf("from " "Russia \ 11 with " "Love.\n"); 12 printf("print 3 double quotes -\" \" \" \n"); 13 } Output 27

1.8 More Escape Sequences Line 01 02 03 { 04 printf("listen to the beep now. \a"); 05 printf("\nwhere is the 't' in cat\b?\n\n"); 06 printf("i earned $50 \r Where is the money?\n); 07 printf("the rabbit jumps \t\t two tabs.\n\n"); 08 printf("welcome to \ 09 New York!\n\n"); 10 printf("from " "Russia \ 11 with " "Love.\n"); 12 printf("print 3 double quotes -\" \" \" \n"); 13 } Output Concatenate two strings 28

1.8 More Escape Sequences 03 { 04 printf("listen to the beep now. \a"); 05 printf("\nwhere is the 't' in cat\b?\n\n"); 06 printf("i earned $50 \r Where is the money?\n); 07 printf("the rabbit jumps \t\t two tabs.\n\n"); 08 printf("welcome to \ 09 New York!\n\n"); 10 printf("from " "Russia \ 11 with " "Love.\n"); 12 printf("print 3 double quotes -\" \" \" \n"); 13 } Print double quotes Output 29

Table 1.4 Character escape sequences Escape Sequence Meaning Result \0 Null character Terminates a character string \a Alert/bell Generates an audible or visible alert \b Backspace Moves back one space on the current line \f Form feed Moves to start of the next logical page \n New line Linefeeds to next line \r Carriage return Moves to initial position of the current line \t Horizontal tab Moves to next horizontal tabulation position \v Vertical tab Moves to next vertical tabulation position \0ddd Octal constant integer constant of base 8 ddd digits 0-7 \xddd Hexadecimal constant integer constant base 16, where \Xddd ddd represents decimal digits, and a f or A F represent values of 10 through 15 respectively \\ Backslash Displays a backslash \' Single quote Displays a single quote \" Double quote Displays a double quote \% Percent Displays a percent character 30