Sunteți pe pagina 1din 12

Introduction to Computer and C language

1. Explain logical organization of computer with a neat diagram. Computer is an electronic device that accepts the data, processes data and produces the desired result.

CU ALU

I/P UNIT
MAIN MEMORY UNIT SECONDARY

O/P UNIT

Dotted lines indicate the control signals issued by Control unit. represent data or instructions. The four functions are carried out by basic functional units namely: 1. 2. 3. 4. Input Unit. Central Processing Unit. Memory Unit. Output Unit.

Input Unit: i. ii. iii. It is an external device that is connected to the computer system. The input unit is used to enter data or program (instructions) to the computer. The various input devices include keyboard, mouse, scanner, joystick etc.

Output Unit: i. The output unit display the processed data (result).

ii. iii.

The output displayed on the monitor is called the softcopy output and the output printed on the paper is called the hardcopy output. The various output devices include monitor, printer, speakers, plotters, LCDs, Plasma panel displays etc.

Note: Some devices act as both input and output devices such as Touch panel and modem(modulator and demodulator) Central Processing Unit: i. ii. iii. iv. The CPU acts as a Brain of the computer. It organizes and processes the data and instructions received from the input source such as keyboard or file. The CPU refers to the microprocessor of the computer. CPU consists of two units namely

a) Control Unit: i. The control unit controls and coordinates the activities of all the units by issuing proper commands. 1. Fetching data and instructions from memory. 2. Interpreting the instructions. 3. Controlling the transfer of data and instructions to and from memory. 4. Controlling the input and output devices. 5. The overall supervision of computer system b) Arithmetic and logic unit: i. ii. iii. iv. v. Actual operations are carried out under the supervision of control unit. The control unit issues signals to ALU to perform the following activities: Arithmetic unit consists of an electronic circuitry which performs basic arithmetic operations +,-,*, / and logical unit consists of logic circuitry which performs logical operations AND ,OR, NOT and relational operations. The results are send to memory unit and then to output unit.

Memory Unit: i. ii. The memory unit store data and instructions. This is also called as storage device

The memory unit does the following. Stores the instructions or data. Stores the intermediate results. Computer Memory is classified in to 2 types namely

Differences between: Primary Memory Also called as Main memory. Accessing the data is faster. CPU can access directly Semiconductor memory. Data storage capacity is less. Expensive. It is Internal memory. Examples : RAM, ROM Secondary Memory Also called as Auxiliary memory. Accessing the data is slow. CPU cannot access directly Magnetic memory. Data storage capacity is more or huge. Not expensive. It is External memory. Examples: hard disk, floppy disk, magnetic tape etc.

Two types of primary memory RAM Random Access Memory. Volatile memory. The contents of the RAM are lost when power is turned off. Temporary storage medium. The data can be read and written.

ROM Read Only Memory. Non-volatile memory. The contents of the ROM are not lost when power is turned off. Permanent storage medium. The data can only be read, but the data cannot be written. The programs are brought into RAM BIOS and monitor programs are stored. just before execution.

Hardware Software The physical components making up Software is a set of programs used the system are termed as Hardware. to perform certain tasks(logical component) The components include keyboard, Softwares include compliers, floppy drive, hard disk, monitor, CPU, loaders, Banking s/w, library s/w, printer, wires, transistors, circuits etc. payroll s/w etc. Hardware works based on instructions Software tell the hardware what to do

Software: It is a set of programs used to perform certain tasks. Program is set of instructions to carry out a particular task

Types of Software:
System software Collection of programs written by expert programmers/manufacturers. System software can be used to control the computer system. System software helps in executing other programs. Examples include compilers, loaders .Operating System etc. Programming Languages Program: Set of instructions to solve a problem . Programming Language: Set of keywords and grammar for instructing a computer to perform specific task. Computer programs are written in a programming language. Different types of Languages M/c Language : Consists of only zeros and ones, which computer can understand. It is m/c dependent. Assembly Language :Consists of symbols / mnemonics which constitute instructions, It is m/c dependent. High level Languages : it is simple and similar to natural language with some grammatical rules. It m/c independent C, C++, COBOL, Pascal, BASIC. Application software Collection of programs written by users (programmers). Application software is written to perform particular task. Application software are not used for executing other programs. Examples include Banking s/w, library s/w, payroll s/w etc

Differences between m/c and assembly lang


Machine Language Instructions are in the form of 0s & 1s Writing program, debugging understanding is difficult Assembly language Instructions are in the form of Mnemonics

andeasy

Translator is not required

Execution is faster

Translator(Assembler) is required to convert assembly language program to m/c language slower

Difference between high level and low level


High Level Language m/c independent Uses English like instructions Low level Language m/c dependent Uses mnemonics

Writing program, debugging, understanding is easy Difficult Compiler/ Interpreter is used to convert high levelAssembler is used to convert language to m/c language Assembly language to m/c language Execution is slower Token and Types of tokens Token is the smallest individual unit in a C program. There are 6 types of C tokens: -- Keywords. -- Identifiers. -- Constants. -- Operators. -- Strings. -- Special Symbols. Character set in C includes: Alphabets or Letters A-Z and a-z Digits 0-9 Special characters $,? ,{, ), \ etc. White spaces \t, \n etc. i. Keywords Keywords are basic building blocks. All keywords have fixed meaning and cannot be changed. Keywords must be written in lowercase. 32 keywords are available in c lang, Ex-auto, int ,switch , break , void , default etc. ii. Identifier It refer to the names of variables, functions and arrays. Execution is faster

Identifiers are user-defined names. It consists of a sequence of letters and digits. Both uppercase and lowercase letters are allowed First character must be an alphabet or underscore. Must consist of only letters,digits or underscore. Only first 31 characters are significant. Keywords cannot be used as identifiers. Should not contain white spaces. iii.Constants Constant is a fixed value that do not change during program execution. Types of Constants: 1. Numeric constants. i.Integer constant - An integer constant is a sequence of digits. Types of Integer constants: a. Decimal integer constants- sequence of digits within the range[0-9] with optional + or -.. Ex: 1234, -7766, 0 , +4646,324567 2.Octal integer constants- sequence of digits within the range[0-7] and number preceded by O Ex: 034, 01234 , 076 3. Hexadecimal constants - sequence of digits within the range[0-9] ,[A-F]/ [a-f], and number preceded by 0X or 0x. Ex: 0x34, 0X9ab , 0x6D ,0x 2. Real constants - used to represent quantities that vary continuously like distance, height, temperature, price etc. Real constants are also called as Floating point constants. Ex: 0.1527, -0.66, 555.89 etc. A real number can be expressed in exponential or scientific notation. General form is : mantissa e exponent where mantissa is either a real number or an integer. exponent is an integer number with optional plus or minus sign. Ex: 215.78 is written as 2.1578e2 0.00034 is written as 3.4E-4 2. Character constants i. A single character constant contains a single character enclosed within a pair of single quotation mark. Ex: 4, P, !, etc. Character constants have integer values known as ASCII (American Standard Code for Information Interchange ) values.

A-Z 65-90 a-z 97-122 0-9 48-57 ii. A String Constant is a sequence of characters enclosed in double quotes. iii. Characters may be letters, numbers, special characters and blank space. Ex: BVB, 1899, ?...!, H, 57-11 etc. Backslash Character Constants These constants are combinations of two characters. Also called as Escape sequences. They are used in output functions. Ex: \n (newline),, \t (horizontal tab), \0 (null) etc.

Variable : A variable is a data name used to store a data value. It is a name given to a memory location which stores a data value. Variable takes different values at different times during execution. Datatypesi. C has a rich set of data types. ii. Storage representation and machine instructions to handle constants differ from machine to machine. iii.The variety of datatypes available allow the programmer to select the type appropriate to the needs of the application as well as the machine. Definition :- We know that variables can have different values in memory during execution of a program.The type of data that a variable can store in the memory can be defined using datatype. C supports three classes of data types: i.Primary or fundamental data type.[int, float, double, char, void]. ii.Derived data type.[arrays, pointers]. iii.User-defined data type.[structure , union]. Data type 1.char 2.int 3.float 4.double Declaration of Variables Syntax: Size (bits) 8 16 32 64 format specifier %c %d %f %lf

data_type v1, v2, v3, vn ; where data_type can be any primary data_type. v1, v2, v3 names of variables. Ex: int count ; float num , x ; Structure of C program- It is nothing but the rules that are to be followed while writing a C program. The general structure of a C program is shown below Documentation section Link section main ( ) { declaration part; executable statements; } user defined functions Documentation section - It contains the explanatory note about the program. This will increase the readability. There are 2 types of documentation 1. External documentation 2. Internal documentation External documentation is done outside the main() function. It contains the purpose of the program, author of the program and date of creation. Internal documentation is done inside the main program or function. Its used to comment on the operation of particular instruction. There are two ways of commenting. 1. Multi line comment 2. Single line comment Syntax for multiline comment is: /*----------------------------------*/

For eg: /*Program to find the area of circle Author : xyz Date : --/--/---- */ Syntax for single line comment : //------------two numbers For eg: sum=a+b //Adds

Link section/Preprocessor directives- The preprocessor statements start with symbol #.These statements instruct the compiler to include some of the files in the beginning of the program. Ex: #include<stdio.h> #include<math.h> Are the files that the compiler includes into the user program.These two files stdio.h and math.h are called header files and hence the extension .h. Using the preprocessor directive the user can define the constants also Ex: #define MAX 1024 #define PI 3.142 Here MAX & PI are called symbolic constants. main()- Every C Program should have main(). There should be only one function by name main(). This is the first function to be executed always .The function main() may call other functions. The statements enclosed within left and right brace is called body of the function main(). Body of this function has 2 parts 1. Local declaration: It contains the declaration for variables required for processing in the main function. 2. Executable statements: These are nothing but the instructions in the program. Declaration section The variables that are used in the function main() should be declared in the declaration section.The variables can also be initialized . Ex: int sum=0; int a; float b; Executable section- These are the building blocks of a program .They represent the instructions given to the computer to perform a specific task . An instruction may represent an expression to be evaluated, i/o statements, simple assignment stmts,control statements etc.Each executable statement should end with semicolon. User defined functions The functions that are written by the user are called as user defined functions. Note : Procedure to create and execute c-program Create or edit the program. (command to open a file to write program is vi filename.c) Save the program. (command to save the program is esc,shift+: wq) Compile the program.

(command to compile program is cc filename.c) Execute the program. (command to execute or run the program is ./a.out )

Input and Output Functions Function is a set of instructions written to perform a particular task. Unformatted input/output functions: i.getchar( ) - used to read a single character syntax: variablename= getchar(); where variable name is of char data type. ii. putchar( ) used to print a single character syntax: putchar(variablename). /* Program to read a character and print the character on output screen */ #include<stdio.h> main() { char ch; ch= getchar(); /*read a character */ putchar(ch); /*display character */ } Formatted input/output functions: i. scanf ( ) ii. printf ( ) i. scanf- Syntax: scanf ( control string , &var1, &var2, --- &varn) ; where control string controls the format in which data is read. &var1,&var2 addresses of variables Write scanf functions for the following: i. Ans 78 88.377 int a=78; float b=88.377 scanf(%d%f, &a,&b); 90.34 N 34 float a=90.34; char b= N;

ii. Ans

int c=34; scanf(%f%c%d,&a,&b,&c); Note- dont use escape sequences in scanf statements. ii. printf-Syntax: printf ( control string , var1, var2, --- varn) ; where control string controls the format in which data is displayed. var1,var2 values of variables State the output produced by the following statements: Assume int x = 65789; float y = 345.11; char h = f ; i. printf( " %d\t%f\n ", x , y ); Ans: 65789 345.11 ii. printf( " \t\n%f\n%c ", y , h ); Ans: 345.11 f All these functions are present in a header file stdio.h (standard input output header file) Initialization Assigning values to the variables Two types of Initialization are as follows:1. Compile time initialization Assigning values to variables : /* Program to initialize value and print the value on output screen */ # include<stdio.h> main ( ) { int a =10; /* initialization of input data */ printf ( The value is = % d , a ) ; } Disadvantage of compile time initialization: To change the value of variable every time we need to make modification in the initialization statement. To overcome this disadvantage we have to use runtime initialization.

2. Run Time Initialization /* Program to read a value from keyboard and print the value on output screen */ #include<stdio.h> main() { int a; /* declaration of variable */ scanf (%d, &a); /*read value */ printf (%d, a); /* display the result */ }

1 Mark Questions with Answers


1. List two types of primary memory. A. RAM & ROM 2. List two categories of constants. A. Numeric & Character constants 3. List three programming languages. A. High level language, assembly level language & machine level language. 4. List the two types of character constants. A. Single character constants & string constants. 5. Give the syntax of Unformatted input function in C. A. char variable; variable=getchar(); 6. Define Identifier. A. Identifiers are user-defined names.It refer to the names of variables, functions and arrays. 7. List four primary data-types. A. int , float , double & char 8. Name the translator which is used to convert high level language to low level language. A. Compiler 9. Write the syntax for declaration of variable. A. datatype variable1, variable2,. variablen; 10. Give one example for a device which acts as both input & output devices.. A. Touch panel and modem(modulator and demodulator)

S-ar putea să vă placă și