In C
programming statements are usually grouped into categories based on what
they do in a program.
Here’s a clear and simple breakdown.
Type of Statement |
Examples |
Purpose |
Declaration |
int x; |
Reserve
memory for variables |
Input/Output |
printf(), scanf() |
Take input
& display output |
Expression |
x = y + 5; |
Perform
operations |
Conditional |
if, switch |
Make
decisions |
Looping |
for, while |
Repeat
tasks |
Jump |
break, continue, return, goto |
Used to
jump to another part of the program |
1. Declaration Statements
These are statements used to declare
variables, constants, arrays, or functions before you use them in a
program.
They tell the compiler:
The name of the data
The type of data (integer, float, char, etc.)
Sometimes the initial value
Syntax :- data_type variable_name;
Or data_type variable_name = value;
Examples
int age; //
Declaring an integer variable
float
price; // Declaring a
floating-point variable
char
grade; // Declaring a
character variable
int rollNo =
101; // Declaring and initializing
const int MAX = 100; //
Declaring a constant
These tell the compiler about the type and name of data we will
use.
2. Input/Output Statements
1. Formatted Output Statement — printf()
·
Used to display
text, variables, and results in a controlled way.
·
Provided by the stdio.h
library.
Syntax:
printf(
"format string", variable1, variable2, ...);
Common Format Specifiers for Output
Data Type |
Format Specifier |
Example Output |
int |
|
25 |
float |
|
3.140000 |
float (2 dp) |
|
3.14 |
char |
|
A |
string |
|
Hello |
Examples:
printf(
"Age: %d\n",
15);
// Integer output
printf(
"Price: %.2f\n",
45.678);
// Float with 2 decimal places
printf(
"Grade: %c\n",
'A');
// Character
printf(
"Name: %s\n",
"Rahul");
// String
2.
Formatted Input Statement — scanf()
·
Used to read
data entered by the user from the keyboard in a specific format.
·
Provided by the stdio.h
library.
Syntax:
scanf(
"format string", &variable1, &variable2, ...);
&
(address-of operator) is used before variable names in scanf()
except for
strings.
Common
Format Specifiers for Input
Data Type |
Format Specifier |
Example Input |
int |
|
25 |
float |
|
3.14 |
char |
|
A |
string |
|
Hello |
int age;
scanf(
"%d", &age);
// Reads an integer
float price;
scanf(
"%f", &price);
// Reads a float
char grade;
scanf(
"%c", &grade);
// Reads a character
char name[
20];
scanf(
"%s", name);
// Reads a string (no & for strings)
Unformatted Input & Output
Statements in C
In C programming, unformatted I/O statements are used for simple character or string reading and writing
without using format specifiers like %d
, %f
, etc.
They work directly
with characters or strings and are also provided by the stdio.h
library.
1.
Unformatted Output Statements
(a) putchar()
Outputs a single character to the screen.
Syntax:
putchar(character);
Example:
putchar(
'A');
// Displays: A
(b) puts()
Outputs a string followed by a newline (\n
).
Syntax:
puts(string_variable);
Example:
char name[] =
"Rahul";
puts(name);
// Displays: Rahul (then goes to next line)
2.
Unformatted Input Statements
(a) getchar()
Reads a single character from the keyboard.
Syntax:
variable = getchar();
Example:
char ch;
ch = getchar();
// User enters 'A'
(b) gets()
(Not recommended in modern C because it is
unsafe)
Reads a string (including spaces) until Enter is
pressed.
Syntax:
gets(string_variable);
Example:
char name[
20];
gets(name);
// User enters: Rahul Kumar
gets()
can cause memory overflow, so in modern C, fgets()
is preferred.
3. Expression Statements
An expression is anything that produces a value — like
a calculation, assignment, or function call.
Example:
1. Simple Expression Statements
Contain only one operation.
Examples:
x = 5; //
Simple assignment
count++; //
Increment
y--; //
Decrement
2. Complex Expression Statements
Contain multiple operations in the same
statement.
Examples:
result = (a + b) * c;
// Arithmetic + assignment
x = y + z - w / 2;
// Multiple operators
sum = a++ + --b;
// Increment + addition
4. Control Statements
Types of
Control Statements
A. Conditional (Decision-making) Statements
Used to make
decisions based on a condition (true or false).
- if statement
if (marks >= 33)
printf("Pass");
- if...else statement
if (marks >= 33)
printf("Pass");
else
printf("Fail");
- switch statement
switch (day) {
case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
default: printf("Invalid
Day");
}
B. Looping (Iteration) Statements
Used to repeat
a block of code multiple times.
- for loop
for (i = 1; i <= 5; i++)
printf("%d\n",
i);
- while loop
i = 1;
while (i <= 5) {
printf("%d\n",
i);
i++;
}
- do...while loop
i = 1;
do {
printf("%d\n",
i);
i++;
} while (i <= 5);
C. Jump Statements
Used to jump
to another part of the program.
- break
for (i = 1; i <= 10; i++) {
if (i == 5) break;
// Exit loop
printf("%d\n",
i);
}
- continue
for (i = 1; i <= 5; i++) {
if (i == 3) continue; // Skip 3
printf("%d\n",
i);
}
- goto
goto label;
printf("This will be skipped");
label:
printf("Jumped here");
- return
return 0; // End function and return control
No comments:
Post a Comment