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