Review of C programming language.pptx...

SthitaprajnaLenka1 13 views 70 slides Mar 02, 2025
Slide 1
Slide 1 of 70
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

About This Presentation

Xyz


Slide Content

Review of C programming language

What is program? A set of logically valid instructions given to the computer in order to solve a predefined problem is called program.

What is Programming? The technique followed to write the program is called programming. Basically it follows “ Divide and Rule ” policy. According to this policy programming is divided into two parts. Procedure Oriented Programming (POP) Object Oriented Programming (OOP)

POP In POP the program is divided into smaller parts known as procedures or functions where each function carries out a specific task. The language which follows POP is known as POPL(Procedure Oriented Programming Language) ‘C’ is a POPL developed by Dennis Ritchie at Bell Laboratory in 1972.

Limitations of POP It is difficult to understand a small function as a part of whole program. It does not give security to the data members.

OOP In OOP the program is divided into smaller parts known as objects . The object is a real world entity having physical and logical existence. The programming language which follows OOP is called OOPL( Object Oriented Programming Language). C++ is an OOPL which was developed by Bjarne Stroustrup at Bell Laboratory.

Elements of C language Alphabets (A-Z, a-z) Numerals( Decimal(0-9), Octal(0-7), Hexadecimal(0-9,A,B,C,D,E,F), Floating point numbers) Special Symbols(,,;,.,:, etc) Identifiers Literals Keywords Operators

Identifiers It is the name given to the memory location. It can be a name of function, variable, constant, array etc It has a property called data type which decides the capacity of an identifier. In C programming the following types of data types are available

Types of Data types Data Type Size Range char 1 byte or 8 bits -128 to 127 int or signed int 2 bytes -32768 to 32767 long int 4 bytes -2147483648 to 2147483647 float 4 bytes 3.4 E-38 to 3.4 E+38 double 8 bytes 1.7E-308 to 1.7E+308 long double 10 bytes 3.4E-4932 to 1.1E+4932

Variable Declaration Syntax:- datatype <identifier name>; Ex:- int x; int x,y ; float z; long int w;

Rules to define an identifier It should not exceed the length of 31 characters. It should begin with an alphabet or underscore(_). It should not be a keyword. It should not contain special characters and not even space in the name of an identifier. It should be case sensitive in nature i.e. uppercase and lowercase letters are treated separately.

Literals Literals are the values assigned to the variables. int x = 20; Datatype Identifier Literal The float literals are suffixed by f or F. The double literals are suffixed by d or D. The character literals are enclosed within single quote. Ex:- char s=‘a’; float y=20.5f;

Keywords Keywords are the reserved words having specific meaning and they can be used in specific place. All the keywords should be written in lower case. Some of the keywords present in ‘C’ language are as follows:- auto, break, case, char, const, continue do, default, double, else, enum , extern, float, for, goto , if, int , long, etc

Operators Operators are the special symbols used to do different operations on the operands. On the basis of operations operators are divided into following categories. Arithmetic operators:- +, -, *, /, % 5/2=2.5 5%2=1 2. Relational Operators:- <, >, <=, >=, ==,!=

More operators 3. Logical Operators :- &&(and),||(or),!(not) 4. Increment & decrement operator(++,--) 5. Assignment Operator =, +=, -=,*=,%=

Bitwise Operators These operators operates on Binary Digits (bits) Some of the bitwise operators are Left Shift(<<) Right Shift(>>) Bitwise And(&) Bitwise Or(|) XOR(^) Complement(~)

Special Operators sizeof():- It finds out the size of the variable or data type passed as its argument. Ex:- sizeof( int ); int x; sizeof(x); Comma operator:- It executes the statements from left to right and result of rightmost expression is the final result.

Structure of a Program There should be a function named main(). All the statements should enclosed within two curly braces. All the statements should end with ;(semicolon). All the keywords should be written in lower case letters. Syntax:- main() { Statement; ………… ………… ………… }

Header File These are the files with the extension name .h. These files are library files already present in the library of ‘C’ programming. The header files consists of functions . Whenever we require those functions in our program we need to attach the header file with the source program by using preprocessor . Some of the header files are stdio.h (Standard Input Output) conio.h (Console Input Output) math.h stdlib.h (Standard Library) etc

Preprocessor It is a part of compiler which preprocesses the program before compilation takes place. Preprocessing means expanding and attaching the header files with the source program. One of the preprocessor is #include . Syntax:- #include<header file name> Ex:- #include< stdio.h > #include< conio.h >

I/O Statements in C ‘C’ programming has Input/output functions present in stdio.h header file. printf ():- This function is used to print something on the monitor. Syntax:- printf (“ Control Statement”, argument list); The control statement is of 3 types Ordinary String Escape Sequences Format Specifier

Ordinary String Any message can be printed on the screen by using printf statement. printf (“Hello Welcome to the World of C”); printf (“My name is Sanjibani ”);

Escape Sequences It is used to format the output Escape Sequence Functions \a Audible Bell \b Backspace \n Newline \t Horizontal Tab \r Carriage Return \v Vertical Tab \’ Single Quote \” Double Quote \? Question Mark \\ Backslash

Format Specifier Format Specifier Datatype %d int %ld long int %c char %s string %f float %lf double %Lf Long double %o octal %x hexadecimal

Examples of printf () int x=20; printf (“x”); o/p:- x printf (“% d”,x ); o/p:- 20 printf (“The value of x is %d”, x); o/p:- The value of x is 20

scanf () This function is used to take input from the keyboard. Syntax:- scanf (“format specifier ”, & variablelist ); int x; printf (“enter a No”); scanf (“% d”,&x );

First Program WAP to input two numbers and find the sum. #include< stdio.h > void main() { int x,y,s ; printf (“Enter two Numbers”); scanf (“% d%d”,&x,&y ); s= x+y ; printf (“The sum is % d”,s ); }

Shortcut Keys To save a file F2 key is used. To open an existing file F3 key is used. To compile Alt+F9 is used. To run Ctrl+F9 is used. To make full screen F5 is used. To see the output Alt+F5 is used.

Decision making and branching statement C programming has decision control statements which controls the flow of instruction execution. if switch goto

If statement Syntax:- if(condition) { statement; …………. …………. } else { statement; ………….. ………….. } When the condition is true the statements after if will be executed otherwise the statements after else will be executed.

Programs based on if….else WAP to input a no and check whether it is even or odd. WAP to input a number and check whether it is positive and negative WAP to input 3 numbers and calculate average. If the average >=30 then print pass otherwise print fail.

If… else ladder Syntax:- if(condition) { statement; ……… …….. } else if(condition) { statement; ……… …….. } ……… …….. else { statement; ……… …….. }

Programs on if else if ladder WAP to input 3 numbers and find the greatest WAP to input marks in three subjects and calculate average. If the average >=60 then print “first Div”. If the average <60 and >=50 then print “second div”. If the average <50 and>=30 then print “third div” otherwise print “fail”.

Switch statement Syntax:- switch(expression) { case expression value: statement; break; case expression value: statement; break; …….. …….. …….. default: statement; }

Questions based on switch Write a program to input a day and print its corresponding day’s name Write a program to input two numbers and a choice and do the following operations according to the choice Choice Operation 1 Addition 2 Substraction 3 Multiplication 4 Division 5 Remainder 6 Exit

goto statement It is an unconditional branching statement. Syntax:- goto label name; Ex:- Wap to print 1 to 100

Some questions based on goto Wap to print the following series 1 4 9 16 25 36 ………100 1 4 10 22 46……… upto 100 2 4 6 8 10 ………50

Looping It executes a group of statements repetitively till the condition is true. Some of the looping statements available in C are while do…. while for

while loop Syntax:- while(condition) { statement; …… …… modification; }

Questions based on while loop Wap to print 1 to 100 Wap to print the sum of all the numbers from 1 to 100 Wap to input a number and find its factorial. Wap to input a no and find its divisors

Questions for Experiment-2 Aim of the assignment1:- WAP to input a number and check whether it is perfect no or not. Aim of the assignment2:- WAP to find the sum of following series. Sum=1-1/1!+1/2!-1/3!+……..1/n! Where n is inputted by the user

do…while loop Syntax:- do { statement; ……….. ……….. modification; } while(condition);

Programs on do…while loop WAP to print 1-100 WAP to input a number and check whether it is palindrome or not WAP to input a number and check whether it is prime no or not

For loop Syntax:- for( initialisation;condition;modification ) { statement; ………… ………… }

Programs based on for loop WAP to print 1-100 WAP to print 100 to 1 backwardly WAP to print all the prime numbers between 1 to 100

Nested for When for is present within another for then it is called nested for. Syntax:- for( initialisation;condition;modification ) { for( initialisation;condition;modification ) { statement; ………. ………. } }

Programs based on nested for Experiment-3 WAP to print the following pattern 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 ***** **** *** ** *

Array Array is a homogeneous data structure which keeps similar types of elements in consecutive locations of memory. The index of an array starts with 0. Array is divided into following categories. 1D Array 2D Array

1D Array When an array is declared the compiler allocates space in memory sufficient to hold all the elements of the array, so the size of array should be known at the compile time. Hence we can’t use variable for specifying the size of array in declaration. Syntax:- datatype arrayname [size]; x Ex:- int x[10]; float y[5]; 0 1 2 3 4 5 6 7 8 9 5 2 6 4 8 7 1 9 3 1

Initialisation of 1D array Syntax:- datatype arrayname [size]={value1,value2,……}; Ex:- int x[5]={10,20,30,40,50} int x[]={10,20,30,40,50}

Problems based on 1D array WAP to input 10 integers in an array & print them. WAP to input 20 float numbers in an array and find the sum and average. WAP to input 10 integers in an array & print those elements which are even. WAP to input 10 integers in an array and find the greatest number and it’s location. WAP to input 10 integers and arrange them in ascending order WAP to input 10 integers and input another number search whether the number is present in the array or not by using binary search.

2D Array It is otherwise known as matrix. It has two subscripts i.e. row and column. Syntax:- datatype arrayname [row][ col ]; E.g.:- int x[4][4]; Index starts with 00,01,02 and so on.

Initialization of 2D array A 2d array can be initialised in the following way. Int x[2][2]={1,2,3,4} Int x[2][2]={{1,2},{3,4}}; Int x[][2]={{1,2},{3,4}};

Programs based on 2D WAP to input integers in an array of size 4x4 and find sum and average of all the elements. WAP to input integers in an array of size 4x4 and print row wise sum. WAP to input integers in an array of size 4x4 and print its diagonal elements. WAP to input integers in two arrays of size 3x4, add them and store the results in third array.

String in C String is a collection of characters under one name. String starts with index 0 and ends with a special character known as null character or ‘\0’. Syntax:- char stringname [size]; Ex:- char x[30]; To input a string printf (“Enter a string”); scanf (“% s”,x );

gets() and puts() These two functions are used in string only. They are used to take input from the keyboard and used to give output to the monitor respectively. Syntax:- gets( stringname ); puts( stringname ); For e.g. char x[20]; puts(“Enter a string”); gets(x);

Difference between gets() and scanf () The basic difference is that scanf () ends taking input upon encountering a whitespace, newline or EOF gets() considers a whitespace as a part of the input string and ends the input upon encountering newline or EOF.

Initialisation of string The string can be initialised while declaration in the following manner. char x[]={‘ c’,’u’,’t’,’t’,’a’,’c’,’k ’}; char x[]=“ cuttack ”;

Programs based on string WAP to input a string and count its length. WAP to input a string and count the number of vowels present in it. WAP to input a string and copy to another string WAP to input a string and print it reversely.

Functions in C A function is a self-contained subprograms that performs some specific well defined task. In C there are two types of function Library Function User Defined Function Library functions are present in the C library and they are predefined. Some of the library functions are scanf (), printf (), getch (), clrscr (), strlen () etc

User Defined Functions Programmers can create their own functions for performing any specific task of the program. These types of functions are user defined function. Management of user defined function A user defined function can be managed in three ways. Function declaration Function invocation Function definition

Function Declaration It is otherwise known as function prototype or signature of a function. Syntax :- <return type> function name(arguments type); The function name should follow the rules of an identifier and it should not be a keyword. The argument type is the type of variables on which function operates. The return type is the type of result the function gives. Ex:- int sum( int,int ); void sum( int,int ); When the function does not return any value its return type is void.

Function Invocation The calling of a function is known as function invocation. A function is always called within another function.

Function Definition It is the actual body of the function where operation takes place.

Programs based on functions WAP to input the radius and find the area of a circle. WAP to input the radius and find the area of a square WAP to find the volume of a cube WAP to find the value of pi.

Programming in C++ cin >>:- It is used to take input from the keyboard. cout << :- It is used to give output to the monitor. Ex:- int x; cout <<“Enter a no”; cin >>x;

Class in C++ A class is a way to bind the data and its associated functions together. It allows data and functions to be hidden from external use. Syntax:- class class_name { private: variable declaration; function declaration; public: variable declaration; function declaration; };

Visibility of members of a class The class members are grouped under private, public and protected. In other words it is called the visibility of the members of a class. If the members are private then it’ll be accessed only by the same class. If the members are public then it’ll be accessed by all the class. If the members are protected then it’ll be accessed by the same class and the class which are inherited from it.

Programs based on class WAP to create a class student having data members name, roll, age and function members input() and output(). WAP to create a class employee having data members name, code, salary and function members input(),output(), findpf () and create an object to access the members.

Thank You
Tags