My under eye areas maybe dark but my future will be bright and that is more important.
--Your friends at LectureNotes

Note for Programming in C - C by Bhubanjit Sahu

  • Programming in C - C
  • Note
  • Biju Patnaik University of Technology Rourkela Odisha - BPUT
  • Uploaded 10 months ago
0 User(s)
Order Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

C Programming Fundamentals C Programming Fundamentals Computer Charles Babbage (1792 – 1871) designed two machines for computation in between 1820 and 1850. One relied on the theory of finite differences and so he called it his Difference Engine. The other embodied many of the principles of a modern digital computer, and this he called his Analytical Engine. The Analytical Engine had an architecture which was remarkably similar to today’s computers. A computer is an electronic digital device. What is a Program The set of instructions or commands, which are given for the computer to do different activity or jobs or works, is called program. A program consists of a sequence of statements (instructions) in order to carry out a defined task. The set of instructions is called a program. Machine Language/ Low Level Programs All instructions to be executed by the computer must be in machine language. a) Binary codes are used to represent the operations instead of meaningful verbs. Therefore, it is not easy to program in machine language or to interpret machine language programs. b) Actual or relative memory addresses are used to represent data instead of meaningful data names. c) Program keeps track of memory addresses where data and instructions are stored. d) Machine Level program are difficult to modify and learn. High-level Programs(Example: Basic, C, C++, JAVA etc.) Programming languages were developed to enable the programmer to write in English. However these programs must be translated into machine language before execution. The translator program compiler or interpreter is used for this purpose. Steps in Programming process 1) Understand program specifications 2) Design a solution 3) Check the design 4) Design a code and compile the program 5) Test the program using test data 6) Integrate the system 7) Install the system 8) Maintain the system Characteristics of a good program 1) Accuracy: Sometimes, incorrect input, results in incorrect output despite the correct procedures being used. Therefore the program should provide a code for checking the validity of input. 2) Reliability: The program should continue to work accurately over a period of time. 3) Efficiency: The program should be able to manage the resources efficiently and ‘throughput’ should be good. 4) Ease of use: Proper documentation should be provided along with the source program. While executing the program, informative prompts should be displayed to help the user. 5) Ease of Modification: The structure of the program should allow any further modifications without having to change much for rest of the program. 6) Readability: For debugging and any further modifications, it is important that the program should be readable. Simple instructions should be used. Names of data items should be meaningful. Program should be properly indented. 1 K. L. Narayana

Text from page-2

C Programming Fundamentals Structured Programming (C is a Structured Programming Language) 1) A structured program is built by breaking down the program into smaller pieces called functions. It helps you to write cleaner code and maintain control over each function. 2) Each function has its own set of local variables and program code, to perform different tasks. 3) There is less need of global variables, which are now replaced by local variables that have smaller and more controllable scope. 4) A rich set of control structures are available, and user defined data types are introduced. 5) A new concept came into existence with structured programming is abstraction. Abstraction permitted the programmer to look at something without being concerned with its internal details. In a structured program it is enough to know which task is performed by function. It does not matter to the programmer how that task is performed. It makes development and maintenance of code fast as well as efficient. History of Programming Languages Year 1957 1958 1959 1960 1961 Language FORTRAN ALGOL LISP APL COBOL Name Derived from Latest Version Developed By Application FORmula TRANslation FORTRAN 77 IBM Science, Engineering ALGOrithmic Language ALGOL 68 International Group Science, Engineering LISt Processing LISP 1.6 MIT, USA Artificial Intelligence A Programming Language APLSV IBM Science, Engineering COmmon Business COBOL 85 Defence Dept. (USA) Business Purpose Oriented Language 1964 BASIC Beginner’s All purpose Standard BASIC Dartmouth College Science, Engg., Symbolic Instruction Business, Education Code 1965 PL/1 Programming Language 1 ANSI standard IBM General PL/1 1970 Pascal Blaise Pascal Standard Pascal Federal Institute of General Technology, Switzerland 1972 PROLOG PROgramming in LOGic Standard University of Marseille Artificial Intelligence PROLOG 1972 C Earlier language called B ANSI C AT & T Bell Labs General History of C C is a high level programming language. It is also called as a system programming language because most of the operating systems, applications and compliers were written in this language. By 1960 many computer languages have come into existence, almost each for a specific purpose. For example, COBOL for Commercial purpose, FORTRAN for Engineering and Scientific purpose and so on. At this stage people started thinking that instead of learning and using so many languages, each for a different purpose, why not use only one language which can program all possible applications. Therefore an international committee was set up to develop such a language. This committee came out with languages called ALGOL 60, CPL (Combined Programming Language) and BCPL (Basic Combined Programming Language by Martin Richards). Around same time a language called B was written by Ken Thompson at AT & T Bell Labs. The main Drawback of BCPL and B language are they are “typeless” language. To over come this C provides different data types. C was developed in the year 1972 by Dennis Ritchie at AT & T Bell Laboratory, Murray Hill, New Jersey, U.S.A. by inheriting the features of B and BCPL and with some added features. By the mid 1980’s the popularity of C had become widespread. Numerous C compilers and interpreters had been written for computers of all sizes and many commercial application programs had been developed. By 1973, C language had become powerful enough that most of the UNIX kernel was rewritten in C. C is now the foundation for all Unix/Linux operating systems. 2 K. L. Narayana

Text from page-3

C Programming Fundamentals Characteristics of C The compiler of the C was written in C. It not only supports the necessary data types and data structures needed by a normal programmer but it can also access the computer hardware through specially designed declarations and functions. It is popular because of the following characteristics: 1) Small in size, and is a general purpose language. 2) Wide use of functions and function calls. 3) Strictly typed language, i.e. variable needs to be declared before their use, but not a strongly typed language i.e. a variable of one type can be assigned to another type. 4) Bitwise low level programming support. 5) Wide use of pointers to access data structures and physical memory of the system. 6) It is quite suitable for system programming. 7) It is a flexible and powerful language. 8) It is a fast running, machine independent and efficient language. 9) C programming language can be applied to any economic problem with or without third–party tools. Compiler and Interpreter  A high-level language is one which is understandable by us humans.  It contains words and phrases from the English (or other) language. But a computer does not understand high-level language. It only understands program written in 0's and 1's in binary, called the machine code.  A program written in high-level language is called a source code.  We need to convert the source code into machine code and this is accomplished by compilers and interpreters. Compiler A compiler is a special program that processes statements written in a particular programming language called as source code and converts them into machine language or “machine code” that a computer’s processor uses. Compiler translates high level language programs directly into machine language program. This process is called compilation. Two Phase Compiler A C program is made by running a compiler which takes the typed source program and converts it into an object file that the computer can execute. A compiler usually operates in two or more phases. Phase 1 scans a source program, perhaps generating an intermediate code which helps to simplify the grammar of the language for subsequent processing. A separate object file is built for each source file. kln@linux:~> cc –c hello.c The above command will generate the object file hello.o Phase 2 is a Linker. This program links standard library code to object file so that the code is complete and can "stand alone". In this stage, the compiler can fail, if it finds that it has a reference to a function which does not exist. kln@linux:~> cc hello.o The above command will generate the a.out executable file 3 K. L. Narayana

Text from page-4

C Programming Fundamentals Run the Program. kln@linux:~> ./a.out You can also use the DIRECT COMMAND TO COMPILE C Codes.. kln@linux:~> cc hello.c The above command will generate the a.out executable file You can compile the program to get your own executable fiile. kln@linux:~> cc –o helloprog hello.c Here helloout is the executable file. RUN: Interpreter kln@linux:~> ./helloprog Interpreter An interpreter translates high-level instructions into an intermediate form, which it then executes. Compiled Programs generally run faster than interpreted programs. The advantage of an interpreter, however, is that it does not need to go through the compilation stage during which machine instructions are generated. This process can be time-consuming if the program is long. Difference between Compiler and Interpreter Compiler Compiler takes entire program as input Intermediate object code is generated Memory requirement: More (Because Object code is generated) Compiler generates the error report after the translation of the entire page, i.e. errors are displayed after entire program is checked. Compiler takes a larger amount of time in analyzing and processing the high level code. Execution time is faster. Interpreter Interpreter takes single instruction as input No intermediate object code is generated Memory requirement: Less an interpreter will stop the translation after it gets the first error, i.e. errors are displayed for every instruction interpreted (if any). Interpreter takes a small amount of time in analyzing and processing the high level code. Execution time is slower. Structure of a C Program C program is made up of functions, these functions are made up of declarations and statments surrounded by curly braces { }. The basic building blocks of the C programming are the functions. Every C program is a collection of one or more functions, written in some arbitrary order. Every program must contain at most one main( ) function other function definitions may precede or follow the main. The program execution will always begin from the main( ) function. Level 0 main( ) Level 1 function1( ) { } function2( ) { } function3( ) { } function4( ) { } Level 2 4 K. L. Narayana

Lecture Notes