C Statements
In this page:
data_type variable_name = value; // declaration statement
variable_name = expression; // expression statement
function_name(arguments); // function call statement
What is a Statement?
A statement is the smallest complete instruction in C that the compiler can execute, such as declaring a variable, calling a function, or evaluating an expression, and a C program is fundamentally a sequence of statements executed in order.
Example: What is a Statement?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Expression Statements
An expression statement consists of an expression, like an assignment or a function call, followed by a semicolon, and it's the most common kind of statement, executed purely for its side effect.
Example: Expression Statements
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int x;
x = 5 + 3;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compound Statements (Blocks)
A compound statement, or block, groups multiple statements together inside curly braces, letting several statements be treated as a single unit wherever the language expects just one statement, such as inside an if or loop.
Example: Compound Statements (Blocks)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Check whether x > 0
if (x > 0) {
// Print formatted text to the screen
printf("Positive");
// Print formatted text to the screen
printf(", counted as one block");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaration Statements
A declaration statement introduces a new variable (or function), specifying its type and name, and optionally an initial value, making that identifier available for use in the statements that follow.
Example: Declaration Statements
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Statement Termination with Semicolons
Every simple statement in C, apart from compound statements enclosed in braces, must be terminated with a semicolon, which tells the compiler exactly where one instruction ends and the next one begins.
Example: Statement Termination with Semicolons
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 1
int a = 1;
// Declare b and set it to 2
int b = 2;
// Print formatted text to the screen
printf("%d", a + b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Leaving out the semicolon at the end of a statement like
int x = 5, which causes an error on the next line. - Putting a semicolon after
if (x > 0);, which makes the empty statement the body so the block always runs. - Using a variable declared inside a
{ }block after the closing brace, where it is out of scope.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers