Language C – Shishir Kant Singh https://shishirkant.com Jada Sir जाड़ा सर :) Wed, 13 May 2020 07:22:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://shishirkant.com/wp-content/uploads/2020/05/cropped-shishir-32x32.jpg Language C – Shishir Kant Singh https://shishirkant.com 32 32 187312365 Pointers in C https://shishirkant.com/pointers-in-c/?utm_source=rss&utm_medium=rss&utm_campaign=pointers-in-c Wed, 13 May 2020 07:22:26 +0000 http://shishirkant.com/?p=178 C provides the important feature of data manipulations with the address of the variables, the execution time is very much reduced such concept is possible with the special data type called Pointers.

Pointer:- A pointer is a variable which stores the address of another variable.

Declaration:-Pointer declaration is similar to normal variable declaration butpreceded by a *

Syntax:- data type  *identifier;

Example:- int *p;

Initialization:- datatype *identifier=address;

 Example:- int n;

                                                                  int *p=&n;

NULL:-NULL  pointer value(empty address)

                            Any type of pointer allocates two bytes of memory because it stores address of memory allocation.In c language the programme keep ([memory]) size is 64 kilo bytes.It is in unsigned integer range.

NOTE:-  Any type of pointer it allocates 2 bytes memory. Because it stores address of memory  location.

#include<stdio.h>
int main()
{
int ptr, q; q = 50; / address of q is assigned to ptr / ptr = &q; / display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
POINTERS IN C – PROGRAM OUTPUT:
50
KEY POINTS TO REMEMBER ABOUT POINTERS IN C:
  • Normal variable stores the value whereas pointer variable stores the address of the variable.
  • The content of the C pointer always be a whole number i.e. address.
  • Always C pointer is initialized to null, i.e. int *p = null.
  • The value of null pointer is 0.
  • & symbol is used to get the address of the variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • Two pointers can be subtracted to know how many elements are available between these two pointers.
  • But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for 16 bit compiler).

Void *:-

It is a generic pointer. It refers the address of any type of variable and also it will assigned to any type of pointer.

Example:-   int n=100;

                                                                    int *p=&n;

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int n;
  int p=&n; 
  clrscr();
  printf("Enter any value into n:"); 
  scanf("%d",p); //scanf("%d",&n); Here in pointers& is not necessary 
  printf("Given number in through pointer:%d",p);
  getch();
}

FUNCTIONS AND POINTERS

Call by value:-The process of passing a value to the function call is known as call by value.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  void disp(int);
  int n;
  clrscr();
  printf("Enter any number:");
  scanf("%d",&n);
  disp(n);
  getch();
}
void disp (int x)
{
  clrscr();
  printf("%5d",x);
}

Call by Reference:The process of calling a function using pointers to pass the address of the variables is known as call by reference.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
void disp(int *);
int n;
clrscr();
disp(&n);
printf("%5d",n);
getch();
}
void disp( int *x)
{
printf("Enter any value into n:");
scanf("%d",x);
}
Program: Swapping two values
#include<stdio.h>
#include<conio.h>
void main()
{
  void swap(int *, int *);
  int a,b;
  clrscr();
  printf("Enter any two values:");
  scanf("%d%d",&a,&b);
  printf("\nBefore swaping:");
  printf("\na=%d",a);
  printf("\nb=%d",b);
  swap(&a,&b);
  printf("\nAfter swaping:");
  printf("\na=%d",a);
  printf("\nb=%d",b);
  getch();
}
void swap(int *x, int *y)
{
  int t;
  t=*x;
  *x=*y;
  *y=t;
}

POINTERS  AND  ARRAYS

When an array is declared, the compiler allocates a BASE address and sufficient amount of storage and contain all the elements of array in continuous memory allocation.

The base address is the location of the first element (index 0 of the array).The compiler also defines the array name as a constant pointer pointed to the first element.

suppose we declare an array ‘a’ as follows.

Example:- int a[5]={1,2,3,4,5};

Suppose the base address of a is 1000 and assuming that each integer requires 2 bytes. Then the 5 elements will be stored as follows.

    Elements——>  a[0] a[1] a[2] a[3] a[4]

                                   ——————————

    values——->          1     2     3     4       5

                                   —————————–

    address——>   1000 1002 1004 1006 1008

                             base address

Here ‘a’ is a pointer that points to the first element. so the value of a is 1000 there fore  a=&a[0]=1000

If we declare ‘p’ is an integer pointer, then we can make the pointerp to point to the array ‘a’ by the following assignment.

int *p;

        p=&a[0]=a;

Now we can access every value of a using p++(or)p+1 to move one element to another.

The relationship between p and a is shown below.

p+0 = &a[0]=1000

p+1 = &a[1]=1002

p+2 = &a[2]=1004

p+3 = &a[3]=1006

p+4 = &a[4]=1008

When handling arrays instead of using array indexing, we can use pointer to access array elements. Note that *(p+k) gives the value of a[k]. Pointer accessing method is much faster than array indexing.

Program: Write a program to accept and display array elements using pointers
#include<stdio.h>
#include<conio.h>
void main()
{
  int a[20],n,i;
  int p=&a[0]; 
  clrscr(); 
  printf("Enter no of elements:"); 
  scanf("%d",&n); 
  printf("Enter array elements:"); 
  for(i=0;i<n;i++)
  {
    scanf("%5d",p+i);
  }
  printf("Given array elements:");
  for(i=0;i<n;i++)
  {
    printf("%5d",*(p+i));
  }
  getch();
}

]]>
178
STORAGE CLASSES – Programming C https://shishirkant.com/storage-classes-programming-c/?utm_source=rss&utm_medium=rss&utm_campaign=storage-classes-programming-c Wed, 13 May 2020 04:05:54 +0000 http://shishirkant.com/?p=159 The scope and lifetime of variables in functions:-Variables in C different behavior from those in most other language.  For example in a basic program a variable returns its value through out the program.  It is not always the case in ‘C’.  It always depends on the storage classes the variable may assume.

            A variable in C can have any one of the four storage classes.

  1. Automatic variables
  2. Static Variables
  3. External Variables or Global Variables
  4. Register Variables.

Automatic variables:-

These variables are declared inside the function block.

Storage – Main Memory
Default value – Garbage value
Scope – Local to the block in which it is defined.
Life – The control remains within the block in which it is Defined.
Keyword – auto

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a,b;
clrscr();
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}

Static Variables:-

The memory of static variables remains unchanged until the end of the program.

Storage – Main Memory
Default value – Zero
Scope – Local to the block in which it is defined.
Life – A Value of static variable consists between different function calls. (No Changes). That means it cannot realized between different function calls.
Keyword – static

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
static int a,b;
clrscr();
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}

External Variables or Global Variables:-    

The variables both alive and active throughout entire program is known as external variable.

Storage – Main Memory
Default value – Zero
Scope – global
Life – as long as the program execution does not come to end
Keyword – extern

Program:1
#include<stdio.h>
#include<conio.h>
int a,b;
void main()
{
clrscr();
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Program:2
#include<stdio.h>
#include<conio.h>
int a=100;
void main()
{
extern int a;
clrscr();
printf("\n a=%d",a);
getch();
}

Register Variables:-

We can use register variables for frequently used variables the fast execution of a program.  These variables are declared inside the function block.

Storage – CPU, Register
Default value – garbage value
Scope – local to the block in which it is defined
Life – the control remains within the block in which it is defined
Keyword – Register

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
register int a,b;
clrscr();
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
getch();
}

Note:-  If there is no storage classes specifier before the function declaration of variables inside the function block by default it takes auto storage class.

Recursive function:-

Calling a function with the same name function definition is known as recursive function.  If you want to work with recursive function we must follow two conditions.

  1. Calling itself
  2. Termination condition
Program: Write a program to display natural numbers from 1 to given number using recursive function
#include<stdio.h>
#include<conio.h>
void main()
{
void disp(int);
int n;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
disp(n);
getch();
}
void disp(int x)
{
if(x>1)
disp(x-1);
printf("%d\t",x);
}
Program: Write a program to calculate and display factorial of given number
#include<stdio.h>
#include<conio.h>
void main()
{
 unsigned long fact(int);
 int n;
 unsigned long f;
 clrscr();
 printf("Enter any number:");
 scanf("%d",&n);
 f=fact(n);
 printf("Factorial is:%lu",f);
 getch();
}
unsigned long fact(int x)
{
 if(x<=1)
 return 1;
 else
 return x*fact(x-1);
}

]]>
159
Programming C – Constant https://shishirkant.com/programming-c-constant/?utm_source=rss&utm_medium=rss&utm_campaign=programming-c-constant Tue, 12 May 2020 07:10:22 +0000 http://shishirkant.com/?p=110
  • C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined.
  • Constants refer to fixed values. They are also called as literals
  • Constants may be belonging to any of the data type.
  • Syntax:
  • const data_type variable_name; (or) const data_type *variable_name;

    TYPES OF C CONSTANT:

    1. Integer constants
    2. Real or Floating point constants
    3. Octal & Hexadecimal constants
    4. Character constants
    5. String constants
    6. Backslash character constants
    Constant typedata type (Example)
    Integer constantsint (53, 762, -478 etc )
    unsigned int (5000u, 1000U etc)
    long int, long long int
    (483,647 2,147,483,680)
    Real or Floating point constantsfloat (10.456789)
    doule (600.123456789)
    Octal constantint (Example: 013 /*starts with 0 */)
    Hexadecimal constantint (Example: 0x90 /*starts with 0x*/)
    character constantschar (Example: ‘A’, ‘B’, ‘C’)
    string constantschar (Example: “ABCD”, “Hai”)

    RULES FOR CONSTRUCTING C CONSTANT:

    1. INTEGER CONSTANTS IN C:
    • An integer constant must have at least one digit.
    • It must not have a decimal point.
    • It can either be positive or negative.
    • No commas or blanks are allowed within an integer constant.
    • If no sign precedes an integer constant, it is assumed to be positive.
    • The allowable range for integer constants is -32768 to 32767.
    2. REAL CONSTANTS IN C:
    • A real constant must have at least one digit
    • It must have a decimal point
    • It could be either positive or negative
    • If no sign precedes an integer constant, it is assumed to be positive.
    • No commas or blanks are allowed within a real constant.
    3. CHARACTER AND STRING CONSTANTS IN C:
    • A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
    • The maximum length of a character constant is 1 character.
    • String constants are  enclosed within double quotes.
    4. BACKSLASH CHARACTER CONSTANTS IN C:
    • There are some characters which have special meaning in C language.
    • They should be preceded by backslash symbol to make use of special function of them.
    • Given below is the list of special characters and their purpose.
    Backslash_characterMeaning
    \bBackspace
    \fForm feed
    \nNew line
    \rCarriage return
    \tHorizontal tab
    \”Double quote
    \’Single quote
    \\Backslash
    \vVertical tab
    \aAlert or bell
    \?Question mark
    \NOctal constant (N is an octal constant)
    \XNHexadecimal constant (N – hex.dcml cnst)
    HOW TO USE CONSTANTS IN A C PROGRAM?

    We can define constants in a C program in the following ways.

    1. By “const” keyword
    2. By “#define” preprocessor directive

    Please note that when you try to change constant values after defining in C program, it will through error.

    1. EXAMPLE PROGRAM USING CONST KEYWORD IN C:
    #include<stdio.h>
    void main()
    {
    const int height = 100; /int constant/
    const float number = 3.14; /Real constant/
    const char letter = 'A'; /char constant/
    const char letter_sequence[10] = "ABC"; /string constant/
    const char backslash_char = '\?'; /special char cnst/
    printf("value of height :%d \n", height );
    printf("value of number : %f \n", number );
    printf("value of letter : %c \n", letter );
    printf("value of letter_sequence : %s \n", letter_sequence);
    printf("value of backslash_char : %c \n", backslash_char);
    }

    OUTPUT:

    value of height : 100
    value of number : 3.140000
    value of letter : A
    value of letter_sequence : ABC
    value of backslash_char : ?
    2. EXAMPLE PROGRAM USING #DEFINE PREPROCESSOR DIRECTIVE IN C:
    #include<stdio.h>
    #define height 100
    #define number 3.14
    #define letter 'A'
    #define letter_sequence "ABC"
    #define backslash_char '\?'
    void main()
    {
    printf("value of height : %d \n", height );
    printf("value of number : %f \n", number );
    printf("value of letter : %c \n", letter );
    printf("value of letter_sequence : %s \n",letter_sequence);
    printf("value of backslash_char : %c \n",backslash_char);
    }
    
    OUTPUT:
    value of height : 100
    value of number : 3.140000
    value of letter : A
    value of letter_sequence : ABC
    value of backslash_char : ?
    ]]>
    110