Undeclared first use in this function ошибка c

In your code

 temp = (Node*) malloc(sizeof(struct Node));

should be

 temp = malloc(sizeof(struct Node));

or, for better,

 temp = malloc(sizeof *temp);

eliminating the issue, as you need not to cast the return value of malloc() and family in C..

The error was, however, because you missed the struct keyword there.

To address the third error,

«expected declaration or statement at end of input.»

well, you missed the ending } for the print() function.

FWIW, you should always check the validity of the returned pointer by malloc() and family of functions before making use of it.

Having said that,

For the first one I’ve searched and found out it’s because of C89 and I have to declare all the variables at the top of my functions

is not true. Yes, in C89, you needed to declare all the variables at top, but that did not allow you to skip the struct keyword, anyway.

Undeclared errors, such as the «first use in this function» issue, are common when programming in C, C++, or other languages that require explicit variable declarations. This guide will help you understand why these errors occur and how to fix them step-by-step.

Table of Contents

  1. Understanding Undeclared Errors
  2. Step-by-Step Guide to Fixing ‘First Use in This Function’ Issues
  3. Related Links
  4. FAQs

Understanding Undeclared Errors {#understanding-undeclared-errors}

An undeclared error occurs when the compiler encounters a variable or function that has not been declared before its first use. In languages like C and C++, you must declare a variable or function before using it. Failing to do so will result in a compilation error, such as the «first use in this function» error.

This error generally occurs due to:

  1. Typographical errors in variable or function names.
  2. Missing or incorrect variable or function declarations.
  3. Misplacement of declarations (e.g., declaring a variable inside a loop or block).
  4. Inclusion issues with header files.

Step-by-Step Guide to Fixing ‘First Use in This Function’ Issues {#step-by-step-guide}

Follow these steps to resolve the «first use in this function» error in your code:

Step 1: Identify the Error

First, locate the error message in your compiler output. It should look similar to the example below:

error: 'variable_name' undeclared (first use in this function)

Take note of the variable or function name causing the error.

Step 2: Verify Spelling and Capitalization

Ensure that the variable or function name is spelled correctly and that the capitalization is consistent throughout your code. A common cause of undeclared errors is a mismatch in variable or function names due to typos or inconsistent capitalization.

Step 3: Check for Missing or Incorrect Declarations

Verify that the variable or function has been declared before its first use. If the declaration is missing, add an appropriate declaration before the first use of the variable or function.

For example, if you’re using a variable named count of type int, ensure that it is declared as:

int count;

Step 4: Ensure Proper Declaration Placement

Make sure that the variable or function declaration is placed correctly in your code. Variables should generally be declared at the beginning of a function or block. Functions should be declared before their definitions or in a separate header file.

If the variable or function is defined in a header file, ensure that the header file is included in the source file where the variable or function is being used. Use the #include directive to include the header file, as shown below:

#include "header_file.h"

Additionally, verify that the header file itself is correctly formatted and that the variable or function declaration is present and accurate.

  1. C Programming: Understanding Variable Scope and Storage Class
  2. C++ Programming: Understanding Variable Scope and Storage Class

FAQs {#faqs}

What is an undeclared error? {#what-is-an-undeclared-error}

An undeclared error occurs when the compiler encounters a variable or function that has not been declared before its first use. This results in a compilation error, such as the «first use in this function» error.

Why do undeclared errors occur? {#why-do-undeclared-errors-occur}

Undeclared errors generally occur due to typographical errors in variable or function names, missing or incorrect variable or function declarations, misplacement of declarations, or inclusion issues with header files.

How do I fix an undeclared error? {#how-do-i-fix-an-undeclared-error}

To fix an undeclared error, follow these steps:

  1. Identify the error.
  2. Verify spelling and capitalization.
  3. Check for missing or incorrect declarations.
  4. Ensure proper declaration placement.
  5. Check header file inclusions.

How do I declare a variable or function in C/C++? {#how-do-i-declare-a-variable-or-function}

In C/C++, you need to specify the data type and the variable name for variable declarations. For example:

int myVariable;

For function declarations, you need to specify the return type, function name, and the argument list. For example:

int myFunction(int a, int b);

What is the scope of a variable? {#what-is-the-scope-of-a-variable}

The scope of a variable refers to the region of the program in which the variable can be accessed. In C/C++, there are four different storage classes that determine the scope and lifetime of a variable: auto, static, extern, and register.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <windows.h>
#include <tchar.h>
#define  DIB_RGB(r, g, b) 
((DWORD)((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF)) 
 
void fill_rect(__int32*, int, int, int, int, int, DWORD);
 
 
 
 
 
// 24/32 бит
BOOL  SaveArrFile(const TCHAR* filename, const __int32* arr, 
                         int width, int height, int bpp = 24){
 
    if((bpp < 24) || (bpp > 32)) // только 24/32 бит
         return FALSE;
 
    DWORD p_row = (DWORD)((width * bpp + 31) & ~31) / 8uL;
    DWORD size  = (DWORD)(height * p_row);
 
    // формируем файловый заголовок
    BITMAPFILEHEADER  hdr;
    ZeroMemory(&hdr, sizeof(BITMAPFILEHEADER));
    hdr.bfType    = 0x4D42;
    hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    hdr.bfSize    = hdr.bfOffBits + size;
 
    // заголовок описателя растра
    BITMAPINFO dib;
    ZeroMemory(&dib, sizeof(BITMAPINFO));
    dib.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
    dib.bmiHeader.biBitCount  = bpp;
    dib.bmiHeader.biCompression  = BI_RGB;
    dib.bmiHeader.biPlanes  = 1u;
    dib.bmiHeader.biWidth   = (long)width;
    dib.bmiHeader.biHeight  = (long)-height;
    dib.bmiHeader.biSizeImage   = size;
    dib.bmiHeader.biXPelsPerMeter = 11811L;
    dib.bmiHeader.biYPelsPerMeter = 11811L;
    dib.bmiHeader.biClrImportant  = 0uL;
    dib.bmiHeader.biClrUsed  = 0uL;
 
    // далее запись в файл
    HANDLE fp = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, 
                                   CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if(fp == INVALID_HANDLE_VALUE)
           return FALSE;
 
    // записываем заголовки...
    DWORD  dwr = 0uL;
    WriteFile(fp, (LPCVOID)&hdr, sizeof(BITMAPFILEHEADER), &dwr, NULL);
    WriteFile(fp, (LPCVOID)&dib.bmiHeader, sizeof(BITMAPINFOHEADER), &dwr, NULL);
 
    // запись массива пикселей
    if(bpp == 32) // 32-бит
           WriteFile(fp, (LPCVOID)arr, size, &dwr, NULL);
    else if(bpp == 24) { // 24-бит с дополнением до 32-разрядной границы
 
          BYTE   nil = 0u;
          int   cb  = sizeof(RGBQUAD);
          int  align = ((cb - ((width*bpp + 7) / 8) % cb) % cb);
        
          for(int y = 0; y < height; y++) {
               for(int x = 0; x < width; x++) 
                    WriteFile(fp, (LPCVOID)&arr[y*width+x], sizeof(RGBTRIPLE), &dwr, NULL);
            
                    for(int i = 0; i < align; i++) // до границы DWORD
                          WriteFile(fp, (LPCVOID)&nil, sizeof(BYTE), &dwr, NULL);
               }
   }
 
   FlushFileBuffers(fp);
   CloseHandle(fp);
   return TRUE;
}
 
 
 
 
 
int  main(void) {
    //массив пикселей
    __int32 arr[111*222] = {0};
    int cw = 222; 
    int ch = 111;
 
    // нарисуем что-нибудь
    DWORD rgb;
    int   sx  = ch / 5;
    int   sy  = cw / 10;
    for(int y = 0; y < 5; y++) {
         for(int x = 0; x < 10; x++) {
              rgb = DIB_RGB(rand()%2*0xFF, rand()%2*0xFF, rand()%2*0xFF);
              fill_rect(arr, cw, x*sx, y*sy, sx, sy, rgb);
        }
    }
 
 
    // сохраняем в файл
    if(SaveArrFile(_T("grid.bmp"), arr, cw, ch, 24))
           _putts(_T("Good save file."));
    else
           _putts(_T("Error save file !"));
 
    _gettchar();
    return 0;
}
 
 
 
// вывод прямоугольника
void fill_rect(__int32* arr, int width, int x, int y, int cx, int cy, DWORD color){
     for(int r = y; r <= (y + cy); r++) {
          for(int c = x; c <= (x + cx); c++)
               arr[r*width + c] = color;
     }
}

Types of Errors in C

Overview

An error in the C language is an issue that arises in a program, making the program not work in the way it was supposed to work or may stop it from compiling as well.
If an error appears in a program, the program can do one of the following three things: the code will not compile, the program will stop working during execution, or the program will generate garbage values or incorrect output. There are five different types of errors in C Programming like Syntax Error, Run Time Error, Logical Error, Semantic Error, and Linker Error.

Scope

  • This article explains errors and their types in C Programming Language.
  • This article covers the explanation and examples for each type of error in C Programming Language (syntax error, run time error, logical error, semantic error, linker error).

Introduction

Let us say you want to create a program that prints today’s date. But instead of writing printf in the code, you wrote print. Because of this, our program will generate an error as the compiler would not understand what the word print means. Hence, today’s date will not print. This is what we call an error. An error is a fault or problem in a program that leads to abnormal behavior of the program. In other words, an error is a situation in which the program does something which it was not supposed to do. This includes producing incorrect or unexpected output, stopping a program that was running, or hindering the code’s compilation. Therefore it is important to remove all errors from our code, this is known as debugging.

How to Read an Error in C?

In order to resolve an error, we must figure out how and why did the error occur. Whenever we encounter an error in our code, the compiler stops the code compilation if it is a syntax error or it either stops the program’s execution or generates a garbage value if it is a run time error.

Syntax errors are easy to figure out because the compiler highlights the line of code that caused the error. Generally, we can find the error’s root cause on the highlighted line or above the highlighted line.

For example:

#include <stdio.h>
int main() {
    int var = 10
    return 0;
}

Output:

error: expected ',' or ';' before 'return'
      4 |  return 0;

As we can see, the compiler shows an error on line 4 of the code. So, in order to figure out the problem, we will go through line 4 and a few lines above it. Once we do that, we can quickly determine that we are missing a semi-colon (;) in line 4. The compiler also suggested the same thing.

Other than the syntax errors, run time errors are often encountered while coding. These errors are the ones that occur while the code is being executed.

Let us now see an example of a run time error:

#include<stdio.h>

void main() {
    
    int var;
    var = 20 / 0;
    
    printf("%d", var);
}

Output:

warning: division by zero [-Wdiv-by-zero]
    6 |     var = 20 / 0;

As we can see, the compiler generated a warning at line 6 because we are dividing a number by zero.

Sometimes, the compiler does not throw a run time error. Instead, it returns a garbage value. In situations like these, we have to figure out why did we get an incorrect output by comparing the output with the expected output. In other cases, the compiler does not display any error at all. The program execution just ends abruptly in cases like these.

Let us take another example to understand this kind of run time error:

#include <stdio.h>
#include <stdlib.h>

int main() {
    
	int arr[1]; 
	arr[0] = 10; 

	int val = arr[10000]; 
	printf("%d", val); 
    return 0;
}

Output:


In the above code, we are trying to access the 10000th element but the size of array is only 1 therefore there is no space allocated to the 10000th element, this is known as segmentation fault.

Types of Errors in C

There are five different types of errors in C.

  1. Syntax Error
  2. Run Time Error
  3. Logical Error
  4. Semantic Error
  5. Linker Error

1. Syntax Error

Syntax errors occur when a programmer makes mistakes in typing the code’s syntax correctly or makes typos. In other words, syntax errors occur when a programmer does not follow the set of rules defined for the syntax of C language.

Syntax errors are sometimes also called compilation errors because they are always detected by the compiler. Generally, these errors can be easily identified and rectified by programmers.

The most commonly occurring syntax errors in C language are:

  • Missing semi-colon (;)
  • Missing parenthesis ({})
  • Assigning value to a variable without declaring it

Let us take an example to understand syntax errors:

#include <stdio.h>

void main() {
    var = 5;    // we did not declare the data type of variable
     
    printf("The variable is: %d", var);
}

Output:

error: 'var' undeclared (first use in this function)

If the user assigns any value to a variable without defining the data type of the variable, the compiler throws a syntax error.

Let’s see another example:

#include <stdio.h>

void main() {
    
    for (int i = 0;) {  // incorrect syntax of the for loop 
        printf("Scaler Academy");
    }
}

Output:

error: expected expression before ')' token

A for loop needs 3 arguments to run. Since we entered only one argument, the compiler threw a syntax error.

2. Runtime Error

Errors that occur during the execution (or running) of a program are called RunTime Errors. These errors occur after the program has been compiled successfully. When a program is running, and it is not able to perform any particular operation, it means that we have encountered a run time error. For example, while a certain program is running, if it encounters the square root of -1 in the code, the program will not be able to generate an output because calculating the square root of -1 is not possible. Hence, the program will produce an error.

Runtime errors can be a little tricky to identify because the compiler can not detect these errors. They can only be identified once the program is running. Some of the most common run time errors are: number not divisible by zero, array index out of bounds, string index out of bounds, etc.

Runtime errors can occur because of various reasons. Some of the reasons are:

  1. Mistakes in the Code: Let us say during the execution of a while loop, the programmer forgets to enter a break statement. This will lead the program to run infinite times, hence resulting in a run time error.
  2. Memory Leaks: If a programmer creates an array in the heap but forgets to delete the array’s data, the program might start leaking memory, resulting in a run time error.
  3. Mathematically Incorrect Operations: Dividing a number by zero, or calculating the square root of -1 will also result in a run time error.
  4. Undefined Variables: If a programmer forgets to define a variable in the code, the program will generate a run time error.

Example 1:

// A program that calculates the square root of integers
#include <stdio.h>
#include <math.h>

int main() {
    for (int i = 4; i >= -2; i--)     {
        printf("%f", sqrt(i));
        printf("n");
    }      
    return 0;
}

Output:

2.000000
1.732051
1.414214
1.000000
0.000000
-1.#IND00
-1.#IND00

**In some compilers, you may also see this output: **

2.000000
1.732051
1.414214
1.000000
0.000000
-nan
-nan

In the above example, we used a for loop to calculate the square root of six integers. But because we also tried calculating the square root of two negative numbers, the program generated two errors (the IND written above stands for «Indeterminate»). These errors are the run time errors.
-nan is similar to IND.

Example 2:

#include<stdio.h>
 
void main() {
    int var = 2147483649;

    printf("%d", var);
}

Output:


This is an integer overflow error. The maximum value an integer can hold in C is 2147483647. Since in the above example, we assigned 2147483649 to the variable var, the variable overflows, and we get -2147483647 as the output (because of the circular property).

3. Logical Error

Sometimes, we do not get the output we expected after the compilation and execution of a program. Even though the code seems error free, the output generated is different from the expected one. These types of errors are called Logical Errors. Logical errors are those errors in which we think that our code is correct, the code compiles without any error and gives no error while it is running, but the output we get is different from the output we expected.

In 1999, NASA lost a spacecraft due to a logical error. This happened because of some miscalculations between the English and the American Units. The software was coded to work for one system but was used with the other.

For Example:

#include <stdio.h>

void main() {
    float a = 10;
    float b = 5;
    
    if (b = 0) {  // we wrote = instead of ==
        printf("Division by zero is not possible");
    } else {
        printf("The output is: %f", a/b);
    }
}

Output:


INF signifies a division by zero error. In the above example, at line 8, we wanted to check whether the variable b was equal to zero. But instead of using the equal to comparison operator (==), we use the assignment operator (=). Because of this, the if statement became false and the value of b became 0. Finally, the else clause got executed.

4. Semantic Error

Errors that occur because the compiler is unable to understand the written code are called Semantic Errors. A semantic error will be generated if the code makes no sense to the compiler, even though it is syntactically correct. It is like using the wrong word in the wrong place in the English language. For example, adding a string to an integer will generate a semantic error.

Semantic errors are different from syntax errors, as syntax errors signify that the structure of a program is incorrect without considering its meaning. On the other hand, semantic errors signify the incorrect implementation of a program by considering the meaning of the program.

The most commonly occurring semantic errors are: use of un-initialized variables, type compatibility, and array index out of bounds.

Example 1:

#include <stdio.h>

void main() {
    int a, b, c;
    
    a * b = c;
    // This will generate a semantic error
}

Output:

error: lvalue required as left operand of assignment

When we have an expression on the left-hand side of an assignment operator (=), the program generates a semantic error. Even though the code is syntactically correct, the compiler does not understand the code.

Example 2:

#include <stdio.h>

void main() {
    int arr[5] = {5, 10, 15, 20, 25};
    
    int arraySize = sizeof(arr)/sizeof(arr[0]);
    
    for (int i = 0; i <= arraySize; i++)
    {
        printf("%d n", arr[i]);
    }
}

Output:


In the above example, we printed six elements while the array arr only had five. Because we tried to access the sixth element of the array, we got a semantic error and hence, the program generated a garbage value.

5. Linker Error

Linker is a program that takes the object files generated by the compiler and combines them into a single executable file. Linker errors are the errors encountered when the executable file of the code can not be generated even though the code gets compiled successfully. This error is generated when a different object file is unable to link with the main object file. We can run into a linked error if we have imported an incorrect header file in the code, we have a wrong function declaration, etc.

For Example:

#include <stdio.h>
 
void Main() { 
    int var = 10;
    printf("%d", var);
}

Output:

undefined reference to `main'

In the above code, as we wrote Main() instead of main(), the program generated a linker error. This happens because every file in the C language must have a main() function. As in the above program, we did not have a main() function, the program was unable to run the code, and we got an error. This is one of the most common type of linker error.

Conclusion

  • There are 5 different types of errors in C programming language: Syntax error, Runtime error, Logical error, Semantic error, and Linker error.
  • Syntax errors, linker errors, and semantic errors can be identified by the compiler during compilation. Logical errors and run time errors are encountered after the program is compiled and executed.
  • Syntax errors, linker errors, and semantic errors are relatively easy to identify and rectify compared to the logical and run time errors. This is so because the compiler generates these 3 (syntax, linker, semantic) errors during compilation itself, while the other 2 errors are generated during or after the execution.

Очень часто начинающие программисты впадают в суеверный ужас, когда видят, что компилятор нашел в тексте программы ошибку, но не понимают, в чем она заключается.

А если помножить этот факт на незнание английского языка («чего там ему не нравится?..») и слабое владение синтаксисом C++ («хм, а может, тут нужна точка с запятой…»), то проблема принимает масштаб катастрофы.

Тот факт, что компилятор в силу своих ограниченных возможностей изо всех сил старается объяснить, что конкретно неверно, не спасает ситуацию. Как быть, если гуглить неохота, а спросить не у кого?

В этом посте на правах копипаста с последующим переводом, дополнениями и исправлениями приведу описание наиболее распространенных сообщений об ошибках и предупреждений компилятора. Неприятность кроется в том факте, что разные компиляторы ругаются на одинаковые ошибки по-разному, а некоторые даже не замечают то, что другие принимают за ошибку. Все зависит от совести разработчиков компилятора, даты его выпуска, и др.

В качестве компилятора возьмем g++, который, в частности, может использоваться в среде Code::Blocks. Версия gcc (куда входит g++) для ОС Windows зовется MinGW. По ходу я буду давать аналоги ошибок из лексикона русскоязычной Microsoft Visual C++.

Итак, частые ошибки:

undeclared identifier

1) Пример

doy.cpp: In function 'int main()':
doy.cpp:25: 'DayOfYear' undeclared (first use this function)
doy.cpp:25: (Each undeclared identifier is reported only once for each function it appears in.)
doy.cpp:25: parse error before ';' token

2) Смысл
Использован идентификатор DayOfYear, но компилятор не нашел его объявления. Он не знает, что такое DayOfYear.

3) Когда бывает

  • Вы забыли включить какой-то заголовочный файл (#include...)
  • Вы где-то ошиблись в написании идентификатора (при объявлении или использовании)
  • Вы вообще забыли, что эту переменную надо объявить

Попытавшись скомпилировать это в Microsoft Visual C++, вы увидите:

error C2065: DayOfYear: необъявленный идентификатор

cout undeclared

1) Пример

xyz.cpp: In function 'int main()':
xyz.cpp:6: 'cout' undeclared (first use this function)
xyz.cpp:6: (Each undeclared identifier is reported only once for each function it appears in.)

2) Смысл
Суперклассика. Без комментариев.

3) Когда бывает

  • Вы забыли включить <iostream>
  • Вы забыли написать using namespace std;

jump to case label

1) Пример

switch.cpp: In function 'int main()':
switch.cpp:14: jump to case label
switch.cpp:11: crosses initialization of 'int y'

2) Смысл
Смысл туманен

3) Когда бывает
Вы попытались объявить и инициализировать переменную (объект, указатель и т.п.) в метке case оператора выбора switch. Правилами C++ это запрещено.

В Microsoft Visual C++ эта ошибка зовется

error C2360: пропуск инициализации 'y' из-за метки 'case'

Выход: заключите операторы этого case’а в фигурные скобки {}.

multi-line string / unterminated string

1) Пример
Программка

#include <iostream>

using namespace std;

int main()
{
cout << "Bob is my buddy;
cout << "and so is Mary" << endl;
}

вызовет бурную реакцию компилятора:

string.cpp:7:12: warning: multi-line string literals are deprecated
string.cpp: In function 'int main()':
string.cpp:7: 'so' undeclared (first use this function)
string.cpp:7: (Each undeclared identifier is reported only once for each function it appears in.)
string.cpp:7: parse error before 'Mary'
string.cpp:8:28: warning: multi-line string literals are deprecated
string.cpp:8:28: missing terminating " character
string.cpp:7:12: possible start of unterminated string literal

2) Смысл
Компилятор думает, что мы хотим создать строковую константу с содержащимся в ней переносом строки, что-то типа

"Hello
world!"

что не поддерживается языком. Также делается предположение о том, что мы, возможно, забыли поставить кавычки в конце первой строки. Собственно, так оно и есть.

3) Когда бывает
Когда не соблюдается правильное количество и положение кавычек в строковых литералах. Надо быть внимательнее.

Microsoft Visual C++ со свойственной ему детской непосредственностью, отметит, что нельзя делать переносы в строках и возмутится, где точка с запятой:

error C2001: newline в константе
error C2146: синтаксическая ошибка: отсутствие ";" перед идентификатором "cout"

comparison between signed and unsigned integer expressions

1) Пример

xyz.cpp: In function 'int main()':
xyz.cpp:54: warning: comparison between signed and unsigned integer expressions

2) Смысл
Это — предупреждение компилятора, которое говорит о том, что мы пытаемся сравнить (==, и т.д.) целочисленное выражение (может принимать положительные, отрицательные значения и 0) и беззнаковое целочисленное выражение (может быть только положительным, либо 0).

3) Когда бывает
Собственно, тогда и бывает. Напомню, что тип int по умолчанию знаковый, а некоторые функции (например, vector::size()) возвращают unsigned int.
К примеру, следующий на первый взгляд безобидный код вызовет описываемое предупреждение:

for (int i = 0; i < grades.size(); i++)
{
// ...
}

Следует помнить, что в памяти знаковые и беззнаковые типы имеют разные внутренние представления, поэтому надо быть чертовски осторожными с указателями.

В Microsoft Visual C++ предупреждение выглядит так:

warning C4018: <: несоответствие типов со знаком и без знака

suggest parentheses around assignment used as truth value

1) Пример

xyz.cpp: In function `int main()':
xyz.cpp:54: warning: suggest parentheses around assignment used as truth value

2) Смысл
Тоже классика. Компилятор предполагает (и в 99% случаев прав), что вы по ошибке включили в скобки в качестве условия для if/while/for вместо условного выражения выражение присваивания.

3) Когда бывает
Чаще всего — в if‘ах, когда вместо "==" используется "="

if (length = maxLength)

вместо

if (length == maxLength)

Заминка в том, что это не ошибка, т.к. в скомпилированной программе (если мы проигнорируем предупреждение) выражение присваивания (которое возвращает значение правого аргумента) во всех случаях, кроме тех, когда оно вернет 0, будет преобразовано к true.

Ссылки для дальнейшего изучения
Ошибки построения Microsoft Visual C++
GCC Compiler error messages
GCC Warnings

P.S. Следует отметить, что кроме ошибок стадии компиляции встречаются (гораздо реже) ошибки препроцессора (например, если не найден заголовочный файл <iostram>), ошибки стадии компоновки (можно избежать, если научиться пользоваться средой программирования) и — самый гнусный тип ошибок! — ошибки стадии выполнения. Т.н. runtime error. С ними может справиться только голова программиста, вооруженная отладчиком.

You are attempting to call a function truncate() at:

centimeters = truncate(centimeters);

You have not yet told the compiler what that function is, so it is undefined and the compiler is objecting.

In C++, all functions must be declared (or defined) before they are used. If you think you are using a standard C++ library function, you need to include its header. If you are not sure that you are using a C++ library function, you need to declare and define your own.

Be aware that on POSIX-based systems, truncate() is a system call that truncates an existing file; it will have a different prototype from what you are trying to use.


Further down your code — hidden off the bottom of the scroll bar — are the function definitions for truncate() and round(). Put the function definitions at the top of the file, so that the compiler knows about their signature before they are used. Or add forward declarations of the functions at the top of the file and leave the definitions where they are.

Очень часто начинающие программисты впадают в суеверный ужас, когда видят, что компилятор нашел в тексте программы ошибку, но не понимают, в чем она заключается.

А если помножить этот факт на незнание английского языка («чего там ему не нравится?..») и слабое владение синтаксисом C++ («хм, а может, тут нужна точка с запятой…»), то проблема принимает масштаб катастрофы.

Тот факт, что компилятор в силу своих ограниченных возможностей изо всех сил старается объяснить, что конкретно неверно, не спасает ситуацию. Как быть, если гуглить неохота, а спросить не у кого?

В этом посте на правах копипаста с последующим переводом, дополнениями и исправлениями приведу описание наиболее распространенных сообщений об ошибках и предупреждений компилятора. Неприятность кроется в том факте, что разные компиляторы ругаются на одинаковые ошибки по-разному, а некоторые даже не замечают то, что другие принимают за ошибку. Все зависит от совести разработчиков компилятора, даты его выпуска, и др.

В качестве компилятора возьмем g++, который, в частности, может использоваться в среде Code::Blocks. Версия gcc (куда входит g++) для ОС Windows зовется MinGW. По ходу я буду давать аналоги ошибок из лексикона русскоязычной Microsoft Visual C++.

Итак, частые ошибки:

undeclared identifier

1) Пример

doy.cpp: In function 'int main()':
doy.cpp:25: 'DayOfYear' undeclared (first use this function)
doy.cpp:25: (Each undeclared identifier is reported only once for each function it appears in.)
doy.cpp:25: parse error before ';' token

2) Смысл
Использован идентификатор DayOfYear, но компилятор не нашел его объявления. Он не знает, что такое DayOfYear.

3) Когда бывает

  • Вы забыли включить какой-то заголовочный файл (#include...)
  • Вы где-то ошиблись в написании идентификатора (при объявлении или использовании)
  • Вы вообще забыли, что эту переменную надо объявить

Попытавшись скомпилировать это в Microsoft Visual C++, вы увидите:

error C2065: DayOfYear: необъявленный идентификатор

cout undeclared

1) Пример

xyz.cpp: In function 'int main()':
xyz.cpp:6: 'cout' undeclared (first use this function)
xyz.cpp:6: (Each undeclared identifier is reported only once for each function it appears in.)

2) Смысл
Суперклассика. Без комментариев.

3) Когда бывает

  • Вы забыли включить <iostream>
  • Вы забыли написать using namespace std;

jump to case label

1) Пример

switch.cpp: In function 'int main()':
switch.cpp:14: jump to case label
switch.cpp:11: crosses initialization of 'int y'

2) Смысл
Смысл туманен

3) Когда бывает
Вы попытались объявить и инициализировать переменную (объект, указатель и т.п.) в метке case оператора выбора switch. Правилами C++ это запрещено.

В Microsoft Visual C++ эта ошибка зовется

error C2360: пропуск инициализации 'y' из-за метки 'case'

Выход: заключите операторы этого case’а в фигурные скобки {}.

multi-line string / unterminated string

1) Пример
Программка

#include <iostream>

using namespace std;

int main()
{
cout << "Bob is my buddy;
cout << "and so is Mary" << endl;
}

вызовет бурную реакцию компилятора:

string.cpp:7:12: warning: multi-line string literals are deprecated
string.cpp: In function 'int main()':
string.cpp:7: 'so' undeclared (first use this function)
string.cpp:7: (Each undeclared identifier is reported only once for each function it appears in.)
string.cpp:7: parse error before 'Mary'
string.cpp:8:28: warning: multi-line string literals are deprecated
string.cpp:8:28: missing terminating " character
string.cpp:7:12: possible start of unterminated string literal

2) Смысл
Компилятор думает, что мы хотим создать строковую константу с содержащимся в ней переносом строки, что-то типа

"Hello
world!"

что не поддерживается языком. Также делается предположение о том, что мы, возможно, забыли поставить кавычки в конце первой строки. Собственно, так оно и есть.

3) Когда бывает
Когда не соблюдается правильное количество и положение кавычек в строковых литералах. Надо быть внимательнее.

Microsoft Visual C++ со свойственной ему детской непосредственностью, отметит, что нельзя делать переносы в строках и возмутится, где точка с запятой:

error C2001: newline в константе
error C2146: синтаксическая ошибка: отсутствие ";" перед идентификатором "cout"

comparison between signed and unsigned integer expressions

1) Пример

xyz.cpp: In function 'int main()':
xyz.cpp:54: warning: comparison between signed and unsigned integer expressions

2) Смысл
Это — предупреждение компилятора, которое говорит о том, что мы пытаемся сравнить (==, и т.д.) целочисленное выражение (может принимать положительные, отрицательные значения и 0) и беззнаковое целочисленное выражение (может быть только положительным, либо 0).

3) Когда бывает
Собственно, тогда и бывает. Напомню, что тип int по умолчанию знаковый, а некоторые функции (например, vector::size()) возвращают unsigned int.
К примеру, следующий на первый взгляд безобидный код вызовет описываемое предупреждение:

for (int i = 0; i < grades.size(); i++)
{
// ...
}

Следует помнить, что в памяти знаковые и беззнаковые типы имеют разные внутренние представления, поэтому надо быть чертовски осторожными с указателями.

В Microsoft Visual C++ предупреждение выглядит так:

warning C4018: <: несоответствие типов со знаком и без знака

suggest parentheses around assignment used as truth value

1) Пример

xyz.cpp: In function `int main()':
xyz.cpp:54: warning: suggest parentheses around assignment used as truth value

2) Смысл
Тоже классика. Компилятор предполагает (и в 99% случаев прав), что вы по ошибке включили в скобки в качестве условия для if/while/for вместо условного выражения выражение присваивания.

3) Когда бывает
Чаще всего — в if‘ах, когда вместо "==" используется "="

if (length = maxLength)

вместо

if (length == maxLength)

Заминка в том, что это не ошибка, т.к. в скомпилированной программе (если мы проигнорируем предупреждение) выражение присваивания (которое возвращает значение правого аргумента) во всех случаях, кроме тех, когда оно вернет 0, будет преобразовано к true.

Ссылки для дальнейшего изучения
Ошибки построения Microsoft Visual C++
GCC Compiler error messages
GCC Warnings

P.S. Следует отметить, что кроме ошибок стадии компиляции встречаются (гораздо реже) ошибки препроцессора (например, если не найден заголовочный файл <iostram>), ошибки стадии компоновки (можно избежать, если научиться пользоваться средой программирования) и — самый гнусный тип ошибок! — ошибки стадии выполнения. Т.н. runtime error. С ними может справиться только голова программиста, вооруженная отладчиком.

это перевод страницы «CSM217: C for Computer Scientists Common gcc Compiler Errors» staff.um.edu.mt/csta1//courses/lectures/csa2060…
(чтобы не затерялось на диске)

Ниже приведены список наиболее часто встречающихся во время компиляции ошибок и предупреждений компилятора gcc, а также объяснения причин их возникновения. Обратите, пожалуйста, внимание, что данный материал применим к UNIX-подобным операционным системам.

В квадратных скобках приводится перевод системных сообщений.

Список ошибок

Описание общих ошибок / предупреждений компилятора

‘variable’ undeclared (first use in this function)

[‘переменная’ не определена (первое использование в этой функции)]

unknown escape sequence

[неизвестная управляющая последовательность]

No such file or directory

[Нет такого файла или каталога]

ld: elf error: file prog:

[ld: ошибка формата elf: файл prog:]

parse error before ‘string’

[ошибка разбора перед ‘строкой’]

Undefined symbol

[Неопределённый символ]

character constant too long

[слишком длинная символьная константа]

passing arg n of ‘function’ makes
data type from another data type without a cast

[передаваемый аргумент n функции ‘function’ создаёт
тип данных из другого типа данных без приведения]

unterminated string or character constant

[незавершённая строка или символьная константа]

parse error at end of input

[ошибка разбора в конце ввода]


Описание общих ошибок / предупреждений компилятора

В Си ошибки компиляции обычно являются критическими, то есть Си-компилятор не может скомпилировать ваш код. В то же время предупреждение — это просто предупреждение: компилятор выявил потенциальную проблему, но, тем не менее, смог создать объектный код. Предупреждения не следует игнорировать, они означают обычно, что что-то не так с вашей программой, и скорее всего, она поведёт себя не совсем так, как вы ожидаете.

Перед сообщениями об ошибках и предупреждениями отображается имена файла и функции, в которых произошла ошибка. Например,

prog.c: In function ‘main’:

[prog.c: В функции ‘main’:]

означает, что ошибка произошла при компиляции программного файла prog.c, а точнее, в функции main. В следующих строках отображаются ошибки / предупреждения для данных файла и функции. Если в коде несколько функций и/или несколько файлов, то каждый файл / функция, содержащий ошибки, будет показан отдельно.

Строки сообщения об ошибке/предупреждения, следующие за именем файла / функции, содержат:

  • имя файла / функции, содержащего ошибку;
  • номер строки в исходном файле, где встретилась ошибка;
  • строку «warning» [«предупреждение»], если сообщение является предупреждающим (при ошибке эта строка пустая);
  • описание встреченной ошибки.

Например,

prog.c:3: warning: unknown escape sequence ‘z’

[prog.c:3: предупреждение: неизвестная управляющая последовательность ‘z’]

означает, что существует проблема в строке 3 файла prog.c. Это сообщение — только предупреждение: не существует управляющей последовательности ‘z’. Программа будет скомпилирована (если только не будет других ошибок), так как это просто предупреждение. Однако, скорее всего, программа поведёт себя не совсем так, как вы ожидаете.

‘variable’ undeclared (first use in this function) [‘переменная’ не определена (первое использование в этой функции)]

Си — это типизированный язык, то есть требуется объявлять переменные перед началом их использования. Си также регистрозависимый язык, то есть var и Var — разные переменные. Вы либо забыли объявить переменную, либо ошиблись в написании её имени.

Это сообщение об ошибке — компилятор не смог скомпилировать код. Обычно данное сообщение сопровождается текстом (Each undeclared identifier is reported only once for each function it appears in.) [(Выводится только одно сообщение на функцию для каждого необъявленного идентификатора.)].
Одна и та же необъявленная переменная может несколько раз встречаться в функции, но компилятор сообщает только о первом случае.

unknown escape sequence ‘z’ [неизвестная управляющая последовательность]

Управляющая последовательность — это символ, перед которым находится обратный слеш ». Наличие » изменяет функцию последующего символа. Например, ‘n’ — это символ n, но ‘n’ — это знак перевода строки. Для некоторых символов, например, z, нет управляющих последовательностей. Программа будет скомпилирована (если только не будет других ошибок), так как это просто предупреждение. Однако, скорее всего, программа поведёт себя не совсем так, как вы ожидаете.

No such file or directory [Нет такого файла или каталога]

Вы дали команду скомпилировать файл, которого не существует. Ожидается, что файлы исходных кодов Си-программ будут иметь расширение .c. Если исходный код находится в файле proc.c, следует использовать команду компиляции gcc prog.c. Попытка выполнить gcc prog приведёт к этой ошибке. Данная ошибка обычно сопровождается сообщением No input files [Нет входных файлов].

ld: elf error: file prog: [ld: ошибка формата elf: файл prog:]

Обычно вы также увидите следующее сообщение…

unknown type, unable to process using elf(3E) libraries

ld: fatal: File processing errors. No output written to a.out

collect2: ld returned 1 exit status

[неизвестный тип, невозможно обработать, используя библиотеки elf(3E)

ld: критическое: Ошибки обработки файла. В файл a.out ничего не записано

collect2: ld вернул статус выхода 1 ]

Неприятная ошибка; к счастью, её легко исправить! Код вашей Си-программы находится в файле prog, но, поскольку вы не использовали расширение .c для этого файла, gcc не может понять, какого типа программный код содержится в указанном файле. Для исправления ошибки просто переименуйте prog в prog.c.

parse error before ‘string’ [ошибка разбора перед ‘строкой’]

Си-компилятор встретил что-то, что он не смог не только распознать, но даже и предположить, что бы это могло быть. Объявления в Си обычно начинаются с зарезервированного слова, имени переменной или имени функции. Подобная ошибка получается, когда не распознаётся первое слово в объявлении, и оно не является ни вызовом функции, ни именем переменной и т.п., например,

main() {
silly printf("Hello, worldn";);
}

Иногда данная ошибка вызывается отсутствующей закрывающей скобкой ()) или фигурной скобкой (}) где-то в районе указанной строки — перед символом или строкой, указанными в маркере (token). Также возможно, что у вас в коде лишние скобки! А может быть пропущена закрывающая предыдущее объявление точка с запятой (;).

Undefined symbol [Неопределённый символ]

Undefined                       first referenced
symbol in file
main /usr/local/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
[Неопределённый                       впервые встречается
символ в файле
main /usr/local/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o
ld: критическое: Ошибки символьных обращений. В файл a.out ничего не записано
collect2: ld вернул статус выхода 1 ]

Gcc встретил что-то похожее на вызов функции, но функции с таким именем не существует. Имя «отсутствующей» функции — это первое слово в третьей строке сообщения об ошибке (main).

Во всех Си-программах должна существовать функция main, поскольку именно с неё начинается исполнение программы.

character constant too long [слишком длинная символьная константа]

В Си строки должны заключаться в двойные кавычки («»;). Если вы используете одинарные кавычки (»), ожидается, что в них будет только один символ (или управляющая последовательность).

warning: passing arg n of ‘function’ makes
pointer from integer without a cast [передаваемый аргумент n функции ‘function’ создаёт указатель из целого без приведения]

Вы вызвали функцию function. Ожидается, что аргумент n будет указателем, но вы передаёте в функцию целое значение (int). Для изменения типа значения используется приведение типов, но у вас этого нет. Си — это типизированный язык, то есть требуется объявлять типы переменных и типы данных, передаваемых функциям, перед началом их использования. Данное предупреждение выводится, если встречено несоответствие типов между данными, передаваемыми в функцию, и данными, ожидаемыми функцией (тип передаваемых
параметров указывается в прототипе функции). Несмотря на то что gcc создаст объектный код, на это предупреждение стоит обратить внимание, так как оно обычно указывает на логическую ошибку.

unterminated string or character constant [незавершённая строка или символьная константа]

Не совпадает число открывающих и закрывающих кавычек (одинарных или двойных).

parse error at end of input [ошибка разбора в конце ввода]

Вы, возможно, потеряли закрывающую фигурную скобку (}) где-то в коде. Си не может сказать, где именно, так что удачи в поисках!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <windows.h> #include <tchar.h> #define  DIB_RGB(r, g, b)  ((DWORD)((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF))    void fill_rect(__int32*, int, int, int, int, int, DWORD);           // 24/32 бит BOOL  SaveArrFile(const TCHAR* filename, const __int32* arr,                          int width, int height, int bpp = 24){       if((bpp < 24) || (bpp > 32)) // только 24/32 бит          return FALSE;       DWORD p_row = (DWORD)((width * bpp + 31) & ~31) / 8uL;     DWORD size  = (DWORD)(height * p_row);       // формируем файловый заголовок     BITMAPFILEHEADER  hdr;     ZeroMemory(&hdr, sizeof(BITMAPFILEHEADER));     hdr.bfType    = 0x4D42;     hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);     hdr.bfSize    = hdr.bfOffBits + size;       // заголовок описателя растра     BITMAPINFO dib;     ZeroMemory(&dib, sizeof(BITMAPINFO));     dib.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);     dib.bmiHeader.biBitCount  = bpp;     dib.bmiHeader.biCompression  = BI_RGB;     dib.bmiHeader.biPlanes  = 1u;     dib.bmiHeader.biWidth   = (long)width;     dib.bmiHeader.biHeight  = (long)-height;     dib.bmiHeader.biSizeImage   = size;     dib.bmiHeader.biXPelsPerMeter = 11811L;     dib.bmiHeader.biYPelsPerMeter = 11811L;     dib.bmiHeader.biClrImportant  = 0uL;     dib.bmiHeader.biClrUsed  = 0uL;       // далее запись в файл     HANDLE fp = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,                                    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);     if(fp == INVALID_HANDLE_VALUE)            return FALSE;       // записываем заголовки...     DWORD  dwr = 0uL;     WriteFile(fp, (LPCVOID)&hdr, sizeof(BITMAPFILEHEADER), &dwr, NULL);     WriteFile(fp, (LPCVOID)&dib.bmiHeader, sizeof(BITMAPINFOHEADER), &dwr, NULL);       // запись массива пикселей     if(bpp == 32) // 32-бит            WriteFile(fp, (LPCVOID)arr, size, &dwr, NULL);     else if(bpp == 24) { // 24-бит с дополнением до 32-разрядной границы             BYTE   nil = 0u;           int   cb  = sizeof(RGBQUAD);           int  align = ((cb - ((width*bpp + 7) / 8) % cb) % cb);                   for(int y = 0; y < height; y++) {                for(int x = 0; x < width; x++)                     WriteFile(fp, (LPCVOID)&arr[y*width+x], sizeof(RGBTRIPLE), &dwr, NULL);                                 for(int i = 0; i < align; i++) // до границы DWORD                           WriteFile(fp, (LPCVOID)&nil, sizeof(BYTE), &dwr, NULL);                }    }      FlushFileBuffers(fp);    CloseHandle(fp);    return TRUE; }           int  main(void) {     //массив пикселей     __int32 arr[111*222] = {0};     int cw = 222;     int ch = 111;       // нарисуем что-нибудь     DWORD rgb;     int   sx  = ch / 5;     int   sy  = cw / 10;     for(int y = 0; y < 5; y++) {          for(int x = 0; x < 10; x++) {               rgb = DIB_RGB(rand()%2*0xFF, rand()%2*0xFF, rand()%2*0xFF);               fill_rect(arr, cw, x*sx, y*sy, sx, sy, rgb);         }     }         // сохраняем в файл     if(SaveArrFile(_T("grid.bmp"), arr, cw, ch, 24))            _putts(_T("Good save file."));     else            _putts(_T("Error save file !"));       _gettchar();     return 0; }       // вывод прямоугольника void fill_rect(__int32* arr, int width, int x, int y, int cx, int cy, DWORD color){      for(int r = y; r <= (y + cy); r++) {           for(int c = x; c <= (x + cx); c++)                arr[r*width + c] = color;      } }
  1. 07-02-2013

    #1

    critixal is offline


    Registered User


    Hello Programmers, I am trying to make a program that will allow a user to enter their name, from this point, three options should be present for the user to choose, then from that point, two more options should be present. I have if else statements in a switch case and I get the «undeclared» error for the a in the first » if(specificage == a) «. Does anyone know the fix to this, it is the only error I get. I’m new to programming, sorry if I sound like a rookie. Run it if you need, thanks!

    NOTE: I use Code::Blocks.

    Here is the code:

    Code:

    #include <stdio.h> #include <string.h> int main() { char name[50]; char ageclass[50]; char specificage[50]; printf("Please enter your name: "); fgets(name, 50, stdin); printf("Hello %s", name); printf("Are you a:n"); printf("1. Minorn"); printf("2. Adultn"); printf("3. Senior Citizenn"); printf("Answer(1, 2, or 3): "); fgets(ageclass, 50, stdin); switch (ageclass[50]) { case'1': { printf("You are between the ages of:n"); printf("a. 0-12n"); printf("b. 13-18n"); printf("Answer(a or b): "); fgets(specificage, 50, stdin); if(specificage == a) { printf("You are a young minor."); } else { printf("You are an older minor."); } } case'2': { printf("You are between the ages of:n"); printf("a. 19-50n"); printf("b. 51-65n"); printf("Answer(a or b): "); fgets(specificage, 50, stdin); if(specificage == a) { printf("You are a young adult."); } else { printf("You are an older adult."); } } case'3': { printf("You are between the ages of:n"); printf("a. 66-90n"); printf("b. 91-110n"); printf("Answer(a or b): "); fgets(specificage, 50, stdin); if(specificage == a) { printf("You are a young senior citizen."); } else { printf("You are an older senior citizen."); } } } getchar(); return 0; }

    Last edited by critixal; 07-02-2013 at 05:49 PM.


  2. 07-02-2013

    #2

    critixal is offline


    Registered User


    Don’t reply, issue resolved!


  3. 07-02-2013

    #3

    anduril462 is offline


    Registered User


    You have two problems. First

    Code:

    if(specificage == a)

    a here is treated as a variable. If you want to check that the user input the letter a, you need to put single quotes around it: ‘a’. If you want a string containing one character, the letter a, use double quotes: «a»

    Second, specificage is a char array/string. You can’t compare strings with ==. You can compare individual characters in the string/array with ==, or use strcmp/strncmp:

    Code:

    if (strcmp(specificage, "some string") == 0) // strcmp returns 0 if they're equal // or if (specificage[3] == 'q') // check that the 4th (remember, arrays in C start at 0) character is a 'q'

    I’m guessing you were more interested in the second example.

    Two more notes:
    1. fgets leaves the newline character (remember, the user pressed enter after ‘a’ or ‘b’). If I enter «foo» followed by enter, the string fgets gives back contains «foon». If you do strcmp, it will not match «foo», since the n is significant. If you need to get rid of it, use the strchr trick here: FAQ > Get a line of text from the user/keyboard (C) — Cprogramming.com.
    2. The user may enter uppercase ‘A’ or ‘B’ for some reason, your program should handle it. Easy enough, use the toupper() or tolower() functions before comparing (remember to #include <ctype.h>)


  4. 07-02-2013

    #4

    critixal is offline


    Registered User


    Quote Originally Posted by anduril462
    View Post

    You have two problems. First

    Code:

    if(specificage == a)

    a here is treated as a variable. If you want to check that the user input the letter a, you need to put single quotes around it: ‘a’. If you want a string containing one character, the letter a, use double quotes: «a»

    Second, specificage is a char array/string. You can’t compare strings with ==. You can compare individual characters in the string/array with ==, or use strcmp/strncmp:

    Code:

    if (strcmp(specificage, "some string") == 0) // strcmp returns 0 if they're equal // or if (specificage[3] == 'q') // check that the 4th (remember, arrays in C start at 0) character is a 'q'

    I’m guessing you were more interested in the second example.

    Two more notes:
    1. fgets leaves the newline character (remember, the user pressed enter after ‘a’ or ‘b’). If I enter «foo» followed by enter, the string fgets gives back contains «foon». If you do strcmp, it will not match «foo», since the n is significant. If you need to get rid of it, use the strchr trick here: FAQ > Get a line of text from the user/keyboard (C) — Cprogramming.com.
    2. The user may enter uppercase ‘A’ or ‘B’ for some reason, your program should handle it. Easy enough, use the toupper() or tolower() functions before comparing (remember to #include <ctype.h>)

    Oh, actually this helped a lot, I will make these changes, thank you tons!
    Could you check out my other post, the more recent one, thanks! It involves the same program.

    Last edited by critixal; 07-02-2013 at 06:33 PM.


Что такое необъявленные ошибки идентификатора? Каковы общие причины и как их исправить?

Пример текстов ошибок:

  • Для компилятора Visual Studio: error C2065: 'cout' : undeclared identifier
  • Для компилятора GCC: 'cout' undeclared (first use in this function)

39

Решение

Чаще всего они приходят из-за того, что забывают включить заголовочный файл, содержащий объявление функции, например, эта программа выдаст ошибку «необъявленный идентификатор»:

Отсутствует заголовок

int main() { std::cout << "Hello world!" << std::endl; return 0; } 

Чтобы это исправить, мы должны включить заголовок:

#include <iostream> int main() { std::cout << "Hello world!" << std::endl; return 0; } 

Если вы написали заголовок и включили его правильно, заголовок может содержать неправильный включить охрану.

Чтобы узнать больше, смотрите http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.

Переменная с ошибкой

Другой распространенный источник ошибки новичка возникает, когда вы неправильно написали переменную:

int main() { int aComplicatedName; AComplicatedName = 1; /* mind the uppercase A */ return 0; } 

Неправильный объем

Например, этот код выдаст ошибку, потому что вам нужно использовать std::string:

#include <string> int main() { std::string s1 = "Hello"; // Correct. string s2 = "world"; // WRONG - would give error. } 

Использовать до объявления

void f() { g(); } void g() { } 

g не был объявлен до его первого использования. Чтобы это исправить, либо переместите определение g до f:

void g() { } void f() { g(); } 

Или добавить декларацию g до f:

void g(); // declaration void f() { g(); } void g() { } // definition 

stdafx.h не сверху (специфично для VS)

Это зависит от Visual Studio. В VS нужно добавить #include "stdafx.h" перед любым кодом. Код до того, как он игнорируется компилятором, так что если у вас есть это:

#include <iostream> #include "stdafx.h"

#include <iostream> будет проигнорировано Вам нужно переместить его ниже:

#include "stdafx.h"#include <iostream> 

Не стесняйтесь редактировать этот ответ.

54

Другие решения

Рассмотрим похожую ситуацию в разговоре. Представьте, что ваш друг говорит вам: «Боб идет на ужин», а ты не представляешь, кто такой Боб. Вы будете в замешательстве, верно? Твой друг должен был сказать: «У меня есть коллега по работе по имени Боб. Боб подходит к обеду». Теперь Боб объявлен, и вы знаете, о ком говорит ваш друг.

Компилятор выдает ошибку «необъявленный идентификатор», когда вы пытаетесь использовать какой-то идентификатор (который будет именем функции, переменной, класса и т. Д.), И компилятор не видит объявления для него. То есть компилятор понятия не имеет, о чем вы говорите, потому что раньше его не видел.

Если вы получаете такую ​​ошибку в C или C ++, это означает, что вы не сказали компилятору о том, что вы пытаетесь использовать. Объявления часто встречаются в заголовочных файлах, поэтому, скорее всего, это означает, что вы не включили соответствующий заголовок. Конечно, может случиться так, что вы просто не помните, чтобы объявить сущность вообще.

Некоторые компиляторы выдают более конкретные ошибки в зависимости от контекста. Например, пытаясь скомпилировать X x; где тип X не был объявлен с Clang скажет вам «неизвестное имя типа X«. Это гораздо полезнее, потому что вы знаете, что он пытается интерпретировать X как тип. Тем не менее, если у вас есть int x = y;, где y еще не объявлено, он скажет вам «использование необъявленного идентификатора y«потому что есть некоторая двусмысленность в том, что именно y может представлять.

12

У меня была такая же проблема с пользовательским классом, который был определен в пространстве имен. Я пытался использовать класс без пространства имен, вызывая ошибку компилятора «идентификатор» MyClass «не определен».
Добавление

using namespace <MyNamespace> 

или используя класс, как

MyNamespace::MyClass myClass; 

решил проблему.

5

В C и C ++ все имена должны быть объявлены перед использованием. Если вы попытаетесь использовать имя переменной или функции, которая не была объявлена, вы получите ошибку «необъявленный идентификатор».

Однако функции — это особый случай в C (и только в C), в котором вам не нужно сначала объявлять их. Компилятор C будет предполагать, что функция существует с числом и типом аргументов, как в вызове. Если фактическое определение функции не совпадает, вы получите еще одну ошибку. Этот особый случай для функций не существует в C ++.

Вы исправляете ошибки такого рода, проверяя, что функции и переменные объявлены до их использования. В случае printf вам нужно включить заголовочный файл <stdio.h> (или же <cstdio> в C ++).

Для стандартных функций я рекомендую вам проверить, например, этот справочный сайт, и найдите функции, которые вы хотите использовать. Документация для каждой функции говорит вам, какой заголовочный файл вам нужен.

4

Эти сообщения об ошибках

1.For the Visual Studio compiler: error C2065: 'printf' : undeclared identifier 2.For the GCC compiler: `printf' undeclared (first use in this function) 

означает, что вы используете имя printf но компилятор не видит, где было объявлено имя, и, соответственно, не знает, что это значит.

Любое имя, используемое в программе, должно быть объявлено до ее использования. Компилятор должен знать, что обозначает имя.

В этом конкретном случае компилятор не видит объявление имени printf , Как мы знаем (но не компилятор) это имя стандартной функции C, объявленной в заголовке <stdio.h> в C или в заголовке <cstdio> в C ++ и размещены в стандарте (std::) и глобальный (::) (не обязательно) пространства имен.

Поэтому, прежде чем использовать эту функцию, мы должны предоставить объявление ее имени компилятору путем включения соответствующих заголовков.

Например
C:

#include <stdio.h> int main( void ) { printf( "Hello Worldn" ); } 

C ++:

#include <cstdio> int main() { std::printf( "Hello Worldn" ); // or printf( "Hello Worldn" ); // or ::printf( "Hello Worldn" ); } 

Иногда причиной такой ошибки является простая опечатка. Например, давайте предположим, что вы определили функцию PrintHello

void PrintHello() { std::printf( "Hello Worldn" ); } 

но в основном вы сделали опечатку и вместо PrintHello ты напечатал printHello с строчной буквы «р».

#include <cstdio> void PrintHello() { std::printf( "Hello Worldn" ); } int main() { printHello(); } 

В этом случае компилятор выдаст такую ​​ошибку, потому что он не видит объявление имени printHello, PrintHello а также printHello два разных имени, одно из которых было объявлено, а другое не объявлено, но используется в теле основного

3

Это похоже на использование функции без ее объявления. заголовочный файл будет содержать
функция printf (). Включите заголовочный файл в вашу программу, это решение для этого.
Некоторые пользовательские функции могут также вызывать ошибки, если они не были объявлены перед использованием. Если
это используется во всем мире без проб.

0

В большинстве случаев, если вы уверены, что импортировали данную библиотеку, Visual Studio поможет вам с IntelliSense.

Вот что сработало для меня:

Удостоверься что #include "stdafx.h" объявляется первым, то есть вверху всех ваших включений.

0

 
`variable' undeclared (first use in this function)
In C and C++ variables must be declared before they can be used. This
error message indicates that the compiler has encountered a variable
name which does not have a corresponding declaration. It can be caused
by a missing declaration, or a typing error in the name. Variable names
are case-sensitive, so foo and Foo represent different
variables. To keep the output short, only the first use of an
undeclared variable is reported.
Example:

int main (void) { int i; j = 0; /* undeclared */ return j; } 

The variable j is not declared and will trigger the error `j'
undeclared
.

parse error before `...'
syntax error
These error messages occur when the compiler encounters unexpected
input, i.e. sequences of characters which do not follow the syntax of
the language. The error messages can be triggered by a missing close
bracket, brace or semicolon preceding the line of the error, or an
invalid keyword.
Example:

#include <stdio.h> int main (void) { printf ("Hello ") /* missing semicolon */ printf ("World!n"); return 0; } 

There is a missing semicolon after the first call to printf,
giving a parse error.

parse error at end of input
This error occurs if the compiler encounters the end of a file
unexpectedly, such as when it has parsed an unbalanced number of opening
and closing braces. It is often caused by a missing closing brace
somewhere.
Example:

#include <stdio.h> int main (void) { if (1) { printf ("Hello World!n"); return 0; /* no closing brace */ } 

An additional closing brace is needed in this program to prevent the
error parse error at end of input.

warning: implicit declaration of function `...'
This warning is generated when a function is used without a prototype
being declared. It can be caused by failing to include a header file,
or otherwise forgetting to provide a function prototype.
Example:

int main (void) { printf ("Hello World!n"); /* no header */ return 0; } 

The system header file ‘stdio.h’ is not included, so the prototype
for printf is not declared. The program needs an initial line
#include <stdio.h>.

unterminated string or character constant
This error is caused by an opening string or character quote which does
not have a corresponding closing quote. Quotes must occur in matching
pairs, either single quotes 'a' for characters or double quotes
"aaa" for strings.
Example:

#include <stdio.h> int main (void) { printf ("Hello World!n); /* no closing quote */ return 0; } 

The opening quote for the string in this program does not have a
corresponding closing quote, so the compiler will read the rest of the
file as part of the string.

character constant too long
In C and C++ character codes are written using single quotes, e.g.
'a' gives the ASCII code for the letter a (67), and 'n'
gives the ASCII code for newline (10). This error occurs if single quotes
are used to enclose more than one character.
Example:

#include <stdio.h> int main (void) { printf ('Hello World!n'); /* wrong quotes */ return 0; } 

The program above confuses single-quotes and double-quotes. A sequence
of characters should be written with double quotes, e.g. "Hello
World!"
. This same problem occurs in the following C++ program,

#include <iostream> int main (void) { std::cout << 'Hello World!n'; // wrong quotes return 0; } 

This error can also occur if the forward slash and backslash are
confused in an escape sequence, e.g. using '/n' instead of
'n'. The sequence /n consists of two separate
characters, ‘/’ and ‘n’.
Note that according to the C standard there is no limit on the length of
a character constant, but the value of a character constant that
contains more than one character is implementation-defined. Recent
versions of GCC provide support multi-byte character constants, and
instead of an error the warnings multiple-character character
constant
or warning: character constant too long for its type
are generated in this case.

warning: initialization makes integer from pointer without a cast
This error indicates a misuse of a pointer in an integer context.
Technically, it is possible to convert between integer and pointer
types, but this is rarely needed outside system-level applications.
More often, this warning is the result of using a pointer without
dereferencing it (e.g. writing int i = p instead of int i
= *p
).
This warning can also occur with char and char * types,
since char is an integer type.
Example:

int main (void) { char c = "n"; /* incorrect */ return 0; } 

The variable c has type char, while the string "n"
evaluates to a const char * pointer (to a 2-byte region of memory
containing the ASCII value for newline followed by a zero byte '',
since strings are null-terminated). The ASCII code for newline can be
found using char c = 'n';
Similar errors can occur with misuse of the macro NULL,

#include <stdlib.h> int main (void) { int i = NULL; /* incorrect */ return 0; } 

In C, the macro NULL is defined as ((void *)0) in
‘stdlib.h’ and should only be used in a pointer context.

dereferencing pointer to incomplete type
This error occurs when a program attempts to access the elements of
struct through a pointer without the layout of the struct being declared
first. In C and C++ it is possible to declare pointers to structs
before declaring their struct layout, provided the pointers are not
dereferenced—this is known as forward declaration.
Example:

struct btree * data; int main (void) { data->size = 0; /* incomplete type */ return 0; } 

This program has a forward declaration of the btree struct
data. However, the definition of the struct is needed before the
pointer can be dereferenced to access individual members.

warning: unknown escape sequence `...'
This error is caused by an incorrect use of the escape character in a
string. Valid escape sequences are:

n newline t tab
b backspace r carriage return
f form feed v vertical tab
a alert (bell)

The combinations , ', " and ? can be
used for individual characters. Escape sequences can also use octal
codes 377 and hexadecimal codes
x00xFF.
Example:

#include <stdio.h> int main (void) { printf ("HELLO WORLD!N"); return 0; } 

The escape sequence N in the program above is invalid—the
correct escape sequence for a newline is n.

warning: suggest parentheses around assignment used as truth value
This warning highlights a potentially serious error, using the assignment
operator ‘=’ instead of the comparison operator ‘==’ in the
test of a conditional statement or other logical expression. While the
assignment operator can be used as part of a logical value, this is rarely
the intended behavior.
Example:

#include <stdio.h> int main (void) { int i = 0; if (i = 1) { /* = should be == */ printf ("unexpected resultn"); } return 0; } 

The test above should be written as if (i == 1), otherwise the
variable i will be set to 1 by the evaluation of the if
statement itself. The operator ‘=’ both assigns and returns the
value of its right-hand side, causing the variable i to be
modified and the unexpected branch taken. Similar unexpected results
occur with if (i = 0) instead of if (i == 0),
except that in this case the body of the if statement would
never be executed.
This warning is suppressed if the assignment is enclosed in additional
parentheses to indicate that it is being used legitimately.

warning: control reaches end of non-void function
A function which has been declared with a return type, such as
int or double, should always have a return
statement returning a value of the corresponding type at all
possible end points—otherwise its return value is not well-defined.
Functions declared void do not need return statements.
Example:

#include <stdio.h> int display (const char * str) { printf ("%sn", str); } 

The program above reaches the end of the display function, which has
a return type of int, without a return statement. An
additional line such as return 0; is needed.
When using gcc the main function of a C program must
return a value of type int (the exit status of the program). In
C++ the return statement can be omitted from the main
function—the return value of the C++ main function defaults
to 0 if unspecified.

warning: unused variable `...'
warning: unused parameter `...'
These warnings indicate that a variable has been declared as a local
variable or in the parameters of a function, but has not been used
anywhere. An unused variable can be the result of a programming error,
such as accidentally using the name of a different variable in place of
the intended one.
Example:

int foo (int k, char * p) { int i, j; j = k; return j; } 

In this program the variable i and the parameter p are
never used. Note that unused variables are reported by -Wall,
while unused parameters are only shown with -Wall -W.

warning: passing arg of ... as ... due to prototype
This warning occurs when a function is called with an argument of a
different type from that specified in the prototype. The option
-Wconversion is needed to enable this warning. See
the description of -Wconversion in section 3.5 Additional warning options for an example.
warning: assignment of read-only location
warning: cast discards qualifiers from pointer target type
warning: assignment discards qualifiers ...
warning: initialization discards qualifiers ...
warning: return discards qualifiers ...
These warnings occur when a pointer is used incorrectly, violating a
type qualifier such as const. Data accessed through a pointer
marked as const should not be modified, and the pointer itself
can only be assigned to other pointers that are also marked
const.
Example:

char * f (const char *s) { *s = ''; /* assigns to read-only data */ return s; /* discards const */ } 

This program attempts to modify constant data, and to discard the
const property of the argument s in the return value.

initializer element is not a constant
In C, global variables can only be initialized with constants, such as
numeric values, NULL or fixed strings. This error occurs if a
non-constant value is used.
Example:

#include <stdio.h> FILE *stream = stdout; /* not constant */ int i = 10; int j = 2 * i; /* not constant */ int main (void) { fprintf (stream, "Hello World!n"); return 0; } 

This program attempts to initialize two variables from other variables.
In particular, the stream stdout is not required to be a constant
by the C standard (although on some systems it is a constant). Note
that non-constant initializers are allowed in C++.

Ezoic

 

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    Variables: A variable is the name given to a memory location. It is the basic unit of storage in a program.

    • The value stored in a variable can be changed during program execution.
    • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
    • All the variables must be declared before use.

    How to declare variables?
    We can declare variables in common languages (like C, C++, Java etc) as follows:
    Variables in Java

    where:
    datatype: Type of data that can be stored in this variable.
    variable_name: Name given to the variable.
    value: It is the initial value stored in the variable.
    

    How to avoid errors while creating variables?

    1. The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn’t been declared yet, an “undeclared identifier” compile-error will occur.

      Example:

      #include <stdio.h>

      int main()

      {

          printf("%d", x);

          return 0;

      }

      Compile Errors:

      prog.c: In function 'main':
      prog.c:5:18: error: 'x' undeclared (first use in this function)
           printf("%d", x);
                        ^
      prog.c:5:18: note: each undeclared identifier is reported
           only once for each function it appears in
      
    2. No initial value is given to the variable: This error commonly occurs when the variable is declared, but not initialized. It means that the variable is created but no value is given to it. Hence it will take the default value then. But in C language, this might lead to error as this variable can have a garbage value (or 0) as its default value. In other languages, 0 will be its default value.

      Example:

      #include <stdio.h>

      int main()

      {

          int x;

          printf("%d", x);

          return 0;

      }

      Output:

      0
      
    3. Using variable out of its Scope: Scope of a variable is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can be determined at compile time and independent of the function call stack.

      Example:

      #include <stdio.h>

      int main()

      {

          {

              int x = 5;

          }

          printf("%d", x);

          return 0;

      }

      Compile Errors:

      prog.c: In function 'main':
      prog.c:5:18: error: 'x' undeclared (first use in this function)
           printf("%d", x);
                        ^
      prog.c:5:18: note: each undeclared identifier is reported
           only once for each function it appears in
      

      How to Correct the above code: Declare the variable x before using it in the outer scope. Or you can use the already defined variable x in its own scope

      Example:

      #include <stdio.h>

      int main()

      {

          {

              int x = 5;

              printf("%d", x);

          }

          return 0;

      }

      Output:

      5
      
    4. Creating a variable with an incorrect type of value: This arises due to the fact that values are implicitly or explicitly converted into another type. Sometimes this can lead to Warnings or errors.

      Example:

      #include <stdio.h>

      int main()

      {

          char* x;

          int i = x;

          printf("%d", x);

          return 0;

      }

      Warning:

      prog.c: In function 'main':
      prog.c:7:13: warning: initialization makes integer
       from pointer without a cast [-Wint-conversion]
           int i = x;
                   ^
      

    Last Updated :
    05 Apr, 2019

    Like Article

    Save Article

    Problem:

    You have C code like

    errno = EFAULT;

    but when you try to compile it you see an error message like

    main.c: In function ‘main’:
    main.c:4:5: error: ‘errno’ undeclared (first use in this function)
         errno = EFAULT;
         ^~~~~
    main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
    main.c:4:13: error: ‘EFAULT’ undeclared (first use in this function)
         errno = EFAULT;
    

    Solution:

    Add

    #include <errno.h>

    at the top of the source file where the error occured. This will include both the errno variable and specific error codes like EFAULT.

    I am facing some problems in my raspberry Pi 3 B+ when I compile this simple program that uses time library:

    #include <stdio.h>
    #include <time.h>
    
    int main(int argc, char** argv) {
        struct timespec now;
    
        clock_gettime(CLOCK_REALTIME, &now);
    
        printf("Nsecs %d n", now.tv_nsec);
    
        return 0;
    }
    

    The output of the compilation shows these errors/warnings:

    main.c: In function ‘main’: main.c: error: storage size of ‘now’ isn’t known struct timespec now;

    main.c: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration] clock_gettime(CLOCK_REALTIME, &now);

    main.c: error: ‘CLOCK_REALTIME’ undeclared (first use in this function) clock_gettime(CLOCK_REALTIME, &now);

    I verified that the struct timespec, the method clock_gettime and the constant CLOCK_REALTIME are available in time.h file.

    I am compiling with gcc & C99. I am using NetBeans in a laptop to create the code and then I build on the raspberry (no problems so far in other projects). The command is:

    gcc -c -g -std=c99 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.c
    

    What is happening? Could you help me to understand it?

    mutex is declared as part of your main function:

    int main(int argc, char *argv[]) {
    
        N = atoi(argv[1]);
    
        sem_t mutex[N];

    which makes it a local variable — its scope is limited to the set of curly brackets which most recently enclose it — the main function itself.
    And then you try to use it within the f0 function:

    void* f0(int j) {
    
        for(int i = 0; i < 100; i++) {
    
            if(j < N-1){
    
                
    
                sem_wait(&mutex[j]);

    Because no global or local variable exists in the f0 function, the system rightly complains.

    Possibly, you want to make mutex either global (by declaring it outside the body of any function), or pass it as a parameter to the f0 function along with j

     
    `variable' undeclared (first use in this function)
    In C and C++ variables must be declared before they can be used. This
    error message indicates that the compiler has encountered a variable
    name which does not have a corresponding declaration. It can be caused
    by a missing declaration, or a typing error in the name. Variable names
    are case-sensitive, so foo and Foo represent different
    variables. To keep the output short, only the first use of an
    undeclared variable is reported.
    Example:

    int
    main (void)
    {
      int i;
      j = 0;     /* undeclared */
      return j;
    }
    

    The variable j is not declared and will trigger the error `j'
    undeclared
    .

    parse error before `...'
    syntax error
    These error messages occur when the compiler encounters unexpected
    input, i.e. sequences of characters which do not follow the syntax of
    the language. The error messages can be triggered by a missing close
    bracket, brace or semicolon preceding the line of the error, or an
    invalid keyword.
    Example:

    #include <stdio.h>
    
    int
    main (void)
    {
      printf ("Hello ")     /* missing semicolon */
      printf ("World!n");
      return 0;
    }
    

    There is a missing semicolon after the first call to printf,
    giving a parse error.

    parse error at end of input
    This error occurs if the compiler encounters the end of a file
    unexpectedly, such as when it has parsed an unbalanced number of opening
    and closing braces. It is often caused by a missing closing brace
    somewhere.
    Example:

    #include <stdio.h>
    
    int
    main (void) 
    {
      if (1) {
        printf ("Hello World!n");
        return 0;  /* no closing brace */
    }
    

    An additional closing brace is needed in this program to prevent the
    error parse error at end of input.

    warning: implicit declaration of function `...'
    This warning is generated when a function is used without a prototype
    being declared. It can be caused by failing to include a header file,
    or otherwise forgetting to provide a function prototype.
    Example:

    int
    main (void)
    {
      printf ("Hello World!n");  /* no header */
      return 0;
    }
    

    The system header file ‘stdio.h’ is not included, so the prototype
    for printf is not declared. The program needs an initial line
    #include <stdio.h>.

    unterminated string or character constant
    This error is caused by an opening string or character quote which does
    not have a corresponding closing quote. Quotes must occur in matching
    pairs, either single quotes 'a' for characters or double quotes
    "aaa" for strings.
    Example:

    #include <stdio.h>
    
    int
    main (void)
    {
      printf ("Hello World!n);  /* no closing quote */
      return 0;
    }
    

    The opening quote for the string in this program does not have a
    corresponding closing quote, so the compiler will read the rest of the
    file as part of the string.

    character constant too long
    In C and C++ character codes are written using single quotes, e.g.
    'a' gives the ASCII code for the letter a (67), and 'n'
    gives the ASCII code for newline (10). This error occurs if single quotes
    are used to enclose more than one character.
    Example:

    #include <stdio.h>
    
    int
    main (void)
    {
      printf ('Hello World!n');  /* wrong quotes */
      return 0;
    }
    

    The program above confuses single-quotes and double-quotes. A sequence
    of characters should be written with double quotes, e.g. "Hello
    World!"
    . This same problem occurs in the following C++ program,

    #include <iostream>
    
    int
    main (void)
    {
      std::cout << 'Hello World!n';  // wrong quotes
      return 0;
    }
    

    This error can also occur if the forward slash and backslash are
    confused in an escape sequence, e.g. using '/n' instead of
    'n'. The sequence /n consists of two separate
    characters, ‘/’ and ‘n’.
    Note that according to the C standard there is no limit on the length of
    a character constant, but the value of a character constant that
    contains more than one character is implementation-defined. Recent
    versions of GCC provide support multi-byte character constants, and
    instead of an error the warnings multiple-character character
    constant
    or warning: character constant too long for its type
    are generated in this case.

    warning: initialization makes integer from pointer without a cast
    This error indicates a misuse of a pointer in an integer context.
    Technically, it is possible to convert between integer and pointer
    types, but this is rarely needed outside system-level applications.
    More often, this warning is the result of using a pointer without
    dereferencing it (e.g. writing int i = p instead of int i
    = *p
    ).
    This warning can also occur with char and char * types,
    since char is an integer type.
    Example:

    int
    main (void)
    {
      char c = "n";  /* incorrect */
      return 0;
    }
    

    The variable c has type char, while the string "n"
    evaluates to a const char * pointer (to a 2-byte region of memory
    containing the ASCII value for newline followed by a zero byte '',
    since strings are null-terminated). The ASCII code for newline can be
    found using char c = 'n';
    Similar errors can occur with misuse of the macro NULL,

    #include <stdlib.h>
    
    int
    main (void)
    {
      int i = NULL;  /* incorrect */
      return 0;
    }
    

    In C, the macro NULL is defined as ((void *)0) in
    ‘stdlib.h’ and should only be used in a pointer context.

    dereferencing pointer to incomplete type
    This error occurs when a program attempts to access the elements of
    struct through a pointer without the layout of the struct being declared
    first. In C and C++ it is possible to declare pointers to structs
    before declaring their struct layout, provided the pointers are not
    dereferenced—this is known as forward declaration.
    Example:

    struct btree * data;
    
    int 
    main (void)
    {
      data->size = 0;  /* incomplete type */
      return 0;
    }
    

    This program has a forward declaration of the btree struct
    data. However, the definition of the struct is needed before the
    pointer can be dereferenced to access individual members.

    warning: unknown escape sequence `...'
    This error is caused by an incorrect use of the escape character in a
    string. Valid escape sequences are:

    n newline t tab
    b backspace r carriage return
    f form feed v vertical tab
    a alert (bell)

    The combinations \, ', " and ? can be
    used for individual characters. Escape sequences can also use octal
    codes 377 and hexadecimal codes
    x00xFF.
    Example:

    #include <stdio.h>
    
    int
    main (void)
    {
      printf ("HELLO WORLD!N");
      return 0;
    }
    

    The escape sequence N in the program above is invalid—the
    correct escape sequence for a newline is n.

    warning: suggest parentheses around assignment used as truth value
    This warning highlights a potentially serious error, using the assignment
    operator ‘=’ instead of the comparison operator ‘==’ in the
    test of a conditional statement or other logical expression. While the
    assignment operator can be used as part of a logical value, this is rarely
    the intended behavior.
    Example:

    #include <stdio.h>
    
    int
    main (void)
    {
      int i = 0;
      if (i = 1) {  /* = should be == */
        printf ("unexpected resultn");
      }
      return 0;
    }
    

    The test above should be written as if (i == 1), otherwise the
    variable i will be set to 1 by the evaluation of the if
    statement itself. The operator ‘=’ both assigns and returns the
    value of its right-hand side, causing the variable i to be
    modified and the unexpected branch taken. Similar unexpected results
    occur with if (i = 0) instead of if (i == 0),
    except that in this case the body of the if statement would
    never be executed.
    This warning is suppressed if the assignment is enclosed in additional
    parentheses to indicate that it is being used legitimately.

    warning: control reaches end of non-void function
    A function which has been declared with a return type, such as
    int or double, should always have a return
    statement returning a value of the corresponding type at all
    possible end points—otherwise its return value is not well-defined.
    Functions declared void do not need return statements.
    Example:

    #include <stdio.h>
    
    int
    display (const char * str)
    {
      printf ("%sn", str);
    }
    

    The program above reaches the end of the display function, which has
    a return type of int, without a return statement. An
    additional line such as return 0; is needed.
    When using gcc the main function of a C program must
    return a value of type int (the exit status of the program). In
    C++ the return statement can be omitted from the main
    function—the return value of the C++ main function defaults
    to 0 if unspecified.

    warning: unused variable `...'
    warning: unused parameter `...'
    These warnings indicate that a variable has been declared as a local
    variable or in the parameters of a function, but has not been used
    anywhere. An unused variable can be the result of a programming error,
    such as accidentally using the name of a different variable in place of
    the intended one.
    Example:

    int
    foo (int k, char * p)
    {
      int i, j;
      j = k;
      return j;
    }
    

    In this program the variable i and the parameter p are
    never used. Note that unused variables are reported by -Wall,
    while unused parameters are only shown with -Wall -W.

    warning: passing arg of ... as ... due to prototype
    This warning occurs when a function is called with an argument of a
    different type from that specified in the prototype. The option
    -Wconversion is needed to enable this warning. See
    the description of -Wconversion in section 3.5 Additional warning options for an example.
    warning: assignment of read-only location
    warning: cast discards qualifiers from pointer target type
    warning: assignment discards qualifiers ...
    warning: initialization discards qualifiers ...
    warning: return discards qualifiers ...
    These warnings occur when a pointer is used incorrectly, violating a
    type qualifier such as const. Data accessed through a pointer
    marked as const should not be modified, and the pointer itself
    can only be assigned to other pointers that are also marked
    const.
    Example:

    char *
    f (const char *s)
    {
      *s = '';  /* assigns to read-only data */
      return s;   /* discards const */
    }
    

    This program attempts to modify constant data, and to discard the
    const property of the argument s in the return value.

    initializer element is not a constant
    In C, global variables can only be initialized with constants, such as
    numeric values, NULL or fixed strings. This error occurs if a
    non-constant value is used.
    Example:

    #include <stdio.h>
    
    FILE *stream = stdout;  /* not constant */
    int i = 10;
    int j = 2 * i;          /* not constant */
    
    int
    main (void)
    {
      fprintf (stream, "Hello World!n");
      return 0;
    }
    

    This program attempts to initialize two variables from other variables.
    In particular, the stream stdout is not required to be a constant
    by the C standard (although on some systems it is a constant). Note
    that non-constant initializers are allowed in C++.

     

    Понравилась статья? Поделить с друзьями:
  • Und ошибка на машинке haier
  • Unconnected line altium ошибка
  • Unclosed quotation mark after the character string ошибка
  • Uncharted legacy of thieves collection ошибка при запуске
  • Uncaught syntaxerror invalid or unexpected token ошибка