error trying to #include headerfile within main function - c

Newbie question: To my knowledge the way #include works is that it copy-pastes whatever is inside the included file to the designated spot. All I want is to #include a file which only holds a printf().
And while this works perfectly in any online IDE I have tried, in Code::Blocks I receive
error: expected declaration specifiers or '...' before string constant
This is the main file:
#include <stdio.h>
int main(void)
{
#include "null.h"
}
And this is the included null.h:
printf("Hello, world\n");
I have tried #include <stdio.h> above printf() function call as well as calling the file null.c however nothing seems to work. I have searched Google but can't wrap my head around what this error has to do with any of the explanations provided.
edit: I now use gcc via cmd in order to compile which works for me.

Related

Program builds sucessfully but netbeans shows errors in the includes

I have this program it builds but shows some errors in the includes
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/** These two files are necessary for calling CTOS API **/
#include <ctosapi.h>
/**
** The main entry of the terminal application
**/
int main(int argc,char *argv[])
{
BYTE key;
// TODO: Add your program here //
CTOS_LCDTClearDisplay();
CTOS_LCDTPrintXY(1, 1, "Hello");
CTOS_KBDGet(&key);
exit(0);
}
here's the print of it:
print of netbeans window
Edited:
here's the error:
Cannot find include file <stdlib.h>.
I think I found the problem but how do I solve this
has u can see in the image the system directories that look for includes are wrong because they have "/gneaubi" twice, the question is how do I change this
It seems that problem is coming from Netbeans IDE.
I would suggest to follow this once and check if the problem is rectified.
Right-click your project and go to properties.
In the properties, click run and change the console type to standard output.
P.S: Please provide proper info about the error by hovering mouse on error ballon. Given info is not sufficient currently.

Troubles with VSCode and Windows for a simple C code

I am trying to use VSCode for writing and executing C codes for a course in Windows 10. I installed VSCode and MinGW as the instructions said. I'm trying to run a simple code (print "Hello world"), but when I run the code, the output says "Access denied"
//Test code for C in Windows 10
#include "stdio.h"
#include "stdlib.h"
void main(){
printf("Hello world");
}
I'm not sure if it's gonna solve your problem but when you include header from LibC or any different lib you must use this syntax
#include <stdio.h>
#include <stdlib.h>
If you use < symbol, the preprocessor will look in special path defined by your environement else if you use " symbol, the preprocessor will look in your current directory,

VsCode crtdbg.h not found, how to fix?

Trying to check memory leak tool but Vscode doesn't recognize #include <crtdbg.h>.
Here is the code:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <stdio.h>
#include <string.h>
int main()
{
char *word = "this still relevant.";
char *mem = (char *)malloc(sizeof(word));
strcpy(mem, word);
printf("%s", mem);
system("pause");
_CrtDumpMemoryLeaks();
}
The compile error:
source.c:4:10: fatal error: crtdbg.h: No such file or directory
#include <crtdbg.h>
How do I properly include crtdbg.h?
PS: I'm using MinGW compiler, everything works except that.
EDIT 1: The solution of that other post doesn't work. If I use the suggested code this appear.
source.c:24:5: error: '_CrtDumpMemoryLeaks' was not declared in this scope
_CrtDumpMemoryLeaks();
EDIT 2: Does anyone uses VsCode with MinGW?
I think that VSCODE couldn't access to header file, crtdbg.h , Pease test a simple following way. Maybe it solved your problem:
1- Run as administrator Developer Command prompt for VS2019. I emphasized run as administrator no run.
2- Type Code .
3- Open project folder.
4- Happy coding and enjoy it.

string.h and stdlib.h functions give me the error: undefined reference to '...'

I'm modifing a c project and is the first time I'm tryng to use the stdlib.h and string.h libraries in my project. I'm working on MCUXpresso (IDE based on eclipse). This is my code:
#include <string.h>
#include <stdlib.h>
#include "config.h"
int number=100;
int n1,n2;
char test[5]="test";
char str[5];
extern void fntest(TTASKTABLE *ptrTaskTable)
{
itoa (number,str,10);
n1=strlen(test);
n2=atoi(test);
}
As you can see I've included the header files but the compiler give the errors: undefined reference to 'itoa' ; undefined reference to 'strlen' ; undefined reference to 'atoi'
And in my includes folder there is already (by default on my project) the folder containing the standard libraries.
I see the functions are used in some other files in the project... I can't understand why I've this error.
In the original code the functions are in a body function, I've corrected that.
Can you help me please?
finally I've solved my problem !
Ijust had to change the linker settings: project-->properties-->C/C++ Build-->settings-->MCU Linker-->general and change form No startup or default libs to Do not use standard start files and then no more errors!
I don't know if you have posted your actual code, but at this stage as you have uploaded its not going to compile.
#include <string.h>
#include <stdlib.h>
#include "config.h"
int number=100; // This is okay
int n1, n2; // This is okay
char test[5]="test"; // This is okay
char str[5]; // This is okay
itoa (number,str,10); // This is wrong
n1=strlen(test); // This is wrong
n2=atoi(test); // This is wrong
Whatever that I have appended with a comment // This is wrong is because, these needs to be in a function body.
Now since you already stated that,
I see the functions are used in some other files in the project..
I don't have to provide you with a way to implement itoa function.
Update:
Since now that you have added it into a function, the point that I talked about is fixed. Now, it should ideally compile, provided that there a valid definition of a itoa function in one of the header files that have been included.

How can I compile code that uses getsubopt()?

I want to parse a list of options of the form key1=val1, key2=val2, etc (like the options to mount -o). The getsubopt() function seems perfect for this task (http://www.gnu.org/s/hello/manual/libc/Suboptions.html). However, when I try to compile my code using gcc, I get:
warning: implicit declaration of function ‘getsubopt’
and the program segfaults when I run it.
I added #include <stdlib.h> but the compiler doesn't pick up the declaration.
Do you have:
#define _XOPEN_SOURCE 500
#include <stdlib.h>
at the top of the file that contains the call to getsubopt? The error you are getting is what you would expect if you call a function which has not been declared.

Resources