| | |
Creating a table in c
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 1
Reputation:
Solved Threads: 0
I wanted to create a simple table by printing lines in between my values with a header at the top and my numbers left justified. However, when I get to a ten digit number or larger, my code seems to break and my count variable goes to zero as far as I can tell, causing a bunch of space to be printed where it doesn't need to be. I was wondering what I'm doing wrong. Thank you for any help you can offer!
c Syntax (Toggle Plain Text)
#include <stdio.h> int main ( int argc, char *argv[] ) { unsigned int y = 1; int x = 1; int limit = sizeof(int) * 8; int z, n, m, count; printf("| x | y | \n"); for(z = 0; z < limit - 1; z++){ x = x << 1; y = y << 1; m = x; count = 0; while(m > 0){ m = m / 10; count++; } printf("|%d", x); for(n = 11; n >= count; n--) printf(" "); printf("|%u", y); for(n = 11; n >= count; n--) printf(" "); printf("|\n"); return 0 ; }
You want to print a table with the powers of 2 and you want your integers to be signed and unsigned. You're making it to complicated I think, use \t to separate your output. Or use something like printf("|%d |%u |",x,y);
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
The upper limit of an int is 2147483647(32-bit system). When x reaches the value 1073741824,
It would be better to stop your table when m becomes negative.
x<<1; multiples that value by 2 which yields 2147483648 which is beyond the upper limit, hence the value goes in a circle and the number -2147483648 gets stored in x and consequently in m. since you've given while(m>0) count never gets incremented and stays at zero.It would be better to stop your table when m becomes negative.
![]() |
Similar Threads
- Creating A Table With Win32 Api (C++)
- Applying Constraints after creating a table. (MS SQL)
- Creating nice borders the same way they have on vbulletin (Graphics and Multimedia)
- Change Access Table Properties (Visual Basic 4 / 5 / 6)
- Autonumber in a SQL Server table (MS SQL)
- Where is the table ???? (ASP.NET)
- SQL Create table error (ASP.NET)
Other Threads in the C Forum
- Previous Thread: Binary Operation help.
- Next Thread: simple program problem - need help
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






