ET156 Introduction to C Programming

Similar documents
ET156 Introduction to C Programming

C: How to Program. Week /Mar/05

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

Chapter 2 - Introduction to C Programming

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

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

Programming and Data Structures

ANSI C Programming Simple Programs

Introduction to Computing Lecture 01: Introduction to C

Fundamentals of Programming Session 4

Fundamental of Programming (C)

A Fast Review of C Essentials Part I

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

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

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

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

Data types, variables, constants

Chapter 1 & 2 Introduction to C Language

Programming for Engineers Introduction to C

BLM2031 Structured Programming. Zeyneb KURT

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

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

Fundamentals of Programming

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

6.096 Introduction to C++ January (IAP) 2009

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

Data Types and Variables in C language

Programming. C++ Basics

C Program Structures

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

2. Numbers In, Numbers Out

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

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

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

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

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

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


2. Numbers In, Numbers Out

Syntax and Variables

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

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

C Programming a Q & A Approach

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

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

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

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

Computers Programming Course 5. Iulian Năstac

DEPARTMENT OF MATHS, MJ COLLEGE

Basic Elements of C. Staff Incharge: S.Sasirekha

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

C Overview Fall 2014 Jinkyu Jeong

Chapter 2: Introduction to C++

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

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

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

Chapter 2. Lexical Elements & Operators

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

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

CSCI 171 Chapter Outlines

INTRODUCTION 1 AND REVIEW

Review of the C Programming Language for Principles of Operating Systems

LECTURE 02 INTRODUCTION TO C++

CSCI 2132 Software Development. Lecture 8: Introduction to C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

CMPE-013/L. Introduction to C Programming

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Preview from Notesale.co.uk Page 6 of 52

Basics of Programming

Chapter 2: Overview of C++

C Language, Token, Keywords, Constant, variable

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

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

Visual C# Instructor s Manual Table of Contents

Data Type Fall 2014 Jinkyu Jeong

Review of the C Programming Language

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Lesson 3 Introduction to Programming in C

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

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++

Creating a C++ Program

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

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

CC112 Structured Programming

Introduction to C# Applications

Basic Types and Formatted I/O

Lecture 02 C FUNDAMENTALS

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Transcription:

ET156 Introduction to C Programming g Unit 22 C Language Elements, Input/output functions, ARITHMETIC EXPRESSIONS AND LIBRARY FUNCTIONS Instructor : Stan Kong Email : skong@itt tech.edutech.edu

General Form of a C program preprocessor directives main function heading { } declarations executable statements

Comments A "comment" is a sequence of characters beginning with a forward slash/asterisk combination (/*) that is treated as a single white space character by the compiler and is otherwise ignored. A comment can include any combination of characters from the representable character set, including newline characters, but excluding the "end comment" delimiter (*/). The compiler ignores the characters in the comment. /* This is a comment */

Preprocessor Directives #include Directive for Defining identifiers from Standard Libraries SYNTAX #include <standard header file> Examples: #include <stdio.h> #include <math.h> Define Directive for Creating Constant Macros SYNTAX #define NAME value Examples: #define PI 3.141593

Variable Declarations Thememorycells used for storing a program s input data and its computational results are called variables. The variable declarations tell the C compiler the names of all variables and what kind of information will be stored in each variable. The general format for variable declaration is: Data type variable name or variable list; Example : int count; where int is data type of integer and count is the variable name.

Data type Common data type int store integer char store single character Double store real number/floating number

Variables/User Defined Identifiers A variable may be defined using any uppercase or lowercase character, a numerical digit (0 through 9), and the underscore character (_). The first character of the variable may not be a numerical digit. C reserved word cannot be used as an variable name. Variable names are case sensitive. Valid identifiers letter_1, cent, Hello, Invalid identifier 1Letter double Two*Four Reason invalid Begin with a letter Reserve word Character * not allowed

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

scanf Function scanf("%lf", &variable name); Read the floating point number into the address of the memorycell referenced by the variable miles.

printf Function function arguments printf("that equals %f kilometers.\n", kms); function name format string print list That equals 16.090000 kilometers

Executable Statements An assignment Statements store a value or computational result in a variable, and is used to perform most arithmetic operations in a program. Example : kms = KMS_PER_MILE MILE * miles;

Table 26 2.6 Arithmetic Operators

Unary vs. Binary Operators Unary Operators Binary Operators Take one operand Take two operands Negation ( ) operator Subtraction Plus (+) operator Addition Examples Multiplication x = -y; Division p = +x * y; Examples x = y + z; z = y z;

Rules of Precedence Parentheses rule Expressions in parentheses are evaluated separately. Nested parentheses are evaluated from the inside out. Operator precedence rule 1.unary +, 2.*, /, % 3.binary +, Associativity rule Unaryoperators at thesame level are evaluated right to left. Binary operators at the same level are evaluated left to right.

Rules of Precedence example result = 4 + 5 * 6 10/2; result = 29

Expression Assignment Assignment can cause data to be lost if target variable is of a data type more narrow than the expression double x; int n; x = 9 * 0.5; 4.5 4 n = 9 * 0.5; x n

Data Type Expression Example m = 3; n = 2; p = 2.0; x = m / p; y = m / n; Int y ;

Casting a Variable's Type

Formatting Output of Type int Table 2.11 Displaying 234 and 234 Using Different Placeholders

Formatting scientific rotation In scientific notation all numbers are written like this: Ordinary decimal Scientific Cformatting C formatting C formatting notation notation output %E output %e output %.2E (normalized) 300 3 10² 3.000000E+02 3.000000e+02 3.00E+02 4,000 4 10³ 4.000000E+03 4.000000e+03 4.00E+03 5,720,000,000 5.72 10⁹ 5.720000E+09 5.720000e+09 5.72E+09 0.0000000061 6.1 10 ⁹ 6.100000E 09 6.100000e 09 6.10E 09

Formatting Output of Type double Table 2.13

Interactive Mode vs. Batch Mode Interactive Mode Accepts input from users Sends output to the display Batch Mode Accepts input from a file metric <mydata Should echo input using printf Might output data to a file metric >myoutput

Input redirection/output redirection Redirect input from file instead of keyboard EXAMPLE : metric metric <mydata Redirect output to a file EXAMPLE : metric >myoutput

Program Controlled input and output Files Declare a file pointer variable in which to store the information necessary to permit access to a file. File pointer variable are of type FILE *. Example : FILE *inp, /* pointer to input file */ *opt; /* pointer to output file */ Prepare file to open for read, write inp = fopen("distance distance.dat dat", "r"); r); opt = fopen("distance.out", "w");

Interactive program example /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ Int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* Get and echo the distance in miles. */ printf("enter Distance in miles is "); scanf( "%lf", &miles); printf("the distance in miles is %.2f.\n", miles); /* Convert the distance to kilometers. */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers. */ printf( "That equals %.2f kilometers.\n", kms); return (0); }

Sample program to use file i/o 1 /* Converts distances from miles to kilometers. */ #include<stdio <stdio.h> /* printf, scanf, fprint, fscanf, fopen, fclose definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ FILE *inp inp, /* pointer to input file */ *outp; /* pointer to output file */ /* Open the input and output files. */ inp = fopen("distance.dat dat", "r"); r); outp = fopen("distance.out", "w"); /* Get and echo the distance in miles. */ fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles);

Sample program to use file i/o 2 /* Convert the distance to kilometers. */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers. */ fprintf(outp, "That equals %.2f kilometers.\n", kms); /* Close files. */ fclose(inp); fclose(outp); } return (0);

Figure 3.6 Function sqrt as a Black Box

Syntax Errors Code violates a grammar error of C Compiler locates syntax errors during translation Examples: Missing semicolon Undeclared variables Unclosed comments Mismatched open and close braces, quotation marks, or parentheses

Runtime Error Detected during program execution Caused by an attempt to perform an illegal operation

Logic Errors Errors in the algorithm Causes program to give incorrect results Avoid idby desk checking ki algorithm before bf coding

Top Down Design A problem solving method in which you first break a problem into its major subproblems and then solve the subproblems to derive the solution to the original problem. Structure Chart A documentation tool that shows the relationships among the subproblems of a problem.

Figure 3.10 Structure Chart for Drawing a Stick Figure

Quiz, Labs, Homework First Quiz next week covered Unit 1 & Unit2. 2 nd Lab due to day. 2 nd Assignment due next week.