1. INTRODUCTION TO C
BASIC STRUCTURE OF A C PROGRAM
Main sections to a basic C program.
Documentation
Link
Definition
Global Declarations
Main functions
Subprograms
3. INTRODUCTION TO C
BASIC STRUCTURE OF A C PROGRAM
Documentation Section
He usually gives the name of the program, the details of the author and
other details. It gives anyone reading the code the overview of the code.
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.
4. INTRODUCTION TO C
BASIC STRUCTURE OF A C PROGRAM
Definition Section
we define different constants. The keyword define is used in this part.
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.
5. INTRODUCTION TO C
BASIC STRUCTURE OF A C PROGRAM
Main Function Section
Every C-programs needs to have the main function.
Each main function contains 2 parts.
Declaration part
Execution part.
Both the declaration and execution part are inside the curly
braces.
6. INTRODUCTION TO C
BASIC STRUCTURE OF A C PROGRAM
Sub Program Section
All the user-defined functions are defined in this section of the
program.
7. INTRODUCTION TO C
FUNDAMENTAL CHARACTER SET
Set of all valid character which is used to form a words, numbers and
expression’s in source program.
Type of C character set
Alphabets
Digit
Special Character
White space
8. INTRODUCTION TO C
IDENTIFIERS
Identifiers are user-defined names of variables, functions and arrays.
It comprises of combination of letters and digits.
It must begin with an alphabet or an underscore and not digits.
It must contain only alphabets, digits or underscore.
A keyword cannot be used as an identifier
Must not contain white space.
9. INTRODUCTION TO C
KEYWORDS
Keywords are special words in C programming which have their own
predefined meaning.
The functions and meanings of these words cannot be altered.
10. INTRODUCTION TO C
KEYWORDS
auto break case char
continue const do default
double else enum extern
for float go if
int long register return
sign static sizeof short
struct switch typedef union
void volatile while unsigned
11. INTRODUCTION TO C
DATA TYPES
Refers to the type of data used to store the information.
Four different data types can be used to differentiate and store
various types of data.
12. INTRODUCTION TO C
DATA TYPES
Type Data types
Basic data types int, char, float, & double
Derived data types array, pointer, structure, & union
Enumeration data type enum
Void data type void
13. INTRODUCTION TO C
CONSTANT
The value which cannot be modified during the execution of the program.
Constant for representing the fixed values which do not change.
Types of Constants
Integer Constants
Floating Point Constants
Character Constants
String Constants
Enumeration Constants
14. INTRODUCTION TO C
VARIABLES
A variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique
name (identifier).
Variable names are just the symbolic representation of a memory
location.
16. INTRODUCTION TO C
EXPRESSION
Expressions are the combination of variables, operands, and operators.
Types of Expressions in C
Arithmetic expressions
Relational expressions
Logical expressions
Conditional expressions
17. INTRODUCTION TO C
STATEMENTS
Statements is an executable part of the program it will do some
action.
Expression Statements
Compound Statements
Selection Statements
Iterative Statements
Jump Statements
18. INTRODUCTION TO C
OPERATORS
Operators take part in a program for manipulating data and variables
and form a part of the mathematical, conditional, or logical
expressions.
19. INTRODUCTION TO C
OPERATORS
Types of Operators
Arithmetic Operator
Assignment Operator
Relational Operator
Logical Operator
Bitwise Operator
20. INTRODUCTION TO C
LIBRARY FUNCTIONS
Library functions or simply C Library functions are inbuilt functions in
C programming.
The prototype and data definitions of these functions are present in
their respective header files. To use these functions we need to
include the header file in our program.
21. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
DATA INPUT OUTPUT FUNCTIONS
Accepting of data as input
The processing of data
The generation of output
22. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
DATA INPUT OUTPUT FUNCTIONS
Types of Input and Output functions
Unformatted Data input and output functions
Formatted Data input and output functions
23. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
DATA INPUT OUTPUT FUNCTIONS
Unformatted Data input and output functions
The unformatted functions are not capable of controlling the format
that is involved in writing and reading the available data.
getchar() & putchar() functions
gets() & puts() functions
24. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
DATA INPUT OUTPUT FUNCTIONS
Formatted Data input and output functions
printf( )
scnanf( )
The formatted functions basically present or accept the available
data (input) in a specific format.
25. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
DATA INPUT OUTPUT FUNCTIONS
printf( ) function
printf() is formatted output function which is used to display some
information on standard output unit.
Syntax
printf(“format_specifies”, var1, var2, var3, …, varN);
26. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
DATA INPUT OUTPUT FUNCTIONS
scanf( ) function
scanf() is formatted input function which is used to read formatted
data from standard input device and automatically converts numeric
information to integers and floats. It is defined in stdio.h.
Syntax
scanf(“format_string”, &var1, &var2, &var3, …, &varN);
27. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
SIMPLE C PROGRAMS
#include <stdio.h>
void main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
}
28. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
FLOW OF CONTROL
A special statement to control the execution of one or more statements
depending on a condition. Such statements are called control statements
or control-flow statements.
Types of control-flow statements
Selection Statement
Iterative Statement
Unconditional Control Statement
29. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
Selection Statement
It is a statement whose execution results in a choice being made as
to which of two or more paths should be followed.
if construct
if-else construct
if-else-if construct
Nested if-else-if construct
switch-case construct
30. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
IF STATEMENT
The if construct is a selective statement, the statements within the
block are executed only once when the condition evaluates to true,
otherwise the control goes to the first statement after the if
construct.
31. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
IF STATEMENT
The general form
if (condition)
{
statement-1;
statement-2;
....
statement-n;
}
33. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
IF-ELSE STATEMENT
An if statement will execute its block only when the condition
evaluates to 1 (true).
We can also conditionally execute another block when the condition
evaluates to 0 (false) using the else construct.
34. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
IF-ELSE STATEMENT
The general syntax
if (expression)
{
statement-1;
}
else
{
statement-2;
}
36. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
IF-ELSE-IF STATEMENT
Multiple if constructs can be chained to any length. The else
construct which appears at the end is optional, and if it is to be
included it has to be only at the end.
The if-else-if construct is used whenever we have multiple mutually
exclusive if conditions that work on the same input.
37. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
IF-ELSE-IF STATEMENT
General form
if (expression_1) {
statement_1;
} else if (expression_2) {
statement_2;
} else if (expression_3) {
statement_3;
} else if (expression_4) {
statement_4;
} else {
statement_5;
}
38. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
The process of repeatedly executing a statement or group of
statements until the condition is satisfied is called looping.
While loop
Do..while loop
for loop
39. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
WHILE LOOP
To evaluate a test condition and iterate over the loop body until the
condition returns True. The loop ends when the condition returns
False.
40. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
while (condition)
{
body of while loop
}
42. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
DO….WHILE LOOP
The body is executed if and only if the condition is true. In some
cases, we have to execute a body of the loop at least once even if the
condition is false.
This type of operation can be achieved by using a do-while loop.
43. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
DO….WHILE LOOP
do {
statements
} while (expression);
45. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
FOR LOOP
The initial value of the for loop is performed only once.
The condition is a Boolean expression that tests and compares the
counter to a fixed value after each iteration, stopping the for loop when
false is returned.
The incrementation/decrementation increases (or decreases) the counter
by a set value.
46. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
LOOP STATEMENT
FOR LOOP
Syntax
for (initial value; condition; incrementation or decrementation )
{
statements;
}
51. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
BREAK AND CONTINUE
Break
The break statement ends the loop immediately when it is
encountered.
syntax
break;
52. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
BREAK AND CONTINUE
Continue
The continue statement skips the current iteration of the loop and
continues with the next iteration.
Syntax
continue;
53. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
GO - TO STATEMENTS
The goto statement allows us to transfer control of the program to the
specified label.
Syntax
goto label;
... .. ...
... .. ...
label:
statement;
54. INPUT, OUTPUT FUNCTIONS AND CONTROL STRUCTURES
COMMA OPERATOR
The comma operator in C is denoted by ,. The comma operator in C
has the least precedence.
The comma operator in C ensures that two or more expressions
separated by commas are evaluated one by one from left to right,
and the result of the entire expression is the value of the rightmost
expression.
55. FUNCTIONS
Definition
A function is a block of code that performs a specific task.
Benefits of functions
Enables reusability and reduces redundancy
Makes a code modular
Provides abstraction functionality
The program becomes easy to understand and manage
Breaks an extensive program into smaller and simpler pieces
56. FUNCTIONS
Basic Syntax of Functions
return_type function_name(arg1, arg2, … argn)
{
Body of the function //Statements to be processed
}
57. FUNCTIONS
FUNCTION PROTO-TYPES
A function prototype is a declaration in the code that instructs the compiler
about the data type of the function, arguments and parameter list. As we all
know that a block of code which performs a specific task is called as a
function.
59. FUNCTIONS
PASSING ARGUMENTS
C programming function arguments also known as parameters are the
variables that will receive the data sent by the calling program. These
arguments serve as input data to the function to carry out the specified
task.
61. FUNCTIONS
Function arguments in c programming
Basically, there are two types of arguments:
Actual arguments
Formal arguments
62. FUNCTIONS
Function arguments in c programming
Two ways to pass arguments to the function
Pass by Value
Pass by value is a method in which a copy of the value of the variables is
passed to the function for the specific operation.
Pass by reference
Pass by reference is a method in which rather than passing direct value the
address of the variable is passed as an argument to the called function.
63. FUNCTIONS
RECURSIONS
A function that calls itself is known as a recursive function. And, this technique is known as recursion.
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
64. FUNCTIONS
STORAGE CLASSES
The variable scope.
The location where the variable will be stored.
The initialized value of a variable.
A lifetime of a variable.
65. FUNCTIONS
There are 4 types of storage class
automatic
external
static
register
66. FUNCTIONS
MULTIFILE PROGRAMS
If a program contains many functions then it is difficult to read and
maintain because when the number of functions increases the size of the
program/file will also increase. To avoid this some functions can be written
in separate file
The individual files will be compiled separately and then linked together to
form one executable object program.
67. FUNCTIONS
MULTIFILE PROGRAMS
If a program contains many functions then it is difficult to read and
maintain because when the number of functions increases the size of the
program/file will also increase. To avoid this some functions can be written
in separate file
The individual files will be compiled separately and then linked together to
form one executable object program.
68. ARRAYS
An array is a collection of variables of same data types.
Advantage of Arrays
Less amount of code :
Easy access of elements :
Easy to implement algorithms :
Random Access :
71. ARRAYS
Single dimensional arrays
Single dimensional array or 1-D array is the simplest form of
arrays that can be found in C. This type of array consists of
elements of similar types and these elements can be accessed
through their indices.
72. ARRAYS
Multi-dimensional Arrays
The most common type of multi-dimensional array that is used in
the C language is a 2-D array. However, the number of dimensions
can be more than 2 depending upon the compiler of the user’s
system. These arrays consist of elements that are array
themselves.
74. PASSING ARRAYS TO FUNCTIONS
An array is a collection of similar data types which are stored in
memory as a contiguous memory block.
This means multi-dimensional arrays are also a continuous block
of data in our memory. In C, there are several times when we are
required to pass an array to a function argument.
75. ARRAYS AND STRING
A string is a sequence of characters terminated with a null
character.
Example
char c[] = "c string";
76. STRUCTURES
A struct (or structure) is a collection of variables (can be of
different types) under a single name.
To define a struct, the struct keyword is used.
79. USER DEFINED DATA TYPES
Union
A union is also a user-defined data type. It also holds members of
different data types under a single name. A union sounds similar
to a structure and they are similar in conceptual terms. But there
are some major differences between the two.
While a structure allocates sufficient memory for all its members,
a union only allocates memory equal to its largest member.
80. USER DEFINED DATA TYPES
Union
Syntax
union union_name
{
data_type var1;
data_type var2;
};
81. USER DEFINED DATA TYPES
Enumeration
Enumeration or simply enum is one of the user-defined data types
in C which provides a special type of flexibility of defining
variables. An enum consists of a set of integer constants that can
be replaced by user-defined names.
Syntax
enum flag {const_name1, const_name2, ..., const_nameN};
82. PASSING STRUCTURES TO FUNCTIONS
A structure can be passed to any function from main function or
from any sub function.
Structure definition will be available within the function only.
It won’t be available to other functions unless it is passed to those
functions by value or by address(reference).
83. PASSING STRUCTURES TO FUNCTIONS
Passing structure to function
It can be done in below 3 ways.
1. Passing structure to a function by value
2. Passing structure to a function by address(reference)
3. No need to pass a structure – Declare structure variable as
global
84. SELF-REFERENTIAL STRUCTURES
A self-referential structure is a structure that contains a pointer to
a variable of the same type. This allows the structure to refer to
itself, creating a linked data structure.
The concept of a self-referential structure is based on the idea of a
linked list, which is a collection of data elements that are
connected to each other through a series of pointers.
85. BITWISE OPERATIONS
In the arithmetic-logic unit (which is within the CPU),
mathematical operations like: addition, subtraction,
multiplication and division are done in bit-level. To perform bit-
level operations in C programming, bitwise operators are used.
86. POINTERS
A Pointer is a variable that holds the memory address of another
variable.
POINTER DECLARATION
datatype *pointer_variableName;
Example
int *ptr;
87. POINTERS
A Pointer is a variable that holds the memory address of another
variable.
POINTER DECLARATION
datatype *pointer_variableName;
Example
int *ptr;
88. PASSING POINTERS TO FUNCTIONS
Pointers can also be passed as an argument to a function like any
other argument. Instead of a variable.
Declare a function which accepts a pointer as an argument
return_type function_name(int*);
89. OPERATION IN POINTERS
An address in a memory is a numeric value we can perform
arithmetic operations on the pointer values.
Different operations
Assignment
Value finding
Taking a pointer address
Incrementing and Decrement a pointer
90. POINTER AND ARRAYS
Pointers and Array representations are very much related to each
other and can be interchangeably used in the right context.
Arrays can be single or multidimensional and are stored in contiguous
memory blocks in our system.
91. ARRAYS AND POINTERS
Pointer
Address of a variable in memory.
Allows us to indirectly access variables.
Arrays
Array is a group of elements that share a common name, and that are
different from one another by their positions within the array.
93. STRUCTURES AND POINTERS
Structure pointer points to the address of the structure variable in
the memory block to which it points.
This pointer can be used to access and change the value of structure
members.
95. FILES
A file represents a sequence of bytes on the disk where a group of
related data is stored. File is created for permanent storage of data. It
is a ready made structure.
A file is a container in computer storage devices used for storing data.
96. FILES
Basic file operations
Function Description
fopen() create a new file or open a existing file
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from a file
fprintf() writes a set of data to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position to desire point
ftell() gives current position in the file
rewind() set the position to the begining point