Home >Backend Development >C++ >What Unfounded Assumptions Do C/C Programmers Often Make?

What Unfounded Assumptions Do C/C Programmers Often Make?

Linda Hamilton
Linda HamiltonOriginal
2024-10-30 18:37:31830browse

What Unfounded Assumptions Do C/C   Programmers Often Make?

What Unfounded Assumptions Exist in C/C Platforms?

Introduction

Many novice and experienced C and C programmers frequently make incorrect assumptions that lead to erroneous code. Recognizing and challenging such baseless assumptions is crucial for improved coding practices.

Test Recommendations

To demonstrate and challenge these assumptions, consider the following test program that examines various "conceivable" assumptions commonly made by programmers:

<code class="c">#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

int main() {
    // Character-related Assumptions
    EXPECT("00: ASCII is consistent", ('A' == 65));
    EXPECT("01: A-Z forms a contiguous block", (('Z' - 'A') + 1 == 26));
    EXPECT("02: Uppercase letters precede lowercase letters", ('A' < 'a'));
    EXPECT("03: A character is 8 bits", (CHAR_BIT == 8));
    EXPECT("04: Characters are signed", (CHAR_MIN == SCHAR_MIN));
}

Additional Tests

  • Integer Assumptions:

    • Test the two's complement behavior of integers: (int_max 1) == (int_min) and (INT_MAX 1) == (INT_MIN)
    • Verify if overshifting is always safe: (1 << bits_per_int) == 0 and (1 << BITS_PER_INT) == 0
    • Examine if left-to-right shifting works correctly, even for negative shifts: (t = -1, (15 << t) == 7)
  • Pointer Assumptions:

    • Test if void* can store function pointers: sizeof(void*) >= sizeof(void(*)())
    • Execution Assumptions:

      • Check if detecting stack growth direction is reliable: check_grow(5, 0) != 0 and check_grow(5, 0) < 0
    • Miscellaneous Assumptions:

      • Verify if sizeof(char) is less than sizeof(short), sizeof(short) is less than sizeof(int), and sizeof(int) is less than sizeof(long)
      • Determine if size_t and ptrdiff_t have the same size: sizeof(size_t) == sizeof(ptrdiff_t)
      • Examine if left-to-right evaluation of subexpressions is consistent, even with function calls: (ltr_fun(1) * ltr_fun(2) * ltr_fun(3) * ltr_fun(4), ltr_result == 1234)
    • Participation and Collaborative Development

      To contribute to the project, consider these platforms:

      • GitHub Repository: https://github.com/lutherblissett/disenchanter
      • Stack Overflow Discussion: [link to the original Stack Overflow question]

      By submitting patches or providing new ideas, you can help improve the accuracy and comprehensiveness of the test program.

      The above is the detailed content of What Unfounded Assumptions Do C/C Programmers Often Make?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn