26.6.20

c program dynamic memory management


DYNAMIC MEMORY MANAGEMENT
void main()
{
    int *a,n,i;

    printf("How many numbers do u want to enter ");
    scanf("%d",&n);

    a=(int*)malloc(n*sizeof(int));
    printf("Enter numbers:");
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    printf("the entered numbers are\n");
    for(i=0;i<n;i++)
        printf("%d\n",*(a+i));
    free(a);
}
OUTPUT
 How many numbers do u want to enter 5
Enter numbers

78
956
324
8562
0159

Entered numbers are
78
956
324
8562
159

c program using arrays


USING ARRAYS:
void main()
{

    int a[100],n,i;

    printf("How many numbers do u want to enter:");
    scanf("%d",&n);

    printf("Enter numbers:");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    printf("The entered numbers are\n");
    for(i=0;i<n;i++)
        printf("%d\n",a[i]);
}
OUTPUT:
How many numbers do u want to enter:4
Enter numbers:23
45
67
21
The entered numbers are
23
45
67
21

call by address example 2


CALL BY ADRESSS EXAMPLE 2
void changeMe(int*a)
{
    *a=72;
}
void main()
{
    int a=145;
    printf("Before :%d\n",a);
    changeMe(&a);
    printf("After : %d",a);
}
OUTPUT
Before:145
After:72

call by address /reference example 1


CALL BY ADDRESS/REFERENCE EXAMPLE 1:
void swap(int*a,int *b)
{

int t;
t=*a;
*a=*b;
*b=t;

}
void main()
{
    int a=10,b=20;
    printf("Before swapping : %d,%d\n",a,b);
    swap(&a,&b);
    printf("After swapping : %d,%d\n",a,b);

}
OUTPUT
Before swapping 10,20
After swapping 20,10

call by value


CALL  BY VALUE:
void swap(int a,int b)
{

int t;
t=a;
a=b;b=t;

}
void main()
{
    int a=10,b=20;
    printf("Before swapping : %d,%d\n",a,b);
    swap(a,b);
    printf("After swapping : %d,%d\n",a,b);

}
OUTPUT
Before swapping : 10,20
After swapping :10,20
Explanation :the a,b in main() and swap() functions are different....
So the changes made to a and b in the swap() function has no affect on a and b of main().

c program on arthmetic example 2


POINTER ARTHMETIC EXAMPLE 2
void main()
{
    int a=10,b=20;
    int*p=&a,*q=&b;
    printf("%u,%u\n",p,q);

    printf("%u\n",p+2);
    printf("%u\n",p-2);
   /* printf("%u\n",p*2);  */

    printf("%u\n",q-p);
    /*printf("%u\n",p+q); illegal */
}
OUTPUT
6356724,6356720
6356732
6356716
4294967295

c program for pointer arthmetic


POINTER ARTHMETIC
void main()
{
    int a=15;
    char ch='x';
    float f=9.7;

    int*p=&a;
    char*q=&ch;
    float*r=&f;

    printf("Before incremention:%d,%c,%f\n",a,ch,f);
    a++;
    ch++;
    f++;
    printf("after incremention:%d,%c,%f\n\n",a,ch,f);

    printf("Before incremention:%d,%c,%f\n",p,q,r);
    p++;
    q++;
    r++;
    printf("after incremention:%d,%c,%f\n",p,q,r);


}
Before incrementation: 15,x,9.7
After incrementation: 16,y,10.7
Before incrementation:100,200,300
After incrementation: 102,201,304

C program for the necessary of different pointers


WHY DIFFERENT TYPES FOR POINTERS
void main()
{

int a=4873;
char ch='p';
float f=2173.964;

int *p;
p=&a;
printf("%u\n",*p);

p=&ch;
printf("u\n",*p);

p=&f;
printf("%u",*p);
}
OUTPUT
4873
garbage
garbage                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

c program for address of and value at operators example 2


ADDRESS OF AND VALUE AT OPERATORS EXAMPLE 2
void main()
{
    int a=35;
    int*p;
    int**q;

    p=&a;
    q=&p;

    printf("%u\n",p);
    printf("%u\n",q);

    printf("u\n",*q);
    printf("u\n",*p);
    /* printf ("%u\n",*a); illegal */
    printf("%u\n",**q);
  /*  printf("%u\n",**p); illegal */
    printf("%u",*&p);

}
OUTPUT
6356728
6356724
u
u
35
6356728

C program for size of a pointer


SIZE OF A POINTER
void main()
{
    int a=10;
    char ch='x';
    float f=9.7;

    int *p=&a;
    char*q=&ch;
    float*r=&f;

    printf("%d,%d,%d\n",sizeof(a),sizeof(ch),sizeof(f));
    printf("%d,%d,%d",sizeof(p),sizeof(q),sizeof(r));

}
OUTPUT
4,1,4
2,2,2

c program for address of and value at operators


ADDRESS OF AND VALUE AT OPERATORS
void main()
{
    int a=35;
    int *p;
    p=&a;
    printf("%u\n",a);
    printf("%u\n",&a);
    printf("%u\n",p);
    printf("%u\n",&p);
    printf("%u\n",*p);
}
OUTPUT
35
6356732
6353732
6357328
35                                                                                         

c program for to print an address


HOW TO PRINT AN  ADDRESS
void main()
{
    int a=10;
    printf("%d\n",a);
    printf("%u",&a);

}
OUTPUT
10
6356732

19.6.20

c global variable


GLOBAL VARIABLE:
int i=78;
void fun1()
{

    printf("%d\n",i);

}
void fun2()
{

    int i= 45;
    printf("%d\n",i);
}
void main()
{

    printf("%d\n",i);
    fun1();
    fun2();
}
OUTPUT:
78
78
45

c register storage class specifier


REGISTER STORAGE CLASS SPECIFIER
void main()
{

    register int i;
    for(i=1;i<=5;i++)
        printf("I LOVE INDIA.\n");

}
OUTPUT
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.

c static storage class


STATIC STORAGE CLASS :
void fun()
{

    static int a=10;
    printf("%d\n",a);
    a++;

}
void main()
{

    fun();
    fun();
    fun();
    fun();
}
OUTPUT:
10
11
12
13

c auto storage class


AUTO STORAGE CLASS
void fun()
{

    int a=10;
    printf("%d\n",a);
    a++;

}
void main()
{

    fun();
    fun();
    fun();
}
OUTPUT
10
10
10

c macro: where we can access a variable


WHERE WE CAN ACCESS A VARIABLE...
void main()
{

    int a=10;
    printf("%d\n",a);
    {

        int b=20;
        printf("%d,%d\n",a,b);

    }
 printf("%d,%d",a,b); /*error: b not accesible */
}
EXPALNATION:
A variable defined in a block can be accessed in that block and all its inner blocks (i.r., a variable defined in a block cannot be accessed in its outer block).It is called as the scope of a variable .

c macro :where to define a variable


WHERE TO DEFINE A VARIABLE
void main()
{

    int a=10;
    printf("%d\n",a);
    {

        int b=20;
        printf("%d",b);
    }
}
OUTPUT:
10
20

c program enclosing the variable in the macro expansion


IMPORTANCE OF ENCLOSING THE VARIABLES IN THE MACRO EXPANSION WITHIN PARENTHESIS
#include<math.h>
#define MAC(x,y)sqrt(x*x+y*y)
float fun(int x,int y)
{
    return sqrt(x*x+y*y);

}
void main()
{

    int a=3,b=4;
    float p,q,r,s;
    p=fun(a,b);
    q=fun(a+1,b+1);
    r=MAC(a,b);
    s=MAC(a+1,b+1);
    printf("Using functions:%f,%f\n",p,q);
    printf("Using macros:%f,%f",r,s);
}
OUTPUT
Using functions: 5.0000000,6.403124
Using macros      5.0000000,4.000000

c program on name less blocks


PROGRAM ON NAMELESS BLOCKS
void main()
{

    int a=10;
    printf("%d\n",a);
    {

    printf("Stella Maris school -Marripalem");
    }
}
OUTPUT
10
Stella Maris School-Marripalem                                                                                                                           

c program on macros with arguments


PROGRAM ON MACROS WITH ARGUMENTS
#define PI 3.142857
#define AREA(x)PI*x*x
void main()
{

    float r,a;
    printf("Enter radius of a circle");
    scanf("%f",&r);
    a=AREA(r);
    printf("AREA of the circle=%f",a);
}

OUTPUT
Enter radius  of a circle 45
AREA of the circle6364.285645                                      

program on macros with arguments


PROGRAM ON MACROS WITH ARGUMENTS
#define SUM(a,b) a+b
void main()
{

    int a,b;
    printf("Enter two numbers");
    scanf("%d%d",&a,&b);
    printf("Sum=%d",SUM(a,b));

}
OUTPUT:
Enter two numbers:30 20
Sum=50

program on macros example 3


PROGRAM FOR MACROS EXAMPLE 3
#define PI 3.142857
void main()
{

    float r,a;
    printf("Enter radius of a circle:");
    scanf("%f",&r);
    a=PI*r*r;
    printf("Area of circle:%f",a);

}
OUTPUT:
Enter radius of circle: 3
Area of circle: 285713

program on macros example 2


PROGRAM FOR MACROS 2
 #define TRUE 1
void main()
{

    while(TRUE)
    {

    printf("This loop runs infinite times\n");
    }
}
OUTPUT
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times.....................

program on macros example 1


PROGRAM FOR MACROS EXAMPLE 1
#define NULL 0
void main()
{

    char st[20];
    int i;
    puts("Enter a string:");
    gets(st);
    for(i=0;st[i]!=NULL;i++);
    printf("length=%d",i);
}
OUTPUT:
Enter a string:
Sirisha
Length=7

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...