Chapter 9 Xii NOTES
Chapter 9 Xii NOTES
Function
A function is a block of statements that performs a specific task. Let’s say you are writing a C
program and you need to perform a same task in that program more than once. In such case you have
two options:
a)Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to perform that task.
.
Library function / pre-define function
Also referred to as predefined functions, library functions are already defined in the C
libraries. This means that we do not have to write a definition or the function’s body to call
them. We can simply call them without defining them as they are already defined. However,
we need to include the library at the beginning of the code for calling a library function. We
can then use the proper syntax of the function to call them. Printf(), scanf(), ceil(), and floor()
are examples of library functions.
User Define Function
A user-defined function is a type of function in C language that is defined by the user himself to
perform some specific task. It provides code reusability and modularity to our program. User-
defined functions are different from built-in functions as their working is specified by the user and
no header file is required for their usage.
In this article, we will learn about user-defined function, function prototype, function definition,
function call, and different ways in which we can pass parameters to a function.
Variables
In C, a variable is a named storage location that holds a value. Variables are fundamental to programming as
they allow you to store and manipulate data in your program. Here are some key points about variables in the C
programming language:
To declare a variable in C, you need to specify its data type and a name. For example:
int age;
float salary;
char initial;
Local Variable
It is generally declared inside a function.
If it isn’t initialized, a garbage value is stored inside it.
It is created when the function begins its execution.
It is lost when the function is terminated.
Data sharing is not possible since the local variable/data can be accessed by a single function.
Parameters need to be passed to local variables so that they can access the value in the
function.
It is stored on a stack, unless mentioned otherwise.
They can be accessed using statement inside the function where they are declared.
When the changes are made to local variable in a function, the changes are not reflected in the
other function.
Local variables can be accessed with the help of statements, inside a function in which they
are declared.
Global Variable
It is declared outside the function.
If it isn’t initialized, the value of zero is stored in it as default.
It is created before the global execution of the program.
It is lost when the program terminates.
Data sharing is possible since multiple functions can access the global variable.
They are visible throughout the program, hence passing parameters is not required.
It can be accessed using any statement within the program.
It is stored on a specific location inside the program, which is decided by the compiler.
When changes are made to the global variable in one function, these changes are reflected in
the other parts of the program as well.
Static Variables in C
A static variable in C is a variable that is defined using the static keyword. It can be defined only once
in a C program and its scope depends upon the region where it is declared (can be global or local).
The default value of static variables is zero.
Syntax of Static Variable in C
static data_type variable_name = initial_value;
Local Variables in C
A Local variable in C is a variable that is declared inside a function or a block of code. Its scope is
limited to the block or function in which it is declared.
#include <stdio.h>
void function()
{
int x = 10; // local variable
printf("%d", x);
}
A Global variable in C is a variable that is declared outside the function or a block of code. Its scope
is the whole program i.e. we can access the global variable anywhere in the C program after it is
declared.
#include <stdio.h>
int x = 20; // global variable
void function1()
{
printf("Function 1: %d\n", x);
}
void function2()
{
printf("Function 2: %d\n", x);
}
int main()
{
function1();
function2();
getch();
}
Automatic Variable in C
All the local variables are automatic variables by default. They are also known as auto variables.
Their scope is local and their lifetime is till the end of the block. If we need, we can use
the auto keyword to define the auto variables.
The default value of the auto variables is a garbage value.
Syntax of Auto Variable in C
Auto data_type variable_name;
or
data_type variable_name; (in local scope)
External Variables in C
External variables in C can be shared between multiple C files. We can declare an external variable
using the extern keyword.
Their scope is global and they exist between multiple C files.
Register Variables in C
Register variables in C are those variables that are stored in the CPU register instead of the
conventional storage place like RAM. Their scope is local and exists till the end of the block or a
function.
These variables are declared using the register keyword.
The default value of register variables is a garbage value.
Syntax of Register Variables in C
register data_type variable_name = initial_value;
Advantages of function
We can avoid rewriting application code by using functions.
We can create an application in module structure by using functions.
By using the function, program development will be simplified.
When an application is developed in module format, it is simple to trace and debug the
programs.
We may track the program's logic implementation by using the functions.
Code reuse is the function's primary goal.
A c program is made up of several functions.
We can call any other function from any other function.
The process of compilation will always go top to bottom.
The main will be the sole variable used during execution, from beginning to end.
To avoid a compilation problem during implementation, we must use a forward declaration,
or prototype, when using a function that was defined later.
Function declaration calls for the specification of the return type, function name, and
parameter type information.
The initial line of a function definition is referred to as the function declarator or function
header.
If a function never returns any values, then specify void as the return type.
Whenever a function returns a value during implementation, the return type must be specified
as the return value type. i.e., a return statement of the same type as the value we are returning
is necessary.
The function's default parameter type is void, and its default return type is an int.
Ch#10 Array
Array
An array is a fixed-size collection of similar data items stored in contiguous memory locations. It
can be used to store the collection of primitive data types such as int, char, float, etc., and also
derived and user-defined data types such as pointers, structures, etc. Array in C is one of the most
used data structures in C programming. It is a simple and fast way of storing multiple values under
a single name. In C, we have to declare the array like any other variable before using it. We can
declare an array by specifying its name, the type of its elements, and the size of its dimensions.
When we declare an array in C, the compiler allocates the memory block of the specified size to the
array name.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
Initialization in C is the process to assign some initial value to the variable. When the array is
declared or allocated memory, the elements of the array contain some garbage value. So, we
need to initialize the array to some meaningful value. There are multiple ways in which we can
initialize an array in C.
One-dimensional array
A one-dimensional array in the C programming language is a collection of elements of the
same data type arranged in a linear sequence. Here's an example of how to declare, initialize,
and access elements of a one-dimensional array in C.
Multi-Dimensional Array
In C, a multi-dimensional array is essentially an array of arrays. C supports two-dimensional
arrays and, to some extent, arrays with more than two dimensions.
The syntax of multi-Dimensional Array
data_type array_name[size1][size2]....[sizeN ];
data_type: Type of data to be stored in the array.
array_name: Name of the array.
size1, size2,…, sizeN: Size of each dimension.
The total number of elements that can be stored in a multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
#include <stdio.h>
int main() {
// Initializing an array during declaration
int numbers1[] = {1, 2, 3, 4, 5};
return 0;
}
In this example, the array numbers1 is declared and initialized with values {1, 2, 3, 4,
5} during the declaration itself.
Pointers in C Language
Pointers are one of the core components of the C programming language. A pointer can be
used to store the memory address of other variables, functions, or even other pointers. The
use of pointers allows low-level memory access, dynamic memory allocation, and many other
functionality in C. To declare a pointer, you use the * (asterisk) symbol.
Syntax of pointers
int *ptr;
Types of Pointers in C
Pointers in C can be classified into many different types based on the parameter on which we are
defining their types. If we consider the type of variable stored in the memory location pointed by the
pointer, then the pointers can be classified into the following types:
1. Integer Pointers
As the name suggests, these are the pointers that point to the integer values.
Syntax
int *ptr;
Array Pointer
Pointers and Array are closely related to each other. Even the array name is the pointer to its first
element. They are also known as Pointer to Arrays. We can create a pointer to an array using the
given syntax.
Syntax
char *ptr = &array_name;
Structure Pointer
The pointer pointing to the structure type is called Structure Pointer or Pointer to Structure. It can
be declared in the same way as we declare the other primitive data types.
Syntax
struct struct_name *ptr;
String in c Language
The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The
character array or the string is used to manipulate text such as word or sentences. Each character in
the array occupies one byte of memory, and the last character must always be 0. The termination
character ('\0') is important in a string since it is the only way to identify where the string ends. When
we define a string as chars[10], the characters[10] is implicitly initialized with the null in the memory.
There are two ways to declare a string in c language.
1. By char array
2. By string literal
Let's see the example of declaring string by char array in C language.
char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
Sr.No
Function & Purpose
.
strcpy(s1, s2);
1
Copies string s2 into string s1.
strcat(s1, s2);
2
Concatenates string s2 onto the end of string s1.
strlen(s1);
3
Returns the length of string s1.
strcmp(s1, s2);
4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0
if s1>s2.
strchr(s1, ch);
5
Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2);
6
Returns a pointer to the first occurrence of string s2 in string s1.
Let's see a simple example where a string is declared and being printed. The '%s' is used as a format
specifier for the string in c language.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
5. char ch2[11]="javatpoint";
6. printf("Char Array Value is: %s\n", ch);
7. printf("String Literal Value is: %s\n", ch2);
8. getch();
9. }
Difference b/w structure and unions.
Parameter Structure Union
Accessing A user can access individual A user can access only one member at
Members members at a given time. a given time.
#include directors
The #include preprocessor directive is used to paste code of given file into current file. It is used
include system-defined and user-defined header files. If included file is not found, compiler renders
error.
By the use of #include directive, we provide information to the preprocessor where to look for the
header files. There are two variants to use #include directive.
1. #include <filename>
2. #include "filename"
The #include <filename> tells the compiler to look for the directory where system header files are
held. In UNIX, it is \usr\include directory.
The #include "filename" tells the compiler to look in the current directory from where program is
running.
Let's see a simple example of #include directive. In this program, we are including stdio.h file because
printf() function is defined in this file.
1. #include<stdio.h>
2. int main(){
3. printf("Hello C");
4. return 0;
5. }
Note 1: In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is treated as
filename.
Note 2: In #include directive, backslash is considered as normal text not escape sequence. So in case of #include
<a\nb>, a\nb is treated as filename.
Note 3: You can use only comment after filename otherwise it will give error.