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:UsersUserDocumentsCodeBlocks filesCB Test 01main.c|8|error: expected identifier or '(' before '{' token|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
a.c
#include<stdio.h>
#include "a.h"
int main(){
s.a = 10;
printf("n add : %d n",add(2,2));
printf("n sub : %d n",sub(2,2));
printf("n struct is : %d n",s.a);
return 0;
}
b.c :
#include<stdio.h>
int add(int a,int b){
return (a+b);
}
int sub(int a,int b){
return (a-b);
}
a.h :
#ifndef A_HEADER
#define A_HEADER
typedef struct _s{
int a;
int b;
}s;
int add(int a,int b);
int sub(int a,int b);
#endif
O/p:
[root@s new]# ls
a.c a.h b.c include
[root@s new]# gcc -c a.c -o a.o
a.c: In function ‘main’:
a.c:4: error: expected identifier or ‘(’ before ‘.’ token
a.c:7: error: expected expression before ‘s’
I ‘m getting an error as above. Couldn’t find why the error is
Patrice fixed your compilation problem. However, the code, as it stands, still has flaws. See the notes below:
//Header file #include <stdio.h> //Main program int main() { //Declaration of variables. int n, r=0 , rev; // Note: r is also initialised // Initialization of variables // Note you didn't initialise 'rev' here scanf("%d",&n); //Processing of variables r = n % 10; rev = rev * 10 + r ; // Note you are using the uninitialised 'rev' n =n / 10; // Note: this operation is pointless, since you didn't use 'n' afterwards //Output printf ("%d",rev); // Note: here the output is 'unpredictable' since you've used the uninitialised 'rev' return 0; }
STAR1337 0 / 0 / 0 Регистрация: 20.12.2017 Сообщений: 19 |
||||
1 |
||||
04.02.2018, 19:30. Показов 12465. Ответов 6 Метки нет (Все метки)
Помогите исправить функцию Вот такая ошибка: prog.c:25:1: error: expected identifier or ‘(’ before ‘{’ token
0 |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
04.02.2018, 19:30 |
Ответы с готовыми решениями: Ошибка в строке 97: «incompatible types», а в 99 ошибка: «identifier expected but ‘^’ found», как исправить? Ошибка: Fatal: Syntax error, «;» expected but «identifier Mas» found. procedure TForm1.Button1Click(Sender: TObject); Ошибка: «expected constructor, destructor, or type conversion before ‘(‘ token» Ошибка «expected primary-expression before ‘>’ token» 6 |
Заклинатель змей 611 / 508 / 213 Регистрация: 30.04.2016 Сообщений: 2,412 |
|
04.02.2018, 19:35 |
2 |
STAR1337, main() сам себя напишет?
0 |
STAR1337 0 / 0 / 0 Регистрация: 20.12.2017 Сообщений: 19 |
||||
04.02.2018, 21:27 [ТС] |
3 |
|||
DobroAlex, я не понял куда его вставить можно показать? Добавлено через 1 час 16 минут
0 |
Супер-модератор 32591 / 21060 / 8134 Регистрация: 22.10.2011 Сообщений: 36,332 Записей в блоге: 8 |
|
04.02.2018, 21:29 |
4 |
Перенести содержимое 5-ой строки в 25-ю попробуй… Вдруг получится?
0 |
STAR1337 0 / 0 / 0 Регистрация: 20.12.2017 Сообщений: 19 |
||||||||
04.02.2018, 22:51 [ТС] |
5 |
|||||||
volvo, я пробовал, не работает Вот рабочий код и из него надо сделать функцию
Добавлено через 1 час 8 минут
0 |
ogner 22 / 26 / 4 Регистрация: 26.03.2017 Сообщений: 190 |
||||
04.02.2018, 22:52 |
6 |
|||
РешениеSTAR1337,
1 |
0 / 0 / 0 Регистрация: 20.12.2017 Сообщений: 19 |
|
04.02.2018, 22:56 [ТС] |
7 |
ogner, Да, спасибо :3
0 |
04-13-2013
#1
Registered User
C and Assembly (error: expected identifier or ‘(‘ before ‘.’ token)
So I’m having trouble linking my assembly code into my C code. Simply, I have one program, but I’m using two different source codes to implement this one program.
For the assembly code, I simply converted from C to assembly using the linux command line «gcc -S..». I ran my program with an equivalent C code and it worked perfectly. But when I try to use assembly with it..it just doesn’t work. So I don’t think it has anything to do with the code. I think I may be linking it incorrectly. Do you guys have more knowledge about this stuff? D:
(I’ll post all of my codes so you guys can see what I’m working with and the errors I’m getting)
«formula.c»
C source code:Code:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "nCr.s" int isNumeric(char *num); int form(int power); int main(int argc, char *argv[]){ clock_t start, end; double time; int num; char value[50]; //error checking if(argc<2){ printf("There are no argumentsn"); }else if(argc>2){ printf("There are too many argumentsn"); }else{ strcpy(value,argv[1]); if(value=="-h"){ printf("Usage: formula<positive integern"); }else{ if(isNumeric(value)==0){ printf("This must be a number or type -h for helpn"); return; { } num = atoi(value); if(num<0){ printf("Number must have a positive valuen"); }else{ start = clock(); //start of time form(num); end = clock(); //end of time time = ((double)(end-start)/CLOCKS_PER_SEC)*1000000; printf("nTime Required = %.0f microsecondn", time); } } } return 0; } int isNumeric(char *num){ char *val; //returns 0 if number and 1 if it's not.. if (num == NULL || *num == '' || isspace(*num)){ return 0; } strtod(num, &val); return *val == ''; } int form(int power){ int powerInd; int index = 0; int val; printf("(1 + x)^%d = ",power); if(power==0){ printf("1"); return; } powerInd=power; while(powerInd>0){ if(index==0){ printf("1 "); index++; }else{ val = nCr(power,index); printf(" + %d*x^%d",val,index); index++; powerInd--; } } }«nCr.s»
ASSEMBLY source code:Code:
.globl nCr .type nCr, @function nCr: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $32, %rsp movl %edi, -20(%rbp) movl %esi, -24(%rbp) movl -24(%rbp), %eax movl -20(%rbp), %edx movl %edx, %ecx subl %eax, %ecx movl %ecx, %eax movl %eax, -12(%rbp) movl -12(%rbp), %eax movl %eax, %edi call Factorial movl %eax, -12(%rbp) movl -20(%rbp), %eax movl %eax, %edi call Factorial movl %eax, -20(%rbp) movl -24(%rbp), %eax movl %eax, %edi call Factorial movl %eax, -24(%rbp) movl -24(%rbp), %eax imull -12(%rbp), %eax movl %eax, -4(%rbp) movl -20(%rbp), %eax movl %eax, %edx sarl $31, %edx idivl -4(%rbp) movl %eax, -8(%rbp) movl -8(%rbp), %eax leave .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size nCr, .-nCr .globl Factorial .type Factorial, @function Factorial: .LFB1: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl %edi, -20(%rbp) movl $1, -4(%rbp) jmp .L4 .L5: movl -4(%rbp), %eax imull -20(%rbp), %eax movl %eax, -4(%rbp) subl $1, -20(%rbp) .L4: cmpl $0, -20(%rbp) jg .L5 movl -4(%rbp), %eax leave .cfi_def_cfa 7, 8 ret .cfi_endproc«nCr.h»
ASSEMBLY header fileCode:
#ifndef _NCR_H_ #define _NCR_H_ extern int nCr(int n, int r); extern int Factorial(int n); #endif /* _NCR_H_ */MY ERROR!!!:
Code:
nCr.s:1: error: expected identifier or '(' before '.' token nCr.s:2: error: stray '@' in program nCr.s:51: error: stray '@' in program make: *** [formula.o] Error 1
04-13-2013
#2
Registered User
Why do you think this line is valid in C.I see no reason to believe your assembly code is valid C code as you included it.
You may have meant to do this
Tim S.
Last edited by stahta01; 04-13-2013 at 10:27 PM.
«…a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match..» Bill Bryson
04-13-2013
#4
and the hat of int overfl
Do what stahta01 suggests, then compile with
gcc nCr.s formula.c
04-13-2013
#7
and the hat of int overfl
> formula.o: formula.o
Surely some mistake — how can a file depend on itself?> How would I incorporate nCr.s into my makefile?
In the same way you have a .c to .o rule, you would have a .s to .o rule as well.Then the executable depends on all the .o file(s)
04-14-2013
#12
and the hat of int overfl
> I realized that I was making my makefile longer than it should have been, but it works better now.
You’ve made the makefile shorter, but you’ve increased the amount of work that needs to be done each time you build the program.You’re recompiling everything, even when only one thing has changed. When you have projects with 1000’s of files, having efficient makefiles is a must.
Code:
formula: nCr.o formula.o $(COMPILER) $(CCFLAGS) nCr.o formula.o -o formula nCr.o : nCr.s $(COMPILER) $(CCFLAGS) -c nCr.s formula.o : formula.c $(COMPILER) $(CCFLAGS) -c formula.c