C intro.pptx.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
Programming For Problem Solving http://www.free-powerpoint-templates-design.com Introduction to Computer Systems What is a Computer? The computer is an electronic device which operates under the control of instructions stored in its memory. A computer can tak...
Programming For Problem Solving http://www.free-powerpoint-templates-design.com Introduction to Computer Systems What is a Computer? The computer is an electronic device which operates under the control of instructions stored in its memory. A computer can take data from the user through input devices (Input), process the user given data (Processing), produces the result to the user through output devices (Output)and stores data (Information) for future use. Components of a computer What does a computer consist of? Every computer mainly consists of three things and those are... Hardware Software User ▪ Here the user interacts with the software, and the software makes the computer hardware parts to work for the user. What is Computer Hardware? All physical components of the computer are called as computer hardware. User can see, touch and feel every hardware of the computer. All hardware components perform any task based on the instructions given by the computer software. The computer hardware components are as follows... Input Devices - These are the parts through which a user can give the data to the computer. Output Devices - These are the physical components of a computer through which the computer gives the result to the user. Storage Devices - These are the physical components of a computer in which the data can be stored. Disk drive - helps users store and retrieve data from a disk, CD, floppy, etc., Cables - Various cables (Wires) are used to make connections in a computer. Other Devices - Other than the above hardware components, a computer also contains components like Motherboard, CPU (Processor), SMPS (Switched Mode Power Supply), Fans, etc., Input Devices Computer input devices are the physical components of the computer. Using input devices the user can give the data to the computer. Example Output Devices Computer output devices are the physical components of the computer which are used to give the computer result to the User. Using output devices, the user can see the computer-generated result. Example Storage Devices Computer storage devices are the physical components of the computer which are used to store data internally or externally. Example Disk Drives helps users store and retrieve data from a disk. Example Computer Cables In a Computer, various cables are used to make connections among the various hardware components of the computer. HDMI cable, VGA cable, DVI cable, Ethernet cable, PS/2 cable, 3.5 mm audio cable, USB cable, and computer power cord cable. Other Devices Other devices of the computer are shown below... CPU (Processor) Motherboard How does Computer work? When a user wants to communicate with the computer, the user interacts with an application. The application interacts with the operating system, and the operating system makes hardware components to work according to the user given instructions. The hardware components send the result back to the operating system, then the operating system forwards the same to the application and the application shows the result to the user. By using input devices, the user interacts with the application and the application uses output devices to show the result. All input and output devices work according to the instructions given by the operating system. How does Computer work? The working process of a computer is shown in the following figure. What is Computer Language? To make communication between two persons we need a language through which persons can express their feelings. To make communication between user and computer or between two or more computers we need a language through which user can give information to the computer and vice versa. When a user wants to give any instruction to the computer the user needs a specific language and that language is known as a computer language. The user interacts with the computer using programs and that programs are created using computer programming languages like C, C++, Java, etc., Computer languages are the languages through which the user can communicate with the computer by writing program instructions. Every computer programming language contains a set of predefined words and a set of rules (syntax) that are used to create instructions of a program. Computer Languages Classification Over the years, computer languages have been evolved from Low-Level to High-Level Languages. In the earliest days of computers, only Binary Language was used to write programs. The computer languages are classified as follows... Low-Level Language (Machine Language) Binary Language is an example of a low-level language or Machine Language or Machine Code. The binary language contains only two symbols 1 & 0. All the instructions of binary language are written in the form of binary numbers 1's & 0's. A computer can directly understand the binary language. Machine language is directly understands the binary language instructions, it does not require any translator. CPU directly starts executing the binary language instructions and takes very less time to execute the instructions as it does not require any translation. Low-level language is considered as the First Generation Language (1GL). Advantages A computer can easily understand the low-level language. Low-level language instructions are executed directly without any translation. Low-level language instructions require very less time for their execution. Disadvantages Low-level language instructions are very difficult to use and understand. Low-level language instructions are machine-dependent, that means a program written for a particular machine does not execute on another machine. In low-level language, there is more chance for errors and it is very difficult to find errors, debug and modify. Middle-Level Language (Assembly Language) Instructions are created using symbols such as letters, digits and special characters. Assembly language is an example of middle-level language. In assembly language, we use predefined words called mnemonics. Binary code instructions in low-level language are replaced with mnemonics and operands in middle-level language. But the computer cannot understand mnemonics, so we use a translator called Assembler to translate mnemonics into binary language. Assembler is a translator which takes assembly code as input and produces machine code as output. Assembler is used to translate middle-level language into low-level language. Middle-Level Language (Assembly Language) Advantages Writing instructions in a middle-level language is easier than writing instructions in a low-level language. Middle-level language is more readable compared to low-level language. Easy to understand, find errors and modify. Disadvantages Middle-level language is specific to a particular machine architecture, that means it is machine-dependent. Middle-level language needs to be translated into low-level language. Middle-level language executes slower compared to low-level language. High-Level Language The high-level language is very similar to human languages and has a set of grammar rules that are used to make instructions more easily. Every high-level language has a set of predefined words known as Keywords and a set of rules known as Syntax to create instructions. The high-level language is easier to understand for the users but the computer can not understand it. High-level language needs to be converted into the low-level language to make it understandable by the computer. Compiler or interpreter to convert high-level language to low-level language. Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-level languages. All these programming languages use human-understandable language like English to write program instructions. These instructions are converted to low-level language by the compiler so that it can be understood by the computer. High-Level Language Advantages Writing instructions in a high-level language is easier. A high-level language is more readable and understandable. The programs created using high-level language runs on different machines with little change or no change. Easy to understand, create programs, find errors and modify. Disadvantages High-level language needs to be translated into low-level language. High-level language executes slower compared to middle and low-level languages. Understanding Computer Languages The following figure provides a few key points related to computer languages. The programming languages like C, C++, Java, etc., are written in High-level language which is more comfortable for the developers. A high-level language is closer to the users. Low-level language is closer to the computer. Computer hardware can understand only the low-level language (Machine Language). The program written in the high-level language needs to be converted to low-level language to make communication between the user and the computer. Middle-level language is not closer to both user and computer. Creating and Running C Program ▪ Popular programming languages like C, C++, Java, etc., use the compiler to convert high-level language instructions into low-level language instructions. ▪ A compiler is a program that converts high-level language instructions into low-level language instructions. ▪ Compiler performs two things, first it verifies the program errors, if errors are found, it returns a list of errors otherwise it converts the complete code into the low-level language. Create and execute C programs To create and execute C programs in the Windows Operating System. Use the following steps to create and execute C programs in Windows OS. Create and execute C programs Step 1: Creating a Source Code Source code is a file with C programming instructions in a high-level language. To create source code, we use any text editor to write the program instructions. The instructions written in the source code must follow the C programming language rules. Step 2: Compile Source Code The compilation is the process of converting high-level language instructions into low-level language instructions. On receiving a source file, the compiler first checks for the Errors. If there are any Errors then compiler returns List of Errors. If there are no errors then the source code is converted into object code and stores it as a file with.obj extension. Then the object code is given to the Linker. The Linker combines both the object code and specified header file code and generates an Executable file with a.exe extension. Create and execute C programs Step 3: Executing / Running Executable File After completing compilation successfully, an executable file is created with a.exe extension. The processor can understand this.exe file content so that it can perform the task specified in the source file..exe file is submitted to the CPU and performs the task according to the instruction written in the file. The result generated from the execution is placed in a window called User Screen. Step 4: Check Result After running the program, the result is placed into User Screen. Create and execute C programs Create and execute C programs The file which contains c program instructions in a high-level language is said to be source code. Every c program source file is saved with.c extension, for example, Sample.c. Compiler checks for the errors. If there are any errors, it returns a list of errors. Otherwise generates object code in a file with name Sample.obj and submit it to the linker. The linker combines the code from specified header file into an object file and generates executable file as Sample.exe. Then run the executable file (Sample.exe) and submit to the CPU. Then CPU performs the task according to the instructions written in that program and place the result into User Screen. Introduction to C Programming What is C? Computer programming language used to design computer software and applications. Why do we use C? To design computer software and applications. Who invented C? Invented in the year 1972 by Dennis Ritchie (Dennis MacAlistair Ritchie). He was an American Computer Scientist worked at Bell Labs as a researcher along with Ken Thompson. He was born on 9th September 1941 and lived till 12th October 2011. He is said to be the Father of C. Software used to create and execute a C Program ❖ Following are the applications and software used to create and execute C programs. Turbo C Turbo C++ GNU C Code Blocks Net Beans C Background C is a structured programming language. It is also known as function orientated programming language. In the year of 1972, the new language was introduced with the name “Traditional C”. The name 'c' was selected from the sequence of previous language ‘B’ (BCPL) because most of the features of 'c' were derived from BCPL (B language). The first outcome of the c language was the UNIX operating system. The initial UNIX OS was completely developed using 'c' programming language. The founder of the ‘C’ language, Dennis Ritchie is known as “Father of C” and also “Father of UNIX”. The c programming language is very popular because it is reliable, simple and easy to use and it is the base for almost all the other programming languages. Language before ‘c’ & various versions of ‘c’. 1. CPL (Common Programming Language) The CPL was invented by Martin Richards at the University of Cambridge in the early of 1960s. 2. BCPL (Basic Combined Programming Language) The BCPL was invented by Martin Richards at the University of Cambridge in the year of 1966. It was a popular programming language at that time. BCPL allows the user, direct access to the computer memory. BCPL is the extension of CPL. 3. B Language B language is derived from BCPL. It was introduced in the year of 1969 by Ken Thompson and Dennis Ritchie at Bell Laboratory, USA. The B language is similar to BCPL. Language before ‘c’ & various versions of ‘c’. 4. C Language C language is derived from the B language. It was introduced in the year of 1972 by Dennis Ritchie at Bell Laboratory, USA. The C language was mainly developed to create an operating system called UNIX. The name C is given based on the previous language B and BCPL. Ninety percent of the UNIX operating system code is written in C language. During the 1970s, the C language became a very popular programming language. Many universities and organizations began creating their version of C language for their respective projects. Language before ‘c’ & various versions of ‘c’. 5. ANSI C (C89) 1983, the ANSI (American National Standards Institute) formed a committee to frame standard specifications for the C language. 1989, this committee introduced a standard version of C with the name "ANSI C" with standard library files. The ANSI C is also called as C89 in short form. 6. C90 1990, the ANSI C was got ISO (International Organization for Standardization) standardization with the inclusion of a few new features like new library files, new processor commands. And it was also added with keywords const, volatile and signed, etc... ISO standardized ANSI C as ISO/IEC 9899:1990. This version is called as C90 in short form. 7. C99 1995, many new features were added to the C90 to create a new version of it. This new version of C was got ISO standardization in the year of 1999 with the name ISO/IEC 9899:1999. In the short form, it is called as C99. Later C99 became the official standard version of C. Structure of C Program C is a structured programming language. Every c program and its statements must be in a particular structure. Every c program has the following general structure... Structure of C Program Line 1: Comments - They are ignored by the compiler To provide a small description of the program. The comment lines are simply ignored by the compiler, that means they are not executed. All the comment lines in a C program just provide the guidelines to understand the program and its code. Two types of comments 1. Single Line Comments: Single line comment begins with // symbol. We can write any number of single line comments. 2. Multiple Lines Comments: Multiple lines comment begins with. We can write any number of multiple lines comments in a program. Comment lines are optional. Structure of C Program Line 2: Preprocessing Commands To include header files and to define constants. We use the #include statement to include the header file into our program. We use a #define statement to define a constant. The preprocessing statements are used according to the requirements. Line 3: Global Declaration The global declaration is used to define the global variables, which are common for all the functions after its declaration. Structure of C Program Line 4: int main() Every C program must write this statement. This statement (main) specifies the starting point of the C program execution. Here, int is a data type of a value that is going to return to the Operating System after completing the main method execution. If we don't want to return any value, we can use it as void. Line 5: Open Brace ( { ) Indicates the beginning of the block which belongs to the main method. In C program, every block begins with a '{' symbol. Line 6: Local Declaration To declare the variables and functions that are local to the function or block in which they are declared. Structure of C Program Line 7: Executable statements To write the statements which perform tasks like reading data, displaying the result, calculations, etc. All the statements in this section are written according to the requirements. Line 9: Closing Brace ( } ) Indicates the end of the block which belongs to the main method. In C program every block ends with a '}' symbol. Line 10, 11, 12,...: User-defined function() The user-defined function implementation can also be performed before the main method. We can define as many user-defined functions as we want. Every user-defined function needs a function call to execute its statements. General rules for any C program 1. Every executable statement must end with a semicolon symbol (;). 2. Every C program must contain exactly one main method (Starting point of the program execution). 3. All the system-defined words (keywords) must be used in lowercase letters. 4. Keywords can not be used as user-defined names(identifiers). 5. For every open brace ({), there must be respective closing brace (}). 6. Every variable must be declared before it is used. Thank You