Declare variables using the dataType variableName;
syntax. For example:
int number;
char character;
float decimalNumber;
In C language, a sequence of instructions is called a statement. A semicolon (;
) is used to terminate each statement.
Functions are self-contained blocks of code that perform specific tasks in your program. Include the function definition using the following syntax:
returnType functionName(parameters) {
// Function body
}
Here's a simple C program demonstrating variable declaration, function, and main function structure:
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 7;
int result = sum(num1, num2);
printf("The sum of %d and %d is: %d", num1, num2, result);
return 0;
}
What causes it: Incorrect syntax or missing semicolons.
# Bad code example
int number = 5, character = 'A'; // Missing semicolon
Error message:
error: expected declaration specifiers or '...' before 'number'
Solution:
# Corrected code
int number = 5;
char character = 'A';
Why it happens: The missing semicolon at the end of a statement is not allowed in C language.
How to prevent it: Always include a semicolon (;
) at the end of each statement.
What causes it: Trying to use an undefined function or variable.
# Bad code example
int result = sum(num1, num2); // The sum() function is not defined in this scope
Error message:
error: 'sum' undeclared (first use in this function)
Solution:
# Corrected code
# Include the sum() function definition in your program
Why it happens: The C compiler cannot find the function or variable being referenced because it has not been defined yet.
How to prevent it: Make sure to include the necessary function and variable declarations before using them in your code.