🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

SpectacularMaracas

Uploaded by SpectacularMaracas

Poornima College of Engineering

Tags

algorithms flowcharts programming

Full Transcript

**UNIT 1 INTRODUCTION TO C PROGRAMMING** **Algorithm** A sequence of steps to accomplish the task. Such a finite sequence of steps required to get the desired output is called an algorithm. It will lead to the desired result in a finite amount of time. Algorithm has a definite beginning and a def...

**UNIT 1 INTRODUCTION TO C PROGRAMMING** **Algorithm** A sequence of steps to accomplish the task. Such a finite sequence of steps required to get the desired output is called an algorithm. It will lead to the desired result in a finite amount of time. Algorithm has a definite beginning and a definite end, and consists of a finite number of steps. **Need an Algorithm** 1. Algorithms are necessary for solving complex problems efficiently and effectively.  1. They help to automate processes and make them more reliable, faster, and easier to perform. 1. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to do manually. 1. They are used in various fields such as mathematics, computer science, engineering, finance, and many others to optimize processes, analyze data, make predictions, and provide solutions to problems. **[Characteristics of an Algorithm]** - **Clear and Unambiguous**: The algorithm should be unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning. - **Well-Defined Inputs**: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not take input. - **Well-Defined Outputs:** The algorithm must clearly define what output will be yielded and it should be well-defined as well. It should produce at least 1 output. - **Finite-ness:** The algorithm must be finite, i.e. it should terminate after a finite time. - **Feasible:** The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some future technology or anything. - **Language Independent:** The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected. **Introduction to Flowcharts** Flowchart is a graphical representation of an algorithm. Programmers often use it as a program-planning tool to solve a problem. It makes use of symbols which are connected among them to indicate the flow of information and processing. **Symbols used in Flowchart Designs** 1. **Terminal:** The oval symbol indicates Start, Stop and Halt in a program's logic flow. A pause/halt is generally used in a program logic under some error conditions. Terminal is the first and last symbols in the flowchart. \   ![A green oval with black outline Description automatically generated](media/image2.jpeg) - **Input/Output:** A parallelogram denotes any function of input/output type. Program instructions that take input from input devices and display output on output devices are indicated with parallelogram in a flowchart. \   A green rectangle with black border Description automatically generated - **Processing:** A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting, multiplication and division are indicated by action or process symbol. \  ![A green rectangle with black border Description automatically generated](media/image4.jpeg) - **Decision** Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are indicated by diamond in flowchart. \   A green hexagon with black outline Description automatically generated **Advantages of Flowchart:** - Flowcharts are a better way of communicating the logic of the system. - Flowcharts act as a guide for blueprint during program designed. - Flowcharts help in debugging process. - With the help of flowcharts programs can be easily analyzed. - It provides better documentation. **Disadvantages of Flowchart:** - It is difficult to draw flowcharts for large and complex programs. - There is no standard to determine the amount of detail. - Difficult to reproduce the flowcharts. - It is very difficult to modify the Flowchart. **Example: draw a flowchart to input two numbers from the user and display the largest of two numbers** - **Draw a flowchart to input two numbers from the user and display the largest of two numbers** - - ![A diagram of a machine Description automatically generated](media/image6.jpeg) TYPES OF LANGUAGE 1. MACHINE LANGUAGE Machine language is a low-level programming language that is understood by computers. Machine language is made up of binary bits 0 and 1. Machine language is also known as machine codes or object code. **Features of Machine Language** - Machine language is a low level language. - Machine language consist of only 0 and 1 bits. - Machine languages are platform dependent. - It is nearly impossible to learn machine language for humans because it requires a lot of memorization. - Machine language is used to create and construct drivers as well. **Assembly Language?** Assembly language is a low-level language that helps to communicate directly with computer hardware. It uses mnemonics to represent the operations that a processor must do. Which is an intermediate language between high-level languages like C++ and the binary language. It uses hexadecimal and binary values, and it is readable by humans. **High-Level Language?** High-level languages are programming languages that are used for writing [programs or software](https://www.geeksforgeeks.org/difference-between-software-and-program) that can be understood by humans and computers. High-level languages are easier to understand for humans because they use a lot of symbols letters phrases to represent logic and instructions in a program. It contains a high level of abstraction compared to low-level languages. **Characteristics of High-Level Language** - **Abstraction:** High-level language is the language that hides the complex details of how computers work. This allows users to focus on what the program should do instead of how it will run on the machine or compiler. - **Human-Readable Syntsx:** A program written in high-level language contains words and symbols that are easier to understand by humans. This allows users to read and write code in a very simple way. - **Portability:** Programs written in a high-level language can be run on different types of computers without any changes. This means we can use the same code on various devices. - **Easy to Use:** High-level languages are user-friendly, with clear commands and helpful tools that make writing, and testing code easier. **ASSEMBLER** An assembler is a computer program that translates assembly language into machine code, allowing a computer to directly communicate with its hardware. The assembler\'s job is to convert assembler or assembly language code into machine code that the computer can then read and execute. **COMPILERS** A Compiler is a software that typically takes a high level language (Like C++ and Java) code as input and converts the input to a lower level language at once. It lists all the errors if the input code does not follow the rules of its language. This process is much faster than interpreter but it becomes difficult to debug all the errors together in a program.  A compiler is a translating program that translates the instructions of high level language to machine level language. A program which is input to the compiler is called a Source program. This program is now converted to a machine level language by a compiler is known as the Object code. **INTERPRETER** The software by which the conversion of the high-level instructions is performed line-by-line to machine-level language, other than compiler and assembler, is known as INTERPRETER. The interpreter in the compiler checks the source code line-by-line and if an error is found on any line, it stops the execution until the error is resolved. Error correction is quite easy for the interpreter as the interpreter provides a line-by-line error. ![](media/image8.png) **Windows (MinGW)** 1. Download turbo c and install. 2. In the turbo c installation Manager, select the C Compiler (system-32-base) and apply. 3. Add C:\\turbo \\bin to the **Environment Variables** under **System Variables** -\> **Path**. **C \"Hello, World!\" Program** **[DISPLAY HELLO WORLD]** \#include \ void main() { // printf() displays the string inside quotation printf(\"Hello, World!\"); } - **To compile a program without running it, hit F9.** - **To compile and run a program, hit ctrl-F9.** **C Comments** The comments in C are human-readable explanations or notes in the source code of a C program.  A comment makes the program easier to read and understand. These are the statements that are not executed by the compiler or an interpreter. **types of comments in C** In C there are two types of comments in C language: - Single-line comment - Multi-line comment 1. **Keywords** 1. **Identifiers** 1. **Constants** 1. **Strings** **Internal Linkage**: An identifier implementing internal linkage is not accessible outside the translation unit it is declared in. Any identifier within the unit can access an identifier having internal linkage. It is implemented by the keyword static. An internally linked identifier is stored in initialized or uninitialized segment of RAM. **External Linkage**: An identifier implementing external linkage is visible to every translation unit. Externally linked identifiers are shared between translation units and are located at the outermost level of the program. **GLOBAL VARIABLE IN C** The variables that are declared outside the given function are known as global variables. These do not stay limited to a specific function- which means that one can use any given function to not only access but also modify the global variables. **Advantages of Global Variable** - Global variables can be accessed by all the functions present in the program. - Only a one-time declaration is required. - Global variables are very useful if all the functions are accessing the same data. **Disadvantages of Global Variable** - The value of a global variable can be changed accidentally as it can be used by any function in the program. - If we use a large number of global variables, then there is a high chance of error generation in the program. **C Language Introduction** C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories. - Easy to learn - Structured language - It produces efficient programs - It can handle low−level activities - It can be compiled on a variety of computer platforms **DATA TYPES** The data type is a collection of data with values having fixed values, meaning as well as its characteristics**.** ![](media/image11.png) **Constants** [What is a constant in C?] ** Constant in C is a variable that cannot be modified once it is declared in the program. **The constants in C are the read-only variables whose values cannot be modified once they are declared in the C program. The type of constant can be an integer constant, a floating pointer constant, a string constant, or a character constant. In C language, the const keyword is used to define the constants. **Syntax to Define Constant** const *data\_type var\_name* = *value*; **Variables** A variable in C is a memory location with some name that helps store some form of data and retrieves it when required. We can store different types of data in the variable and reuse the same variable for storing some other data any number of times. Example int var; // integer variable char a; // character variable float fff; // float variables **There are 3 aspects of defining a variable:** 1. Variable Declaration 1. Variable Definition 1. Variable Initialization **1. C Variable Declaration** Variable declaration in C tells the compiler about the existence of the variable with the given name and data type. When the variable is declared, an entry in symbol table is created and memory will be allocated at the time of initialization of the variable. **2. C Variable Definition** In the definition of a C variable, the compiler allocates some memory and some value to it. A defined variable will contain some random garbage value till it is not initialized. **Example** int var; char var2; **3. C Variable Initialization** Initialization of a variable is the process where the user assigns some meaningful value to the variable when creating the variable. **Example** int var = 10; // *variable declaration and definition (i.e. Vairable Initialization)* **Literals** Literals are the actual values that are directly written into your code to represent specific data. They are used to provide initial values for variables, as operands in expressions, or as direct values in statements. **OPERATORS AND EXPRESSION** **What is a C Operator?** An operator in C can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables. The values and variables used with operators are called operands. So we can say that the operators are the symbols that perform operations on operands. **Types of Operators in C** 1. Arithmetic Operators 1. Relational Operators 1. Logical Operators 1. Bitwise Operators 1. Assignment Operators **Relational Operators** Relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. ![](media/image13.png)

Use Quizgecko on...
Browser
Browser