This question already has an answer here:
Syntax error near unexpected token '(' when using terminal [closed]
(1 answer)
Closed 7 years ago.
I am having problem with the following code
#include <stdio.h>
int main()
{
int tall = 175;
int weight = 68;
printf("I am %d slim.\n", tall);
printf("I am %d kilogram.\n", weight);
return 0;
}
i have created these code following a course but i have an issue (The Compiler said that )
./file.c: line 3: syntax error near unexpected token `('
./file.c: line 3: `int main()'
for more information I am using GCC in linux destro ..
and i am using terminal To run ...
there's No IDE
It seems that you are not compiling your program, but that you are trying to execute it without compiling. The errors that you see is /bin/bash that tries to interpret this as shell code.
Related
This question already has answers here:
Beginner: syntax error before int main ()
(2 answers)
Closed 5 years ago.
Tish is the program:
#include <stdio.h>
int main()
{
printf("77777");
return 0;
}
yaki#ubuntu:~/Desktop/yakima$ gcc yakima.c -o yakima.o
yaki#ubuntu:~/Desktop/yakima$ ./yakima.c
This is the error:
./yakima.c: line 3: syntax error near unexpected token `('
./yakima.c: line 3: `int main()'
What can you do with this problem?
You are trying to execute source file.
After you create object file you have to link object file(s) to binary like
gcc -c yakima.c -o yakima.o
gcc yakima.o -o yakima
and execute binary
./yakima
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
sorry for bothering you with such basic question but I am trying to learn C (and later C++) programming but I can not get compiler to compile so while struggling to get to understand how to the IDEs work (I tried both Code Blocks and CodeLite to get this code to work) then I keep getting the same errors .
IDE keep showing this problem at first opening brace which I do not understand as the braces looks placed correctly to me (?).
I have also tried moving the main function to above the addtwo function but it doesn't seem to make any difference .
Program :
/* program to add two numbers and return result */
#include <stdio.h>
/* This function adds two numbers */
int addtwo( int x , int y );
{
int result;
int result = x+y ;
return (result)
}
int main()
{
int sum ;
sum=addtwo(25,49);
printf("25 + 49 = %d \n",sum);
getchar();
return sum;
}
Compiler output :
||=== Build: Debug in CB Test 01 (compiler: GNU GCC Compiler) ===|
C:\Users\User\Documents\CodeBlocks files\CB Test 01\main.c|8|error: expected identifier or '(' before '{' token|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
No semicolon after (result) in addtwo function. Also the parentheses () are not necessary.
Extra semicolon in,
int addtwo( int x , int y );
/* ^ here */
{
int result;
int result = x+y ;
return (result)
}
This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 5 years ago.
I noticed something strange with the following code.
int main()
{
printf("Test"); // Section 1 do something here....
while(1)
{
;
}
}
Section 1 should be executed first, then the program should get stuck in while loop.
But the result was that "Test" didn't get printed, but it got stuck in the while loop. I wonder why the code in Section 1 does not get executed?
I ran the code on Ubuntu 14.04 LTS(compiled with the default gcc compiler)
The stdout stream is buffered, therefore it will only display what's in the buffer after it reaches a newline. Add :
fflush(stdout);
after line :
printf("Test");
See also this link for other alternatives.
This must work:
#include <stdio.h>
int main() {
printf("Test");
while(1){}
}
To compile:
gcc file.c
To execute:
./a.out
This question already has answers here:
C/C++ line number
(10 answers)
Closed 6 years ago.
Is there any way to get the source code line number in C code in run time?
The thought behind it is, suppose a software is made in plain C language. the .exe file is distributed to a user who knows nothing about C. Now if there is any error, the user can see the line number and report the error to the manufacturer so that the debugging can be done at manufacturer site. I just to debug the run time errors and get the corresponding line numbers in the source code while running it. I am a beginner in these stuffs.
If you are using the GCC compiler, you can use the Standard Predefined Macro - __LINE__ as a line number placeholder.
The compiler will fill in the line number while compiling.
Eg :-
printf("%d",__LINE__);
Use gdb instead. But I guess it will work:
if(someThingsWrong())
printf("wrong at line number %d in file %s\n", __LINE__, __FILE__);
You can use the built-in macros __LINE__ and __FILE__, which always expand to the current file name and line number.
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc<2)
{
printf("Error: invalid arguments (%s:%d)\n", __FILE__, __LINE__);
return 0;
}
printf("You said: %s\n", argv[1]);
return 0;
}
This question already has answers here:
fatal error: iostream.h no such file or directory [duplicate]
(3 answers)
Closed 8 years ago.
I wanted to run C programs on windows in order to achieve this I downloaded cygwin(Linux like environment for windows) made a program and kept it on a directory called ..\cygwin\home\Computer
Code goes here
#include<iostream.h>
void main(){
printf("Hai");
}
When i am trying to execute this program using command prompt.
$ g++ hai.c
Its throwing out an error
hai.c:1:21: fatal error: iostream.h: No such file or directory
#include<iostream.h>
^
compilation terminated.
What is going on any idea?
change to
#include <iostream>
#include <cstdio>
int main(){
printf("Hai");
}
or with g++ -x c hai.c or gcc hai.c
#include <stdio.h>
int main(){
printf("Hai");
}