Introduction to Programming with C-19CSE23 Module 1
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to programming with C-19CSE23 Module 1 Hardware Components Processor, memoryand Input/Output devices, are the important components of adigital computer. Processor will have ALU (Arithmetic Logic Unit) and Control Unit(CU) as its components. Memory can be subdivided into primary and secondary memory. Input devices accept data and control signals from the user. Examples of input devices are keyboard, mouse, pen based systems, data scanners, game controllers, voice recognition systems etc. Output devices communicate the processed data to the user. Examples of output devices are Monitor, Printer, Plotter, sound system etc. Processor Central Processing Unit (CPU) -It is the brain of the computer. It performs the bulk ofthe data processing operations. The function of the processor is to fetch the instructionfrom memory, examine (decode) the instruction and execute the instructions. It consists of Control unit, Arithmetic Logic Unit (ALU) and registers. Control unit is responsible for fetching the instructions from memory and interpreting them. ALU performs arithmetic and logical operations. Registers are very high speed memory unitsfor storing very small amount of data. Program counter, Instruction register, Memoryaddress register, memory buffer register and accumulator are some examples of registers.Bus is a collection of wires. They may be unidirectional or bidirectional. Bus is usedto connect different parts of a computer. Bus may be serial or parallel. USB is anexample of a serial bus. Bus connecting computer and a dot matrix printer is normallya parallel bus. Parallel bus carries several bits at a time. These bits may indicateinstruction, data, address or commands. Bus width and Bus speed are the two major components for performance measure of a computer. Memory In the memory, the information is stored in terms of bits or bytes or words. Byte ismade of 8 bits and word is a collection of 16, 32 or 64 bits. Memory can be volatile ornon volatile. Information present in Volatile memory is lost as soon as the power isturned off. Figure-1 gives the classification of memory devices in a digital computer. Secondary memories are non volatile in nature. Examples of secondary memoryinclude Hard disk, Pen drive, DVD-ROM, Recordable DVD,CD-RW, Blue-Ray,Magnetic tapes. Main memory devices are ones in which any memory location can beaccessed in any order (not necessarily in a sequential order). RAM (Random AccessMemory) and ROM(Read Only Memory) are the two types of main memory devices.RAM is also called Read-Write Memory. It is volatile memory. ROM is non- volatilememory. It is also considered an example of firmware. Cache memory is a memory placed between CPU and main memory. It contains a partof main memory content. Processor when needs some information, first looks in thecache. If not found in cache, the portion of memory containing the needed information ismoved to the cache and is also read by the processor. Both internal and external cache memories are volatile in nature. External cache is mounted on the motherboard. Registers are small memory units internally available within the processor. C Language : An Introduction C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973. It was mainly developed as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development. Many later languages have borrowed syntax/features directly or indirectly from C language. Like syntax of Java, PHP, JavaScript and many other languages is mainly based on C language. C++ is nearly a superset of C language (There are few programs that may compile in C, but not in C++). Basic Structure of a C program 1. Documentation section: The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later. 2. Link section: The link section provides instructions to the compiler to link functions from the system library such as using the #include directive. 3. Definition section: The definition section defines all symbolic constants such using the #define directive. 4. Global declaration section: There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions. 5. main () function section: Every C program must have one main function section. This section contains two parts; declaration part and executable part 1. Declaration part: The declaration part declares all the variables used in the executable part. 2. Executable part: There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program executionbegins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program. All statements in the declaration and executable part end with a semicolon. 6. Subprogram section: If the program is a multi-function program then the subprogram section contains all the user-defined functions that are called in the main () function. User-defined functions are generally placed immediately after the main () function, although they may appear in any order. All section, except the main () function section may be absent when they are not required. 2) Writing first program: Following is first program in C #include <stdio.h> void main() { printf("Welcome to C Programming!!"); } Output: Welcome to C Programming!! Let us analyze the program line by line. Line 1: [ #include <stdio.h> ] In a C program, all lines that start with # are processed by preprocessor which is a program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program. The produced program has no lines starting with #, all such lines are processed by the preprocessor. In the above example, preprocessor copies the preprocessed code of stdio.h to our file. The .h files are called header files in C. These header files generally contain declaration of functions. We need stdio.h for the function printf() used in the program. Line 2 [voidmain() ] There must to be starting point from where execution of compiled C program begins. In C, the execution typically begins with first line of main(). The void written before main indicates return type of main(). The value returned by main indicates status of program termination. Line 3 and 6: [ { and } ] In C language, a pair of curly brackets define a scope and mainly used in functions and control statements like if, else, loops. All functions must start and end with curly brackets. Line 4 [ printf(“Welcome to C Programming!!");”); ]printf() is a standard library function to print something on standard output. The semicolon at the end of printf indicates line termination. In C, semicolon is always used to indicate end of statement. Interesting Facts about Macros and Preprocessors in C In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program without any #. Following are some interesting facts about preprocessors in C. 1) When we use includedirective, the contents of included header file (after preprocessing) are copied to the current file. Angular brackets < and > instruct the preprocessor to look in the standard folder where all header files are held. Double quotes “ and“ instruct the preprocessor to look into the current folder and if the file is not present in current folder, then in standard folder of all header files. 2) When we use define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression. For example in the following program max is defined as 100. #include<stdio.h> #define max 100 int main() { printf("max is %d", max); return 0; } // Output: max is 100 // Note that the max inside "" is not replaced 4) The macro arguments are not evaluated before macro expansion. For example consider the following program #include <stdio.h> #define MULTIPLY(a, b) a*b int main() { // The macro is expended as 2 + 3 * 3 + 5, not as 5*8 printf("%d", MULTIPLY(2+3, 3+5)); return 0; } // Output: 16 Benefits of C language 1. As a middle level language, C combines the features of both high level and low level languages. It can be used for low-level programming, such as scripting for drivers and kernels and it also supports functions of high level programming languages, such as scripting for software applications etc. 2. C is a structured programming language which allows a complex program to be broken into simpler programs called functions. It also allows free movement of data across these functions. 3. Various features of C including direct access to machine level hardware APIs, presence of C compilers, deterministic resource use and dynamic memory allocation make C language an optimum choice for scripting applications and drivers of embedded systems. 4. C language is case-sensitive which means lowercase and uppercase letters are treated differently. 5. C is highly portable and is used for scripting system applications which form a major part of Windows, UNIX and Linux operating system. 6. C is a general purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations etc. 7. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.