Showing posts with label PST. Show all posts
Showing posts with label PST. Show all posts

Storage Classes and its type in c

                          Instagram                              Twitter                        YouTube

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

--------------------------------------------------------------------------------------------------------------------

        INTRODUCTION STORAGE CLASS & THERE TYPES

Storage Class

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify. We have four different storage classes in a C program −


auto
register
static
extern
The auto Storage Class
The auto storage class is the default storage class for all local variables.

{
   int mount;
   auto int month;
}
The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.

The register Storage Class
The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

{
   register int miles;
}
The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

The static Storage Class
The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

Live Demo
#include <stdio.h>
 
/* function declaration */
void func(void);
 
static int count = 5; /* global variable */
 
main() {

   while(count--) {
      func();
   }
   return 0;
}

/* function definition */
void func( void ) {

   static int i = 5; /* local static variable */
   i++;

   printf("i is %d and count is %d\n", i, count);
}
When the above code is compiled and executed, it produces the following result −

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
The extern Storage Class
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

First File: main.c

#include <stdio.h>
 
int count ;
extern void write_extern();
 
main() {
   count = 5;
   write_extern();
}
Second File: support.c

#include <stdio.h>
 
extern int count;
 
void write_extern(void) {
   printf("count is %d\n", count);
}


---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BCA                                        PST & Programming C                                   Allrounder Sita Ram Sahu
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>

Function

 https://drive.google.com/file/d/1U9sYX33X1tzGqnRMbrnUuXQB8uJjbJlk/view?usp=sharing

https://drive.google.com/file/d/1f1UYWItGjVoDHo9WeLqDm7nRWLRlVePB/view?usp=sharing

https://drive.google.com/file/d/1AwqJEzzMaoQL_F9ufOQExdYWtukgc430/view?usp=sharing

https://drive.google.com/file/d/1RAwKTsnjgfyRETxpiCMuNGkIFWZtfpSm/view?usp=sharing

https://drive.google.com/file/d/13fcVGyU0koFNKebqKrIw2EzuWNn_PawE/view?usp=sharing

 PST-pdf                    Instagram                              Twitter                        YouTube

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

--------------------------------------------------------------------------------------------------------------------

1. INTRODUCTION  PST-PDF

-------------------------------------------------------------------------------------------------------------------

2. OPERATORS   PST-pdf

-------------------------------------------------------------------------------------------------------------------

3. LOOPING  PST-pdf

------------------------------------------------------------------------------------------------------------------

4. CONDITIONAL  PST-pdf

------------------------------------------------------------------------------------------------------------------

5. JUMPING STATEMENTS PST-pdf

------------------------------------------------------------------------------------------------------------------

6. ARRAY  PST-pdf

------------------------------------------------------------------------------------------------------------------

6. FUNCTION

------------------------------------------------------------------------------------------------------------------

7.1 Function

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

✔ There are many situations where we might need to write same line of code for more than once in a program.
✔ This may lead to unnecessary repetition of code, bugs and even becomes boring for the programmer.
✔ So, 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.

These functions defined by the user are also know as User-defined Functions

C functions can be classified into two categories,
✔ Library functions
✔ User-defined functions

Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.

A 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.


For Programming Training:
DM me Instagram

✔ It provides modularity to your programs 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.

Function Declaration Syntax:
returntype functionName(type1 parameter1, type2 parameter2,...);

✔ 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.
✔ Its also called as Function Prototyping.

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

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.

Note: In case your function doesnot return any value, the return type would be void.

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.

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.


For Programming Training:
DM me Instagram

 call by value is the default way of calling a function in c programming.
lets understand the terminologies that we will use in call by value.

actual parameters: the parameters that appear in function calls.
formal parameters: the parameters that appear in function declarations.
✔ when we pass the actual parameters while calling a function then this is known as function call by value.
✔ in this case the values of actual parameters are copied to the formal parameters.
✔ thus operations performed on the formal parameters don’t reflect in the actual parameters.



for programming training:
DM me Instagram
  
The Call by reference method of passing arguments to a function copies the address of an argument into the formal parameter.
✔ Inside the function, the address is used to access the actual argument used in the call.
✔ It means the changes made to the parameter affect the passed argument.
When we call a function by passing the addresses of actual parameters then this way of calling the function is known as call by reference.
✔ In call by reference, the operation performed on formal parameters, affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters.

For Programming Training:
DM me Instagram

 Library functions in C language are inbuilt functions which are grouped together and placed in a common place called library. We are including these header files in our C program using “#include” command to make use of the functions those are declared in the header files.

Library Functions in stdio.h
☞ printf()
☞ fopen()
☞ freopen()
☞ fclose()
☞ feof()
☞ ferror()
☞ putc()
☞ fgetc()
☞ fputc()
☞ fflush()
☞ fgetpos()
☞ fprintf()
☞ fputs()
☞ fread()
☞ fseek()
☞ fsetpos()
☞ ftell()
☞ fwrite()
☞ getc()
☞ snprintf()
☞ getchar()
☞ putchar()
☞ puts()
☞ remove()
☞ rename()
☞ rewind()

List of Most used Header Files
☞ stdio.h
☞ conio.h
☞ string.h
☞ stdlib.h
☞ math.h
☞ time.h
☞ ctype.h
☞ stdarg.h
☞ signal.h
☞ setjmp.h
☞ locale.h
☞ errno.h
☞ assert.h


For Programming Training:
DM me Instagram

 User Define Functions are those functions which are defined and declared by the programmer to do some specific task. These are designed to re-use the code.

Benefits:
Using functions, you can divide your large program into small pieces according to different task which will provide you easy access to read and debug. Using functions, you can re-use your code. You do not need to rewrite the code, just call the function and use it. Using functions, you can modularize your code.

Declaration:
return-type functionName(type-of-local-argument-list);

Define:
return-type functionName(type-of-local-argument-list);
{
Function-body/ set-of-statements;
}

Calling an User Define Functions
functionName(actual-argument-list);

return-type
It is the data type of function’s return value. If function does not return value, function’s return type must be void. void is a data type which represent nothing i.e. function will not return any value.

functionName
It is the name of those set of statements which are written in function’s body. functionName can be any valid identifier’s name, please do not use any reserve word as a function name.

type-of-local-argument-list
List of the data type of those arguments (parameters) which are using in the function definition.

actual-argument-list
List of those parameters/ arguments, which are being passed in calling function.


For Programming Training:
Dm me Instagram

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BCA                                        PST & Programming C                                   Allrounder Sita Ram Sahu
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>

Array

https://drive.google.com/file/d/1U9sYX33X1tzGqnRMbrnUuXQB8uJjbJlk/view?usp=sharing

https://drive.google.com/file/d/1f1UYWItGjVoDHo9WeLqDm7nRWLRlVePB/view?usp=sharing

https://drive.google.com/file/d/1AwqJEzzMaoQL_F9ufOQExdYWtukgc430/view?usp=sharing

https://drive.google.com/file/d/1RAwKTsnjgfyRETxpiCMuNGkIFWZtfpSm/view?usp=sharing

https://drive.google.com/file/d/13fcVGyU0koFNKebqKrIw2EzuWNn_PawE/view?usp=sharing

 PST-pdf                    Instagram                              Twitter                        YouTube

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

--------------------------------------------------------------------------------------------------------------------

1. INTRODUCTION  PST-PDF

-------------------------------------------------------------------------------------------------------------------

2. OPERATORS   PST-pdf

-------------------------------------------------------------------------------------------------------------------

3. LOOPING  PST-pdf

------------------------------------------------------------------------------------------------------------------

4. CONDITIONAL  PST-pdf

------------------------------------------------------------------------------------------------------------------

5. JUMPING STATEMENTS PST-pdf

------------------------------------------------------------------------------------------------------------------

6. ARRAY

------------------------------------------------------------------------------------------------------------------

6.1 Array

In C language, arrays are referred to as structured data types. An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.


Here the words,
✔ Finite means data range must be defined.
✔ Ordered means data must be stored in continuous memory addresses.
✔ Homogenous means data must be of similar data type.

Example where arrays are used,
✔ To store list of Employee or Student names,
✔ To store marks of students,
✔ To store list of numbers or characters etc.
✔ Since arrays provide an easy way to represent data, it is classified amongst the data structures in C. Other data structures in c are structure, lists, queues, trees etc. Array can be used to represent not only simple list of data but also table of data in two or three dimensions.

Declaring an Array
Like any other variable, arrays must be declared before they are used. General form of array declaration is,

data-type variable-name[size];

/* Example of array declaration */
int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means array arr can only contain 10 elements of int type.

Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0] address and the last element will occupy arr[9].

For Programming Training:
DM me Instagram

6.2 Array Initialization

Initialization of an Array 
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any random value). An array can be initialized at either compile time or at runtime.

Compile time Array initialization 
Compile time initialization of array elements is same as ordinary variable initialization. The general form of initialization of array is,

data-type array-name[size] = { list of values }; 

Here are a few examples 
int marks[4]={ 67, 87, 56, 77 }; 
// integer array initialization 

float area[5]={ 23.4, 6.8, 5.5 }; 
// float array initialization 

int marks[4]={ 67, 87, 56, 77, 59 }; 
// Compile time error 

One important thing to remember is that when you will give more initializer(array elements) than the declared array size than the compiler will give an error.

For Programming Training:
DM me Instagram

6.3 Runtime Array initialization
✔ An array can also be initialized at runtime using scanf() function.
✔ This approach is usually used for initializing large array, or to initialize array with user specified values.


For Programming Training:
DM me Instagram

6.4 2D Array
C language supports multidimensional arrays also. The simplest form of a multidimensional array is the two-dimensional array. Both the rows and columns index begins from 0.

Two-dimensional arrays are declared as follows,

data-type array-name[row-size][column-size] 

/* Example */
int a[3][4];

An array can also be declared and initialized together. For example,

int arr[][3] = {
{0,0,0},
{1,1,1}
};

Note: 
✔ We have not assigned any row value to our array in the above example. 
✔ It means we can initialize any number of rows. 
✔ But, we must always specify number of columns, else it will give a compile time error. 
✔ Here, a 2*3 multi-dimensional matrix is created.


For Programming Training:
DM me Instagram

6.5 Characteristics of an Array

Array Characteristics
An array is defined as the group of similar data types, which takes contiguous memory locations. Array stores same kind of data.

Its just normal definition, which you can find anywhere. Here, we are discussing some of the characteristics of an array data type.

✔ An array is a derived data type, which is defined using basic data types like int, char, float and even structures (which is called the array of structures).
✔ Array elements are stored in contiguous memory blocks/subsequent memory blocks in primary memory.
✔ Array name represents its base address. The base address is the address of the first element of the array.
✔ Array index starts with 0 and ends with N-1. Here, N stands for the number of elements. For Example, there is an integer array of 5 elements, then it’s indexing will be 0 to 4.
✔ Only constants and literal values (an integer value like 5, 10, 12,…) can be assigned the number of elements in an array.
✔ While declaring an array, we can also assign each element with 0 like this.

Like: int age[10]={0};

✔ There is no need to provide the number of elements while declaring an array, but we have to provide the values at the time of declaration. In this case, array size will be the number of values that you have provided.

Like: int age[]={21,22,25,45,56};

Here, size of the array will be 5, because there are 5 values.
✔ An array can be allocated anywhere in these memory permanents:
A) Data segment for static or globally declare array
B) Heap segment for dynamically allocated array
C) Stack segment for locally declare array


For Programming Training:
DM me Instagram

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BCA                                        PST & Programming C                                   Allrounder Sita Ram Sahu
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>

Jumping Statements:

https://drive.google.com/file/d/1U9sYX33X1tzGqnRMbrnUuXQB8uJjbJlk/view?usp=sharing

https://drive.google.com/file/d/1f1UYWItGjVoDHo9WeLqDm7nRWLRlVePB/view?usp=sharing

https://drive.google.com/file/d/1AwqJEzzMaoQL_F9ufOQExdYWtukgc430/view?usp=sharing

https://drive.google.com/file/d/1RAwKTsnjgfyRETxpiCMuNGkIFWZtfpSm/view?usp=sharing

https://drive.google.com/file/d/13fcVGyU0koFNKebqKrIw2EzuWNn_PawE/view?usp=sharing

 PST-pdf                    Instagram                              Twitter                        YouTube

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

--------------------------------------------------------------------------------------------------------------------

1. INTRODUCTION  PST-PDF

-------------------------------------------------------------------------------------------------------------------

2. OPERATORS   PST-pdf

-------------------------------------------------------------------------------------------------------------------

3. LOOPING  PST-pdf

------------------------------------------------------------------------------------------------------------------

4. CONDITIONAL  PST-pdf

------------------------------------------------------------------------------------------------------------------

5. JUMPING STATEMENTS

------------------------------------------------------------------------------------------------------------------
5.1 Break 
break is a keyword in C programming language and it is used with two statements:
Looping statements (for, while and do while) - break is used to break (terminate) the execution of loop body and transfers the program’s control to the next statement written after the loop body.

Switch case statement - break is used to transfer the program’s control from switch statement body to the next statement written after the switch.

Here, is the syntax of break statement with loop statement in C programming:

for (counter_initialization; test_condition; counter_increment)
{
//statement(s)
if(test_condition)
break;
//statement(s)
}
//outer statement(s)

If the test_condition written within the loop body is true (non zero) value, execution of loop will be stopped and program’s control will move to outer statement(s).

For Programming Training:
DM me Instagram

5.2 Continue 
continue is a keyword in C programming language and it is used to transfer the program’s control to starting of loop. It continues the execution of loop without executing the statements written after continue within the loop body.

Syntax: 
for (counter_initialization; test_condition; counter_increment)
{
//statement(s)
if(test_condition)
continue;
//statement(s)
}

If the test_condition written within the loop body is true (non zero) value, statement(s) written after the continue will not print and program’s control will move to the beginning.


For Programming Training:
DM me Instagram

 5.3 Goto
goto is a jumping statement in c language, which transfer the program’s control from one statement to another statement (where label is defined).

goto can transfer the programs within the same block and there must a label, where you want to transfer program’s control.

Defining a label Syntax:
label_name:
label_name should be a valid identifier name.
: (colon) should be used after the label_name.
Transferring the control using goto

Programs control can be transfer following by the given syntax goto label_name;

Two styles of goto statement
We can use goto statement to transfer programs control from down to top (?) and top to down (?).

Style 1 (Down to Top)
label-name:
statement1;
statement2;
..
if(any-test-condition)
goto label-name;

Here, if any-test-condition is true, goto will transfer the programs control to the specified label-name.

Style 2 (Top to Down)
statements;
if(any-test-condition)
goto label-name;
statement1;
statement2;
label-name:
Other statements;

Here, if any-test-condition is true, goto will transfer the programs control to the specified label-name.


For Programming Training:
DM me Instagram

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BCA                                        PST & Programming C                                   Allrounder Sita Ram Sahu
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
>