The syntax of the command is incorrect in c - c

I'm using gcc compiler for c programming.I'm trying to run "for loop" but in command prompt when I execute the program it gives "The syntax of the command is incorrect".
The program is:
#include <stdio.h>
int main()
{
int x;
for (x=1; x<=10; x++)
{
printf("%d\n",x);
}
return 0;
}
enter image description here

Your computer does not understand that for is the name of your program because the shell has a builtin command named for. Give the executable another name, like for1, and it should work.

Related

"Hello world" program not running

I typed this program on code blocks but it is showing error on int main line
Here's the program
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
The message return is " multiple definition of main"
The sample code is correct. It might be an IDE configuration error. Use gcc to compile your code on Linux and on Windows you can install MinGW and execute from a command window.

Linux shell, C programming, script execution, syntax error: "(" unexpected

I tried to execute script within C program. I tried:
system("/home/olaudix/weather.sh")
system("sh /home/olaudix/weather.sh")
execvp("/home/olaudix/weather.sh")
but all of them throw
syntax error: "(" unexpected at line 1.
Scripts starts with function getData() { but it runs fine when executed in terminal.
The shebang (i.e.: #!) at the very beginning of the scripts is missing. You need it in order to specify the corresponding interpreter (bash in this case), i.e.:
#!/bin/bash
It is also possible to source the shell script in the system command. Below is an example
$ cat 48465591.c
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
system(". ./myshellscript.sh"); // Note the '.' which stands for sourcing
return 0;
}
$ cat myshellscript.sh
printhello()
{
echo "Hello";
}

Errors when Compiling my First C Program

I am running Ubuntu on a virtual machine with gcc downloaded. I wrote up a code in gedit which contains:
#include <stdio.h>
/* This is a comment. */
int main(int argc, char *argv[])
{
int distance = 100;
// this is also a comment
printf("You are %d miles away.\n", distance);
return 0;
}
When I do the make Ex1.c it says that my file is 'up to date.' so I type in ./Ex1.c and it gives me these errors:
./Ex1.c: line 3: /bin: Is a directory
./Ex1.c: line 4: syntax error near unexpected token '('
./Ex1.c: line 4: 'int main(int arc, char*argv[])'
I don't understand this, I thought it might be how I am typing the code in but then I pasted the code in from the 'Learn C the Hard Way' GitHub and I still get these errors! I just want to run my dang code!
Do it this way.
Open terminal write gedit ex1.c
In the new gedit window write the code.
Close the gedit window.
In the terminal write gcc ex1.c
In the terminal write ./a.out
You're trying to run the source code file.
You have to run the compiled binary.
The compiled binary is often called a.out.
Try doing ./a.out

Cannot enter value from netbeans but working in cmd

I have installed MinGW on netbeans for C and C++ programming.
Here is a simple code that I am trying to run on netbeans :
#include <stdio.h>
int main(int argc, char** argv) {
printf("Inside Main...\n");
int n;
printf("Enter : ");
scanf("%d", &n); // When I remove this line, it is working.
printf("You have entered %d.", n);
return (1);
}
Whenever I try to access any value from netbeans console, I don't see anything.
Output with scanf(...)
Output without scanf(...)
And if I try to run these code from cmd, all are working
for scanf() you must use Netbeans External Terminal !
Normal Run
You can also use Netbeans Standard Output !
But this is more misleading.
While you see an empty Terminal do input 123
after hit enter , you get the output all at once .
I had the same issue while running a CPP program.External output didn't helped me. I set the console type to Standard output and it solved the issue.
Right Click cpp Application-->properties-->run--->Consoletype to standard output

Confusion about output of program

I am new to C programming and I am currently learning loops. In the below program,
#include<stdio.h>
main()
{
int i;
for(i=1;i++<=5;printf("%d",i));
}
i tried to compile in dev c++ compiler but it is giving error "[Error] ld returned 1 exit status"
You need to include the <stdio.h> header, and also, main needs a return type (int) and a return value. Changing the program to this will make it compile (at least it did using GCC) and run:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1;i++<=5;printf("%d",i));
return 0;
}
The quotes you used in the ā€œ%dā€ are illegal too, use normal quotes: "%d".
Apart from that, doing the printf inside the loop head might be legal, but it's pretty bad style. Usually in a for-loop you would have have initialization;condition;increment(or decrement or w/e) in the head, and do side-effects in the body of the statement.
I would try writing the for loop as:
for(i=1;i < 6;i++) { printf(ā€œ%dā€,i); }
I have run this program manually on my notebook and i got Output 23456
Then i run this on Dev c++ and it is giving the same output 23456 without any error and i have just copied and pasted from ur question dun know why its showing error on ur runtime may be u have not saved it as C file

Resources