0% found this document useful (0 votes)
3 views

Unit 1 Powerpoint Slides

Uploaded by

Suresh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 1 Powerpoint Slides

Uploaded by

Suresh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 94

UNIT 1

Programming In C and C++ (SBSA1102)

BACHELOR OF SCIENCE IN COMPUTER SCIENCE

1
PROGRAMMING IN C AND C++ (SBSA1102)

COURSE OUTCOMES

On completion of the course, you will be able to learn

CO1 - Develop simple applications in C using basic constructs.

CO2 - Design and Implement applications using arrays and strings.

CO3 - Develop and Implement applications using memory allocation and File concepts.

CO4 - Use proper class protection to provide security.

CO5 - Describe the reusability of code through Inheritance.

CO6 - Demonstrate the use of virtual functions to implement polymorphism.

2
INTRODUCTION TO C

 C is a structured programming language developed by Dennis Ritchie in 1973 at Bell Laboratories.


 It is one of the most popular computer languages today because of its structure, high-level abstraction,
machine independent feature etc.

It is widely used professional language for various reasons.

1. Easy to learn

2. Structured language

3. It produces efficient programs.

4. It can handle low-level activities.

5. It can be compiled on a variety of computers.

3
HISTORY OF C LANGUAGE

 C language has evolved from three different structured language ALGOL, BCPL and B Language.

 It uses many concepts from these languages while introduced many new concepts such as datatypes,

struct, pointer etc.

 In 1988, the language was formalised by American National Standard Institute(ANSI).

 In 1990, a version of C language was approved by the International Standard Organisation(ISO)

4
HISTORY OF C LANGUAGE

5
FEATURES OF C

 It is a robust language with rich set of built-in functions and operators that can be used to write any

complex program.

 The C compiler combines the capabilities of an assembly language with features of a high-level

language.

 Programs Written in C are efficient and fast.

 This is due to its variety of data type and powerful operators.

 It is many time faster than BASIC.

6
FEATURES OF C

 C is highly portable this means that programs once written can be run on another machines with little or

no modification.

 Another important feature of C program, is its ability to extend itself.

 A C program is basically a collection of functions that are supported by C library.

 We can also create our own function and add it to C library.

 C language is the most widely used language in operating systems and embedded system

development today.

7
FEATURES OF C

8
STRUCTURE OF C PROGRAMMING

 A C program is divided into different sections.

 There are six main sections to a basic c program.

 The whole code follows this outline.

 Each code has a similar outline.

9
STRUCTURE OF C (CONT...)

Documentation Section

Link Section

Definition Section

Global Documentation
Section
Main Function

Sub Program Section

10
STRUCTURE OF C (CONT...)

Documentation Section

 The documentation section is the part of the program where the programmer gives the details

associated with the program.

 It usually gives the name of the program

 It gives anyone to read the code to give overview of the code.

Example

File Name: Helloworld.c

Description: a program to display hello world

11
STRUCTURE OF C (CONT...)

Link Section

This part of the code is used to declare all the header files that will be used in the program. This leads to

the compiler being told to link the header files to the system libraries.

Example

#include<stdio.h>

#include<conio.h>

12
STRUCTURE OF C (CONT...)

Definition Section

In this section, we define different constants. The keyword define is used in this part.

Example:

#define PI=3.14

13
STRUCTURE OF C (CONT...)

Global Declaration Section

 This part of the code is the part where the global variables are declared.

 All the global variable used are declared in this part.

 The user-defined functions are also declared in this part of the code.

Example:

float area(float r);

int a=7;

14
STRUCTURE OF C (CONT...)

Main Function Section


 Every C-programs needs to have the main function.
 Each main function contains 2 parts.
 A declaration part and an Execution part.
 The declaration part is the part where all the variables are declared.
 The execution part begins with the curly brackets and ends with the curly close bracket.

Example:
int main(void)
{
int a=10;
printf(" %d", a);
return 0;
15
STRUCTURE OF C (CONT...)

Sub Program Section

All the user-defined functions are defined in this section of the program.

Example:

int add(int a, int b)

return a+b;

16
FIRST C PROGRAM

#include <stdio.h>

int main()

printf("HelloWorld"); //single line comment return 0;

Output:

HelloWorld

17
DATA TYPES

C - DATA TYPES

 The data-type in a programming language is the collection of data with values having fixed meaning as

well as characteristics.

 Some of them are an integer, floating point, character, etc.

 A data-type in C programming is a set of values and is determined to act on those values.

 C provides various types of data-types which allow the programmer to select the appropriate type for

the variable to set its value.

18
DATA TYPES (CONT....)

C DATA TYPES ARE USED TO

 Identify the type of a variable when it declared.

 Identify the type of the return value of a function.

 Identify the type of a parameter expected by a function.

19
DATA TYPES (CONT....)

C PROVIDES THREE TYPES OF DATA TYPES

Primary(Built-in) Data Types:

void, int, char, double and float.

Derived Data Types:

Array, References, and Pointers.

User Defined Data Types:

Structure, Union, and Enumeration

20
PRIMARY (BUILT-IN) DATA TYPES

21
22
DERIVED DATA TYPES

23
USER DEFINED DATA TYPES

C allows the feature called type definition which allows programmers to define their identifier that would
represent an existing data type. There are three such types:

24
C - INPUT AND OUTPUT

 Input means to provide the program with some data to be used in the program

 Output means to display data on screen or write the data to a printer or a file.

 C programming language provides many built-in functions to read any given input and to display data

on screen when there is a need to output the result.

25
SCANF() AND PRINTF() FUNCTIONS

 The standard input-output header file, named stdio.h contains the definition of the functions printf() and
scanf(), which are used to display output on screen and to take input from user respectively.

Syntax
 scanf (“format string”, &var1,&var2….);
 scanf (“%d”, &i);

 printf (“format string”, var1,var2….);


 printf (“%d”, i);

26
27
28
29
GETCHAR() & PUTCHAR() FUNCTIONS

 getchar() function is used to get/read a character from keyboard input.

 putchar() function is used to write a character on standard output/screen.

30
31
GETS() & PUTS() FUNCTIONS

gets() function

 The gets() function is similar to scanf() function


 but it allows entering some characters by the user in string format with the double quotation and stored
in a character array format.

puts() function
 The puts() function is similar to printf() function.
 but puts() function is used to display only the string after reading by gets() function entered by
user(gets similar to scanf() function)

32
33
DIFFERENCE BETWEEN SCANF() AND GETS()

 The main difference between these two functions is that

 scanf() stops reading characters when it encounters a space,

 but gets() reads space as character too.

34
CONTROL STATEMENTS IN C

DECISION MAKING IN C
• Decision making is about deciding the order of execution of statements based on certain conditions or
repeat a group of statements until certain specified conditions are met.

• C language handles decision-making by supporting the following statements,

 if statement
 switch statement
 conditional operator statement (? : operator)
 goto statement

35
DECISION MAKING WITH IF STATEMENT

 The if statement may be implemented in different forms depending on the complexity of conditions to

be tested.

 The different forms are,

 Simple if statement

 if....else statement

 Nested if....else statement

 Using else if statement

36
SIMPLE IF STATEMENT

 The general form of a simple if statement is,

 If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is
skipped and only the statement-outside is executed.

37
SIMPLE IF STATEMENT

38
IF...ELSE STATEMENT

 The general form of a simple if...else statement is,

 If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and
statement-block2 is executed.

39
IF...ELSE STATEMENT

40
NESTED IF....ELSE STATEMENT

 The general form of a nested if...else statement is,

 if expression is false then statement-block3 will be


executed, otherwise the execution continues and
enters inside the first if to perform the check for
the next if block, where if expression 1 is true the
statement-block1 is executed otherwise statement-
block2 is executed.

41
NESTED IF....ELSE STATEMENT

42
ELSE IF LADDER

 The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found,
the statement associated with it is executed.

43
ELSE IF LADDER

44
SWITCH STATEMENT IN C

 Switch statement is a control statement that allows us to choose only one choice among the many

given choices.

 The expression in switch evaluates to return an integral value, which is then compared to the values

present in different cases.

 It executes that block of code which matches the case value.

 If there is no match, then default block is executed (if present).

45
SWITCH STATEMENT IN C

The general form of switch statement is,

46
RULES FOR USING SWITCH STATEMENT

 The expression (after switch keyword) must yield an integer value i.e the expression should be an
integer or a variable or an expression that evaluates to an integer.
 The case label values must be unique.
 The case label must end with a colon (:)
 The next line, after the case statement, can be any valid C statement.
 break statements are used to exit the switch block.
 It isn't necessary to use break after each block, but if you do not use it, then all the consecutive blocks
of code will get executed after the matching block.

47
SWITCH STATEMENT IN C

48
49
DIFFERENCE BETWEEN SWITCH AND IF

 if statements can evaluate float conditions. switch statements cannot evaluate float conditions.

 if statement can evaluate relational operators.

 switch statement cannot evaluate relational operators

 i.e they are not allowed in switch statement.

50
LOOPING STATEMENTS IN C

 In any programming language including C, loops are used to execute a set of statements repeatedly
until a particular condition is satisfied.

• As per the above diagram, if the Test Condition is


true, then the loop is executed, and if it is false
then the execution breaks out of the loop.
• After the loop is successfully executed the
execution again starts from the Loop entry and
again checks for the Test condition, and this keeps
on repeating.

51
TYPES OF LOOP

There are 3 types of Loop in C language, namely:

 while loop

 for loop

 do while loop

52
53
54
55
The for loop is executed as follows:
 It first evaluates the initialization code.
 Then it checks the condition expression.
 If it is true, it executes the for-loop body.
 Then it evaluate the increment/decrement condition and again follows from step 2.
 When the condition expression becomes false, it exits the loop.

56
57
58
59
60
61
Jumping Out of Loops
 Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop
as soon as certain condition becomes true. This is known as jumping out of loop.
break statement
 When break statement is encountered inside a loop, the loop is immediately exited and the program
continues with the statement immediately following the loop.

62
continue statement
o It causes the control to go directly to the test-condition and then continue the loop process.
o On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.

63
FUNCTIONS IN C

 A function is a block of code that performs a particular task


 C language provides an approach in which you can declare and define a group of statements once in
the form of a function and it can be called and used whenever required.

 C functions can be classified into two categories,


 Library functions
 User-defined functions

64
FUNCTIONS IN C

 Library functions are those functions which are already defined in C library, example printf(), scanf(),
strcat() etc.
 We just need to include appropriate header files to use these functions.
 These are already declared and defined in C libraries.
 User-defined functions on the other hand, are those functions which are defined by the user at the time
of writing program.
 These functions are made for code reusability and for saving time and space.

65
BENEFITS OF USING FUNCTIONS

 It provides modularity to your program's structure.

 It makes your code reusable.

 You just have to call the function by its name to use it, wherever required.

 In case of large programs with thousands of code lines, debugging and editing becomes easier if you

use functions.

 It makes the program more readable and easy to understand.

66
FUNCTION DECLARATION

 Like any variable or an array, a function must also be declared before its used.

 Function declaration informs the compiler about the function name, parameters is accept, and its return

type.

 The actual body of the function can be defined separately.

 It's also called as Function Prototyping.

67
Function declaration consists of 4 parts.
 returntype
 function name
 parameter list
 terminating semicolon

68
returntype

 When a function is declared to perform some sort of calculation or any operation and is expected to

provide with some result at the end, in such cases, a return statement is added at the end of function

body.

 Return type specifies the type of value (int, float, char, double) that function is expected to return to the

program which called the function.

functionName

 Function name is an identifier and it specifies the name of the function.

 The function name is any valid C identifier and therefore must follow the same naming rules like other

variables in C language. 69
parameter list

 The parameter list declares the type and number of arguments that the function expects when it is

called.

 Also, the parameters in the parameter list receives the argument values when the function is called.

 They are often referred as formal parameters.

70
71
FUNCTION DEFINITION

 •The first line returntype functionName(type1 parameter1, type2 parameter2,...) is known as function
header and the statement(s) within curly braces is called function body.
Syntax

Note: While defining a function, there is no semicolon(;) after the parenthesis in the function
header, unlike while declaring the function or calling the function.

72
functionbody

 The function body contains the declarations and the statements(algorithm) necessary for performing

the required task.

 The body is enclosed within curly braces { ... } and consists of three parts.

 local variable declaration (if required).

 function statements to perform the task inside the function.

 a return statement to return the result evaluated by the function (if return type is void,

then no return statement is required).

73
CALLING A FUNCTION

 When a function is called, control of the program gets transferred to the function.

Syntax

 In the example above, the statement multiply(i, j); inside the main() function is function call.

74
PASSING ARGUMENTS TO A FUNCTION

 Arguments are the values specified during the function call, for which the formal parameters are
declared while defining the function.

75
RETURNING A VALUE FROM FUNCTION

 A function may or may not return a result.

 But if it does, we must use the return statement to output the result.

 return statement also ends the function execution, hence it must be the last statement of any function.

 If you write any statement after the return statement, it won't be executed.

 The datatype of the value returned using the return statement should be same as the return type

mentioned at function declaration and definition.

 If any of it mismatches, you will get compilation error.

76
TYPE OF USER-DEFINED FUNCTIONS IN C

There can be 4 different types of user-defined functions, they are:

 Function with no arguments and no return value

 Function with no arguments and a return value

 Function with arguments and no return value

 Function with arguments and a return value

77
FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUE

• Such functions can either be used to display information or they are completely dependent on user
inputs.

78
FUNCTION WITH NO ARGUMENTS AND A RETURN VALUE

 We have modified the above example to make the function greatNum() return the number which is
greater amongst the 2 input numbers.

79
FUNCTION WITH ARGUMENTS AND NO RETURN VALUE

 We are using the same function as example again and again, to demonstrate that to solve a problem

there can be many different ways.

 This time, we have modified the above example to make the function greatNum() take two int values as

arguments, but it will not be returning anything

80
81
FUNCTION WITH ARGUMENTS AND A RETURN VALUE

• This is the best type, as this makes the function completely independent of inputs and outputs, and only
the logic is defined inside the function body.

82
NESTING OF FUNCTIONS

• C language also allows nesting of functions i.e to use/call one function inside another function's body.

83
RECURSION

 Recursion is a special way of nesting functions, where a function calls itself inside it.
Syntax

84
85
CALL BY VALUE

 Calling a function by value means, we pass the values of the arguments which are stored or copied into
the formal parameters of the function.
 Hence, the original values are unchanged only the parameters inside the function changes.

86
87
CALL BY REFERENCE

 In call by reference we pass the address(reference) of a variable as argument to any function.

 When we pass the address of any variable as argument, then the function will have access to our

variable, as it now knows where it is stored and hence can easily update its value.

88
89
90
91
92
93
94

You might also like