Chapter 2 THE STRUCTURE OF C LANGUAGE

Similar documents
Data Types and Variables in C language

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

Basic Elements of C. Staff Incharge: S.Sasirekha

BSM540 Basics of C Language

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Differentiate Between Keywords and Identifiers

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

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

INTRODUCTION 1 AND REVIEW

BITG 1233: Introduction to C++

Lecture 2 Tao Wang 1

Computer Programming : C++

Input/Output Week 5:Lesson 16.1

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

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

Unit 4. Input/Output Functions

Work relative to other classes

Computer System and programming in C

Number Systems, Scalar Types, and Input and Output

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

C - Basic Introduction

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

UNIT- 3 Introduction to C++

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

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

Fundamentals of C Programming

Lecture 3. More About C

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

CSc Introduction to Computing

Week 3 Lecture 2. Types Constants and Variables

ANSI C Programming Simple Programs

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

LECTURE 02 INTRODUCTION TO C++

VARIABLES AND CONSTANTS

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

2 nd Week Lecture Notes

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

C: How to Program. Week /Mar/05

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

Programming and Data Structures

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

Java Notes. 10th ICSE. Saravanan Ganesh

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

CSC 1107: Structured Programming

Should you know scanf and printf?

Fundamentals of Programming. Lecture 11: C Characters and Strings

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Full file at

Chapter 2 - Introduction to C Programming

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

THE FUNDAMENTAL DATA TYPES

Reserved Words and Identifiers

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

JAVA Programming Fundamentals

Chapter 2 Basic Elements of C++

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

A Fast Review of C Essentials Part I

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Chapter 2: Introduction to C++

BLM2031 Structured Programming. Zeyneb KURT

Variables and Literals

Fundamentals of C. Structure of a C Program

Chapter 2. Lexical Elements & Operators

CS102: Variables and Expressions

Fundamental of Programming (C)

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

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Chapter 2: Programming Concepts

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

DEPARTMENT OF MATHS, MJ COLLEGE

Introduction to Computing Lecture 03: Basic input / output operations

Basic Types and Formatted I/O

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

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

CS Programming In C

Objectives. In this chapter, you will:

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

Exercise 1.1 Hello world

The C++ Language. Arizona State University 1

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

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

CC112 Structured Programming

CS149: Elements of Computer Science. Fundamental C++ objects

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

CS242 COMPUTER PROGRAMMING

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

Programming for Engineers Introduction to C

Basics of Java Programming

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

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

Some Computer Preliminaries

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

LESSON 4. The DATA TYPE char

AWK - PRETTY PRINTING

Transcription:

Lecture # 5 Chapter 2 THE STRUCTURE OF C LANGUAGE 1 Compiled by SIA CHEE KIONG DEPARTMENT OF MATERIAL AND DESIGN ENGINEERING FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING

Contents Introduction to C language, Basic C, Function Variable, Constant and Identifier, Data Types, Pre-processor Directives. 2

Data types Represents types of data can be stored in computer Types of data to be stored and used in programming should be informed to the compiler/system Integer Types Floating point Character 3

Data type (Integer) Integer Represents any round number with +/- values. Divided into short and long integer. Reserved word for integer int Valid until 5 places of integer number. Example: age is used to represent the age of students between 18 and 25 years old. The declaration for the variable is as follow: int age; 4

Data types (Floating number) Floating number Represents any floating point numbers +/- Reserved word double /float Example: height is used to represent the student s height between 150 cm and 180 cm. The declaration for the variable is as follow: float height; 5

Data types (Character) Character Represents character data. Reserved word char Example: gender is used to represent the gender of a student. The declaration for the variable is as follow: char gender; 6

Determine whether the following identifiers is valid or invalid. Give reason for invalid cases. 1) Parit Raja 2) 20thCentury 3) int 4) INTEGER 5) _BMW2003 6) Reservedword 7) BIT1033 8) markah_pelajar 9) jam*kredit 10) printf 7

Exercise Write a suitable variable declaration for each of the following statement: i. Salary of an employee ii. iii. iv. Student s mark for programming subject ATM pin number Phone number v. Price of one item vi. vii. Bus seat number Student name 8

Exercise Based on the following problem, determine the appropriate variables can be declared: Given the value of x is 10 and a is 12, find the result of the following equation: y = 2x + a - 6 9

Exercise Based on the following problem, determine the appropriate variables can be declared: Mrs Leeya needs to determine her students grade for programming subject based on the mark scored during final examination. The A grade will be given if the Mark scored is between 85 to 100. If a student has scored 90 marks, what is the grade should Mrs Leeya give to the student? 10

Exercise Based on the following problem, determine the appropriate variables can be declared: A box has height, width and length. Calculate the volume of a box. 11

Exercise Based on the following problem, determine the appropriate variables can be declared: Uncle Degawan wants to buy 5 tins of paint from Cinda s shop. The price of each tin of the paint is RM 15.60. Calculate the price which Uncle Degawan Have to pay for all the tin of paints he bought. 12

Function variable In C, a function is equivalent to a subroutine or function in Fortran, or a procedure or function in Pascal. A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient. C makes the sue of functions easy, convenient and efficient; you will often see a short function defined and called only once, just because it clarifies some piece of code. 13

Constant and identifier Constants in C refer to fixed values that do not change during the execution of a program. C supports several types of constants as illustrated in Fig Constant Numeric constant Character constant Integer constant Real constant Single character Constant String Constant 14

Integer constants An integer constant refers to a sequence of digits. There are three types of integers, namely decimal, octal and hexadecimal. Descriptions Decimal integers consist of a set of digits, 0 through 9, preceded by an optional or +. An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0. A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A through F or a through f. The letters A through F represent numbers 10 through 15. Examples 12, -546, 0, 354647, +56 045, 0, 0567 0X6, 0 5B, 0Xbcd, 0X 15

Real constants Real constants Descriptions Example real(or floating point) constants. real(or floating point) constants. A real number may also be expressed in exponential(scientifi c) notation. The numbers containing fractional parts like 67.45 are called real(or floating point) constants. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separating the mantissa and the exponent can be written in either lowercase or uppercase. 0.0045, -8.5, +345.678 7500000000 may be written as 7.5E9 or 75E8. 16

Character constants Print the letter a Descriptions Statement A single character constant( or simple character constant) contains a single character enclosed within a pair of single quote marks. printf( %d, a ); Character constants have integer values known as ASCII values. For example, the statement would print the number 97, the ASCII value of the letter a. Similarly, the statement printf( %c, 97); 17

Character Constants A character is a single letter from an alphabet. C defines two basic alphabets; one in which the source code is written and the one which is used when the program is run. They are usually the same, but they do not have to be. If they are different, it is the compiler's job to translate character constants from the source code alphabet to the runtime alphabet. The basic C alphabet contains the following characters: upper and lower case A-Z, decimal digits 0-9, the space, horizontal tab, vertical tab, newline, backspace, carriage return, alert and form feed characters and the following symbol characters:! " # % & ' ( ) * +, -. / : ; < = >? [ \ ] ^ _ { } ~ 18

String constants A string constant is a sequence of characters enclosed within double quotes. The characters may be letters, numbers, special characters and blank space. Examples: Hello!, 1947, 5+3 19

Data type In the C programming language, data types refers to an extensive system for declaring variables of different types. The language itself provides basic arithmetic types and syntax to build array and compound types. Several headers in the standard library contain definitions of support types, that have additional properties, such as exact size, guaranteed 20

Data type There are only a few basic data types in C: char int float a single byte, capable of holding one character in the local character set an integer, typically reflecting the natural size of integers on the host machine single-precision floating point double double-precision floating point 21

Variable declarations A declaration tells the compiler the name and type of a variable you'll be using in your program. In its simplest form, a declaration consists of the type, the name of the variable, and a terminating semicolon: char c; int i; float f; You can also declare several variables of the same type in one declaration, separating them with commas: int i1, i2; Later we'll see that declarations may also contain initializers, qualifiers and storage classes, and that we can declare arrays, functions, pointers, and other kinds of data structures 22

Why variables must be declared before use? 1. It makes things somewhat easier on the compiler; it knows right away what kind of storage to allocate and what code to emit to store and manipulate each variable; it doesn't have to try to intuit the programmer's intentions. 2. It forces a bit of useful discipline on the programmer: you cannot introduce variables willy-nilly; you must think about them enough to pick appropriate types for them. (The compiler's error messages to you, telling you that you apparently forgot to declare a variable, are as often helpful as they are a nuisance: they're helpful when they tell you that you misspelled a variable, or forgot to think about exactly how you were going to use it.) 23

Others declaration technique A declaration for a variable can also contain an initial value. This initializer consists of an equals sign and an expression, which is usually a single constant: int i = 1; int i1 = 10, i2 = 20; 24

Program 4.1: The following program illustrates typical declarations, assignments and values stored in various types of variables. main() { /*.DECLARATIONS..*/ float x, p; double y, q; unsigned k; /*.DECLARATIONS AND ASSIGNMENTS..*/ int m=54321; long int n=1234567890; 25

/*..ASSIGNMENTS */ x = 1.234567890000; y = 9.87654321; k = 54321; p=q=1.0; /*.PRINTING.*/ printf( m=%d\n,m); printf( n=%ld\n,n); printf( x=%.12lf\n,x); printf( x=%f\n,x); printf( y=%.12lf\n,y); printf( y=%lf\n,y); printf( k=%u p= %f q=%.12lf\n,k,p,q); } 26

Output m = -11215 n = 1234567890 x = 1.234567880630 x = 1.234568 y = 9.876543210000 y = 9.876543 k = 54321 p = 1.000000 q= 1.000000000000 Program 4.2: Program to calculate the average of N numbers #define N 10 /* SYMBOLIC CONSTANT */ main() { int count; /* DECLARATION OF float sum, average, number; VARIABLES */ sum = 0; / * INITIALIZATION OF count = 0; VARIABLES*/ while (count<n) { scanf( %f, &number); sum = sum + number; count = count + 1; } 27

average = sum / N; printf( N = % d Sum = %f, N, sum); printf( Average = %f, average); Output 1 2.3 4.67 1.42 7 3.67 4.08 2.2 4.25 8.21 N= 10 Sum= 38.799999 Average= 3.880000 28