Você está na página 1de 13

UNIT-1

CHAPTER1

FUNDAMENTALS OF PROBLEM SOLVING


Creating and running programs
Programmer

Text editor

#include<stdio.h>

-----------------------

Source

Compiler

0011010010 1110110110
Object

Linker

0101010101 0101010101

Executable

Library

Runner

__________ __________ __

Result

There are four steps in this process 1) Writing and editing the program. 2) Compiling the program. 3) Linking the program with the required library modules. 4) Executing the program. 1) Writing and editing the program: The software used to write the program is known as text editor. The text editor helps to enter, change and store character data. After completing the programming it is saved on disc. This file will be input to the compiler. It is known as source file. 2) Compiling programs: The code in a source file stored on the disc must be translated into machine language. This is the job of compiler. The compiler has two separate programs: the preprocessor and the translator. The preprocessor reads the source code and prepares it for the translator. While preparing the code, it scans preprocessor commands. After preprocessor has prepared the code for compilation, the translator does the actual work of converting the program into machine language. The translator reads the translation unit writes the result in object module to a file that can be combined with other precompiled units to form the final program. An object module is the code in machine language. 3) Linking the program: Required C library files are attached to the program by the linker. The linker assembles all of the functions & systems, to form final executable program. 4) Executing the program: When the program is ready for execution, the operating system command, such as run, is used to load the program into primary memory & execute it. Getting the program into primary memory is the function of operating system program known as loader. When everything is loaded the program takes control & begins execution. Computer concepts & C programming notes by: B Vimalesh Rao 1

During the execution of the program the program reads the data for processing either from the user or from a file. After the program processes the data, it prepares the output. Data output is given to the user either through the monitor or saved in a file. When the program has finished its job, it tells the operating system, which then removes the program from memory. System development life cycle: System development life cycle is a model of steps required to develop a software that begins with the need for software & concludes with its implementation. Very popular development life cycle is waterfall model. Waterfall model: Waterfall model is a system development life cycle in which each phase of development such as requirement, analysis, & design are completed before the next phase starts. System requirement: It is a step in which the programmer has to understand the needs of the user. Analysis: There are different methods to solve a program. In this step the programmer has to select an efficient method to solve the given program. Design: The programmer has to produce the rough design of the program.

System Requirement

Analysis

Design

Code

System Test

Maintenances

Program development: Program development is a multi step process that requires understanding the problem, develop a solution, writing the program & then testing it. Program testing: Program testing is the process that validates the program operation & verifies whether it meets its design requirements. There are two types of program testing: 1) Black box testing 2) White box testing Black box testing is the concept of testing the program without knowing how it works.

Computer concepts & C programming notes by: B Vimalesh Rao

White box testing is the concept of testing the program by the tester who knows everything about the program operations. White box testing is more complicated than black box testing. There are three type of designing the program 1) Algorithm 2) Pseudo code 3) Flowchart Algorithm: Algorithm is the step by step description of the program logic where each step is numbered in hierarchical order. Pseudo code: Pseudo code is an outline of a program which is expressed in a formally styled neutral language in compare to programming language. OR English like statements that follows a loosely defined syntax & are used to convey the design of algorithm is known as pseudo code. Flowchart: Flowchart is a program designing tool in which standard graphical symbols are used to represent logical flow of data through a function. Oval represents terminal points.

Parallelogram indicates input and output.

Rectangle indicates the processing steps. Arrows shows flowchart directions & connects the various flowchart symbols. Diamond indicates decision & questioning process.

Circle shows continuation from one point in the process flow to another.

*******************************

Computer concepts & C programming notes by: B Vimalesh Rao

CHAPTER 2 INTRODUCTION TO C LANGUAGE History of C language: ALGOL was the first programming language to use a block structure. ALGOL never gained wide acceptance in United states, but widely used in Europe. Several obscure languages preceded the development of C. In 1967, Martin Richards developed a language known as Basic Combined Programming Language (BCPL). In 1970, Ken Thompson developed a similar language known as B. Finally in 1972, Dennis Ritchie developed C, which took many concepts from ALGOL, BCPL, B & added the concept of data types. C is a structured, procedural programming language used to develop portable application software. Features of C: a) It is a structured programming language. b) Can be compiled & executed on any computer. c) Provides various data types such as int, float, char etc. d) Helps to develop efficient programs and system software. e) Provides various operators such as arithmetic, increment, decrement, & relational. Basic components of C language are: Character sets Tokens Character sets: In C language, character set refers to a set of characters defined in one or more source file. They o o o are classified into three types: Alphabets Digits Special characters A to Z a to z 0 to 9

Alphabet characters Upper case Lower case Digits Decimal digits Special characters Comma Period Semicolon Colon Question mark Apostrophe Quotation mark Underscore Hash/Number sign Dollar sign Percentage sign Pipeline character

, . ; : ? _ # $ % |

Computer concepts & C programming notes by: B Vimalesh Rao

Slash Back slash Ampersand Tilde Caret Asterisk Plus sign Minus sign Less than sign More than sign Left parenthesis Right parenthesis Left bracket Right bracket Left brace Right brace

/ \ & ~ ^ * + < > ( ) [ ] { }

Tokens: The token referred to the smallest unit of a C program. Tokens are the text provided in the source program that cannot be broken down into elements. Types of tokens are: Keywords Identifiers Constants Strings Operators Keywords: In C language keywords are the reserved words having special meaning to the C compiler. They cannot be used to name the variables or functions. Example: auto, break, else, const etc. Identifiers: Identifiers are the names provided to the elements of the program, such as variable, functions & arrays. Rules for identifiers: 1) An identifier may consist a letter, a number or an underscore. 2) An identifier must not have any special character or punctuation symbol, except underscore. 3) An identifier must not have two consecutive underscores. 4) First 63 characters of an identifier are significant. 5) An identifier cannot have C reserved keywords. 6) Identifiers are case sensitive i.e., salary, Salary & SALARY are three different identifiers. Constants: Constants are data values that cannot be changed during the execution of the program. It can be a number, character or a character string that may be used in a program. String: The string literals are the characters from source character set & are enclosed in double quotes. Operators: The operators are used to perform some operations. The operator acts upon the operands.

Computer concepts & C programming notes by: B Vimalesh Rao

Constants There are four types of constants: Integer constant Floating point constant Character constant String constant Integer constant: Integer constant represents a whole number without decimal point. It can be prefixed with positive or negative sign.
Sign Digits

Example: 102, -58, 564etc. Floating point constant: Floating point constant represents a number with decimal point. It has an integral part, a decimal point & a fractional part.
Sign Integer part Decimal Fractional part

Example: 102.0254, -98.2354, 2.02 etc. Character constant: Character constant represents a single character enclosed within a pair of single quotes. Example: c , S, A etc. String constant: String constant represents a sequence of characters enclosed with in a pair of double quotes. Example: summary, XXX , Helloetc. Different ways of coding a constant There are thee ways of coding a constant 1) Literal constant 2) Defined constant 3) Memory constant Literal constants: Literal constants are unnamed constants used to specify data. If we know that the data cannot be changed, then we can simply code the data value itself in a statement. Example: c=a+5; Area=3.14*radius*radius; Defined constants: This is another way of coding a constant, in which the constant is defined in the preprocessor command. The define commands are usually placed at the beginning of the program, although they are legal anywhere. Placing them at the beginning of the program makes them easy to find & change. Example: #define pai 3.14 #define tax 0.50 Memory constant: Memory constants use a C type qualifier, const, to indicate that the data cannot be changed. Its format is ; const data type identifier=value; Examples: const float pai=3.14; const float tax=0.50; Variables: Variables are named memory locations that have a type, such as integer or character. Each variable in a program must be declared & defined. To declare a variable first we specify the type, which automatically specifies its size & then its identifier. Computer concepts & C programming notes by: B Vimalesh Rao 6

Example: float price;


Variables type Variables Identifiers

int i; char code;

Variable initialization: The initializer establishes the first value that the variable will contain. To initialize a variable when it is defined, the identifier is followed by the assignment operator (=) & then the initializer which is the value the variable is to have when the function starts. Example: int sum=0; Backslash constants: A backslash constant is always a combination of two characters where the first character is always the backslash (\) character & second character is one of the following: a, b, f, n, r, t, v, , , \, or 0. They are also known as escape sequence. \a - Represents system alarm (bell) \b - Represents back space. \f - Represents form feed. \n - Represents new line. \r - Represents carriage return. \t - Represents horizontal tab. \v - Represents vertical tab. \ - Represents double quote. \ - Represents single quote. \0 - Represents null character. \\ - Represents backslash character. Data types: C type

Void

Integral

Floating point

Derived

Boolean

Integer

Character

Real

Imaginary

Complex

A data type is defined as the type of data stored in a variable. Void type: The void is a data type which stores no value. So cannot perform any operation on a variable that is declared as void. Integral type: The C language has three integral types: Boolean, Character, & integer types. Boolean: Boolean type is designated by the keyword bool. A Boolean type can represent only two values true & false. Character: Character type is designated by a keyword char. Each character contains an integer value which is provided by ASCII Computer concepts & C programming notes by: B Vimalesh Rao 7

(American Standard Code for Information Interchange). 1byte of memory is allocated to store character data type. Integer: An integer is a number without decimal point. C supports different sizes of integer data type: shortint, int, longint, longlongint, Data type shortint int longint longlongint Memory allocated 2 bytes 2 or 4 bytes 4 bytes 8 bytes

Each integer size signed or unsigned. If the integer is signed, then one bit must be used for a sign (0 is + & 1 is -) Sizeof(shortint)< sizeof(int)<sizeof(longint)<sizeof(longlongint) Floating point type: The C standard recognizes three floating point types: real, imaginary & complex. Real type is always signed. Real: The real type holds the value that has an integral & fractional part. The C language supports three different sizes of real types: float, double, & longdouble. Data type float double longdouble Memory allocated 4 bytes 8 bytes 10 bytes

Sizeof(float)<sizeof(double)<sizeof(longdouble) Imaginary: An imaginary number is a real number multiplied by the square root of -1. There are three different sizes: float imaginary, double imaginary, longdouble imaginary. Complex: A complex number is the combination of real & an imaginary number. There are three different sizes: float complex, double complex Long double complex. Data type modifiers: Signed: It indicates that the value stored in a variable is capable of storing both positive & negative values. Unsigned: It indicates that the value stored in the variable is capable of storing only positive values. Long: It refers that in a C program you can increase the storage capacity of a variable from the storage capacity of the basic data type. Short: It refers that in a C program you can decrease the storage capacity of a variable from the storage capacity of the basic data type. Remember:

Computer concepts & C programming notes by: B Vimalesh Rao

\0

Null character Empty string

Structure of a C program: Preprocessor commands Global declarations int main (void) { Local declarations Statements } Other functions Preprocessor commands: Preprocessor commands come at the beginning of the program. All preprocessor commands starts with the Hash sign (#). Preprocessor command tells the compiler to include certain library files in the program. For example #include<stdio.h> is a preprocessor command which tells the compiler to include standard input output file to the program. Local declarations: The declaration in a function are known as local declarations because they are visible only to the function that contains them. Statement: Statement section follows the declaration section. It contains instruction to the computer that causes it to do something such as add two numbers.

*******************

Computer concepts & C programming notes by: B Vimalesh Rao

CHAPTER 3 Input Output Function Input/Output


Data source Key board File
Input text stream

Program

Data

Monitor File Data destination


Output text stream

Data

Input to the program is can be given through a keyboard or can be saved in a file. Output can be saved in a file or output can be given through the monitor. A stream is a source of data or destination for data. It is associated with a physical device such as terminal or with file stored in secondary memory. Input/Output functions Formatted input function: scanf( ) Formatted output function: printf( ) Unformatted input function: gets( ),getchar( ) Unformatted output function: puts( ),putchar( ) The printf( ) function (The formatted output function) The printf( ) function is a formatted output function in C that is used to display information on the screen. There are two parameters in a printf function. They are format string and list of data value. Format string: It consists text to be printed on the monitor which is enclosed in double quotes. Formatted string may also have escape characters and conversion specification. To insert data into the stream, we use a conversion specification that contains a start token (%), a conversion code and upto four optional modifiers. Format of the conversion specification is shown below:

Flag

Minimum width

Precision

Size

Code

Code: Conversion codes are used to describe the data type. Data type Code

Computer concepts & C programming notes by: B Vimalesh Rao

10

integer floating point character string

d f c s

Size: Size modifier is used to modify the type specified by the conversion code. There are four different sizes: h (short), l (longer), ll (longlong), & L (longdouble). Precision: Precision modifier will specify the number of decimal places to be printed. It has the format .m. If no precision is specified printf prints six decimal positions. Minimum width: Minimum width modifier is used to specify the minimum number of positions that are allocated to the output. Flag: The flag modifier is used for print modifications. *Justification: Justification flag controls the placement of a value when it is shorter than the specified width. Justification can be right or left. If there is no flag the value is right justified. To left justify the value the flag is set to minus. *Padding: Padding defines the character that fills the unused space when the value is smaller than print width. If padding flag is not defined the unused width is filled with spaces. If flag is zero the unused width is filled with zeros. Zero flag is ignored if the value is left justified, because placing zeros after a number may change its value. *Sign: Sign flag defines the presence or absence of a sign in a numeric value. When flag is set to a plus(+) signs are printed for both positive and negative values. If the flag is a space the positive numbers are printed with a leading space & negative numbers with a minus (-) sign. Flag type Justification Padding Sign Flag code None None 0 None + Formatting Right justified Left justified Space padding Zero padding Positive value: space Negative value: Positive value: + Negative value: -

The scanf() function (The formatted input function) The scanf() function is a formatted output function in C that is used to read information from standard input device such as keyboard. The format of the scanf() function is shown below. scanf(Format string, address list); Format string has a conversion specification. The conversion specification has a code & upto three optional modifiers.

Computer concepts & C programming notes by: B Vimalesh Rao

11

flag

Maximum width

size

code

Code: Codes are used to describe data typed. Data types code integer d floating point f character c string s Size: Size modifier is used to modify the type specified by the conversion code. There are four different types of modifiers: h(short), l(long), ll(longlong), & L(longdouble). Maximum width: The maximum width modifier specifies the maximum number of character that read for one conversion code. When maximum width is included the scanf reads until the scanf finds a whitespace character. Flag: There is only one flag for input formatting, the assignment suppression flag (*). The assignment suppression flag tells scanf that the next input field is to be read but not stored. Rules for using scanf function: 1) There must be a conversion specification for each variable to be read. 2) There must be a variable address of the proper type for each conversion specification. 3) Any character in the format string other than whitespace character or conversion specification must be exactly matched by the user during the input. 4) It is fatal error to end the format string with a whitespace character. Software engineering: Program documentation: Some of the comments are placed in a program these comments are helpful for a reader to understand the codes given by programmer. The compiler ignores these comments when it translates the program into executable code. There are two types of commands: 1) Block comments 2) Line comments 1) Block comments: A block comment is used when the comment will span several lines. The opening token is /* and the closing token is */. Everything between opening and closing token are ignored by the compiler. 2) Line comments: A line comment is uses two slashes (//) to identify a comment. This format does not require an end of comment token. The end of line automatically ends the comment. The programmer uses this format for short comments.

Computer concepts & C programming notes by: B Vimalesh Rao

12

Tips and common errors: 1) Well structured programs use global constants but not global variables. 2) Forgetting comma after the format string in read or write statement is a compile error. 3) Forgetting to close the format string in the printf or scanf statement is a compile error. 4) Not terminating a block comment with close token (*/) is a compile error. 5) Forgetting the address operator (&) on a scanf statement is a logic error or runtime error. 6) Using address operator with a variable in printf statement is usually a run time error. 7) Using an incorrect conversion code for the data type being read or written is run time error. 8) It is a fatal error to end the format string with a whitespace character. *******************

Computer concepts & C programming notes by: B Vimalesh Rao

13

Você também pode gostar