Jan 4, 2012

K & R C Programs Exercise 3-4.

K and R C, Solution to Exercise 3-4:
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 represent the binary numbers in a negative sign.There are a number of ways of representing signed integers in binary, for example, signed-magnitude, excess-M, one's complement and two's complement. In this program , we handle the problem that is, the value of n equal to -(2 to the power (wordsize - 1))(C Programming Language (2nd Edition) by Brian W.Kernighan & Dennis M.Richie page no 64) by changing the (n /= 10) > 0 to (n /= 10) != 0. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define abs(x)  ((x) < 0 ? -(x) : (x))
void itoa(int n, char s[]);
void reverse(char s[]);

int main(void) {
 char buffer[20];

 printf("INT_MIN: %d\n", INT_MIN);
 itoa(INT_MIN, buffer);
 printf("Buffer : %s\n", buffer);

 return 0;
}
//itoa: convert to n characters in s - modified.
void itoa(int n, char s[])
{
 void int i, sign;
 void everse(char s[]);
 sign = n;
 i = 0;
 do{
  s[i++] = abs(n%10) + '0';
 }while((n/=10)!=0);
 if(sign < 0)
  s[i++] = '-';
 s[i] = '\0';
 reverse(s);
}
void reverse(char s[]) {
 int c, i, j;
 for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
  c = s[i];
  s[i] = s[j];
  s[j] = 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

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