Ошибка expected declaration specifiers or before

I’ve been trying to learn C from a book called C Programming Absolute Beginner’s Guide and I have come across a problem and the one bad thing is you can’t ask a book a question! Typed the error into Google and it brought me to this site. The error I’m getting is in the question title and this is my code.

#include <stdio.h>

 main(

{

 //Set up the variables, as well as define a few

    char firstInitial, middleInitial;
    int number_of_pencils;
    int number_of_notebooks;
    float pencils = 0.23;
    float notebooks = 2.89;
    float lunchbox = 4.99;


    //The information for the first child
    firstInitial = 'J';
    middleInitial = 'R';

    number_of_pencils = 7;
    number_of_notebooks = 4;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchboxn",
        firstInitial, middleInitial,number_of_pencils,
        number_of_notebooks);
    printf("The total cost is £%.2fnn", number_of_pencils*pencils + number_of_notebooks*notebooks + lunchbox);

    //The information for the second child
    firstInitial ='A';
    middleInitial = 'J';

    number_of_pencils = 10;
    number_of_notebooks = 3;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchboxn",
        firstInitial, middleInitial,number_of_pencils,
        number_of_notebooks);
    printf("The total cost is £%.2fnn", number_of_pencils*pencills
        + number_of_notebooks*notebooks + lunchbox);

    //The information for the third child
    firstInitial = 'M';
    middleInitial = 'T';

    number_of_pencils = 9;
    number_of_notebooks = 2;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchboxn",
        firstInitial, middleInitial,number_of_pencils,
        number_of_notebooks);
    printf("The total cost is £%.2fn",
        number_of_pencils8pencils + number_of_notebooks*notebooks + lunchbox);

        return0;
}

)

What’s wrong with this code?

If you’re a developer, you may have encountered a syntax error that reads «expected declaration specifiers before» while coding. This error message can be frustrating and confusing, but it’s essential to understand what it means and how to fix it. In this guide, we’ll explain what the «expected declaration specifiers» error is and provide you with a step-by-step solution to fix it.

What is the ‘Expected Declaration Specifiers’ Error?

The «expected declaration specifiers before» error is a syntax error that occurs when there is a mistake in the declaration of a function or variable. This error message is usually accompanied by a line number, which can help you locate the error in your code.

The error message is typically caused by one of the following mistakes:

  • Missing semicolon at the end of a line
  • Misspelling of a keyword or variable name
  • Incorrect use of parentheses or brackets

How to Fix the ‘Expected Declaration Specifiers’ Error?

Fixing the «expected declaration specifiers» error requires you to carefully review your code and identify the mistake. Here’s a step-by-step guide to help you fix this error:

Step 1: Identify the Line Number

The first step is to identify the line number where the error occurred. The error message should give you a clue about the line number. Once you know the line number, you can go directly to that line in your code and start reviewing it.

Step 2: Check for Missing Semicolons

One of the most common causes of the «expected declaration specifiers» error is a missing semicolon at the end of a line. Check the line where the error occurred and ensure that there is a semicolon at the end of the line. If there isn’t, add one and save your changes.

Step 3: Check for Misspelled Keywords or Variable Names

Another common cause of the «expected declaration specifiers» error is a misspelled keyword or variable name. Check the line where the error occurred and ensure that all keywords and variable names are spelled correctly. If there is a mistake, correct it and save your changes.

Step 4: Check for Incorrect Use of Parentheses or Brackets

The «expected declaration specifiers» error can also be caused by an incorrect use of parentheses or brackets. Check the line where the error occurred and ensure that all parentheses and brackets are used correctly. If there is a mistake, correct it and save your changes.

Step 5: Compile Your Code

Once you’ve made the necessary changes to your code, compile it to check if the error has been fixed. If the error persists, review your code again and look for any other mistakes.

FAQ

What is a declaration specifier?

A declaration specifier is a keyword that indicates the type of a variable or function. Examples of declaration specifiers include «int,» «char,» and «void.»

Why am I getting an ‘expected declaration specifiers’ error?

You may be getting this error because there is a mistake in the declaration of a function or variable, such as a missing semicolon, misspelled keyword or variable name, or incorrect use of parentheses or brackets.

How do I fix an ‘expected declaration specifiers’ error?

To fix this error, you need to carefully review your code, identify the mistake, and make the necessary changes. This may involve adding a missing semicolon, correcting a misspelled keyword or variable name, or fixing an incorrect use of parentheses or brackets.

How can I avoid getting an ‘expected declaration specifiers’ error?

To avoid getting this error, be sure to carefully review your code before compiling it. Check for missing semicolons, misspelled keywords or variable names, and incorrect use of parentheses or brackets.

What other errors can occur when coding?

Other common errors that can occur when coding include syntax errors, logical errors, and runtime errors.

  • C Programming Basics
  • Common C Programming Errors
  • Debugging Tips for C and C++ Programs

I had the same problem. My error messages were:

TarHeader.h:15:24: error: expected declaration specifiers or '...' before numeric constant
TarHeader.c:69:61: error: expected declaration specifiers or '...' before string constant

And the specific lines of code were:

* Line 15 in TarHeader.h:

#define TAR_BLOCK_SIZE 512

* Line 69 (v1) in TarHeader.c:

static_assert(TAR_BLOCK_SIZE == sizeof(tar_posix_header_t), "Incorrect header definition");

When I tried to change it I changed the includes in «TarHeader.c» (removed include of «TarHeader.h»)
The line seems then like this (Line 69 (v2) in TarHeader.c)

static_assert(512== sizeof(tar_posix_header_t), "Incorrect header definition");

And the error codes changed:

TarHeader.c:69:15: error: expected declaration specifiers or '...' before numeric constant
TarHeader.c:69:50: error: expected declaration specifiers or '...' before string constant

When I removed line 65 of «TarHeader.c» no error was reported even with the previously used include.

The static assert was copied from another project (also in C) ane there was no problem. But it was a different compiler.

Solution:

Search for the use of these defines and see what use causes this problem.


0

0

Добрый день!
При компиляции libpcap возникла непонятная ошибка:

gcc -nodefaultlibs -nostdlib -fPIC -static  -I/home/xvilka/build/cross/sys-root/usr/include -O2 
-fPIC  -nodefaultlibs -nostdlib -fPIC -static  -I/home/xvilka/build/cross/sys-root/usr/include -I. 
  -DHAVE_CONFIG_H  -D_U_="__attribute__((unused))" -c ./pcap-usb-linux.c
In file included from ./pcap-usb-linux.c:57:
/home/xvilka/build/cross/sys-root/usr/include/string.h:431: error: expected declaration specifiers or '...' before '(' token
/home/xvilka/build/cross/sys-root/usr/include/string.h:431: error: expected declaration specifiers or '...' before '(' token
/home/xvilka/build/cross/sys-root/usr/include/string.h:431: error: expected declaration specifiers or '...' before '(' token
/home/xvilka/build/cross/sys-root/usr/include/string.h:431: error: expected ')' before ',' token
make[1]: *** [pcap-usb-linux.o] Error 1

В чем может быть проблема? (такую ошибку в первый раз встречаю)
Некоторые флаги компилятора повторяются, хотя при .configure были заданы один раз, но уверен это не играет здесь роли.
Помогите с решением, спасибо!

Home »
C programs »
C common errors programs

Here, we are going to learn why an error expected declaration specifies before printf() in C occurred and how to fix it in C programming language?

Submitted by IncludeHelp, on September 09, 2018

The main cause of this error is — missing opening curly brace ({), before the printf() function.

Example:

#include <stdio.h>

int main(void)
	printf("Hello world");
	return 0;
}

Output

prog.c: In function ‘main’:
prog.c:4:2: error: expected declaration specifiers before ‘printf’
  printf("Hello world");
  ^~~~~~
prog.c:5:2: error: expected declaration specifiers before ‘return’
  return 0;
  ^~~~~~
prog.c:6:1: error: expected declaration specifiers before ‘}’ token
 }
 ^
prog.c:6:1: error: expected ‘{’ at end of input

In this program, opening brace of the main() block is missing

How to fix?

To fix this and such errors, please take care of curly braces, they are properly opened and closed.

Correct code:

#include <stdio.h>

int main(void){
	printf("Hello world");
	return 0;
}

Output

Hello world

C Common Errors Programs »


Понравилась статья? Поделить с друзьями:

Не пропустите эти материалы по теме:

  • Яндекс еда ошибка привязки карты
  • Ошибка expected at end of input
  • Ошибка exiting pxe rom при загрузке
  • Ошибка exit fatal error cannot initialize
  • Ошибка execute command error

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии