Fundamentals of C Programming Notes presented by : Professor Piyush S. Indorkar
Unit 1 : Introduction to Programming Introduction to C language Before learning how to write programs in C it would be important to know a brief history, features, structure and applications of C language. In this section, the attempt is made to present this information in short.
History of C Language The origin of C is closed related with the development of UNIX operating system for PDP-7 computers. UNIX operating system was originally written by Ritchie and Thompson using assembly language. Even for PDP-11, the operating system was developed using assembly language. Developers were planning to rewrite OS using the B language using the Thompson’s simplified version of BCPL (Basic Combined Programming Language).
Features of C Programming
1. Simple : Every c program can be written in simple English language so that it is very easy to understand and developed by programmer. 2. Portability : C programs are portable. It is the concept of carrying the instruction from one system to another system. When we write and compile any C program on window operating system that program easily run on other window based system.
3. Powerful : C is a very powerful programming language, it have a wide verity of data types, functions, control statements, decision making statements, etc. 4. Structure oriented : C is a Structure oriented programming language.Structure oriented programming language aimed on clarity of program, reduce the complexity of code, using this approach code is divided into sub-program/subroutines.
5. Modularity : It is a case sensitive programming language. In C programming 'break and BREAK' both are different It is concept of designing an application in subprogram that is procedure oriented approach. In c programming we can break our code in subprogram. For example we can write a calculator programs in C language with divide our code in subprograms.
6. Middle level language : C programming language can supports two level programming instructions with the combination of low level and high level language that's why it is called middle level programming language.
Computational thinking
What is computational thinking? Computers can be used to help us solve problems. However, before a problem can be tackled, the problem and the ways in which it could be solved needs to be understood. Computational thinking allows us to do this. It allows us to take a complex problem, understand what the problem is and develop possible solutions.
These solutions can then be presented in a way that a computer, a human, or both, can understand. Thinking computationally is not programming. It is not even thinking like a computer! Simply put, programming tells a computer what to do and how to do it. Computational thinking enables us to work out exactly what to tell the computer to do.
For example, if you agree to meet your friends somewhere you have never been before, you would probably plan your route before you step out of your house. You might consider the routes available and which route is ‘best’ - this might be the route that is the shortest, the quickest, or the one which goes past your favourite shop on the way. You’d then follow the step-by-step directions to get there.
In this case, the planning part is like computational thinking, and following the directions is like programming! Being able to turn a complex problem into one we can easily understand is a skill that is extremely useful. In fact, it’s a skill you already have and probably use every day.
Let’s say you need to decide what to do with your group of friends. If all of you like different things, you would need to decide : What you could do Where you could go Who wants to do what What have you previously done that has been a success in the past How much money you have and the cost of any of the options How much time you have
From this information, you and your friends could decide more easily where to go and what to do – in order to keep most of your friends happy. You could also use a computer to help you to collect and analyze the data to devise the best solution to the problem, both now and even in a future scenario.
This brings us to the four key techniques of computational thinking: 1 . Decomposition - breaking down a complex problem or system into smaller, more manageable parts (e.g. where to go, how to complete the level) 2 . Abstraction - focusing on the important information only, ignoring irrelevant detail (e.g. weather, location of exit )
3. Pattern recognition – looking for similarities among and within problems (e.g. knowledge of previous similar problems used) 4 . Algorithms - developing a step-by-step solution to the problem, or the rules to follow to solve the problem (e.g. to work out a step-by-step plan of action)
Purpose of CT A complex problem is one that, at first glance, we don't know how to solve easily. Computational thinking involves taking that complex problem and breaking it down into a series of small, more manageable problems ( decomposition ).
Each of these smaller problems can then be looked at individually, considering how similar problems have been solved previously ( pattern recognition ) and focusing only on the important details, while ignoring irrelevant information ( abstraction ). Next , simple steps or rules to solve each of the smaller problems can be designed ( algorithms ). Finally, these simple steps or rules are used to program a computer to help solve the complex problem in the best way.
Logical Thinking Logic is a way of reasoning and evaluating arguments or statements systematically and rationally to determine their validity or truthfulness. It involves using rules and principles of reasoning to draw conclusions and make inferences based on given information or premises.
Logic provides a framework for critical thinking and helps us analyze and evaluate the consistency, coherence, and validity of arguments and reasoning, allowing us to arrive at sound and reliable conclusions . In its simplest form, logic is a tool for making sense of and drawing reliable conclusions from information and reasoning in a structured and coherent manner.
Logical reasoning is a fundamental skill that plays a crucial role in programming. It involves using systematic and structured thinking to analyze problems, design solutions, identify and fix errors, optimize performance, and ensure the correctness and reliability of software systems.
CT and Problem Solving Strategies At the heart of programming is problem-solving. Logical reasoning helps programmers break down complex problems into smaller, more manageable components and devise practical solutions.
It involves analyzing the requirements of a problem, understanding the relationships between different elements, and devising a plan to solve the problem logically and efficiently. Logical reasoning helps programmers approach problems with a structured mindset, enabling them to think critically and develop creative solutions.
1. The steps of problem solving techniques include defining the problem, formulating a mathematical model, developing an algorithm using a flowchart or pseudocode , writing code, and testing the program. 2 . A program consists of a series of instructions and fixed data to perform required operations. The program development cycle involves problem analysis, design, coding, compilation and execution, debugging and testing, and documentation.
3. An algorithm is a finite sequence of steps to solve a problem. Flowcharts use graphical symbols to represent the steps of an algorithm and show the program logic through connections between these symbols.
Algorithm for program writing The algorithm : An algorithm is a finite sequence of instructions which can be carried out to solve a particular problem in order to obtain the desired results. A finite sequence of instructions. The program must be planned before writing.
Represents the logic of the program Instructions are to be written in the proper sequence. Expressed in the programming language, becomes the program. Characteristics of an algorithm An algorithm must satisfy the following criteria: 1) Input : Zero or more quantities are externally supplied as inputs. 2) Output : At least one quantity as output is produced.
3) Finiteness : An algorithm terminates after a finite number of steps. 4) Definiteness : Each instruction must be clear and unambiguous. 5) Effectiveness : Every instruction must be very basic so that it can b carried out in principle, by a person using just a pencil and paper.
Flowcharts A flowchart is a pictorial representation of an algorithm. Flow chart consists of set of flow chart symbols connected by arrows. Each symbol has its own meaning, tells to compiler what must be done at that point. The sequence of flow chart symbols can be considered as a program.
Flow charts are better way of communicating logic of system. Problem can be analyzed in effective way with the flowchart. If the algorithms are represented in a graphical or pictorial form they are easy to understand. Efficient coding acts as Guide or Blue print during system analysis or development phase.
Top-down structured programming Top–down approaches emphasize planning and a complete understanding of the system. It is inherent that no coding can begin until a sufficient level of detail has been reached in the design of at least some part of the system.
Top–down is a programming style, the mainstay of traditional procedural languages, in which design begins by specifying complex pieces and then dividing them into successively smaller pieces. The technique for writing a program using top–down methods is to write a main procedure that names all the major functions it will need. Later , the programming team looks at the requirements of each of those functions and the process is repeated.
These compartmentalized subroutines eventually will perform actions so simple they can be easily and concisely coded. When all the various subroutines have been coded the program is ready for testing.
Types of Program errors Every programmer encounters programming errors while writing and dealing with computer code. But what is an error? In computer programming, it is a flaw resulting in the abnormal behavior of a program. In other words, it is a mistake in a program that causes it to produce an unexpected outcome. Encountering and dealing with errors are common aspects of the development process.
Four types of errors to be learned : 1. Syntax Errors : Human languages have rules that define the way of speaking and writing. We refer to the rules as grammar. The same applies to programming languages . But we refer to those rules as the syntax in the programming context. Syntax is the set of rules defining a programming language's structure.However , humans can communicate with less strict grammar. This is not the case with computer languages. They strictly follow their syntaxes. A single mistake in the syntax results in a syntax error.
2. Logical errors : Imagine you wrote a syntactically correct code. But, when you run it, you get a different output than expected. It is very tricky to track down what has gone wrong. Isn't it? This is what a logical error is. It is a kind of runtime error that forces a specific program to produce unanticipated output. Sometimes, it may result in the crashing of a program while executing.
Logical errors in a program are tricky to identify. Though you have written the correct syntax, your program behaves differently. It means you have programmed a computer to perform the wrong things. It happens when you don't know the requirements beforehand.
3. Runtime error : Often referred to as bugs, they prevent a program from executing completely. This means they prevent you from using a specific application or computer. The logical error is a variant of it. A program looks perfect syntax-wise but highlights errors only during the execution or runtime. You do not receive them at the compilation step.
Debugging errors : Debugging is the process in which a developer (who could be the author or not of the code he is trying to debug), using various tools available to him, makes an effort to change part of a previously written code in order to get rid of an error that was not caught at the compile (or syntax checking) time (in other words that has surfaced at the run time subject to certain conditions.
Reference websites Name : INFLIBNET Centre , Unit 1 : Introduction to C language.