basics of c programming for naiver.pptx

ssuser96ba7e1 41 views 77 slides Jul 09, 2024
Slide 1
Slide 1 of 77
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77

About This Presentation

basics of c programming


Slide Content

Day – 01 Problem Solving with Flow Charts & Basics of C Programming

The following symbols are used to represent the Flow Charts Ellipse : To represent start or stop   Parellagram : To represent input/output   Rectangle : To represent the processing or calculations  

Rhambus : To represent the conditional statements   Small Circle : To use as a connector   Arrows : For directions

1. Write an algorithm to calculate the addition of 2 numbers. start step 1: read a step 2: read b step 3: c := a+b step 4: print a step 5: print b step 6: print c stop

2. Draw the flow chart to calculate the addition of 2 numbers.

3. write an algorithm to calculate the addition of 3 numbers. start step 1: read a step 2: read b step 3: read c step 4: d := a+b+c step 5: print a step 6: print b step 7: print c step 8: print d stop  

Rewrite the above the algorithm to represent more input and output(read or print) statements in a single line. start step 1: read a,b,c step 2: d := a+b+c step 3: print a,b,c,d stop

Draw the flow chart for addition of 3 numbers.

Algorithm 3. write an algorithm to calculate the total marks and average marks of 3 subjects ( maths,physics and chemistry) start step 1: read maths,physics,chemistry step 2: total := maths+physics+chemistry step 3: avg := total/3 step 4: print maths,physics,chemistry,total,avg stop

Draw the flow chart for the above algorithm

Algorithm 4. write an algorithm to calculate the area of a circle. start step 1: read radius step 2: area_circle := 3.14 * radius * radius step 3: print radius,area_circle stop

Draw the flow chart for the above algorithm

Algorithm 7. write an algorithm to find the biggest of two numbers. start step 1: read num1,num2 step 2: if num1 > num2 then print "num1 is big" else print "num2 is big" stop

Draw the flow chart for above algorithm

Algorithm 8. write an algorithm to find the biggest of three numbers. start step 1: read num1,num2,num3 step 2: if num1 > num2 and num1 > num3 then print "num1 is big" else if num2 > num1 and num2 > num3 then print "num2 is big" else print "num3 is big" stop

Draw the flow chart for the above algorithm

Introduction to C Programming Language C Programming Language is Procedural Programming Language It is developed by Dennis Ritchie in the year 1972 It is developed as system programming language to write operating systems and compilers.

Features of C Programming Language: Low level access to memory rich set of keywords Alphabets, Digits and Symbols keywords statements programs Alphabets : a-z, A-Z

Digits : 0-9 Symbols : ~ tilde ! Exclamatory @ at the rate # Hash(preprocessor) $ Dollar % Percentage ^ carret & ampersand * star ( parenthesis open ) parenthesis close - highfun /minus _ underscore + plus = equal : colo n

; semi colon " Double Quotes ' Single Quote < less than > greater than , comma . dot ? question mark / slash or division \ backward slash | vertical bar { brace or curley brace open } brace or curley brace close [ bracket open ] bracket close

Keywords : Keywords are the reserved words in the programming language. Each keyword in c language is having its own specific purpose and meaning. These keywords are not used for any other purpose. All the keywords are written in lower case alphabets.

The following are the keywords of C language auto break case char const or constant continue default do double else enum extern

float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Structure of C Programming Language

Link/Header File Include Section: In this section we can include the header libraries or we can link the other programs with the current programs We can include the header library using the preprocessor directive # includein the program

Here # symbol is known as a preprocessor and #include is a preprocessor directive. Example : #include< stdio.h > here stdio.h is a standard input/output header library. here .h indicates and header library/file.

Definition Section: Here, we define the macro's(constants) or user defined function prototypes. Global Variable Decleration Section: Here we declare the global variables required in the program.

main function: main function is compulsory in all the c programs. The program execution always begins from the main function only. (or) The starting point of C program execution is from main only.

We can write the main function as follows. main() { } : main followed by open parenthesis and closed parenthesis then immediately continued with open brace and closed brace.

We cannot write anything between the closed parenthesis and the open brace of main function. In between the open brace and closed brace of main function we write the decleration section and execution section.

Decleration Section: In the decleration section we can declare the variables required in the program. The first section of main function must be the declreration section.

Execution Section: All the executable statements can be written under the execution section. The executable statements like input, output and calculation statements.

The main function returns an integer value always, Hence we must write a return statement at the end of main function. We can write the return statement using the keyword return. example: return 0;

User defined functions: We can write zero or n number of user defined functions in the C program.

Datatypes : A datatype specifies the compiler that a type of value is stored in the variable.

Datatypes : 1. Predefined Datatypes /Basic Datatypes i . integer types - short int - int - long int ii. real types - float - double - long double iii. charecter types - char

2. Derived Datatypes i . arrays ii. strings iii. functions iv. pointers

3. User Defined Datatypes i . structures ii. unions iii. enumerations

Datatypes sizes and range:

Identifier: An identifier is a name of variable, constant, pointer, array, string, function, structure and union.

Rules for defining the identifiers: 1. we can use all the lower case alphabets from a to z, digits from 0 to 9 and only one symbol underscore _ in defining the identifier name. 2. we can start with either any alphabet from lower case a to z or _ symbol. 3. we cannot start with any digit. 4. we cannot give any white spaces( space,tab or newline charecter ) in the identifier name. 5. The length of an identifier will be from one charecter to 36 charecters .

Ex: a student_name gross_salary _subject result_1

Variable: A variable is a named memory location to store the values. We can declare the variables using the datatype as follows. syntax: datatype variablename ;

ex: int num1; int num2; int num3; (or) int num1,num2,num3; num1=10; num2=20; num3=num1+num2;

Input/Output functions: In C language the input/output functions are available in the stdio.h (standard input/output header library). The input/output functions are classified into 2 categories. 1. Formatted Input/Output Functions 2. Unformatted Input/Output Functions

Formatted Input/Output Functions: In Formatted Input/Output functions we should specify the format specifier to make the compiler to understad the type of values used to read/write.

The following are the Formatted input/output functions 1. printf () 2. scanf () 3. fprintf () 4. fscanf ()

printf (): This function used to display the output on the console. The following the is the syntax to write the printf () function. syntax: printf (" Formatstring",list of variables);

Format string is divided into 2 parts they are i . Text or Message : Some message or text to be displayed on console. If no text to be displayed then we cannot write any text. ii. Format specifiers : Format specifiers of corresponding datatype will be written. List of Variables: The variables list to be displayed on the console. When there is more than one variable to be displayed, each variable should be separted by comma (,) operator.

ex:1 int a; => %d float b; => %f char c; => %c

printf ("the values are % d%f%c",a,b,c ); printf ("the values are % d%f%c",b,c,a ); <= Its incorrect [Mismatch of datatypes ] printf ("the values are % d%f%c",a,b ); <= Its incorrect [Mismatch of Format specifiers and variables number]

Note: > When we give different types of variables we must follow the order of format specifiers with the order of variables list. > The number of variables must be matched with the number of format specifiers .

ex:2 int a,b ; printf ("% d%d",a,b ); 2. scanf (): This function will read the input from the standard input device(key board) to the variables. syntax: scanf ("Format Specifiers",List of variables);

Ex:1 int a,b ; scanf ("% d%d",&a,&b ); Here the variables must preceed with address(&) operator. All the rules of printf () are applicable here.

Ex:2 int a; float b; char c; scanf("%d%f%c",&a,&b,&c);

3. fprintf (): This function will writes the data to the file. syntax: fprintf ( filepointer,"Format String",List of variables);

Ex: fprintf ( stdout ,"% d",a ); Here stdout is standard output device You can also specify any other file pointer

4. fscanf (): This function will read the data from a file. syntax: fscanf ( filepointer,"Format Specifiers",List of variables);

Ex: fscanf ( stdin ,"% d",&a ); Here stdin is a standard input device(keyboard) You can also specify any other file pointer.

2. Unformatted Input/Output Functions: These functions does not required to specify the format specifier . The following are the unformatted input/output functions. 1. getchar () 2. putchar () 3. getc ()/ fgetc () 4. putc ()/ fputc () 5. gets() 6. puts()

1. getchar (): This function will read a single charecter from the keyboard. ex: char ch ; ch = getchar (); 2. putchar (): This function will display a single charecter on the console. ex: putchar ( ch );

3. getc (): This function will read a single charecter from the file. ex: char ch ; ch = getc ( fp ); here fp is a file pointer from which we need to read a single charecter .

4. putc (): This function will write a single charecter to the file. ex:putc ( ch,fp ); here fp is file pointer to which we need to write a single charecter .

5. gets(): This function will read a string. A string is a collection of charecters . ex:gets ( str ); here str is a string variable. 6. puts() This function will display the string on the console. ex:puts ( str ); here str is a string variable.

Comment lines: Comment lines are ignored by the compiler. These are for the information purpose to the programmer The comment lines can be written as a single line or multiple lines as per theneed . We can write the comment line in C Program between /* and */ symbol.

Ex: /* its a declerative statement */ Note: 1. All the statements in C language should be written in lower case alphabets only. C language is a case sensitive language. 2. All the statements must be terminated with semi colon.

write a program to display a welcome message

To erase the previous output on the console we can use clrscr () function inthe program.

#include< stdio.h >