C Programming Mr KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT

  • Slides: 27
Download presentation
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS B. B. COLLEGE ASANSOL

C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS B. B. COLLEGE ASANSOL

What is C Program? A sequence of logically related instructions that enables the computer

What is C Program? A sequence of logically related instructions that enables the computer to perform a complete task is known as a program. n A computer can only understand instructions written in binary form i. e. 0 and 1. n A program written in binary form is referred to as machine language. n Dept. of Physics, B. B. College Asansol

Two types of language are used 1. Low level language 2. High level language

Two types of language are used 1. Low level language 2. High level language n Low level language are machine and assembly languages. n High level language are FORTRAN, COBOL etc. n Dept. of Physics, B. B. College Asansol

What is Compiler? A high level language is translated into machine language by using

What is Compiler? A high level language is translated into machine language by using a program in the memory. n A computer program written in high level language is known as source program. n This program when translated into machine language is called an object program. n Dept. of Physics, B. B. College Asansol

Character, Identifiers and Keywords Characters: A to Z, lowercase letters a to z. The

Character, Identifiers and Keywords Characters: A to Z, lowercase letters a to z. The digit 0 9. n Special characters: #, @, &, %, (), *, ; etc. n Combinations characters b, n, t, etc n Keywords: auto, char, else, for, float, goto, int, if, retrun, void, while, const. n Identifiers: gross, sum , name, area, name. n Dept. of Physics, B. B. College Asansol

Variables refer the objects that can be stored in computer’s memory locations. Variables are

Variables refer the objects that can be stored in computer’s memory locations. Variables are named (identifiers). n Variables must be declared and initialized. n Variables can store data of various types. Some basic data types are characters (1/2 bytes) integers (2/4 bytes) float rationals (4 bytes) double rationals(8 bytes n Dept. of Physics, B. B. College Asansol

Declarations have form type var 1, var 2, var 3; n Merely announces the

Declarations have form type var 1, var 2, var 3; n Merely announces the data type to be associated with a variable n Examples char name; int a, b; float c, d; n Dept. of Physics, B. B. College Asansol

Array An array is a collective name of variables of the same data type.

Array An array is a collective name of variables of the same data type. n The individual variable making up an array is called an element of the array. n Example: roll[1], roll[1]………. roll[20] n Dept. of Physics, B. B. College Asansol

Operators and Expressions n n An operator is a symbol that tells the computer

Operators and Expressions n n An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations of data variables. There are five types of operators: 1. Arithmetic operators 2. Relational operators 3. logical operators 4. Assignment operators 5. Unary operators Dept. of Physics, B. B. College Asansol

Arithmetic operators + * / % addition subtraction multiplication division percentage Dept. of Physics,

Arithmetic operators + * / % addition subtraction multiplication division percentage Dept. of Physics, B. B. College Asansol

Relational operators a>b a>=b a greater than or equal to a< b a<=b a==b

Relational operators a>b a>=b a greater than or equal to a< b a<=b a==b a ! =b a less than or equal to b a equal to b b Dept. of Physics, B. B. College Asansol

logical operators && ‖ ! logical AND logical OR logical NOT Dept. of Physics,

logical operators && ‖ ! logical AND logical OR logical NOT Dept. of Physics, B. B. College Asansol

Assignment operators x= x+3 x=x-3 x=x*(n+2) x=x/3 x=x%3 x+=3 x-=3 x*=n+2 x/=3 x%=3 Dept.

Assignment operators x= x+3 x=x-3 x=x*(n+2) x=x/3 x=x%3 x+=3 x-=3 x*=n+2 x/=3 x%=3 Dept. of Physics, B. B. College Asansol

Unary operators ++x or x++ increments x by 1 -- x or x-- decrements

Unary operators ++x or x++ increments x by 1 -- x or x-- decrements x by 1 -x negates the value of x n Example; x=4; y= ++x; y=5; Dept. of Physics, B. B. College Asansol

Library Functions main (): Every c program must have function main() which may access

Library Functions main (): Every c program must have function main() which may access (call) other function thus main () is a calling function. n Input and output function: get char: Input function variable name= getchar (); scanf : Input function scanf(“control string”, argument 1); n Dept. of Physics, B. B. College Asansol

Example: int x; float y; scanf(“%d, %f”, &x, &y); putchar: output function. putchar(ch); printf:

Example: int x; float y; scanf(“%d, %f”, &x, &y); putchar: output function. putchar(ch); printf: output function. printf(“control string”, argument 1); Example: printf(“%d %f”, x, y); Dept. of Physics, B. B. College Asansol

If-else statement n It is used to carry out a logical test and then

If-else statement n It is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test. if (test expression) statement 1 else statement 2 Dept. of Physics, B. B. College Asansol

n Example: y=x 4 if x>2 = x+3 otherwise C-program: ……………. . if (x>2)

n Example: y=x 4 if x>2 = x+3 otherwise C-program: ……………. . if (x>2) y=pow(x, 4); else y=x+3 ……………… Dept. of Physics, B. B. College Asansol

Looping In C language looping cab be done by using while, do while and

Looping In C language looping cab be done by using while, do while and for statements 1. while loop: while (test condition) { body of the loop } n Dept. of Physics, B. B. College Asansol

n Example: ……… sum=0; n=1; while (n<=100) { sum=sum+m; n=n+1; } Dept. of Physics,

n Example: ……… sum=0; n=1; while (n<=100) { sum=sum+m; n=n+1; } Dept. of Physics, B. B. College Asansol

2. Do while loop: do { body of the loop } while (test condition)

2. Do while loop: do { body of the loop } while (test condition) Example: int num=0; do { printf(“%dn”, num); ++num; } while(num<=11) Dept. of Physics, B. B. College Asansol

3. for loop: for( initialization; test condition; increment) { body of the loop }

3. for loop: for( initialization; test condition; increment) { body of the loop } Example: ………. for (i=0; i<4; i++) { printf(“%d”. i); } Dept. of Physics, B. B. College Asansol

Algorithm and Flowchart To write a program for a particular problem one must fully

Algorithm and Flowchart To write a program for a particular problem one must fully understand the nature of the problem, the kind of data to be given as input, kind of outputs need and the constraints and conditions under which the program has to operate. Depending on these one has to decide a solution procedure. n Flowchart is a pictorial representation of an algorithm. n Dept. of Physics, B. B. College Asansol

Example: Let us consider the algorithm and flowchart for the problem of finding the

Example: Let us consider the algorithm and flowchart for the problem of finding the larger of two numbers. Algorithm: Step 1 : Start the process by declaring two variables a and b. Step 2 : Give the inputs and store them in the variables a and b. Step 3 : Check, if a>b. If the answer is ‘yes’, go to step 4. Otherwise go to step 5. Step 4 : Print “a is greater than b” and go to step 6. Step 5 : Print “b is greater than a” and go to step 6. Step 6 : Stop. Dept. of Physics, B. B. College Asansol

Flowchart: Start Declare variables a, b Input a, b and store their values Is

Flowchart: Start Declare variables a, b Input a, b and store their values Is a>b? NO Yes Print b>a Print a>b Stop Dept. of Physics, B. B. College Asansol

Example: Area of the Sphere #include<stdio. h> main() { float r=5, Area; Area= 4*3.

Example: Area of the Sphere #include<stdio. h> main() { float r=5, Area; Area= 4*3. 41*r*r; printf(“The area of the sphere is %fn”, area); } Dept. of Physics, B. B. College Asansol

Example: Matrix #include<stdio. h> main() { int a=1, i, j, m, n, mat[10]; printf("Enter

Example: Matrix #include<stdio. h> main() { int a=1, i, j, m, n, mat[10]; printf("Enter the row numbers : t"); scanf("%d", &m); printf("Enter the columb numbers : t"); scanf("%d", &n); printf("Enter the elements of the matrix : t"); for(i=0; i<m; i++) for(j=0; j<n; j++) scanf("%d", & mat[i][j]); printf("The given matrix is: n"); for(i=0; i<m; i++) { for(j=0; j<n; j++) printf("%dt", mat[i][j]); printf("n"); } } Dept. of Physics, B. B. College Asansol