#include errors detected in vs code for c lang - c

I don't know what to do
please can any one help me
I'm trying to use c in vs code for the first time
#include <stdio.h>
int main (void)
{
printf("Hello, World!");
}
The problem

Related

Why is #include <stdio.h> is underlined in red?

I have this basic code in C. A program to say 'Hello World'. And the first line That say #include <stdio.h> is underlined in red as though an error is occurred.
#include<stdio.h>
main()
{
printf("hello, world\n")
}
I'm using vs code to run the program, is it a bug in vs code? or maybe a problem in the installation of C?
please help.
I don't understand where the problem originates so no actions have been pursued.
Add space between #include and <stdio.h>
Also, specify the return type of your main function.
Also at the end of printf("hello, world\n"), there should be semi colon
#include <stdio.h>
void main(){
printf("hello, world\n");
}
it is void since it is not returning anything.

How to play sound effect or Music in C?

I am making a game and I have to add some sounds effects and Music.
I Googled it and I found The flowing Code:
#include <conio.h>
#include "inc/fmod.h"
FSOUND_SAMPLE* handle;
int main ()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
But when I compile it I got the flowing error:
➜ Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
^~~~~~~~~
1 error generated.
Well, firstly <conio.h> is a C++ library and you're programming in C. It's different!
Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}
Then, this is the other source file, called file.c:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
void scala1(unsigned char x){
if(x=='a')beep(131,50);
if(x=='s')beep(147,50);
if(x=='d')beep(165,50);
if(x=='f')beep(175,50);
if(x=='g')beep(196,50);
if(x=='h')beep(220,50);
if(x=='j')beep(247,50);
if(x=='k')beep(262,50);
if(x=='l')beep(294,50);
if(x=='w')beep(139,50);
if(x=='e')beep(156,50);
if(x=='r')beep(185,50);
if(x=='t')beep(208,50);
if(x=='y')beep(233,50);
}
void scala2(unsigned char x){
if(x=='a')beep(262,50);
if(x=='s')beep(294,50);
if(x=='d')beep(330,50);
if(x=='f')beep(349,50);
if(x=='g')beep(392,50);
if(x=='h')beep(440,50);
if(x=='j')beep(494,50);
if(x=='k')beep(523,50);
if(x=='l')beep(587,50);
if(x=='w')beep(277,50);
if(x=='e')beep(311,50);
if(x=='r')beep(370,50);
if(x=='t')beep(415,50);
if(x=='y')beep(466,50);
}
void scala3(unsigned char x){
if(x=='a')beep(523,50);
if(x=='s')beep(587,50);
if(x=='d')beep(659,50);
if(x=='f')beep(698,50);
if(x=='g')beep(784,50);
if(x=='h')beep(880,50);
if(x=='j')beep(988,50);
if(x=='k')beep(1046,50);
if(x=='l')beep(1175,50);
if(x=='w')beep(554,50);
if(x=='e')beep(622,50);
if(x=='r')beep(740,50);
if(x=='t')beep(831,50);
if(x=='y')beep(932,50);
}
The last one, the file header.h. It's code is the following one:
void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);
All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)

wchar_t strings not working C

I'm trying to create a program that needs to get string input from the user, since it's has to work with portuguese words i'm using wchar_t,the problem is,
C seems to have a will, because when i need it to work it doens't but then out of the blue it works in some simple test.
the following code worked 5 minutes ago and now it doesn't:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
/
int main(){
setlocale(LC_ALL,"Portuguese");
wchar_t meu[3];
fgetws(meu,3,stdin);
fputws(meu,stdout);
return 0;
}

segmentation fault (core dumped) error when using time function

Hello i am new to Linux and c programming so this might be stupid question but i couldn't find an answer.
I am writing a home work and they want me to print the execution time at the end of program using time() function, so when i used the function in my program i got the message segmentation fault (core dumped) and when i remove it the program works agine.
Then i created a test file in the code below :
#include <stdio.h>
int main()
{
time();
return 0;
}
And i got the same error message.
Also tried :
#include <stdio.h>
int main()
{
time(NULL);
return 0;
}
And
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
return 0;
}
And got the same error.
so what i am doing wrong ?
Thanks
In your first two examples, you forget to include time.h. That is the cause of the segmentation fault in those examples. If you're using gcc, try compiling with -Wall (which turns on all warnings). You should get a warning indicating an implicit declaration of function "time" - in other words, that you've forgotten to include time.h.
Your final example, however, works fine for me. If you keep getting a segmentation fault, however, try debugging with gdb.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
time_t now;
time(&now);
printf("%s", ctime(&now));
return EXIT_SUCCESS;
}

if defined(__AIX) not working on AIX 6.1?

I have the following on AIX 6.1:
#include <stdio.h>
#include <stdlib.h>
int main(){
#if defined(__AIX)
printf("hello world");
#endif
return 0;
}
I have followed the instructions from http://predef.sourceforge.net/preos.html#sec2 but am scratching my head as to why it does not work or print out "hello world" when I run it?
if I do a "uname" on the box I get "AIX"...and "uname -v" returns "6"...
Any ideas what I can put into the code?
Thanks for the help
Lynton
should be 1 underscore not 2 underscores before "AIX"....solved now ;-)

Resources