I'm writing a code for a simple program that calculates the perimeter of a polygon when its given the amount of sides it has and the length of each side as homework, problem is that when I do for (size_t i = 0; i < sidesnumber; i++) it gives me an error on size_t saying "identifier "size_t" is undefined". I have already tried also including the library stddef.h but it stays the same.
This is the whole code:
#include <stdio.h>
int sidesnumber = 0;
int sides[100];
int results;
main()
{
printf("Number of Sides\n");
sidesnumber = getchar();
sidesnumber -= 48;
for (size_t i = 0; i < sidesnumber; i++)
{
/* code */
getchar();
sides[i] = getchar();
sides[i] -= 48;
}
for (size_t i = 0; i < sidesnumber; i++)
{
/* code */
results += sides[i];
}
printf("The perimeter is:%d", results);
}
I'm using MinGW on Windows 10.
Code of all the .jsons I'm currently using
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile C File",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${workspaceRoot}\\${fileBasename}.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Run C File",
"type": "shell",
"command": ".\\${fileBasename}.exe"
},
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw64/bin"
}
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}\\${fileBasename}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compile C File"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"browse": {
"path": [
"${workspaceFolder}",
"C:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include"
],
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"${workspaceFolder}",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/mingw64/x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"compilerPath": "C:/mingw64/bin/gcc.exe"
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
},
{
"name": "Test",
"includePath": [
"${workspaceFolder}/**",
"C:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\tr1",
"C:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++",
"C:\\mingw64\\x86_64-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"compilerArgs": [],
"browse": {
"path": [
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}
If you are only concerned about the squiggles under size_t in vscode, you can use std::size_t instead.
Related
this is my launch.json
{
"version": "0.2.0",
"program": "${workspaceFolder}/pc",
"configurations": []
}
this is my task.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cpp 生成活动文件",
"command": "/usr/bin/cpp",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.c", //这里是我修改的位置,修改后便提示如题所述的错误
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lpthread"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
In the args of the task, I changed "${file}" to "${workspaceFolder}/*. c", and tried to use the command line to include all c files under the folder
In the launch, I added the setting option of "program" according to the guidance file, but I don't understand what it is for
Unable to use IDE due to course requirements
In addition, it should be mentioned that the code will disappear and display as two lines of address after the code reports an error (too low reputation to send a picture)
I'm trying to use the debugger in vs code.
I have problems to activate and run it.
This is my code:
int main(int argc, char const *argv[])}
int arr [] = {54,25,7,2,12,65};
for (int i = 0; i<6; i++)
{
printf("%d ,",arr[i]);
}
printf("\n");
insertion_sort(arr,6);
for (int i = 0; i<6; i++)
{
printf("%d ,",arr[i]);
}
return 0;}
This is my JSON file.
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-win32-dwarf-rt_v6-rev0\\mingw32\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-win32-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]}
I'm getting this error: Unable to start debugging. program path is missing or invalid.
Thx!
I'm having trouble trying to view the contents of an array during debugging. Instead of characters I see .
My code:
#include <stdio.h>
int main()
{
char str[100] = {0};
fgets(str, 100, stdin);
return 0;
}
What I see in the VARIABLES window:
But when I try to debug int arrays everything works fine.
Exemple:
#include <stdio.h>
int main()
{
int str[100];
for (int i = 0; i < 100; i++)
{
str[i] = i+1;
}
return 0;
}
What I see:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
}
]
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
What can I do to see the characters in the array?
Because pretty-printer is not displaying utf-8 char correctly. To display utf-8 char, edit the setupCommands in launch.json:
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Fix pretty-printing for gdb",
"text": "set charset UTF-8"
}
],
Original answer: VSCode debugger having issues with character encoding
HeIIo. This is my first post on Stackoverflow.
I like computer programming and after some experience with Python and VBA I'm trying to learn the C.
Since I took up with it I use Visual Studio Code for training in order to learn how to use modern programming tools at the same time.
I've never had any problem until now, but at the moment I want to compile multiple files.
When I try to compile a file with a warning the terminal shows it
Executing task: C:/mingw-w64/mingw64/bin/gcc.exe -Wall -W -pedantic -ansi -std=c99 -O -g 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackclient.c' 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c' -o 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT.exe'
d:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c: In function 'is_full':
d:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c:41:20: warning: unused parameter 's' [-Wunused-parameter]
bool is_full(Stack s) {
~~~~~~^
but in the "Problems" tab it's not shown
(see the screenshot).
In every other folder where there is only a .c file to compile it works flawlessly.
The followings are my jsons file from which you can easily understand my VSC configuration and platform.
Thank you for reading.
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw-w64/mingw64/bin/gcc.exe",
"cStandard": "c99",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\stackADT.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc.exe build active file"
}
]
}
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:/mingw-w64/mingw64/bin/gcc.exe",
"args": [
"-Wall",
"-W",
"-pedantic",
"-ansi",
"-std=c99",
"-O",
"-g",
"${file}",
"${fileDirname}\\stackADT3.c",
"-o",
"${fileDirname}\\stackADT.exe"
],
"options": {
"cwd": "C:/mingw-w64/mingw64/bin"
}
}
],
"version": "2.0.0"
}
It happens because you compile with flag "-Wall". It means that all warnings will be considered errors. You should use this variable.
for instance
(void*) &s;
I'm new to vscode editor and I want to run simple C project like below, but when I was run this debug console says like below. I want to know how to run this proper way.
Type "apropos word" to search for commands related to "word".
=cmd-param-changed,param="pagination",value="off"
[New Thread 3076.0x2314]
[New Thread 3076.0x20c4]
Thread 1 hit Breakpoint 1, 0x00401603 in main ()
[Thread 3076.0x20c4 exited with code 0]
[Inferior 1 (process 3076) exited normally]
The program 'c:\Users\Lenovo\Desktop\Example\a.exe' has exited with code 0 (0x00000000).
(this is test.c file )
#include <stdio.h>
int main()
{
printf("hellow world");
return 0;
}
I was configured tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "build Hello"
}
]
}
And this is launch.json code I was configured:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
This is my c_cpp_properties.json file:
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/test.c",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++//tr1",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/i686-w64-mingw32"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceFolder}/test.c",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++//tr1"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
It's just trying to tell you that all went fine. Return value 0 is "no error".
You have a typo in "hello" though, the trailing "w" shouldn't be there.
And main()'s parameters should be either (void) or (int argc, char *argv[]).