{"id":7761,"date":"2026-09-08T00:01:52","date_gmt":"2026-09-08T07:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7761"},"modified":"2026-09-05T12:18:31","modified_gmt":"2026-09-05T19:18:31","slug":"too-big-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7761","title":{"rendered":"Too Big &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7743\">month&#8217;s Exercise<\/a> is about finding the limits, or how large a signed or unsigned <em>char<\/em> value can get before it flips over to negative or to zero. And the trick is performing this feat in your code without using the defined constants for a <em>char<\/em> data type available in the <code>limits.h<\/code> header file.<br \/>\n<!--more--><br \/>\nI have a reason for choosing the <em>char<\/em> data type as opposed to something larger, which I&#8217;ll explain at the end of this post.<\/p>\n<p>For my solution, I use <em>int<\/em> variable <code>count<\/code> to do the incrementing. Two <em>while<\/em> loops process <em>signed char<\/em> <code>c<\/code> and then <em>unsigned char<\/em> <code>uc<\/code> values, typecasting them from variable <code>count<\/code>. Tests are made in each loop to determine when the signed value is less than zero or when the unsigned value returns to zero.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_09-Exercise.c\" rel=\"noopener\" target=\"_blank\">2026_09-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int count;\r\n    char c;\r\n    unsigned char uc;\r\n\r\n    <span class=\"comments\">\/* initialize counting variable *\/<\/span>\r\n    count = 1;\r\n\r\n    <span class=\"comments\">\/* signed test *\/<\/span>\r\n    while(count)\r\n    {\r\n        c = (signed char)count;\r\n        if( c&lt;0 )\r\n        {\r\n            printf(\"'signed char' overflow at %d\\n\",count-1);\r\n            break;\r\n        }\r\n        count++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* unsigned test *\/<\/span>\r\n    while(count)\r\n    {\r\n        uc = (unsigned char)count;\r\n        if( uc==0 )\r\n        {\r\n            printf(\"'unsigned char' overflow at %d\\n\",count-1);\r\n            break;\r\n        }\r\n        count++;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The value of <code>count<\/code> need not be reset between the loops as 128 is a valid <em>unsigned char<\/em> value whereas in the <em>signed<\/em> domain it&#8217;s read as -128. Here is the output from a sample run:<\/p>\n<pre>'signed char' overflow at 127\r\n'unsigned char' overflow at 255<\/pre>\n<p>Similar tests can be preformed with other integer types. In fact, the original assignment I coded tests for overflow with all the integer data types. The problem is that it took a few hours for the <em>long<\/em> test to trigger. After six days, I stopped testing for <em>long long<\/em> overflows.<\/p>\n<p>I don&#8217;t know whether my <em>long long<\/em> tests were flawless or that it really takes that long to overflow. Regardless, my recommendation is that you use the defined constants in the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1190\"><code>limits.h<\/code><\/a> header file for overflow tests as opposed to coding your own tests, though it is an interesting exercise.<\/p>\n<p>I hope that your solution met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is about finding the limits, or how large a signed or unsigned char value can get before it flips over to negative or to zero. And the trick is performing this feat in your code without using &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7761\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7761","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7761"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7761\/revisions"}],"predecessor-version":[{"id":7773,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7761\/revisions\/7773"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}