C Program to demonstrate how the escape characters work.

C Program to demonstrate how the escape characters work.Escape character is the single character, and it is compiled as the character constant.
Escape characters are used to solve the c format problems.
An escape sequence is denoted by using the backslash () as an escape character, followed by a single character indicating the nonprintable character desired.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>
main()
{
clrscr();
printf("Escape operators");

printf("Example program to show the escape characters:nn");
printf("t here is tab");
printf("n Here starts new line");
getch();
}

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to find the position of a sub string.

C Strings:
C Program to find the position of a sub string in a given string.
In this program, We find the position of a sub string where it starts  in the main string. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
main()
{
char firststr[30], substr[10];
char *secondstr, *thirdstr;
clrscr();
printf("Enter any string");
scanf("%s",firststr);
printf("Enter sub string");
scanf("%s",substr);
secondstr=firststr;
thirdstr=substr;
i=substr(secondstr, thirdstr);
printf("nn");
if(i!=-1)
printf("The starting location/position of substring in main string: %d", i;
else
printf("String not found");
getch();
}


int substr(secondstr, thirdstr)

{
char *secondstr, *thirdstr;

int j=0,l=0;
static int k;
if(*second==*third)
{
while(i<strlen(third))
{
if(*second==*third)
k=j+1;
else
k+=1;
l++;
}
third++;
second++;
}
second++;
j++;

if(k!=0)
return(k);
else
return(-1);
}
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to Demonstrate the Recursive function.

Write a C program to demonstrate the recursive function.
Recursion  is the programming technique that a process invoking itself again and again. In this program, We reverse the given number and checks it is a palindrome or not. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

int main(){
clrscr();
int num,num1,rev;
printf("nEnter a number :n");
scanf("%d",&num);
num1=num;
//call recursive function
rev=reverse(num);

printf("nAfter reverse the number is :n%d",rev);
if(num1==rev){
printf("nnNumber %d is Palindromen",num1);
}else
{
printf("nnNumber %d is NOT a Palindromen",num1);
}

return 0;
}
int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}

Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to count the leaves of the binary tree.

Write a C program to count the leaves of the binary tree.
Binary tree is the ordered directed tree data structure, in which each node has at most two nodes.
A node is called as a leaf node,  if it does not contains any child elements. In this program, We used the structures to create the binary tree.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct node
{
int data;
struct node* leftnode;
struct node* rightnode;
};

//get the leaves count
unsigned int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->leftnode == NULL && node->rightnode==NULL)
return 1;
else
return getLeafCount(node->leftnode)+
getLeafCount(node->rightnode);
}


struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->leftnode = NULL;
node->rightnode = NULL;

return(node);
}


int main()
{

clrcsr();
struct node *root = newNode(1);
root->leftnode = newNode(2);
root->rightnode = newNode(3);
root->leftnode->leftnode = newNode(4);
root->leftnode->rightnode = newNode(5);


printf("nnLeaf count of the binary tree is %d", getLeafCount(root));

getch();
return 0;
}


Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement hashing.

Write a C Program to implement hashing.
Hashing is the function or routine used to assign the key values to the each entity in the database. Using hashing, We can easily access or search the values from database.
In this program we used the open addressing hashing, also called as closed hashing.
Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include<stdio.h>
#include<conio.h>
void main() {
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n, value;
int temp, hash;
clrscr();
printf("nEnter the value of n(table size):");
scanf("%d", &n);
do {
printf("nEnter the hash value");
scanf("%d", &value);
hash = value % n;
if (a[hash] == 0) {
a[hash] = value;
printf("na[%d]the value %d is stored", hash, value);
} else {
for (hash++; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}
}

for (hash = 0; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}
}
printf("nnERRORn");
printf("nEnter '0' and press 'Enter key' twice to exit");

}

menu:

printf("n Do u want enter more");

scanf("%d", &temp);

}

while (temp == 1);

getch();

}

Read more Similar C Programs
C Basic
C Data Structure
Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to find the size of a given file.

Write a C Program to find the size of a given file.
The function fseek() is used to set the file pointer at the specified position. In general, it is used to move the file pointer to the desired position within the file.
In this program we moved the file pointer to the end of file, and obtain the current position of the file pointer. Function rewind() also moves the file pointer to the beginning of the file, but syntax and number of parameters are different. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<process.h>
long file_size(FILE *fp)
{
long len;
//move file pointer to end of the file */
fseek(fp, 0L, 2);
//Obtain the current position of file pointer
len = ftell(fp);
return len;
}
void main(void)
{
FILE *fp;
char filename[20];
printf("nEnter the file name:nn");
scanf("%s",filename);

fp = fopen(filename, r);

if(fp == NULL)
{
printf("Error: file not found!nn");
exit(0);
}
printf("File size of %s is %ld bytesnn",filename, file_size(fp));
fclose(fp);
}

Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement address calculation sort.

Write a C Program to implement address calculation sort.
Address calculation sort is the sorting method which sorts the given array by using insertion method.
In this algorithm, a hash function is used and applied to the each key. Result of hash function is placed in the linked lists. The hash function must be a order preserving function which places the elements in linked lists are called as sub files. An item is placed into a sub-file in correct sequence by using any sorting method. After all the elements are placed into subfiles, the list is concatenated to produce the sorted list.
 Address calculation sort is used to efficiently store non-contiguous keys (account numbers, part numbers, etc.) that may have wide gaps in their alphabetic and numeric sequences.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>

#include<malloc.h>

#define MAX 20

struct node

{

int info ;

struct node *link;

};

struct node *head[10];

int n,i,arr[MAX];

main()

{

int i;

printf("Enter the number of elements in the list : ");

scanf("%d", &n);

for(i=0;i<n;i++)

{

printf("Enter element %d : ",i+1);

scanf("%d",&arr[i]);

}/*End of for */

printf("Unsorted list is :n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

printf("n");

addr_sort();

printf("Sorted list is :n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

printf("n");

}/*End of main()*/

addr_sort()

{

int i,k,dig;

struct node *p;

int addr;

k=large_dig();

for(i=0;i<=9;i++)

head[i]=NULL;

for(i=0;i<n;i++)

{

addr=hash_fn( arr[i],k );

insert(arr[i],addr);

}

for(i=0; i<=9 ; i++)

{

printf("head(%d) -> ",i);

p=head[i];

while(p!=NULL)

{

printf("%d ",p->info);

p=p->link;

}

printf("n");

}


i=0;

for(k=0;k<=9;k++)

{

p=head[k];

while(p!=NULL)

{

arr[i++]=p->info;

p=p->link;

}

}

}/*End of addr_sort()*/

/*Inserts the number in sorted linked list*/

insert(int num,int addr)

{

struct node *q,*tmp;

tmp= malloc(sizeof(struct node));

tmp->info=num;

/*list empty or item to be added in begining */

if(head[addr] == NULL || num < head[addr]->info)

{

tmp->link=head[addr];

head[addr]=tmp;

return;

}

else

{

q=head[addr];

while(q->link != NULL && q->link->info < num)

q=q->link;

tmp->link=q->link;

q->link=tmp;

}

}/*End of insert()*/

/* Finds number of digits in the largest element of the list */

int large_dig()

{

int large = 0,ndig = 0 ;

for(i=0;i<n;i++)

{

if(arr[i] > large)

large = arr[i];

}

printf("Largest Element is %d , ",large);

while(large != 0)

{

ndig++;

large = large/10 ;

}

printf("Number of digits in it are %dn",ndig);

return(ndig);

} /*End of large_dig()*/

hash_fn(int number,int k)

{

/*Find kth digit of the number*/

int digit,addr,i;

for(i = 1 ; i <=k ; i++)

{

digit = number % 10 ;

number = number /10 ;

}

addr=digit;

return(addr);

}/*End of hash_fn()*/




Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to solve Dining philosophers problem.

Write a C Program to solve Dining philosophers problem.
Dining philosophers problem is a classic synchronization problem.A problem introduced by Dijkstra concerning resource allocation between processes. Five silent philosophers sit  around table with a bowl of spaghetti. A fork is placed between each pair of adjacent philosophers.

Each philosopher must alternately think and eat.
Eating is not limited by the amount of spaghetti left: assume an infinite supply.
However, a philosopher can only eat while holding both the fork to the left and the fork to the right
(an alternative problem formulation uses rice and chopsticks instead of spaghetti and forks).

Each philosopher can pick up an adjacent fork, when available, and put it down, when holding it.
These are separate actions: forks must be picked up and put down one by one.
The problem is how to design a discipline of behavior (a concurrent algorithm) such that each philosopher won’t starve, i.e. can forever continue to alternate between eating and thinking.
Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>

#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N

sem_t mutex;
sem_t S[N];

void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);

int state[N];
int phil_num[N]={0,1,2,3,4};

int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinkingn",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}

void *philospher(void *num)
{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}

void take_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungryn",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}

void test(int ph_num)
{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and %dn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is Eatingn",ph_num+1);
sem_post(&S[ph_num]);
}
}

void put_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d downn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinkingn",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to generate and print prime numbers in a given range.

Write a C program to generate and print prime numbers in a given range. Also print the number of prime numbers.
Prime number is a whole number and greater than 1, which is divisible by one or itself.
Example: 2, 3, 5, 7, 11, 13………… Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
int M, N, i, j, flag, temp, count = 0;

clrscr();

printf("Enter the value of M and Nn");
scanf("%d %d", &M,&N);

if(N < 2)
{
printf("There are no primes upto %dn", N);
exit(0);
}
printf("Prime numbers aren");
temp = M;

if ( M % 2 == 0)
{
M++;
}
for (i=M; i<=N; i=i+2)
{
flag = 0;

for (j=2; j<=i/2; j++)
{
if( (i%j) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("%dn",i);
count++;
}
}
printf("Number of primes between %d and %d = %dn",temp,N,count);
}



Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to convert from infix expression into prefix expression

C Program to convert from infix expression into prefix expression.
We use the infix expressions for the mathematical expressions in a program, these expressions will converted into equivalent machine instructions by the compiler using stacks.
Using stacks we can efficiently convert the expressions from infix to postfix, infix to prefix, postfix to infix, and postfix to prefix.
Example: infix to prefix:
infix: x^y^z-m+n+p/q,
postfix: ++-^x^yzmn/pq. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>

#include<conio.h>

//Stack precedence function

int F(char symbol)

{

switch(symbol)

{

case ‘+’ :

case ‘-‘ :

return 1;

case ‘*’:

case ‘^’:

return 6:

case ‘)’:

return 0:;

case ‘#’:

return -1;

default:

return 8;

}

}

//Input precedence function

int G(char symbol)

{

switch(symbol)

{

case ‘+’ :

case ‘-‘ :

return 2;

case ‘*’:

return 4;

case ‘^’:

return 5:

case ‘(‘:

return 0;

case ‘)’:

return 9:;

case ‘#’:

return -1;

default:

return 7;

}

}

Void infix_prefix(char infix[], char prefix[])

{

int top, j, i;

char symbol, s[40];

top = -1;

s[++top] = ‘#’;

J = 0;

strrev(infix);

for(i = 0;i < strlen(infix); i++)

{

symbol= infix[i];

while(F(s[top]) > G(symbol))

{

prefix[j] = s[top--];

j++;

}

if(F(s[top]) != G(symbol))

s[++top] = symbol;

else

top--;

}

while(s[top != ‘#’)

{

prefix[j++] = s[top--];

}

prefix[j] = ‘’;

strrev(prefix);

}

void main()

{

char infix[20];

char prefix[20];

printf(“/nEnter a valid infix expressionn”);

scanf(“%s”,infix);

infix_prefix(infix, prefix);

printf(“nnThe prefix expression isn”);

printf(“%sn”,prefix);

}


Read more Similar C Programs
C Basic
C Data Structure
Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com