DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • Using Unblocked to Fix a Service That Nobody Owns
  • Proper Java Exception Handling
  • Python Variables Declaration

Trending

  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Next-Gen IoT Performance Depends on Advanced Power Management ICs
  • Go 1.24+ Native FIPS Support for Easier Compliance
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  1. DZone
  2. Data Engineering
  3. Data
  4. 'Expected Declaration or Statement at End of Input' in C Solved

'Expected Declaration or Statement at End of Input' in C Solved

In this article, you will learn how to identify where the problem is, how to deal with it, and how to avoid it.

By 
Ankur Ranpariya user avatar
Ankur Ranpariya
·
Sep. 06, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.0K Views

Join the DZone community and get the full member experience.

Join For Free

Programming in C requires careful attention to detail, as even small syntax errors can cause unexpected problems in your code. One common error message that developers may encounter when writing C code is "Expected declaration or statement at the end of input." This error message can be frustrating to deal with, but fortunately, it is usually straightforward to diagnose and fix. In this article, you will learn how to identify where the problem is, how to deal with it, and how to avoid it.

Identify the Problem

When the "Expected declaration or statement at the end of input" error occurs, it means that the compiler has reached the end of the file or function without finding a complete statement or declaration. In other words, the compiler is expecting to find some additional code but instead has reached the end of the program without finding it.

Identifying the problem that caused the error can be tricky, but it usually comes down to a missing bracket, semicolon, or parenthesis in your code. In order to fix the error, you need to determine exactly what is missing and where it needs to be added. This can involve carefully reviewing your code line by line to find the missing expression.

In the next section, we will explore some common examples of what can cause this error and how to resolve them.

Examples and Solutions

Missing Semicolon

One common cause of the error is a missing semicolon at the end of a line of code. For example:

C
 
int main() {
  printf("Hello, World!")
  return 0;
}


In this example, the error message will indicate that the problem is on line 2, where there is a missing semicolon after the printf() statement. To fix the error, you simply need to add the semicolon.

Missing Bracket

Another cause of the error is a missing bracket after a function call. For example:

C
 
#include 

int sum(int x, int y) {
  return x + y;
//missing closing bracket

int main() {
  int result = sum(3, 4);
  printf("The result is %d\n", result);
  return 0;
}


In this case, the error message still indicate there is an error but might not point out exactly where the problem is. To fix the error, you need to review carefully each line, especially in the non-main function.

Missing Parenthesis

A missing parenthesis, whether in the condition after the if-else statement or in a function call, can also cause this type of error. For example:

C
 
int main() {
  int x = 5;
  if (x < 10 
    printf("x is less than 10\n");
  
  return 0;
}


Unless the message error shows you where the problem lies, review your code carefully and add the missing parenthesis where the place is needed.

Other Examples

This type of error can easily be caused by syntax errors, mainly from the developers themself. You can see some examples below:

Unclosed Quotes

C
 
int main() {
  printf("Hello, World!); //Missing double quotation marks
  return 0;
}


Mismatched Bracket

C
 
int main() {
    printf("Hello, world!\n");
        if (1) {
            printf("This statement will execute.\n"); //Missing closing bracket
        else { 
            printf("This statement will not execute.\n");
        }
    }
}


Depending on your compiler and IDE, the error message for the "expected declaration or statement at end of input" error may vary. However, in most cases, the error message will specifically point out the missing expression. Regardless of the specific error message, the cause of the problem is still a syntax error in your code. Therefore, it's important to carefully check your code for any missing expressions and practice good coding habits to prevent these types of errors. You can learn it in the next section.

How To Prevent

Syntax errors like "expected declaration or statement at end of input" can be prevented with good coding practices. Here are some tips to prevent this error from happening:

  • Always ensure that every loop and function has an opening and a closing bracket.
  • At the end of every command line, there should be a semicolon.
  • Wrap every conditional statement in parentheses.
  • Every time a function is declared or called, its parameters/arguments should be wrapped in opening and closing parentheses.
  • Practice indentation: Proper indentation makes the code more readable and easier to understand. It also helps in identifying syntax errors, including the "expected declaration or statement at the end of input" error.
  • Use an IDE with syntax highlighting.
  • Use an online compiler: Similar to a modern IDE, online compilers can be useful for identifying syntax errors. They often highlight syntax errors and provide error messages that can help programmers fix the errors.
  • Run your code through a code formatted like "indent": This is especially useful for large codebases where it can be difficult to identify syntax errors by hand. Here's a brief guide on how to install indent on Ubuntu:
    • Open the terminal and run the following command:
      C
       
      sudo apt-get update


    • After the update completes, install the indent package using the following command:
      C
       
      sudo apt-get install indent


    • Once the installation is complete, you can use the "indent" command to format your C code. Simply navigate to the directory containing your C code file and run this command in the terminal:
      C
       
      indent .c


      Now, open your file in the text editor and verify that the code is now formatted correctly.

By following these tips, you can nearly prevent any syntax errors in your code. Last but not least, remember to always double-check the code for errors before compiling it.

Conclusion

In this article, we have learned that the "expected declaration or statement at the end of input" error is a common error in C programming that can be caused by missing braces, semicolons, or parentheses in our code. To prevent this error, it's important to practice good coding habits such as always ensuring loops and functions have opening and closing brackets, using semicolons at the end of each command line, and wrapping every conditional statement with parentheses. If you encounter this error, check your code for missing expressions and use an online compiler or code formatter to help identify the issue. By following these practices and using the appropriate tools, you can write more robust and error-free C code.

That's all for now. Happy coding!

Error message Integrated development environment Text editor Syntax (programming languages) Data Types Language code

Published at DZone with permission of Ankur Ranpariya. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • Using Unblocked to Fix a Service That Nobody Owns
  • Proper Java Exception Handling
  • Python Variables Declaration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!