Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)



Similar documents
Number Representation

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Pemrograman Dasar. Basic Elements Of Java

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

The programming language C. sws1 1

Chapter One Introduction to Programming

Informatica e Sistemi in Tempo Reale

Chapter 4: Computer Codes

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

Systems I: Computer Organization and Architecture

C++ Language Tutorial

Data Storage: Each time you create a variable in memory, a certain amount of memory is allocated for that variable based on its data type (or class).

Objective-C Tutorial

Numbering Systems. InThisAppendix...

Section 1.4 Place Value Systems of Numeration in Other Bases

The string of digits in the binary number system represents the quantity

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Introduction to Python

Ed. v1.0 PROGRAMMING LANGUAGES WORKING PAPER DRAFT PROGRAMMING LANGUAGES. Ed. v1.0

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Base Conversion written by Cathy Saxton

Chapter 2: Elements of Java

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Lecture 22: C Programming 4 Embedded Systems

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Sources: On the Web: Slides will be available on:

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

2011, The McGraw-Hill Companies, Inc. Chapter 3

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Storing Measurement Data

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Java CPD (I) Frans Coenen Department of Computer Science

3 Data Properties and Validation Rules

Specifications of Paradox for Windows

Chapter 2 Elementary Programming

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

HOMEWORK # 2 SOLUTIO

1 The Java Virtual Machine

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Instruction Set Architecture (ISA)

Introduction to Java. CS 3: Computer Programming in Java

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Useful Number Systems

Moving from CS 61A Scheme to CS 61B Java

CSI 333 Lecture 1 Number Systems

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Chapter 5 Names, Bindings, Type Checking, and Scopes

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

PL / SQL Basics. Chapter 3

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

How To Write Portable Programs In C

.NET Standard DateTime Format Strings

DNA Data and Program Representation. Alexandre David

The C Programming Language course syllabus associate level

Lecture 11: Number Systems

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Instructor Özgür ZEYDAN BEU Dept. of Enve. Eng. CIV 112 Computer Programming Lecture Notes (1)

McGraw-Hill The McGraw-Hill Companies, Inc.,

Variables, Constants, and Data Types

MS ACCESS DATABASE DATA TYPES

Example of a Java program

MACHINE INSTRUCTIONS AND PROGRAMS

Computer Science 281 Binary and Hexadecimal Review

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Memory Systems. Static Random Access Memory (SRAM) Cell

ERserver. DB2 Universal Database for iseries SQL Programming with Host Languages. iseries. Version 5

Introduction to Python

6.087 Lecture 2 January 12, 2010

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

6.170 Tutorial 3 - Ruby Basics

Java Crash Course Part I

Programming Languages & Tools

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Chapter 1: Digital Systems and Binary Numbers

OpenOffice.org 3.2 BASIC Guide

Computer Science 217

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Ch. 10 Software Development. (Computer Programming)

3 SOFTWARE AND PROGRAMMING LANGUAGES

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Chapter 7D The Java Virtual Machine

Introduction to UNIX and SFTP

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Transcription:

Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. C language is widely used in the development of operating systems. An Operating System(OS) is a software(collection of programs) that controls the various functions of a computer. Also it makes other programs on your computer work. For example, you cannot work with a word processor program, such as Microsoft Word, if there is no operating system installed on your computer. Windows, Unix, Linux, Solaris, and MacOS are some of the popular operating systems. Although C was designed for implementing system software, it is also widely used for developing portable application software. Powerful features, simple syntax, and portability make C a preferred language among programmers for business and industrial applications. What kind of language is C? C is a structured programming language, which means that it allows you to develop programs using welldefined control structures (you will learn about control structures in the articles to come), and provides modularity (breaking the task into multiple sub tasks that are simple enough to understand and to reuse). C is often called a middle-level language because it combines the best elements of low-level or machine language with high-level languages. History: In the early days of computers, the only programming languages available to programmers were two low level languages: machine and assembly language. Programming in these languages was tedious and time-consuming because the programmer had manage the hardware itself (memory, stack, interrupts, etc ).. When computers became increasingly available, many high level languages such as Fortran, Pascal, and Basic developed in order to make the underlying hardware invisible to the programmer and to make syntax easier; therefore making computers easier and faster to program. In 1972, the Unix Operating system was being developed. During this time, the concept of a system programming language having attributes of both low level and high level languages also developed. System programming languages have advantages of a high level language but allow the programmer to take control of the underlying hardware if desired. Brian W. Kernigahn and Dennis M. Ritchie developed C at Bell Laboratories as a system programming language. Their underlying goal was to develop a language that was simple and [Author: Gopal B. Vashistha] Page 1

flexible enough to be used in a variety of different processors. (Two early versions were named A and B.) In 1983, the American National Standards Institute formed a committee to produce a C programming language standard. This "ANSI C" was completed in 1988. Throughout its history, C has been closely associated with the UNIX operating system, with system programming in general, and with the challenge of writing "portable" code that can be easily transferred to any one of many target computers. Where is C useful? C s ability to communicate directly with hardware makes it a powerful choice for system programmers. In fact, popular operating systems such as Unix and Linux are written entirely in C. Additionally, even compilers and interpreters for other languages such as FORTRAN, Pascal, and BASIC are written in C. However, C s scope is not just limited to developing system programs. It is also used to develop any kind of application, including complex business ones. The following is a partial list of areas where C language is used: Ø Ø Ø Ø Ø Ø Ø Ø Embedded Systems Systems Programming Artificial Intelligence Industrial Automation Computer Graphics Space Research Image Processing Game Programming Getting equipped to write C programs The tools required for C programming are simple. All you need are: 1. A text editor: A text editor allows you to type in, modify, and save your program. Notepad in Windows, Edit in DOS, and vi and Emacs in Unix/Linux are some popular text editors. 2. A C compiler: A compiler is a program that converts the high-level language (HLL) program (referred to as source code) into machine language (object code). Borland C, GCC, and Microsoft C are some of the popular compilers. [Author: Gopal B. Vashistha] Page 2

Earlier text editors and compiler had to be used separately. However, today most software vendors provide them as a package called IDE (Integrated Development Environment). Popular IDE s include Turbo C from Borland Inc., and Microsoft C from Microsoft Inc. Why you should learn C? You should learn C because: C is simple. There are only 32 keywords so C is very easy to master. Keywords are words that have special meaning in C language. C programs run faster than programs written in most other languages. C enables easy communication with computer hardware making it easy to write system programs such as compilers and interpreters. Identifiers Identifiers are the names that are given to various program elements such as variables, symbolic constants and functions.variable or function identifier that is called a symbolic constant name. Identifier can be freely named, the following restrictions. Alphanumeric characters ( a ~ z, A~Z, 0~9 ) and half underscore ( _ ) can only be used. The first character of the first contain letters ( a ~ z, A~Z ) or half underscore ( _ ) can only be used. Case is distinguishable. That is, word and WORD is recognized as a separate identifier. Reserved words are not allowed. However, part of an identifier reserved words can be included. Here are the rules you need to know: 1. Identifier name must be a sequence of letter and digits, and must begin with a letter. 2. The underscore character ( _ ) is considered as letter. 3. Names shouldn't be a keyword (such as int, float, if,break, for etc) 4. Both upper-case letter and lower-case letter characters are allowed. However, they're not interchangeable. 5. No identifier may be keyword. 6. No special characters, such as semicolon,period,blank space, slash or comma are permitted Examples of legal and illegal identifiers follow, first some legal identifiers: [Author: Gopal B. Vashistha] Page 3

float _number; float a; int this_is_a_very_detailed_name_for_an_identifier; The following are illegal (it's your job to recognize why): float :e; float for; float 9PI; float.3.14; float 7g; Example : Keywords Keywords are standard identifiers that have standard predefined meaning in C. Keywords are all lowercase, since uppercase and lowercase characters are not equivalent it's possible to utilize an uppercase keyword as an identifier but it's not a good programming practice. [Author: Gopal B. Vashistha] Page 4

Points to remember 1. Keywords can be used only for their intended purpose. 2. Keywords can't be used as programmer defined identifier. 3. The keywords can't be used as names for variables. The standard keywords are given below: Data Types C offers a standard, minimal set of basic data types. Sometimes these are called "primitive" types. A lot of complex data structures can be developed from these basic data types. The C language defines 4 fundamental data types: character integer floating-point and double floating-point This data types are declared using the keywords char,int,float and double respectively. Typical memory requirements of the basic data types are given below The size and range of these data types may vary among processor types and compilers. Data type qualifiers modify the behavior of variable type to which they are applied. Data type qualifiers can be classified into two types.two types. 1. size qualifiers 2. sign qualifiers Size qualifiers: Size qualifiers alter the size of the basic data types. There are two size qualifiers that can be applied to integer: short and long. The minimum size of short int is 16 bit. The size of int must be greater than or equal to that of a short int. The size of long int must be greater than or equal to a short int. The minimum size of a long int is 32 bits. [Author: Gopal B. Vashistha] Page 5

Sign qualifiers: The keywords signed and unsigned are the two sign qualifiers that specify whether a variable can hold both ve and +ve numbers, or only +ve numbers. These qualifiers can be applied to the data types int and char only. Example: unsigned int I; The following table shows all valid data type combinations supported by C, along with their minimal ranges and typical memory size. INT int An integer is a whole number (a number without a fractional part). It can be positive or negative numbers like 1, -2, 3, etc., or zero. The sizes of the integer variables depend on the hardware and operating system of the computer. On a typical 16-bit system, the sizes of the integer types are as follows. TYPE Bytes Possible Values le Values int 2 or 4-32,767 to 32,767 unsigned int 2 or 4 0 to 65,535 signed int 2 or 4-32,767 to 32,767 short int 2-32,767 to 32,767 [Author: Gopal B. Vashistha] Page 6

unsigned short int 2 0 to 65,535 signed short int 2-32,767 to 32,767 long int 4-2,147,483,647 to 2,147,483,647 signed long int 4-2,147,483,647 to 2,147,483,647 unsigned long int 4 0 to 4,294,967,295 Example: char char is a special integer type designed for storing single characters. The integer value of a char corresponds to an ASCII character. E.g., a value of 65 corresponds to the letter A, 66 corresponds to B, 67 to C, and so on. As in the table below, unsigned char permits values from 0 to 255, and signed char permits values from -127 (or -128) to 127. The char type is signed by default on some computers, but unsigned on others the sizes of the char types are as follows. Type Bytes Minimal range [Author: Gopal B. Vashistha] Page 7

char 1-127 to 127 unsigned char 1 0 to 255 signed char 1-127 to 127 Example: Float Floating point numbers are numbers with a decimal point. The float type can take large floating point numbers with a small degree of precision (Precision is simply the number of decimal places to which a number can be calculated with accuracy. If a number can be calculated to three decimal places, it is said to have three significant digits.) Memory size : 4 bytes Minimal range : IE-38 to IE+38 with six digit of precision Example: [Author: Gopal B. Vashistha] Page 8

Double Double-precision floating point numbers are also numbers with a decimal point. We know that the float type can take large floating point numbers with a small degree of precision but the doubleprecision double type can hold even larger numbers with a higher degree of precision. The sizes of the double types are as follows. Type Bytes Minimal range doubleprecision 8 IE-38 to IE+38 with 10 digit of precision long double 8 IE-38 to IE+38 with 10 digit of precision Example: [Author: Gopal B. Vashistha] Page 9

Constants The term constant means that it does not change during the execution of program.in the language C, constant and is the data with a constant value that does not change in the program. For example, in the program "100" "3.14" "'A'" "" Hello "" and the like, if you write data directly, and constant. Moreover, also called a literal constant. Constant expression is an expression consisting only of constants. There are four basic types of constants in C. They are: 1.Integer constants 2.Floating-point constants 3.Character constants 4.String constants Integer and floating-point constants represent numbers. They are often referred to as numerictype constants. The following rule applies to all numeric type constants: Comma and blank spaces cannot be included within the constants. Constants can be preceded by a or + sign, if desired. If either sign does not precede the constant it is assumed to be positive. The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from one C compiler to another. Integer constants Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits. Integer constants can be written in three different number systems: Decimal, Octal and Hexadecimal. [Author: Gopal B. Vashistha] Page 10

A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants. 0 1 1234-786 Example: The following decimal integer constants are written incorrectly for the reasons stated. 12,001 Illegal character (,). 103.0 Illegal decimal point (.). 10 20 30 Illegal character (blank space). 045 First digit cannot be zero. Example: [Author: Gopal B. Vashistha] Page 11

An octal integer constant can consist any combination of digits taken from the set 0 through 7. However, the first digit must be 0, in order to identify the constant as an octal number. The following are valid octal integer constants. 0 01 0125 0555 Example : The following decimal integer constants are written incorrectly for the reasons stated. 2010 Does not begin with 0. 2008 Illegal digit (8). 05.10 Illegal character (.). A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lowercase). The following are valid hexadecimal integer constants. 0X0 0x1 0XAB125-0x555 Example: [Author: Gopal B. Vashistha] Page 12

The following hexadecimal integer constants are written incorrectly for the reasons stated. 0x20.10 Illegal character (.). 02008 Does not begins with 0x or 0X. 0xabcdefg Illegal character (g). Floating-point constants A floating-point constant is a base-10 number that contains either a decimal point or an exponent or both. A floating-point constant can be written in two forms: Factorial form or Exponential form. A floating-point constant in a fractional form must have at least one digit each to the left and right of the decimal point. A floating-point in exponent form consists of a mantissa and an exponent. The mantissa itself is represented as a decimal integer constant or a decimal floatingpoint constant in fractional form. The letter E or e and the exponent follow the mantissa. The exponent must be a decimal integer. The actual number of digits in the mantissa and the exponent depends on the computer being used. The following are valid floating-point constants.</1.0 0.1 2E-4-0.1555e-4 The following floating-point constants are written incorrectly for the reasons stated.written incorrectly for the reasons stated. 1 No decimal point or exponent. 2,00.8 Illegal character (,). 2 E+10.20 Exponent must be an integer. 3 E10 Illegal character (space). Example: [Author: Gopal B. Vashistha] Page 13

Character constants A character constant is a single character, enclosed in single quotation marks. e.g., A B 1 Characters are stored internally in computer as coded set of binary digits, which have positive decimal integer equivalents. The value of a character constant is the numeric value of the character in the machine s character set. This means that the value of a character constant can vary from one machine to the next, depending on the character set being used on the particular machine. For example, on ASCII machine the value of A is 65 and on EBCDIC machine it is 193. Example: [Author: Gopal B. Vashistha] Page 14

String constants A string constant consists of zero or more character enclosed in quotation marks. Several string constants are given below. Welcome to C Programming a+b\n Error\a\a\a There is a difference between the constant A and A in C, the first A is a character constant while the second A is string constant. The notation A is a constant occupying a single byte containing the ASCII code of the character A. The notation A on the other hand,is a constant that occupies two bytes one for ASCII code of A and another for the null character with the value 0,that terminates all the string. Example: Variables Variables are means for location in memory used by a program to store data. The size of that block depends upon the range over which the variable is allowed to vary. For example, on personal computer the size of an integer variable is two bytes, and that of a long integer is four bytes. A variable region is temporarily remember a number or string value, such as covered by the program. To identify the variables, you have a name unique to every single variable. This is called a variable name. Before using a variable, use variables to what is called a variable declaration that you have to reveal the [Author: Gopal B. Vashistha] Page 15

names and data types that can be stored in the variable variable. The format for declaring a variable in C. [Storage-class] type data variable name [= initial value]; Storage class and the initial value can be omitted. The same data type and storage class variable can be declared, separated by commas. [Storage-class] type data variable name [= initial value] variable [= initial value] variable [= initial value]; In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type character with the name i by writing: char i; On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character. It also sets up a symbol table. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Thus, later if we write: i = 'x'; we expect that,at run time when this statement is executed, the value 'x' will be placed in that memory location reserved for the storage of the value of i. Following are the rules for naming the variables: 1. All variables must be declared before they can appear in executable statement. 2. A declaration consists of a data type followed by one or more variable names separated by commas. Example: int a,b,c; 3. Variables can be distributed among declarations in any fashion. The above declaration can be written as int a; int b,c; 4. Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities. Example: short int a,b,c; long int a,b,c; [Author: Gopal B. Vashistha] Page 16

5. An integer variable can also be declared to be un signed by writing unsigned int. Example: Example: unsigned int; Expressions An expression is a sequence of operators and operands that specifies computation of a value. An expression may consist of single entity or some combination of such entities interconnected by one or more operators. All expression represents a logical connection that's either true or false. Thus logical type expression actually represents numerical quantities. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of expressions are shown in the table given below. A+b 3.14*r*r a*a+2*a*b+b*b Example: [Author: Gopal B. Vashistha] Page 17

Symbolic constant in c Language A symbolic constant is name that substitute for a sequence of character that cannot be changed. The character may represent a numeric constant, a character constant, or a string. When the program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. They are usually defined at the beginning of the program. The symbolic constants may then appear later in the program in place of the numeric constants, character constants, etc., that the symbolic constants represent. For example, a C program consists of the following symbolic constant definitions. #define PI 3.141593 #define TRUE 1 #define FALSE 0 #define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is preprocessed, all occurrences of the symbolic constant PI are replaced with the replacement text 3.141593. Note that the preprocessor statements begin with a #symbol, and are not end with a semicolon. By convention, preprocessor constants are written in UPPERCASE. Example: 1 #include<stdio.h> #include<conio.h> [Author: Gopal B. Vashistha] Page 18

#define TRUE 1 #define PI 3.141593 void main() { float a; float b; float c; float d=pi; clrscr(); if(true) { a=100; b=a*10; c=b-a; } printf("\na=%f\nb=%f\nc=%f\npi=%f",a,b,c,d); getch(); } Example: 2 [Author: Gopal B. Vashistha] Page 19