C - sprintf打印变量名

时间:2016-06-03 09:32:11

标签: c string printf

由于某种原因sprintf打印我的char数组的名称,其内容我想附加一个字符串,使结果无效。它看起来没问题,应该有效,只有它没有。 这就是我所拥有的:

Before: name
After: nameST1

然后给了我:

Before: 
After: ST1
从小小的我知道这应该给我

{{1}}

请帮忙。

2 个答案:

答案 0 :(得分:3)

您的第一个SELECT CASE When MM01.MM021_RATE = 1 Then ISNULL(PO101_EXT_COST, 0)*MM01.MM021_FOREIGN_RATE Else ISNULL(PO101_EXT_COST, 0)*MM01.MM021_RATE END AS Amt END AS Amount, ISNULL(IN100_INV_CODE,'') AS [Item Code], ISNULL(IN006_DESC,'') AS Major, ISNULL(IN007_DESC,'') AS Minor, ISNULL(IN100_DESC,'') AS Item, ISNULL(PO100_PO_REQ, 0) AS [PO #], ISNULL(PO100_EVT_ID, 0) AS [Ev ID], ISNULL(EV200_EVT_DESC, '') AS [Ev Desc], ISNULL(PO101_EXT_COST, 0) AS OriginalAmt, ISNULL(PO101_PHASE, '') AS Phase, ISNULL(PO101_STATUS, '') AS Status, ISNULL(EV870_ACCT_SECURITY,'') AS [ARCtl], ISNULL(PO102_ACCOUNT,'') AS [GL Account], MM01.MM021_EFFECTIVE_DATE, MM01.MM021_RATE, MM01.MM021_FOREIGN_RATE, (PO101_ORG_CODE) AS ORG, (PO101_CURRENCY) AS Currency, (PO100_APPROVAL) AS Approval FROM PO101_ORD_DTL LEFT OUTER JOIN PO100_ORDERS ON PO100_ORG_CODE = PO101_ORG_CODE AND PO100_PO_REQ = PO101_PO_REQ LEFT JOIN PO102_DISTR ON PO102_ORG_CODE = PO101_ORG_CODE AND PO102_PO_REQ = PO101_PO_REQ AND PO102_PO_REQ_SEQ = PO101_ORD_SEQ LEFT OUTER JOIN EV200_EVENT_MASTER ON PO100_ORG_CODE = EV200_ORG_CODE AND PO100_EVT_ID = EV200_EVT_ID LEFT OUTER JOIN MM021_CURRENCY_RATES MM01 ON PO101_CURRENCY = MM01.MM021_CODE INNER JOIN (SELECT MM021_CODE, MAX(MM021_EFFECTIVE_DATE) AS EFFECTIVE_DATE FROM MM021_CURRENCY_RATES WHERE MM021_FOREIGN_RATE > 0 AND MM021_TO_CODE = '***' GROUP BY MM021_CODE) MM02 ON MM01.MM021_CODE = MM02.MM021_CODE AND MM01.MM021_EFFECTIVE_DATE = MM02.EFFECTIVE_DATE LEFT OUTER JOIN IN100_INV_MASTER ON PO101_ITEM = IN100_INV_CODE AND PO101_ORG_CODE = IN100_ORG_CODE LEFT OUTER JOIN IN007_MINOR_GROUP ON IN100_ORG_CODE = IN007_ORG_CODE AND IN100_MINOR = IN007_MINOR AND IN100_MAJOR = IN007_MAJOR LEFT JOIN IN006_MAJOR_GROUP ON IN006_ORG_CODE = IN007_ORG_CODE AND IN006_MAJOR = IN007_MAJOR LEFT JOIN EV870_ACCT_MASTER ON PO101_BILLTO=EV870_ACCT_CODE AND PO101_ORG_CODE=EV870_ORG_CODE WHERE (PO101_ORG_CODE = '40' ) AND (EV200_EVT_DESC = 'Tyrexpo India 2016') AND (ISNULL(PO100_APPROVAL,'') IN ('A9')) AND (ISNULL(PO101_STATUS, '') IN ('O','C','H')) AND (ISNULL(PO101_PHASE, '') IN ('1')) AND (PO101_TAX_RES_TYPE <> '8APGST') 调用Undefined Behavior,因为您没有初始化缓冲区。

你可以这样做:

printf

或使用memset重置整个数组:

chat name[50] = {0};

关于第二个memset (name, 0x00, sizeof(name)); 我想你没有发布你的真实代码。因此,发布代码不会导致printf成为第二个nameST1

答案 1 :(得分:2)

char name[]可以包含垃圾值。如图所示使用memset或初始化。

char name[50];
memset(name, 0, sizeof(name));

或使用

char name[50] = {0};