Expected identifier or before token ошибка

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
{
^

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
 
int kek(double x1,double x2,double y1,double y2,double a,double b,double V)
 
{
        printf("Enter the x1n");
        scanf("%lf",&x1);
        printf("Enter the x2n");
        scanf("%lf",&x2);
        printf("Enter the y1n");
        scanf("%lf",&y1);
        printf("Enter the y2n");
        scanf("%lf",&y2);
        
    a = (x1*x2+y1*y2);
    b = sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2);
    V = a/b ;
    
    printf("%lfn",V);
    return 0;
}
 
{
    
    int x1,x2,y1,y2,a,b,V;
    V = kek(x1,x2,y1,y2,a,b)
}



0



Programming

Эксперт

94731 / 64177 / 26122

Регистрация: 12.04.2006

Сообщений: 116,782

04.02.2018, 19:30

Ответы с готовыми решениями:

Ошибка в строке 97: «incompatible types», а в 99 ошибка: «identifier expected but ‘^’ found», как исправить?
var
tmp,tmp1,tmp2:point;
begin
tmp:=head;
while tmp &lt;&gt; nil do
begin
tmp1:=…

Ошибка: Fatal: Syntax error, «;» expected but «identifier Mas» found.
Где здесь синтаксическая ошибка( &quot;mas:=A2;&quot; )?

procedure TForm1.Button1Click(Sender: TObject);

Ошибка: «expected constructor, destructor, or type conversion before ‘(‘ token»
connect(CommandLinkButton,clicked(),MainWindow,MainWindow::knopka());
Выдаёт ошибку expected…

Ошибка «expected primary-expression before ‘>’ token»
Задача: Задано линейный массив целых чисел. Увеличить на 2 каждый неотъемлемый элемент
массива….

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 минут
Все равно не работает

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
 
int main ()
int kek(double x1,double x2,double y1,double y2,double a,double b,double V)
 
{
        printf("Enter the x1n");
        scanf("%lf",&x1);
        printf("Enter the x2n");
        scanf("%lf",&x2);
        printf("Enter the y1n");
        scanf("%lf",&y1);
        printf("Enter the y2n");
        scanf("%lf",&y2);
        
    a = (x1*x2+y1*y2);
    b = sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2);
    V = a/b ;
    
    printf("%lfn",V);
    return 0;
}
 
{
    
    int x1,x2,y1,y2,a,b,V;
    V = kek(x1,x2,y1,y2,a,b)
}



0



Супер-модератор

Эксперт Pascal/DelphiАвтор FAQ

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, я пробовал, не работает

Вот рабочий код и из него надо сделать функцию

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
 
int main()
 
{
    double x1,x2,y1,y2,a,b,V;
    
        printf("Enter the x1n");
        scanf("%lf",&x1);
        printf("Enter the x2n");
        scanf("%lf",&x2);
        printf("Enter the y1n");
        scanf("%lf",&y1);
        printf("Enter the y2n");
        scanf("%lf",&y2);
        
    a = (x1*x2+y1*y2);
    b = sqrt(x1*x1+y1*y1);
    V = a/(b*b);
    
    {printf("%lfn",V);}
    return 0;
}

Добавлено через 1 час 8 минут
Все я исправил свои ошибки

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
 
 
float function(float x1,float x2,float y1,float y2,float a,float b)
{
  float V;
    a = (x1*x2+y1*y2);
    b = sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2);
    V = a/b;
  return(V);
}
 
int main()
 
{
    double x1,x2,y1,y2,a,b,V;
    scanf("%lf %lf %lf %lf %lf %lf",&x1,&x2,&y1,&y2,&a,&b);
    V=function(x1,x2,y1,y2,a,b);
    printf("%lf",V);
    system("PAUSE");
    return 0;
}



0



ogner

22 / 26 / 4

Регистрация: 26.03.2017

Сообщений: 190

04.02.2018, 22:52

6

Лучший ответ Сообщение было отмечено STAR1337 как решение

Решение

STAR1337,
Устроит?

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
 
double func(double, double, double, double);
 
int main()
{
    double x1,x2,y1,y2;
    
        printf("Enter the x1n");
        scanf("%lf",&x1);
        printf("Enter the x2n");
        scanf("%lf",&x2);
        printf("Enter the y1n");
        scanf("%lf",&y1);
        printf("Enter the y2n");
        scanf("%lf",&y2);
     
    printf("%lfn",func(x1,x2,y1,y2));
    return 0;
}
 
double func(double x1, double x2, double y1, double y2){
    double a,b,V;
    
    a = (x1*x2+y1*y2);
    b = sqrt(x1*x1+y1*y1);
    
    return V = a/(b*b);
    
}



1



0 / 0 / 0

Регистрация: 20.12.2017

Сообщений: 19

04.02.2018, 22:56

 [ТС]

7

ogner, Да, спасибо :3



0



04-13-2013


#1

ChickenChowMinh is offline


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 file

Code:

#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

stahta01 is offline


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

Salem is offline


and the hat of int overfl

Salem's Avatar


Do what stahta01 suggests, then compile with

gcc nCr.s formula.c


04-13-2013


#7

Salem is offline


and the hat of int overfl

Salem's Avatar


> 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

Salem is offline


and the hat of int overfl

Salem's Avatar


> 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


Понравилась статья? Поделить с друзьями:
  • Expected function or variable vba ошибка
  • Expected end sub ошибка vba
  • Expected array ошибка vba excel
  • Expected an indented block python ошибка что значит
  • Expected an indented block python ошибка как исправить