Unit 1 Powerpoint Slides
Unit 1 Powerpoint Slides
1
PROGRAMMING IN C AND C++ (SBSA1102)
COURSE OUTCOMES
CO3 - Develop and Implement applications using memory allocation and File concepts.
2
INTRODUCTION TO C
1. Easy to learn
2. Structured language
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,
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.
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.
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
9
STRUCTURE OF C (CONT...)
Documentation Section
Link Section
Definition Section
Global Documentation
Section
Main Function
10
STRUCTURE OF C (CONT...)
Documentation Section
The documentation section is the part of the program where the programmer gives the details
Example
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...)
This part of the code is the part where the global variables are declared.
The user-defined functions are also declared in this part of the code.
Example:
int a=7;
14
STRUCTURE OF C (CONT...)
Example:
int main(void)
{
int a=10;
printf(" %d", a);
return 0;
15
STRUCTURE OF C (CONT...)
All the user-defined functions are defined in this section of the program.
Example:
return a+b;
16
FIRST C PROGRAM
#include <stdio.h>
int main()
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.
C provides various types of data-types which allow the programmer to select the appropriate type for
18
DATA TYPES (CONT....)
19
DATA TYPES (CONT....)
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
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);
26
27
28
29
GETCHAR() & PUTCHAR() FUNCTIONS
30
31
GETS() & PUTS() FUNCTIONS
gets() function
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()
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.
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.
Simple if statement
if....else statement
36
SIMPLE IF STATEMENT
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
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
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
45
SWITCH STATEMENT IN C
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.
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.
51
TYPES OF LOOP
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
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
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.
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.
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
functionName
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.
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 body is enclosed within curly braces { ... } and consists of three parts.
a return statement to return the result evaluated by the function (if return type is void,
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
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
76
TYPE OF USER-DEFINED FUNCTIONS IN C
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
This time, we have modified the above example to make the function greatNum() take two int values as
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
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