C Program to demonstrate sscanf statement.

C Program to demonstrate the ‘sscanf’ statement.
sscanf statement reads formatted data from the string, Different syntax of sscanf are:
A = sscanf(str, format)
A = sscanf(str, format, sizeA)
[A, count] = sscanf(…)
[A, count, errmsg] = sscanf(…)
[A, count, errmsg, nextindex] = sscanf(…).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>

main()
{
char Host[64];
char User[64];
char *Buff = "Jobname=job1 Hostname=arnamul User=leslim Time=11:15";
/* <----------> <-----> <---------> <-------->
* | | | |
* | ------------ | |
* | | ------------------ V
* | | | NULL
* V V V */
sscanf (Buff, "%*s Hostname=%s %s", Host, User);

printf("Host is %sn", Host);
printf("User is %sn", User);
exit(0);
}


Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

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 sprintf statement.

C Program to demonstrate the ‘sprintf‘ statement. This example is a bit lame as the same effect can be seen with a ‘printf’. But, it does show a string being built and passed into a function. 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>

main()
{
int i=1; /* Define an integer variable. */
char message[80]; /* Text string */

/* format text and put into 'message' this a great
* improvement over using 'strcpy' and 'strcat' to
* build a text string.
*/
sprintf (message, "i is %i", i);
/* I may be stating the obvious but a '' is
* put on the end of the string. */

puts(message); /* Display message */

}

Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

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

K & R C Programs Exercise 5-9.

K and R C, Solution to Exercise 5-9:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C program to , Rewrite the routines of K & R C Programs Exercise 5-8 day_of_year and month_day with pointers instead of indexing.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>

static char daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};



int day_of_year(int year, int month, int day)
{
int i, leap;
char *p;

leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;

/* Set `p' to point at first month in the correct row. */
p = &daytab[leap][1];

/* Move `p' along the row, to each successive month. */
for (i = 1; i < month; i++) {
day += *p;
++p;
}
return day;
}

void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
char *p;

leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
p = &daytab[leap][1];
for (i = 1; yearday > *p; i++) {
yearday -= *p;
++p;
}
*pmonth = i;
*pday = yearday;
}


int main(void)
{
int year, month, day, yearday;

year = 2012;
month = 7;
day = 9;
printf("The date is: %d-%02d-%02dn", year, month, day);
printf("day_of_year: %dn", day_of_year(year, month, day));



yearday = 61; /* 2012-03-01 */
printf("Yearday is %dn", yearday);
month_day(year, yearday, &month, &day);
printf("month_day_pointer: %d %dn", month, day);

return 0;
}


Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

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

K & R C Programs Exercise 5-8.

K and R C, Solution to Exercise 5-8:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
There is no error-checking in the function day_of_year or month_day. Remedy this defect.
In day_of_year we check for reasonable values in month and day. If month is less than one or greater than twelve, day_of_year returns -1. If day is less than one or day exceeds the number of days for the month, the function returns -1.
In month_day we first check for negative year. You may want to add this kind of check in day_of_year also. Then we proceed to decrement year day while we check that the month does not exceed 12. 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>

static char daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};

/* day_of_year: set day of year from month & day */
int day_of_year(int year, int month, int day)
{
int i, leap;

if (year < 1752 || month < 1 || month > 12 || day < 1)
return -1;

leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
if (day > daytab[leap][month])
return -1;

for (i = 1; i < month; i++)
day += daytab[leap][i];
return day;
}

/* month_day: set month, day from day of year */
int month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;

if (year < 1752 || yearday < 1)
return -1;

leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
if ((leap && yearday > 366) || (!leap && yearday > 365))
return -1;

for (i = 1; yearday > daytab[leap][i]; i++)
yearday -= daytab[leap][i];
*pmonth = i;
*pday = yearday;

return 0;
}


/* main: test day_of_year and month_day */
int main(void)
{
int year, month, day, yearday;

for (year = 1970; year <= 2000; ++year) {
for (yearday = 1; yearday < 366; ++yearday) {
if (month_day(year, yearday, &month, &day) == -1) {
printf("month_day failed: %d %dn",
year, yearday);
} else if (day_of_year(year, month, day) != yearday) {
printf("bad result: %d %dn", year, yearday);
printf("month = %d, day = %dn", month, day);
}
}
}

return 0;
}

Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

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

K & R C Programs Exercise 5-6.

K and R C, Solution to Exercise 5-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4). 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>

/* getline: get line into s, return length */
int getline(char *s, int lim)
{
char *p;
int c;

p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != 'n')
*p++ = c;
if (c == 'n')
*p++ = c;
*p = '';
return (int)(p - s);
}


int atoi(char *s)
{
int n, sign;

while (isspace(*s))
s++;
sign = (*s == '+' || *s == '-') ? ((*s++ == '+') ? 1 : -1) : 1;
for (n = 0; isdigit(*s); s++)
n = (n * 10) + (*s - '0');
return sign * n;
}

/*The itoa() function converts an integer value into an
ASCII string of digits.*/

char *utoa(unsigned value, char *digits, int base)
{
char *s, *p;

s = "0123456789abcdefghijklmnopqrstuvwxyz";
if (base == 0)
base = 10;
if (digits == NULL || base < 2 || base > 36)
return NULL;
if (value < (unsigned) base) {
digits[0] = s[value];
digits[1] = '';
} else {
for (p = utoa(value / ((unsigned)base), digits, base);
*p;
p++);
utoa( value % ((unsigned)base), p, base);
}
return digits;
}

char *itoa(int value, char *digits, int base)
{
char *d;
unsigned u;

d = digits;
if (base == 0)
base = 10;
if (digits == NULL || base < 2 || base > 36)
return NULL;
if (value < 0) {
*d++ = '-';
u = -((unsigned)value);
} else
u = value;
utoa(u, d, base);
return digits;
}


static void swap(char *a, char *b, size_t n)
{
while (n--) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
a++;
b++;
}
}

void my_memrev(char *s, size_t n)
{
switch (n) {
case 0:
case 1:
break;
case 2:
case 3:
swap(s, s + n - 1, 1);
break;
default:
my_memrev(s, n / 2);
my_memrev(s + ((n + 1) / 2), n / 2);
swap(s, s + ((n + 1) / 2), n / 2);
break;
}
}

void reverse(char *s)
{
char *p;

for (p = s; *p; p++)
;
my_memrev(s, (size_t)(p - s));
}



static char *strchr(char *s, int c)
{
char ch = c;

for ( ; *s != ch; ++s)
if (*s == '')
return NULL;
return s;
}

int strindex(char *s, char *t)
{
char *u, *v, *w;

if (*t == '')
return 0;
for (u = s; (u = strchr(u, *t)) != NULL; ++u) {
for (v = u, w = t; ; )
if (*++w == '')
return (int)(u - s);
else if (*++v != *w)
break;
}
return -1;
}


#define NUMBER '0'

int getop(char *s)
{
int c;

while ((*s = c = getch()) == ' ' || c == 't')
;
*(s + 1) = '';
if (!isdigit(c) && c != '.')
return c; /* not a number */
if (isdigit(c)) /* collect integer part */
while (isdigit(*++s = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(*++s = c = getch()))
;
*++s = '';
if (c != EOF)
ungetch(c);
return NUMBER;
}




Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

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

K & R C Programs Exercise 5-5.

K and R C, Solution to Exercise 5-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to write the versions of the library functions strncpy, strncat, and strncmp, which operate on at most the first n characters of their argument strings.
For example, strncpy(s, t, n) copies at most n characters of t to s. 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
***********************************************************/

/*strncpy: copy n characters from t to s*/
void strncpy(char *s, char *t, int n)
{
while(*t && n--> 0)
*s++ = *t++;
while( n--> 0)
*s++ = '';
}

/*strncat: concatenate n characters of t to end of s*/
void strncat(char *s, char *t, int n)
{
void strncpy(char *s, char *t, int n);
int strlen(char *);
strncpy(s+strlen(s), t, n);
}

/*strncmp: comapre at most n characters of t with s */
int strncmp(char *s, char *t, int n)
{
for( ; *s == *t; s++, t++)
if(*s == '' || --n <= 0)
{
return 0;
}
return *s - *t;
}
Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

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

K & R C Programs Exercise 4-13.

K and R C, Solution to Exercise 4-13:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C Program to reverse the string using the recursive methods.
In this program reverse determines the length of the string and then calls the reverser, which reverses the string s in place. 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>
/* reverse: reverse the string s in place */
void reverse(char s[])
{

void reverser(char s[], int i, int len);
reverser(s,0,strlen(s));
}

/* reverser: reverse string s in place recursive */
void reverser(char s[], int i, int len)
{
int c, j;
j = len - (i + 1);
if(i < j)
{
c = s[i];
s[i] = s[j];
s[j] = c;
reverser(s, ++i, len);
}
}

Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

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

K & R C Programs Exercise 4-12.

K and R C, Solution to Exercise 4-12:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program to convert an integer into a string by calling a recursive routine.Recursive routine is the programming technique that a routine invoking itself again and again. 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<math.h>
void itoa(int n, char s[]);
int main(void) {
char buffer[20];

//for testing!
printf("INT_MIN: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", buffer);

return 0;
}
/* itoa: convert n to characters in s; recursive */
void itoa(int n, char s[])
{
static int i;
if(n / 10)
itoa(n /10, s);
else{
i = 0;
if(n < 0)
s[i++] = '-';
}
s[i++] = abs(n) %10 + '0';
s[i] = '';
}

Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

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 sum and product using macros.

Write C Program to find sum and product using macros.
Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.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>
#define SUM(A, B) (A + B)
#define PROD(A, B) (A * B)
int main()
{
int num1, num2, sum, product;
printf("nEnter the Two numbersn");
scanf("%d%d",&num1,&num2);
sum=SUM(num1,num2);
product=PROD(num1,num2);
printf("nnSum of two numbers using Macros is:%dn",sum);
printf("nnProduct of two numbers using macros is:%dn",product);
return 0;
}
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

K & R C Programs Exercise 4-9.

K and R C, Solution to Exercise 4-9:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to modify the K & R C Programs Exercise 4-8, getch and ungetch do not handlea pushed back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement the design
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>
#define BUFSIZE 100
int buf[BUFSIZE];
int bufp = 0;

//getch: get a (possibly pushed back) character
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
//ungetch: push a character back onto the input
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characters!");
else
buf[bufp++] = c;
}
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

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