Learn C-Programming: Basic fundamentals







#include<stdio.h>
#include<conio.h>
main()
{
 clrscr();
 printf("Hello World\n");
 getch();
}


#include<stdio.h>
#include<conio.h>
main()
{
 int arr[5],i;
 clrscr();
 for(i=0; i<5; i++)
 {
  printf("Enter the value for arr[%d] : ",i);
  scanf("%d", &arr[i]);
 }
 printf("The array elements are : \n");
 for(i=0; i<5; i++)
 {
  printf("%d\t", arr[i]);
 }
 printf("\n");
 getch();
}


#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
main( )
{
 int i,arr[10]={ 2, 5, 4, 1, 8, 9, 11, 6, 3, 7 };
 int small, large;
 clrscr();
 small = large = arr[0];
 for(i=1; i<10; i++)
 {
  if(arr[i] < small)
   small = arr[i];
  if(arr[i] > large )
   large = arr[i];
 }
 printf("Smallest = %d, Largest = %d\n", small, large);
 getch();
}


#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
main()
{
 int i, j, temp, arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 for(i=0,j=9; i&lt;j; i++,j--)
 {
  temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
 }
 printf("After reversing, the array is : " );
 for(i=0; i<10; i++)
  printf("%d  ", arr[i]);
 printf("\n");
 getch();
}


#include<stdio.h>
#include<conio.h>
void check(int num);
main( )
{
 int arr[10], i;
 printf("Enter the array elements : ");
 for(i=0; i<10; i++)
 {
  scanf("%d", &arr[i]);
  check(arr[i]);
 }
 getch();
}
void check(int num)
{
 if(num%2==0)
  printf("%d is even\n", num);
 else
        printf("%d is odd\n", num);
}


#include<stdio.h>
#include<conio.h>
void func(int val[]);
main( )
{
 int i, arr[6] = {1, 2, 3, 4, 5, 6};
 func(arr);
 printf("Contents of array are now : ");
 for(i=0; i<6; i++)
  printf("%d ", arr[i]);
 printf("\n");
 getch();
}

void func(int val[])
{
 int sum=0, i;
 for(i=0; i<6; i++)
 {
  val[i]=val[i]*val[i];
  sum+=val[i];
 }
 printf("The sum of squares  =  %d\n", sum);
}


#define ROW 3
#define COL 4
#include<stdio.h>
#include<conio.h>
main( )
{
 int mat[ROW][COL], i, j;
 printf("Enter the elements of the matrix(%dx%d) row-wise :\n", ROW, COL);
 for(i=0; i < ROW; i++ )
  for(j=0; j < COL; j++ )
   scanf("%d", &mat[i][j]);

 printf("The matrix that you have entered is :\n");
 for(i=0; i< ROW; i++)
 {
  for(j=0; j < COL; j++)
   printf("%5d", mat[i][j]);
  printf("\n");
 }
 printf("\n");
 getch();
}


#define ROW 3
#define COL 4
#include<stdio.h>
#include<conio.h>
main( )
{
 int i, j, mat1[ROW][COL], mat2[ROW][COL], mat3[ROW][COL];

 printf("Enter matrix mat1(%dx%d)row-wise :\n", ROW, COL);
 for(i=0; i< ROW; i++)
  for(j=0; j< COL; j++)
   scanf("%d", &mat1[i][j]);
 
 printf("Enter matrix mat2(%dx%d)row-wise :\n", ROW, COL);
 for(i=0; i< ROW; i++)
  for(j=0; j< COL; j++)
   scanf("%d", &mat2[i][j] );

 /*Addition*/
 for(i=0; i< ROW; i++)
  for(j=0; j< COL; j++)
   mat3[i][j] = mat1[i][j] + mat2[i][j];

 printf("The resultant matrix mat3 is :\n");
 for(i=0; i< ROW; i++)
 {
  for(j=0; j< COL; j++)
   printf("%5d", mat3[i][j] );
  printf("\n");
 }
 getch();
}


#include<stdio.h>
#include<conio.h>
#define ROW1 3
#define COL1 4
#define ROW2 COL1
#define COL2 2
main( )
{
 int mat1[ROW1][COL1], mat2[ROW2][COL2], mat3[ROW1][COL2];
 int i, j, k;

 printf("Enter matrix mat1(%dx%d)row-wise :\n", ROW1, COL1);
 for(i=0; i<ROW1; i++)
  for(j=0; j<COL1; j++)
   scanf("%d", &mat1[i][j] );

 printf("Enter matrix mat2(%dx%d)row-wise :\n", ROW2, COL2);
 for(i=0; i<ROW2; i++)
  for(j=0; j<COL2; j++)
   scanf("%d", &mat2[i][j] );

 /*Multiplication */
 for(i=0; i<ROW1; i++)
  for(j=0; j<COL2; j++)
  {
   mat3[i][j] = 0;
   for(k=0; k<COL1; k++)
    mat3[i][j] += mat1[i][k] * mat2[k][j];
  }

 printf("The Resultant matrix mat3 is :\n");
 for(i=0; i<ROW1; i++)
 {
  for(j=0; j<COL2; j++)
   printf("%5d", mat3[i][j] );
  printf("\n");
 }
 getch();
}


#include<stdio.h>
#include<conio.h>
main( )
{
 int a = 87;
 float b = 4.5;
 int *p1 = &a;
 float *p2 = &b;
 printf("Value of p1 = Address of a = %p\n", p1);
 printf("Value of p2 = Address of b = %p\n", p2);
 printf("Address of p1 = %p\n", &p1);
 printf("Address of p2 = %p\n", &p2);
 printf("Value of  a = %d  %d  %d \n", a , *p1, *(&a) );
 printf("Value of  b = %f  %f  %f \n",  b , *p2, *(&b));
 getch();
}


#include<stdio.h>
#include<conio.h>
main( )
{
 int a = 5;
 int *pa;
 int **ppa;
 pa = &a;
 ppa = &pa;
 printf("Address of a = %p\n", &a);
 printf("Value of pa = Address of a = %p\n", pa);
 printf("Value of *pa = Value of a = %d\n", *pa);
 printf("Address of pa = %p\n", &pa);
 printf("Value of ppa = Address of pa = %p\n", ppa);
 printf("Value of *ppa = Value of  pa = %p\n", *ppa);
 printf("Value of **ppa = Value of a = %d\n", **ppa);
 printf("Address of ppa = %p\n", &ppa);
 getch();
}


#include<stdio.h>
#include<conio.h>
main( )
{
 int arr[5] = {5,10,15,20,25};
 int i;
 for(i=0; i<5; i++)
 {
  printf("Value of arr[%d] = %d\t", i, arr[i]);
  printf("Address of arr[%d] = %p\n", i, &arr[i] );
 }
 getch();
}


#include<stdio.h>
#include<conio.h>
main( )
{
 int arr[5] = {5, 10, 15, 20, 25};
 int i;
 for(i=0; i<5; i++)
 {
  printf("Value of arr[%d] = %d\t", i,*(arr+i));
  printf("Address of arr[%d] = %p\n",i,arr+i);
 getch();
 }
}


#include<stdio.h>
#include<conio.h>
void value(int x, int y);
main( )
{
 int a=5, b=8;
 printf("Before calling the function, a = %d and b = %d\n", a, b);
 value(a, b);
 printf("After calling the function, a = %d and b = %d\n", a, b);
 getch();
}
void value(int x, int y)
{
 x++;
 y++;
 printf("Inside function x = %d , y = %d\n",x,y);
}


#include<stdio.h>
#include<conio.h>
void ref(int *p, int *q);
main()
{
 int a = 5;
 int b = 8;
 printf("Before calling the function, a = %d and b = %d\n", a, b);
 ref(&a, &b);
 printf("After calling the function, a = %d and b = %d\n", a, b);
 getch();
}
void ref(int *p, int *q)
{
 (*p)++;
 (*q)++;
 printf("Inside function *p = %d, *q = %d\n", *p, *q);
}


#include<stdio.h>
#include<conio.h>
func(int x, int y, int *ps, int *pd, int *pp);
main( )
{
 int a, b, sum, diff, prod;
 a = 6;
 b = 4;
 func( a, b, &sum, &diff, &prod );
 printf("Sum = %d, Difference = %d, Product = %d\n", sum, diff, prod );
 getch();
}
func(int x, int y, int *ps, int *pd, int *pp)
{
 *ps = x+y;
 *pd = x-y;  
 *pp = x*y;  
}


#include<stdio.h>
#include<conio.h>
int *fun(int *p, int n);
main( )
{
 int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, n, *ptr;
 n = 5;
 ptr = fun(arr, n);
 printf("arr = %p, ptr = %p, *ptr = %d\n", arr, ptr, *ptr);
 getch();
}
int *fun(int *p, int n)  
{
 p = p+n;
 return  p;
}


#include<stdio.h>
#include<conio.h>
void func(int a[]);
main( )
{
 int i, arr[5] = {3, 6, 2, 7, 1};
 func(arr);
 printf("Inside main( ) : ");
 for(i=0; i<5; i++)
        printf("%d  ", arr[i]);
 printf("\n");
 getch();
}

void func(int a[])
{
 int i;
 printf("Inside func( ) : ");
 for(i=0; i<5; i++)
 {
  a[i] = a[i] + 2;
  printf("%d  ",a[i]);
 }
 printf("\n");
}


#include<stdio.h>
#include<conio.h>
func(float f[ ] , int *i, char c[5]);
main( )
{
 float f_arr[5] ={1.4, 2.5, 3.7, 4.1, 5.9 };
 int i_arr[5] = { 1, 2, 3, 4, 5};
 char c_arr[5] = {'a', 'b', 'c', 'd', 'e'};
 printf("Inside main( )  :   ");
 printf("Size of f_arr = %u\t", sizeof(f_arr));
 printf("Size of i_arr = %u\t", sizeof(i_arr));
 printf("Size of c_arr = %u\n", sizeof(c_arr));
 func(f_arr, i_arr, c_arr);
 getch();
}     
func(float f[ ], int *i, char c[5])
{
 printf("Inside func( )  : ");
 printf("Size of f = %u\t", sizeof(f)); 
 printf("Size of i = %u\t", sizeof(i));
 printf("Size of c = %u\n", sizeof(c));
}                                                


#include<stdio.h>
#include<conio.h>
main( )
{
 int *pa[3];
 int i, a=5, b=10, c=15;
 pa[0] = &a;
 pa[1] = &b;
 pa[2] = &c;

 for(i=0; i<3; i++)
 {
  printf("pa[%d] =  %p\t",i,pa[i]);
  printf("*pa[%d] =  %d\n",i,*pa[i]);
 }
 getch();
}


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main( )
{
 int *p, n, i;
 printf("Enter the number of integers to be entered : ");
 scanf("%d", &n);
 p = (int *)malloc(n * sizeof(int)); 
 if(p==NULL)
 {
  printf("Memory not available\n");
  exit(1);
 }
 for(i=0; i<n; i++)
 {
  printf("Enter an integer : ");
  scanf("%d", p+i);
 }
 for(i=0; i<n; i++)
  printf("%d\t", *(p+i));
 getch();
}


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main( )
{
 int i, *ptr;
 ptr = (int *)malloc(5 *sizeof(int));
 if(ptr == NULL)
 {
  printf("Memory not available\n");
  exit(1);
 }
 printf("Enter 5 integers : ");
 for(i=0; i<5; i++)
  scanf("%d", ptr+i);
 /*Allocate memory for 4 more integers*/
 ptr = (int *)realloc(ptr, 9*sizeof(int)); 
 if(ptr == NULL) 
 {
  printf("Memory not available\n");
  exit(1);
 }
 printf("Enter 4 more integers : ");
 for(i=5; i<9; i++)
  scanf("%d", ptr+i);
 for(i=0; i<9; i++)
  printf("%d ", *(ptr+i));
 getch();
}


#include<stdio.h>
#include<string.h>
#include<conio.h>
struct student {
           char name[20];
           int rollno;
           float marks;
            };
main( )
{
 struct student stu1 = {"Mary", 25, 68};
 struct student stu2, stu3;
 strcpy(stu2.name, "John");
 stu2.rollno = 26;
 stu2.marks  =  98;
 printf("Enter name, rollno and marks for stu3 : ");
 scanf("%s %d %f", stu3.name, &stu3.rollno, &stu3.marks);
 printf("stu1 : %s  %d  %.2f\n", stu1.name, stu1.rollno, stu1.marks);
 printf("stu2 : %s  %d  %.2f\n", stu2.name, stu2.rollno, stu2.marks);
 printf("stu3 : %s  %d  %.2f\n", stu3.name, stu3.rollno, stu3.marks);
 getch();
}


#include<stdio.h>
#include<conio.h>
struct student {
           char name[20];
           int rollno;
          float marks;
        };
main( )
{
 struct student stu1 = {"Oliver", 12, 98},stu2;
 stu2 = stu1;
 printf("stu1 : %s   %d   %.2f\n", stu1.name, stu1.rollno, stu1.marks);
 printf("stu2 : %s   %d   %.2f\n", stu2.name, stu2.rollno, stu2.marks);
 getch();
}


#include<stdio.h>
#include<conio.h>
struct student {
  char name[20];
  int rollno;
  float marks;
        };
main( )
{
 int i;
 struct student stuarr[10];
 for(i=0; i<10; i++)
 {
  printf("Enter name, rollno and marks  : "); 
  scanf("%s%d%f", stuarr[i].name, &stuarr[i].rollno, &stuarr[i].marks);
 }
 for(i=0; i<10; i++)
  printf("%s  %d   %f \n", stuarr[i].name, stuarr[i].rollno, stuarr[i].marks);
 getch();
}
Previous
Next Post »