Unit 1 - C programming Notes PDF

Title Unit 1 - C programming Notes
Author Firoz Nazeer
Course C programming
Institution Mahatma Gandhi University
Pages 42
File Size 922.2 KB
File Type PDF
Total Downloads 80
Total Views 157

Summary

C programming Notes...


Description

S5BCom CA_Module 1

1

Module 1 – Basic Concepts of Programming Program A program is a group of closely related and interdependent activities. A computer program is a series of organized instructions that directs a computer to perform tasks. A programming language is a set of words, symbols and codes that enables humans to communicate with computers. Hundreds of programming languages exist today. Each language has its own standard or rules for writing the commands and/or instructions. Examples of programming languages are: BASIC (Beginner’s All Purpose Symbolic Instruction Code), Pascal, C, C++, Java etc PROGRAMMING LANGUAGES Since a digital computer is a programmable device, the user must instruct the computer according to his requirements. The programmer must use some languages that can be understandable by the computer. These languages are called programming languages. Machine level languages Digital computers work only on binary digits 1 and 0. So before anything to be done by the computer the user must provide data and his instructions in a format that can be understandable by the computer, i.e., in the binary format. The instructions in the binary format are called as machine language programs. A machine language instruction is represented as a string of binary digits, which identifies the operations to be performed and the data, etc. to be used. A user must convert all his programming requirements to the binary string. Each instruction in a machine language consists of an op-code and an operand. Op-code is the instruction and operand is the data on which the instruction to be operated. Assembly language Because binary notation is difficult to use, programmers started using symbolic codes for the registers and the instructions instead of actual address locations and binary instructions. The programs in these symbolic codes are called assembly language program. Assembly language and machine languages are called low level languages. The computer cannot directly execute symbolic instructions. So these instructions must be translated in to machine language. Assembler is a translator which convert Assembly language to machine language. High level language Writing programs in machine and assembly languages need a high level knowledge in the machine architecture. So ordinary people tends to stand away from the computers. In the fifties came a breakthrough in Dalbina Dalan

S5BCom CA_Module 1

2

computer programming with the introduction of high level language. A high level language is also a symbolic language but it has many advantages over assembly and machine languages. It uses many English like commands and conventional mathematical notations to make programming easier and quicker. A program in high level language must be translated into its equivalent machine language program before it is executed. A translator is used to translate a high level language program to machine language program. High level languages are also called problem-oriented or procedure oriented languages to indicate that they are geared towards the methods or logic to solve a problem and not oriented towards how it is executed in the machine. That is, where to store the values, how the registers work, how to control the input/output devices etc. The translators mainly used are 1.Compiler The compiler translates the whole source program to object program. the resulting executable is some form of machine- specific binary code 2.Interpreter It is also a translator.It takes one statement of the high level language program, translate it into machine language instructions and execute the resulting machine language instruction immediately. The input to an interpreter is a source program and output is the result of program execution and not object program. Various stages in program development Before a problem to be solved by a computer, it needs preparation. In spite of their impressive capabilities, computers still have to be told exactly what to do, in a step-by-step fashion. The process of achieving the required level of detail is called problem analysis. Problem analysis can be divided into several more or less distinct parts. 1.Problem identification and study: This step involves the identification of the problem and identification of the input, output and processing requirements of the problem. 2.Find a solution: Once the study is complete, the programmer can easily develop a solution to solve the problem. There may be even more than one solution to solve a' problem. In such cases the best solution is selected. In some cases no satisfactory solution method may be known. In this case a method must be developed, or the problem has to be simplified. 3.Develop an algorithm: When a solution method has been selected, it must be reduced to the level of detail that computer understand. This is usually done in stages. 4.Program coding: Once the algorithm is ready it must be coded in a high level language to execute it by the computer. Dalbina Dalan

S5BCom CA_Module 1

3

5.Program testing and debugging: After developing a program, it must be tested before the implementation of the program in the real application area. The program is tested by giving sample data ( test data ) and checked the output against the known results. If it is not matching, then the programmer must check the program for errors and debug the program till it gives the correct result. 6.Program implementation: Once the program is ready and error free, it can be implemented in the real application area. 7.Program maintenance: Once the program is implemented, the user may find more errors that was not visible in the test phase. These errors must -be eliminated whenever it occurs. In addition to this, after using the software, the user may require certain modification or additions to the program. These modifications and debugging must be carried out by the programmer throughout the life of the program. 8.Documentation: At each stage of the problem analysis the analyst must record each and every conclusion he reached. These records are useful for the future modifications and debugging of the program. ALGORITHMS An algorithm is the step-by-step procedure to solve a problem. It should explain the method to solve the given problem in precise instructions, the instructions are written mostly in plain English. An algorithm must be able to solve the given problem when it is converted into a computer language and executed. The following are the basic characteristics of an algorithm. 1. The algorithm must consist of one or more inputs. 2. It must produce at least one output. 3. The steps in algorithm must be very basic. Advantages of writing an algorithm before writing a computer program are as follows. 1. An algorithm can be written in plain English. So a person without any knowledge of computers can understand and write algorithms. 2. Algorithms can be written without considering the syntax and rules of a programming language 3. Program coding will be easier if it is coded following an algorithm. 4. The algorithm is independent of the language and type of the computer. Eg:Algorithm to find the sum of two numbers Dalbina Dalan

S5BCom CA_Module 1

4

Step 1: Start Step 2: Read 2 numbers as a, b. Step3: Compute sum=a+b. Step 4: Print sum. Step 5: Stop.

Algorithm to find the largest of 2 numbers Step 1: Start Step 2: read a,b Step 3:if a>b then print a is greater Else print b is greater Step 4: Stop Algorithm to find the largest of 3 numbers Step 1: Start Step 2: Read 3 numbers a, b, c Step3: If a> b and a>c then largest =a Else if b>a and b>c then largest =b else largest=c Step 4: Print largest Step 5: Stop Algorithm to find factorial of a number 5!=1*2*3*4*5 N

i=1,2,3,….,N

f=f*i

Step 1:Start Step 2: Read a number N Step 3: i=1 and f=1 Step 4: f=f*i Step 5:i=i+1; Step 6: Repeat steps 4 and 5 until i5 false 0

greater than. If the expression on the left is greater than the one on the right the result is 1. If the expression on the left is less than or equal to the one on the right the result is 0.

=

greater than or equal to. If the value on the left is greater than or equal to the one on the right you get 1, otherwise it is 0. Operator

Meaning

<

less than

>

greater than

=

greater than or equal to

==

is equal to

!=

not equal to

Dalbina Dalan

S5BCom CA_Module 1

17

Logical operators : The logical operators && and || are used when we want to test more than one conditions and make decisions. The three logical operators are: Operator

Meaning

&&

Logical AND

||

Logical OR

!

Logical NOT

a>b && a>c

age>65 || covid f dose>112 !(4>2) faslse

An example is: a>b && x==10 An expression of this kind, which combines two or more relational expressions, is termed as a logical expression or a compound relational expression. The logical expression given above is true only if a>b is true and x==10 is true. If either (or both) of them are false, the expression is false. Assignment operators : The assignment operator is used to assign a value to a variable. The assignment operator takes the expression on its right side and stores it in the variable on its left side. The assignment operator is the single equal sign ( = ). It has this general form: var = expression;

sum=a+b

In addition, C provides special assignment operators (shorthand assignment operators) that can be used to combine an arithmetic operation with an assignment They are:

Simple Symbol

Shorthand

Name Assignment +=

Addition Assignment

-=

Subtraction Assignment

*=

Multiplication Assignment

/=

Division Assignment

Assignment

a=a+b

a += b

a=a-b

a -= b

a=a*b

a *= b

a=a/b

a /= b

Dalbina Dalan

S5BCom CA_Module 1

%=

18

Modulus Assignment

a=a%b

a %= b

Increment and Decrement operators : The ++ and the – – are C’s increment and decrement operators. The increment operator increases its operand by one. The decrement operator decreases its operand by one. For example, this statement: x = x + 1; can be rewritten like this by use of the increment operator: x++; Similarly, this statement: x = x - 1; is equivalent to x--; Increment and Decrement operators are unary operators and they require variable as their operands. We use the increment and decrement statements in for and while loops extensively. These operators are unique in that they can appear both in postfix form, where they follow the operand as just shown, and prefix form, where they precede the operand. In the foregoing examples, there is no difference between the prefix and postfix forms. However, when the increment and/or decrement operators are part of a larger expression, then a subtle, yet powerful, difference between these two forms appears. In the prefix form, the operand is incremented or decremented before the value is obtained for use in the expression. In postfix form, the previous value is obtained for use in the expression, and then the operand is modified. Conditional operator ?:( The ? Operator ): C includes a special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the conditional operator, and it has this general form: expression1 ? expression2 : expression3 Here, expression1 can be any expression that evaluates first. If expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated. The result of the condition operation is that of the expression evaluated. Both expression2 and expression3 are required to return the same type, which can’t be void. Here is an example of the way that the ? is employed: a = 25; Dalbina Dalan

S5BCom CA_Module 1

19

b = 35; x = (a > b) ? a : b; When C evaluates this assignment expression, it first looks at the expression to the left of the question mark. If a > b, then the expression between the question mark and the colon is evaluated and used as the value of the entire ? expression. If a !> b, then the expression after the colon is evaluated and used for the value of the entire ? expression. The result produced by the ? operator is then assigned to a. Bit-wise operators : Bitwise operators are binary operators that perform an action up an individual bit of operands. These operators are used for testing the bits, or shifting them right or left. C provides several types of bit-wise operators that we can use with only integer data type. The various types of bit-wise operators are: Operator

Meaning

&

Bitwise AND

|

Bitwise OR

^

Bitwise exclusive OR

>

Shift Right

Special operators : C provides some special operators, such as comma operator, sizeof operator, pointer operators (& and *) and member selection operator (. And ->). The following are the types of special operators: Comma operator :The comma operator can be used to link the related expressions together. For example, Value = ( x = 10, y = 5, x+y); First assign the value 10 to x, then assigns 5 to y, and finally assigns 15 to Value. Since comma operator has the lowest precedence of all operators, the parentheses are necessary. The sizeof operator : This is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. For example, m = sizeof(sum); n = sizeof(long int); The sizeof operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during execution Dalbina Dalan

S5BCom CA_Module 1

20

of a program. The pointer operator : The actual location of a variable in the memory is system dependent and therefore , the address of a variable is not known to us immediately. We can determine the address of variable with the help of the & operator. The operator & immediately preceding a variable returns the address of the variable associated with it. For example, P = &quantity; Constants A constant value is the one which does not change during the execution of a program. C supports several types of constants. Two important types of constants are: 1. Numeric Constant

and

2. Character Constant

Numeric Constant : Numeric Constants are generally classified into Integer constants and Real constants. Integer Constants :An integer constant is a sequence of digits. There are 3 types of integer’s namely decimal integer, octal integers and hexadecimal integer. Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas and non digit characters are not permitted between digits. Example for valid decimal integer constants are 123 , -31 , 0 , 562321 , + 78 Some examples for invalid integer constants are 15 750

(space)

20,000

(comma)

Rs. 1000

(Rs.)

Octal Integers constant consists of any combination of digits from 0 through 7 with a O at the beginning. Some examples of octal integers are

Dalbina Dalan

S5BCom CA_Module 1

21

O26 , O347 , O676 Invalid octal constants 743

(Does not begin with 0)

05283

(Illegal digit 8)

0666.66

(Illegal decimal point)

Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are OX2 , OX8C , OXbcd Real Constants : Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. Examples of real constants are 0.0026 , -0.97 , 435.29 , +487.0 Real Numbers can also be represented by exponential notation. The general form for exponential notation is eg: 215.65 is also written as 2.1565e2(ie 215.16*10^2) Character Constant: Character Constants are generally classified into Single character constants and String constants. Single Character Constants :A Single Character constant represent a single character which is enclosed in a pair of quotation symbols. Example for character constants are '5' , 'x' , ';' , ' ' All character constants have an equivalent integer value which are called ASCII Values. For example the statement a ---

printf(“%d”,’a’);

a. Similarly, the statement

--

---

would print the number 97, the ASCII value of the letter

printf(“%c”,’97’);

--

would output the letter ‘a’.

String Constants:A string constant is a set of characters enclosed in double quotation marks. The characters in a Dalbina Dalan

S5BCom CA_Module 1

22

string constant sequence may be a alphabet, number, special character and blank space. Example of string constants are "HELLO" , "1234" , "God Bless" , "!.....?" Backslash Character Constants [Escape Sequences] Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character. Given below is the table of escape sequence and their meanings. Constant '\a'

Meaning Audible Alert (Bell)

'\b'

Backspace

'\f'

Formfeed

'\n'

New Line

'\r'

.Carriage Return

'\t'

Horizontal tab

'\v'

Vertical Tab

'\''

.Single Quote

'\"'

Double Quote

'\?'

Question Mark

'\\'

Back Slash

'\0'

Null

Special Symbols In C, there are a few symbols that are used as separators. They are used to inform the C compiler about how statements are grouped together in the code. The most commonly used separator in C is the semicolon, it is used to terminate statements. The following are the separators supported by Java. Symbol

Name

Purpose

Dalbina Dalan

S5BCom CA_Module 1

()

Parentheses

Used to enclose parameters in function definition. Used to contain the values of automatically initialized arrays

{ Braces } []

23

Brackets

;

Semicolon

,

Comma

and to define a block of code for functions Used to declare array types Used to separate statements Used to separate consecutive identifiers in variable declaration

Defining Symbolic Constants A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is # define symbolic_name value of constant Valid examples of constant definitions are : # define marks 100 # define total 50 # define pi 3.14159 These values may appear anywhere in the program, but must come before it is referenced in the program. It is a standard practice to place them at the beginning of the program.

Using Library functions in Math.h math.h is a header file in the standard library of the C programming lang...


Similar Free PDFs