C Programming


Introduction to C:


Every full C program begins inside a function called "main". A function is simply a collection of commands that do "something". The main function is always called when the program first executes. From main, we can call other functions, whether they be written by us or by others or use built-in language features. To access the standard functions that comes with your compiler, you need to include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program.

Array:
 An array is a variable that holds multiple values of the same type

Characteristics of an Array:
1) An array holds elements that have the same data type
2) Array elements are stored in subsequent memory locations
3) Two-dimensional array elements are stored row by row in subsequent memory locations.
4) Array name represents the address of the starting element.
5) Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.



Defining an Array:
<type-of-array> <name-of-array>[<number of elements in array>];

Initializing each Array element with 'ZERO':
int a[10]={0};


Accessing Values in an Array:

code in main():
int arr[10];
int i = 0;
for(i=0;i<sizeof(arr);i++) 
  arr[i] = i; // Initializing each element separately 
int k = arr[5]; // Accessing the 4th element of integer array arr and assigning its value to integer 'k'.


Structures in 'C':
A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members,



How to define a Structure in 'C':
A structure type is usually defined near to the start of a file using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.
Here is an example structure definition.
typedef struct{
char name[64]; 
char course[128];
                      int age;
                      int year;
} student;

This defines a new type student, variables of type student can be declared as follows.
 student st_rec;
The variable name is st_rec, it has members called name, course, age and year.
How to Access Members Of a Structure?
Each member of a structure can be used just like a normal variable, but its name will be a bit longer. To return to the examples above, member name of structure st_rec will behave just like a normal array of char, however we refer to it by the name
 st_rec.name
Here the dot is an operator which selects a member from a structure.
Where we have a pointer to a structure we could dereference the pointer and then use dot as a member selector. This method is a little clumsy to type. Since selecting a
member from a structure pointer happens frequently, it has its own operator -> which acts as follows. Assume that st_ptr is a pointer to a structure of type student We would refer to the name member as
st_ptr -> name
Uses of Structures:
As we have seen, a structure is a good way of storing related data together. It is also a good way of representing certain types of information. Complex numbers in mathematics inhabit a two dimensional plane (stretching in real and imaginary directions). These could easily be represented here by
typedef struct {
        double real;
       double imag;
} complex;
doubles have been used for each field because their range is greater than floats and because the majority of mathematical library functions deal with doubles by default.
In a similar way, structures could be used to hold the locations of points in multi-dimensional space. Mathematicians and engineers might see a storage efficient implementation for sparse arrays here.
Apart from holding data, structures can be used as members of other structures. Arrays of structures are possible, and are a good way of storing lists of data with regular fields, such as databases.
Another possibility is a structure whose fields include pointers to its own type. These
can be used to build chains (programmers call these linked lists), trees or other connected structures. These are rather daunting to the new programmer, so we won't deal with them here.
C Program for Quick Sort:
Quick sort is a sorting algorithm developed by Tony Hoare that, on average, comparisons to sort n number of items Various types of sorting techniques are available in c, Quick Sort is quite popular.

Program:
#include<stdio.h>
#include<conio.h>
int arr[20];
int comp=0;//Variable For Comparison
int mov=0;//variable For Movements
int n;
void get();
void quick(int,int);
void show();
void main(){
clrscr();
printf("- This Program Explains Sorting Using Quick Sort - \n");
get();
quick(0,n-1);
show();
getch();
}

void get(){
 int i;
 while(1){
  printf("Enter the size of the elements :\n");
  scanf("%d",&n);
  if(n<=20)
   break;
  else
   printf("Sorry the maximum no of elements is 20\n\n");
  }
 printf("\n");
 printf("----------------------\n");
 printf("Enter the values \n");
 printf("----------------------\n\n");
 for(i=0;i<n;i++)
  scanf("%d",&arr[i]);
}
void quick(int l,int h)  // l = Low,h = High
{
 int p,i,j;              // p = Pivot
 if(l>h)
  return;
 i=l+1;
 j=h;
 p=arr[l];
 while(i<=j){
  while((arr[i]<=p)&&(i<=h)){
   i++;
   comp++;
}
  comp++;
  while((arr[j]>p)&&(j>=l)){
   j--;
   comp++;
}
  comp++;
  if(i<j){
   int t;
   t=arr[i];
   arr[i]=arr[j];
   arr[j]=t;
   mov++;
}
  }
  if(l<j){
   int t;
   t=arr[l];
   arr[l]=arr[j];
   arr[j]=t;
   mov++;
  }
  quick(l,j-1);
  quick(j+1,h);
}
void show(){
 int i;
 printf("\n");
 printf("-----------------------\n");
 printf("Sorted Array Elements\n");
 printf("-----------------------\n");
 for(i=0;i<n;i++){
  printf("%d\n",arr[i]);

}
 printf("\nNumber Of Comparison : %d",comp);
 printf("\nNumber Of Movements : %d",mov);

}



C Program for Base Conversion from Binary to Decimal:

#include<stdio.h>
#include<conio.h>
int power (int);
void main()
{
long int n;
int x,s=0,i=0,flag=1;
clrscr();
printf("Input a Binary Number\n\n");
scanf("%ld",&n);
while(flag==1)
{
x=n%10;
s=s+x*power(i);
i=i+1;
n=n/10;
if(n==0)
flag=0;
}
printf("\nDecimal Equivalent = %d",s);
getch();
}
power(int i)
{
int j,p=1;
for(j=1;j<=i;j++)
p = p*2;
return(p);

}



Day of Week 



For a given date (i.e., year, month & day), we may need to know the day of the week 
(i.e., Sunday or Monday…). We have so many ways to find that. But the code by Tomohiko 
Sakamoto is very interesting as well as mysterious! Here is the code…It works for the years 
greater than 1752 (Gregorian Calendar). 

int DayOfWeek( int y, int m, int d ) /* returns Day of Week: 
0 = Sunday, 1= Monday... 
*/ 
 static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; 
 y -= m < 3; 
 return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7; 
} /*--DayOfWeek( )--------*/ 



Write a C program to display the computers present date.

#include<stdio.h>
int main()
{
printf(“Date  from your computer: \n”);
// Display an Output
printf("%s",__DATE__);
return 0;
}


1 comment:

Contributors

Followers