Download presentation
Presentation is loading. Please wait.
Published byDoddy Muljana Modified over 5 years ago
1
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
B.B. COLLEGE ASANSOL
2
Dept. of Physics, B.B.College Asansol
What is C Program? A sequence of logically related instructions that enables the computer to perform a complete task is known as a program. A computer can only understand instructions written in binary form i.e. 0 and 1. A program written in binary form is referred to as machine language. Dept. of Physics, B.B.College Asansol
3
Dept. of Physics, B.B.College Asansol
Two types of language are used 1. Low level language 2. High level language Low level language are machine and assembly languages. High level language are FORTRAN, COBOL etc. Dept. of Physics, B.B.College Asansol
4
Dept. of Physics, B.B.College Asansol
What is Compiler? A high level language is translated into machine language by using a program in the memory. A computer program written in high level language is known as source program. This program when translated into machine language is called an object program. Dept. of Physics, B.B.College Asansol
5
Character, Identifiers and Keywords
Characters: A to Z, lowercase letters a to z. The digit 0 t0 9. Special characters: ; etc. Combinations characters \b,\n, \t, etc Keywords: auto, char, else, for, float, goto, int, if, retrun, void, while, const. Identifiers: gross, sum , name, area, name. Dept. of Physics, B.B.College Asansol
6
Dept. of Physics, B.B.College Asansol
Variables Variables refer the objects that can be stored in computer’s memory locations. Variables are named (identifiers). Variables must be declared and initialized. Variables can store data of various types. Some basic data types are char characters (1/2 bytes) int integers (2/4 bytes) float rationals (4 bytes) double rationals(8 bytes Dept. of Physics, B.B.College Asansol
7
Dept. of Physics, B.B.College Asansol
Declaration Declarations have form type var1, var2, var3; Merely announces the data type to be associated with a variable Examples char name; int a,b; float c,d; Dept. of Physics, B.B.College Asansol
8
Dept. of Physics, B.B.College Asansol
Array An array is a collective name of variables of the same data type. The individual variable making up an array is called an element of the array. Example: roll[1], roll[1]……….roll[20] Dept. of Physics, B.B.College Asansol
9
Operators and Expressions
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
10
Dept. of Physics, B.B.College Asansol
Arithmetic operators addition subtraction * multiplication / division % percentage Dept. of Physics, B.B.College Asansol
11
Dept. of Physics, B.B.College Asansol
Relational operators a>b a greater than b a>=b a greater than or equal to b a< b a less than b a<=b a less than or equal to b a==b a equal to b a ! =b a equal to b Dept. of Physics, B.B.College Asansol
12
Dept. of Physics, B.B.College Asansol
logical operators && logical AND ‖ logical OR ! logical NOT Dept. of Physics, B.B.College Asansol
13
Dept. of Physics, B.B.College Asansol
Assignment operators x= x x+=3 x=x x-=3 x=x*(n+2) x*=n+2 x=x/ x/=3 x=x% x%=3 Dept. of Physics, B.B.College Asansol
14
Dept. of Physics, B.B.College Asansol
Unary operators ++x or x++ increments x by 1 -- x or x-- decrements x by 1 -x negates the value of x Example; x=4; y= ++x; y=5; Dept. of Physics, B.B.College Asansol
15
Dept. of Physics, B.B.College Asansol
Library Functions main (): Every c program must have function main() which may access (call) other function thus main () is a calling function. Input and output function: get char: Input function variable name= getchar (); scanf : Input function scanf(“control string”, argument1); Dept. of Physics, B.B.College Asansol
16
Dept. of Physics, B.B.College Asansol
Example: int x; float y; scanf(“%d,%f”,&x,&y); putchar: output function. putchar(ch); printf: output function. printf(“control string”, argument 1); printf(“%d %f”,x,y); Dept. of Physics, B.B.College Asansol
17
Dept. of Physics, B.B.College Asansol
If-else statement 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
18
Dept. of Physics, B.B.College Asansol
Example: y=x4 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
19
Dept. of Physics, B.B.College Asansol
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 } Dept. of Physics, B.B.College Asansol
20
Dept. of Physics, B.B.College Asansol
Example: ……… sum=0; n=1; while (n<=100) { sum=sum+m; n=n+1; } Dept. of Physics, B.B.College Asansol
21
Dept. of Physics, B.B.College Asansol
2. Do while loop: do { body of the loop } while (test condition) Example: int num=0; printf(“%d\n”,num); ++num; while(num<=11) Dept. of Physics, B.B.College Asansol
22
Dept. of Physics, B.B.College Asansol
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
23
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. Flowchart is a pictorial representation of an algorithm. Dept. of Physics, B.B.College Asansol
24
Dept. of Physics, B.B.College Asansol
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 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
25
Dept. of Physics, B.B.College Asansol
Flowchart: Start Declare variables a,b Input a,b and store their values Is a>b? NO Yes Print Print b>a a>b Stop Stop Dept. of Physics, B.B.College Asansol
26
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 %f\n”,area); } Dept. of Physics, B.B.College Asansol
27
Dept. of Physics, B.B.College Asansol
Example: Matrix #include<stdio.h> main() { int a=1,i,j,m, n,mat[10][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"); printf("%d\t",mat[i][j]); printf("\n"); } Dept. of Physics, B.B.College Asansol
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.