首页 > 其他分享 >Warnings for C programming

Warnings for C programming

时间:2024-07-07 22:53:14浏览次数:15  
标签:code Run Warnings int programming printf define out

Warnings for C programming


"Undetectable null bugs" from dead bytes:

The following code shows an example of a variable declared but not set to a value. Instead of returning the familiar "null", it returns what was left in the memory used for storing the variable, in this case, number 64:

// This is isolated code
int ghost;
printf("%d",ghost);
// This is console io
in  > Run code
out > 64

Turning negative mistakenly

printf() accesses data through the variable name, receives bytes, and converts them into specific formats, hence print"f". Sometimes there is a mismatch between the type of variable put into the printf() parenthesis and the %tag used in the string constant. In the following case:

Variable Type Mismatch Tag Correct Tag
unsigned integer %detc. %u
double %detc. %f
// This is isolated code
unsigned int spook = 0xffffffff;
//4 Bytes are filled
printf("%d",spook);
printf("\n");
printf("%u",spook);

// This is console io
in  > Run code
out > -1
out > 4294967295

Any int divided by int results in int, oddly enough

The arithmatic operation of division for integers is defined as follows:

let p and q be two signed integers, at least one is negative
p%q =def= sign(p)*(abs(p)%abs(q))

let a and b ve two unsigned integers,
define a%b such that:
0 <= a%b < b
b*n <= a < b*(n+1)
b*n + a%b = a

define p/q such that:
q*(p/q)+p%q = p

This means things get tricky when there's a remainder, when p or q is negative or when both misfortunes are faced:

//Code
int p = 5;
int q = -2;

printf("%d",p/q);
printf("%d",p%q);

//Console
in  > Run Code
out > -2
out > 1

#define means Replacing with unexpected Danger

If a constant is defined in the #define part, don't set up variables of the same name. #define means thorough replacement!

// HEAD
#define PIG 233f

//Main, isolated code
float pig = 2024f

//console
in  > Run code
ERROR: 
main.c:x:xx: note: in expansion of macro 'PIG'
      |     float P = 0.001f;
      |           ^

标签:code,Run,Warnings,int,programming,printf,define,out
From: https://www.cnblogs.com/great-cat-42/p/18289068

相关文章

  • Denso Create Programming Contest 2024(AtCoder Beginner Contest 361)E-F
    E求一条树上的路径,使得走遍整棵树花费最小。我们容易发现树上的某条简单路径只需走一次,除此之外所有的路径都需要走两次,那么显而易见,我们需要求树的直径,之后将剩余的路径权值和乘二加上直径权值就可以。F数学题,对于数学题而言,个人感觉时间复杂度的计算对于题目的求解非常重......
  • 《Programming from the Ground Up》阅读笔记:p19-p48
    《ProgrammingfromtheGroundUp》学习第2天,p19-p48总结,总计30页。一、技术总结1.objectfilep20,Anobjectfileiscodethatisinthemachine'slanguage,buthasnotbeencompletelyputtogether。之前在很多地方都看到objectfile这个概念,但都没有看到起定义,这次......
  • Denso Create Programming Contest 2024(AtCoder Beginner Contest 361)
    DensoCreateProgrammingContest2024(AtCoderBeginnerContest361)\(A\)Insert\(AC\)循环结构。点击查看代码inta[200];intmain(){intn,k,x,i;cin>>n>>k>>x;for(i=1;i<=n;i++){cin>>a[i];cout......
  • UFCFVQ-15-M Programming for Data Science
     CollegeofArts,TechnologyandEnvironmentaCADEMICYEAR2023/24 ResitAssessmentBriefSubmissionandfeedbackdatesSubmissiondeadline:Before14:00on15thJuly2024 Thisassessmentiseligiblefor48-hourlatesubmissionwindow.MarksandFeedb......
  • Paper Reading: Genetic programming for multiple-feature construction on high-dim
    目录研究动机文章贡献预备知识本文方法MCIFC:一种多类无关的特征构建方法CDFC:一种多类相关特征构建方法实验结果数据集和实验设置多特征构造与单特征构造对比多树GP对比单树GPfilter对比混合方法类依赖对比类独立非GP对比基于GP的特征构建优点和创新点PaperReading是从......
  • 《Programming from the Ground Up》阅读笔记:p1-p18
    《ProgrammingfromtheGroundUp》学习第1天,p1-18总结,总计18页。一、技术总结1.fetch-executecyclep9,TheCPUreadsininstructionsfrommemoryoneatatimeandexecutesthem.Thisisknownasthefetch-executecycle。2.general-purposevsspecial-purpose(......
  • UNIQUE VISION Programming Contest 2024 Summer (AtCoder Beginner Contest 359)
    A-CountTakahashi(abc359A)解题思路遍历判断即可神奇的代码#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#include<set>#include<cstring>usingnamespacestd;#defineendl'\n......
  • UNIQUE VISION Programming Contest 2024 Summer (AtCoder Beginner Contest 359) 题
    点我看题A-CountTakahashi没啥好说的点击查看代码#include<bits/stdc++.h>#definerep(i,n)for(inti=0;i<n;++i)#definerepn(i,n)for(inti=1;i<=n;++i)#defineLLlonglong#definefifirst#definesesecond#definepbpush_back#definemprmake_pair......
  • 2023 Jiangsu Collegiate Programming Contest, National Invitational of CCPC (Huna
    题目思路来源乱搞ac题解枚举gcd,gcd一定是x的因子,由于lcm+gcd=x,有lcm/gcd+1=x/gcd,还有lcm/gcd>=1枚举lcm/gcd=y,显然如果gcd>1,让gcd和lcm同除以gcd即可,所以可以认为gcd=1,问题转化为,大小为k的集合,k个不同的数,满足gcd=1,且lcm=y的方案数,然后写了个大暴力容斥,没想到过了…......
  • Apple - Authorization Services Programming Guide
    本文翻译整理自:AuthorizationServicesProgrammingGuide(更新日期:2011-10-19https://developer.apple.com/library/archive/documentation/Security/Conceptual/authorization_concepts/01introduction/introduction.html#//apple_ref/doc/uid/TP30000995文章目录一......