Tag Archives: C

[Solved] error C2041: illegal digit ‘9‘ for base ‘8‘ | error C2059: syntax error: ‘bad suffix on number‘

Error log

Text

Octal value is out of range

1> E:\CProject\test12\Source. c(5,10): error C2041: illegal digit ‘8’ for base ‘8’

Hexadecimal value is out of range

1> E:\CProject\test12\Source. c(5,10): error C2059: syntax error: ‘bad suffix on number’
1> E:\CProject\test12\Source. c(5,10): error C2153: integer literals must have at least one digit
1> E:\CProject\test12\Source. c(5,13): error C2021: expected exponent value, not ‘;’
1> E:\CProject\test12\Source. c(5,10): warning C4244: ‘initializing’: conversion from ‘double’ to ‘int’, possible loss of data
1> Done building project “test12.vcxproj” – FAILED.

Screenshot (hexadecimal value exceeds the range)

Solution:

C2041 series error: this kind of number, which is generally octal or hexadecimal, is out of range
for example:

  1. 09; because there is no 9 in octal
  2. 0xq; because hex doesn’t have the q character.

This requires us to carefully check whether the relevant figures exceed the range.

[Solved] fatal error C1189: #error: STL1003: Unexpected compiler, expected C++ compiler

Error Log:

screenshot

text version:

F:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\yvals_core.h(23,1): fatal error C1189: #error: STL1003: Unexpected compiler, expected C++ compiler.
F:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\yvals_core.h(23,1): fatal error C1189: #error: STL1003: Unexpected compiler, expected C++ compiler.

 

Error

Solution:
This problem is because the C++ header file is referenced in the C file. Just replace cmath with math.h.

C++ Compile Error: error: invalid conversion from ‘void*‘ to ‘char*‘ [-fpermissive]

error: invalid conversion from ‘void*‘ to ‘char*‘ [-fpermissive]

#include <stdio.h>
#include<malloc.h>
#define IN
#define OUT

// Get file size
int FileSize(IN char *file)
{
	FILE *fil;
	fil = fopen(file,"rb");
	fseek(fil,0L,SEEK_END);
	int filesize = ftell(fil);
	fseek(fil,0,0);
	return filesize;
}

// read the file
int ReadFileData(IN char *fileName, OUT char *filedata)
{
	FILE *fpIN;
	int fileSizes = FileSize(fileName);
	fpIN = fopen(fileName,"rb");
	fread(filedata,1,fileSizes,fpIN);
	fclose(fpIN);
}

// write the file
int WriteToFile(char *filedata, int size, OUT char *outFileName)
{
	FILE *fpOUT;
	fpOUT = fopen(outFileName,"w+");
	fwrite(filedata,1,size,fpOUT);
	fclose(fpOUT);
}

int main()
{
	char *origin_file = "test.cpp";
	int orgfilesize = FileSize(origin_file);  // Get file size



	char *file_data=  malloc(orgfilesize);      // Allocate file size memory
    if (file_data == NULL)
        return NULL;
	ReadFileData(origin_file, file_data);     // read the file
	char *outFile = "test.txt";
	WriteToFile(file_data,orgfilesize,outFile);  // write the file

	return 0;
}

The following line of code

char *file_data=  malloc(orgfilesize);

Malloc function is used to allocate space in C language. The return type is void*. Void* indicates a pointer of undetermined type. C. C++ specifies that the void* type can cast any other type of pointer.

The malloc() function actually finds a space of a specified size in memory and ranges the first address of that space to a pointer variable.
Here the pointer variable can be a single pointer or the first address of an array.
It depends on the size of the malloc() function.

Use GCC compilation to directly pass and print out the following results

Original String: testing.

When compiling with g++, an error and warning will appear, as follows

error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

The reason for the error is that c++ is designed to be more secure than C, and it cannot automatically convert void * to other pointer types.

The reason for warning is that the program attempts to convert the string literal (const char [] in c++ and char [] in C language) to char * type,,

char *file_data= (char*) malloc(orgfilesize); 
# The return value of the malloc function is a void*, which is assigned to a variable by adding a forced conversion in front of malloc

Introduction to malloc function
malloc function is often used in C language and c++ to dynamically allocate memory space for variables. Malloc requests the system to allocate memory space of the specified size bytes

function void malloc(int size)

explain:

Malloc requests the system to allocate memory space of the specified size bytes. If the allocation is successful, a pointer to the allocated memory is returned; otherwise, a null pointer is returned
this function is included in the header file: \include < malloc.h> you should import the header file *< malloc.h>  or  < stdlib.h>** when you are using

Note: when the memory is no longer used, the free() function should be used to free the memory block
Common usage

1. When you do not know the definite memory required by a variable

For example, when defining an array, the size of the array is not known until the program is compiled. In this case, you can use the malloc function

int main()
{
	int n;
	scanf("%d",&n);
	int *m=(int *)malloc(sizeof(int)*n);  //Defining a pointer variable that points to n int is equivalent to opening an array of n int elements.
	// If n is very large, more than 1000000, then opening an int array of this size will cause a stack overflow.
	int m[1000000]; //Stack overflow will occur.
	return 0;
}

2. Allocate space for structural variables
define a common variable of structure type. You can dynamically apply for memory without malloc. The CPU will allocate memory for structure variables.

typedef struct
{
    int n;
    char *p;
}node;

int  main()
{
	node a;  //The definition is a structured ordinary variable, you can request memory without using malloc, the CPU will allocate memory for this structured variable
    a.n=4;
    printf("%d",a->n); //can output successfully
    node *b; //defines a structure pointer variable, the CPU will open up memory for this pointer with a size of 4 bytes. But to store the data members of the structure this space is not enough, it will raise a segment error, at this time you must malloc request a structure type size of dynamic memory to store the data members.
    //b=(node *)malloc(sizeof(node));
    printf("%d",sizeof(b)); // use sizeof(b) to see the size of b is 4
    char p[]="abcd";
    printf("%d",b->n);
    (a->p)=p;
    printf("%c",a->p[0]);
    return 0;
}

If malloc is not used to allocate space for structure pointer variable B, warning: ‘B’ is used uninitialized in this function [-wuninitialized]|.

3. When defining a structure, you need to pay attention to allocating space for its members in turn
in normal use, after allocating space for a structure with malloc function, operate on its member variable (pointer type).

For example, when the pointer p=null, it will always report “program received signal SIGSEGV, segmentation fault.”
use malloc function

[Solved] Ubuntu Eclipse C/C++ Error: launch failed.binary not found

Questions

Running on windows, there is either a problem with the installation of mingw64, or the header file cannot be found. Anyway, it is to verify whether the simulator can be used. What is the trouble? Just switch to Linux. The following are the results of running on Ubuntu.

Process

To run the square studio export project, follow the readme of the exported project,

    1. install gcc
sudo apt-get install gcc g++ gdb build-essential
    2. install sdl
sudo apt-get install libsdl2-dev

3. Install eclipse c/c++
4. Select the parent folder of the exported project as “Workspace”
5. Select File->Import->General->Exisiting project into Workspace click “Next” and browse the project
6. Build the project with Project->Build
7. Run the project with Run->Run.
Then report an error: could not found lpng

 

Solution:

    1Install lpng
sudo apt-get install libpng-dev

2. install CDT

help-> Check for updates

check CDT and install 

3. Modify the compiler

4. Just follow steps 6 and 7 above. The effect drawing is as follows

The semget function error: errno is set to 28 [How to Solve]

When running semget under Linux to create semaphores, it returns – 1 and the creation fails;

1. This function is a system function. You can only confirm the actual error code with errno, print errno through strError, and return no space left on device. Is the system space insufficient? Insufficient space to create semaphores?

2. Go to errno. H to check the error message enospc corresponding to the actual error code. What does this field mean?

3. Does the semget function have its own error field? Check the function manual: check the man Manual of semget function: a semaphore set has to be created but the system limit for the maximum number of semaphore sets (semmni), or the system-wide maximum number of semaphores. Semaphore exceeds system limit.

It is basically determined that it is caused by the system semaphore. First, temporarily modify the kernel semaphore parameters and run again to see whether it has been solved.

4. The following commands are used in viewing semaphores

#1)The sysctl command can view and set system kernel parameters
# The 4 corresponding values from left to right are SEMMSL, SEMMNS, SEMOPM and SEMMNI.
sysctl -a | grep sem #View the setting value of the system semaphore
kernel.sem = 250 32000 32 128


#2) There are three ways to modify: the numbers are for reference only
echo 610 86620 100 142 > /proc/sys/kernel/sem

sysctl -w kernel.sem="610 86620 100 142"

echo "kernel.sem=610 86620 100 142" >> /etc/sysctl.conf`


#3) View the current semaphore and pid of the system as well as user information, view more information and check --help
ipcs -s -p -c


#4) Delete the semaphore method of the specified semid, and check more usage --help
ipcrm -s semid


#5) Delete all semid semaphore methods
ipcrm  -asem

5. Here, in the process of finding semaphore resource leakage, in order to facilitate real-time viewing of semaphore information, the semaphore output is written into the script and printed circularly

#ipcs.sh
echo “ipcs -s loop”

while [ 1 ]
do
	sleep 1
	ipcs -s
done

6. Note: the final problem here is to see why the semaphore in the code exceeds the limit. Normally, the semaphore will not exceed the system limit.

[Solved] Vs error: link: fatal error lnk1168: unable to open for writing

Project scenario:

use VS to program in C language and generate solution report – > Execution error


Problem Description:

error: link: fatal error lnk1168: unable to open C:\users\86139\desktop\plan\notes\C\tryproject \ debug\tryproject.exe for writing

#include <stdio.h>
#include "math.h"//Since the library function sqrt() is to be used

int main(){
	//Requirement: determine if the quadratic function has real roots, and output if it does?
	double a,b,c,disc,x1,x2,p,q;//declare variables
	printf("Please enter the values of a,b,c respectively: \n");
	scanf("%lf%lf%lf",&a,&b,&c);//enter the value and store it at address a,b,c
	disc=b*b-4*a*c;//discriminant
	if(disc<0){
		printf("This equation has no real roots!!!");
	}else{//disc>=0
		p=-b/(2.0*a);
		q=sqrt(disc)/(2.0*a);
		x1=p+q;
		x2=p-q;
		printf("此方程的两实根为:\nx1=%7.2f\nx2=%7.6f\n",x1,x2);
	}
}

Cause analysis:

the process may already exist, so it cannot be opened and run


Solution:

just finish the process of the program. You can open “process manager” (Task Manager), Ctrl + Alt + delete, find the corresponding process, and right-click to finish.

[solution] warning: this statement may fall through [- wimplicit fallthrough]) encountered during GCC compilation

When practicing the switch statement in Ubuntu, if break is not added in the case statement, the warning message will pop up, causing the program to fail to compile,

[email protected]:~/code$ g++ -Wall -W test_switch.c -o test_switch 
test_switch.c: In function ‘int main()’:
test_switch.c:13:10: warning: this statement may fall through [-Wimplicit-fallthrough=]
   13 |    printf("1\n");
      |    ~~~~~~^~~~~~~~~~~~~
test_switch.c:15:3: note: here
   15 |   case 3:
      |   ^~~~
test_switch.c:16:10: warning: this statement may fall through [-Wimplicit-fallthrough=]
   16 |    printf("3\n");
      |    ~~~~~~^~~~~~~~~~~~~
test_switch.c:18:3: note: here
   18 |   case 5:
      |   ^~~~

At this time, adding – W when compiling the code can shield the alarm, and the code can be compiled normally

[email protected]:~/code$ g++ -Wall -W test_switch.c -o test_switch -w

[C]fatal error: mysql.h: No such file or directory

Environment: under the Linux distribution of windows system, Ubuntu 20.04.2 lts;

Problem Description: MySQL C API programming, after installing MySQL C development library, sudo apt install default libmysqlclient dev , compile version. C file: GCC version. C - O version. O
version. C is as follows:

#include <mysql.h>
#include <stdio.h>

int main(int argc, char **argv)
{
        printf("MySQL client version: %s\n", mysql_get_client_info());

        exit(0);
}

report errors:

version.c:1:10: fatal error: mysql.h: No such file or directory
    1 | #include <mysql.h>

Problem reason: no header file path parameter, unable to address
problem modification: Where is MySQL find mysql. H path: /usr/include/MySQL
recompile: GCC version. C - O version. O - I/usr/include/MySQL
report error again:

/usr/bin/ld: /tmp/ccEn7kBU.o: in function `main':
version.c:(.text+0x14): undefined reference to `mysql_get_client_info'
collect2: error: ld returned 1 exit status

Cause of the problem: no lib parameter is added, so the function cannot be referenced
problem modification: GCC version. C - O version. O - I/usr/include/MySQL - lmysqlclient
problem solving.

_ASSERTE((unsigned)(c + 1) <= 256);

_ Asserte ((unsigned) (c + 1) & lt; = 256);

when using VS2010 to write a program, it is a very simple program, but when inputting Chinese characters, the following error prompt appears.

at the beginning, I was obsessed with the “cannot find or open the pdb file” of the error prompt, but later found that it was not the problem there. I started to notice the following problems.

click Retry, and a breakpoint message will appear.

next, click interrupt, and the breakpoint information will appear.

it can be seen from the figure that this is one of the problems in isctype. C file.

the solution is to right-click on the project – properties – configuration properties – C/C + + – command line

and then enter “/ J”

in the input box on the lower right side to re-enter Chinese characters. It is found that the error has been solved.

reference: msdn.microsoft.com

note: for other editors, such as VS2005, 2008 and 2012, you can choose from the top of the above website. As shown in the figure below

Vs2013 error msb8031 solution today ran a MFC program, found that the error: error msb8031: building an MFC project for a non-U

Vs2013 error msb8031 solution

Today, in the process of porting and compiling, we found an error: error msb8031: building an MFC project for a non Unicode character set is decoded

You must change the project property to Unicode or download an additional library.  

 

Solution:
go to the Microsoft website to download this component https://www.microsoft.com/zh-cn/download/details.aspx?id=40770

Select the corresponding language. Microsoft removed the multi byte character set support in vs2013,

Gcc compiler warning solution: “extra tokens at end of ifndef directive” Tags: GCC, warning

Recently, after checking all the warnings in the project, we found an interesting warning: “extra tokens at end of ifndef directive”. Literally, “there are invalid instructions after ifndef.”. I wrote a small program verification in private, and found that there are two situations that can generate this warning.

Procedure 1:

// directive_1.c
#include <stdio.h>

#ifndef MIN
  #define MIN(x, y) ((x) > (y) ? (y) : (x)) 
#endif /**/x

int main()
{
	printf("min val = %d\n", MIN(100, -1));

	return 0;
}

The above program intentionally adds an invalid character X after “ENDIF / * * / and compiles with GCC with – wall parameter as follows:
GCC directive_ 1. C - wall - O out1
an alarm is generated immediately:

directive_1.c:5:12: warning: extra tokens at end of #endif directive [-Wendif-labels]
 #endif /**/x

I was immediately shocked. Such an obvious “error” problem was that GCC was just a warning, and generated an executable binary file to execute . / out1 . The result was normal, and the effect was as follows:

min val = -1

Procedure 2:

// directive_2.c
#include <stdio.h>

#ifndef MIN
  #define MIN(x, y) ((x) > (y) ? (y) : (x)) 
#endif

int main()
{
	printf("min val = %d\n", MIN(100, -1));

	return 0;
}

Remove the redundant “X” above, and the compilation will be completely normal. As follows:

[email protected]:extra-tokens-at-end-of-#ifndef-directive$ gcc directive_2.c -Wall -o out1
[email protected]:extra-tokens-at-end-of-#ifndef-directive$ 

After further divergence of the possible situation after ifndef, it is found that there is another situation that can cause this alarm.

Procedure 3:

// directive_3.c
#include <stdio.h>

#ifndef MIN(x, y)
  #define MIN(x, y) ((x) > (y) ? (y) : (x)) 
#endif

int main()
{
	printf("min val = %d\n", MIN(100, -1));

	return 0;
}

Execute the command “ GCC directive"_ 3. C - wall - O out3 , this alarm also magically appears, as follows:

[email protected]:extra-tokens-at-end-of-#ifndef-directive$ gcc directive_3.c -Wall -o out3
directive_3.c:3:12: warning: extra tokens at end of #ifndef directive
 #ifndef MIN(x, y)
            ^

The reason is that the preprocessing of # ifndef in C language only checks the keywords, and the “(x, y)” after it is considered as redundant characters. If the brackets are removed and written as # ifndef min , there will be no alarm.

After installing CodeBlocks, run the prompt: tried to run compiler executable ‘C: / / MinGW / bin / mingw32- gcc.exe ‘, but failed!

Goto “Settings->Compiler…->Global compiler settings->GNU GCC Compiler->Toolchain executables” and fix the compiler’s setup.

Tried to run compiler executable ‘C:\MinGW/bin/mingw32- gcc.exe ‘, but failed!

Skipping…
Nothing to be done (all items are up-to-date).

 

Contact C for the first time, run the code after installation and report the above error, which means:

An attempt was made to run the compiler executable ‘C’:’mingw / bin / mingw32 ‘- gcc.exe ‘but failed!

The reason is that the compiler location cannot be found. The solution is as follows:

 

Auto detect: automatic detection

Then run it again