My Note
My Note
C Keywords
do if static while
Constants
If you want to define a variable whose value cannot be changed, you can use
the const keyword. This will create a constant. For example,
Basic types
Here's a table containing commonly used types in C programming for quick
access.
Type Size (bytes) Format Specifier
char 1 %c
float 4 %f
double 8 %lf
signed char 1 %c
unsigned char 1 %c
C Output
In C programming, printf() is one of the main output function. The function
sends formatted output to the screen. For example,
Example 1: C Output
#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
Output
C Programming
All valid C programs must contain the main() function. The code execution
begins from the start of the main() function.
The printf() is a library function to send formatted output to the screen. The
function prints the string inside quotations.
To use printf() in our program, we need to include stdio.h header file using
the #include <stdio.h> statement.
The return 0; statement inside the main() function is the "Exit status" of the
program. It's optional.
Example 8: ASCII Value
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
Output
Enter a character: g
You entered g.
ASCII value is 103.
Here's a list of commonly used C data types and their format specifiers.
int %d
char %c
float %f
double %lf
unsigned int %u
Data Type Format Specifier
signed char %c
unsigned char %c
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and
variables).
* multiplication
/ division
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
C Relational Operators
== Equal to 5 == 3 is evaluated to 0
C Logical Operators
&& Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5)
Operator Meaning Example
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5)
||
operand is true || (d>5)) equals to 1.
return 0;
}
Output
if (test expression)
{
// code
}
Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
return 0;
}
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
Output 2
Enter an integer: 5
The if statement is easy.
C if...else Statement
The if statement may have an optional else block. The syntax of
the if..else statement is:
if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
C if...else Ladder
The if...else statement executes two different codes depending upon
whether the test expression is true or false. Sometimes, a choice has to be
made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and
execute different statements.
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example 3: C if...else Ladder
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
return 0;
}
Output
This program given below relates two integers using either < , > and = similar
to the if...else ladder's example. However, we will use a
nested if...else statement to solve this problem.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
return 0;
}
If the body of an if...else statement has only one statement, you do not
need to use brackets {} .
if (a > b) {
printf("Hello");
}
printf("Hi");
is equivalent to
if (a > b)
printf("Hello");
printf("Hi");
for Loop
The syntax of the for loop is:
To learn more about test expression (when the test expression is evaluated to
true and false), check out relational and logical operators.
#include <stdio.h>
int main()
{
int num, count, sum = 0;
return 0;
}
Output
while (testExpression) {
// the body of the loop
}
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
Output
1
2
3
4
5
do...while loop
The do..while loop is similar to the while loop with one important difference.
The body of do...while loop is executed at least once. Only then, the test
expression is evaluated.
The syntax of the do...while loop is:
do {
// the body of the loop
}
while (testExpression);
#include <stdio.h>
int main() {
double number, sum = 0;
printf("Sum = %.2lf",sum);
return 0;
}
Run Code
Output
Here, we have used a do...while loop to prompt the user to enter a number.
The loop works as long as the input number is not 0 .
The do...while loop executes at least once i.e. the first iteration runs without
checking the condition. The condition is checked only after the first iteration
has been executed.
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
C break
The break statement ends the loop immediately when it is encountered. Its
syntax is:
break;
The break statement is almost always used with if...else statement inside
the loop.
Example 1: break statement
// Program to calculate the sum of numbers (10 numbers max)
// If the user enters a negative number, the loop terminates
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
return 0;
}
Run Code
Output
C continue
The continue statement skips the current iteration of the loop and continues
with the next iteration. Its syntax is:
continue;
The continue statement is almost always used with the if...else statement.
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
return 0;
}
Output
Enter n1: 1.1
Enter n2: 2.2
Enter n3: 5.5
Enter n4: 4.4
Enter n5: -3.4
Enter n6: -45.5
Enter n7: 34.5
Enter n8: -4.2
Enter n9: -1000
Enter n10: 12
Sum = 59.70
In this program, when the user enters a positive number, the sum is calculated
using sum += number; statement.
When the user enters a negative number, the continue statement is executed
and it skips the negative number from the calculation.
C switch Statement
In this tutorial, you will learn to create the switch statement in C programming
with the help of an example.
The switch statement allows us to execute one code block among many
alternatives.
You can do the same thing with the if...else..if ladder. However, the
syntax of the switch statement is much easier to read and write.
Syntax of switch...case
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
Notes:
If we do not use the break statement, all statements after the matching label
are also executed.
The default clause inside the switch statement is optional.
Example: Simple Calculator
// Program to create a simple calculator
#include <stdio.h>
int main() {
char operation;
double n1, n2;
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
return 0;
}
Output
The - operator entered by the user is stored in the operation variable. And,
two operands 32.5 and 12.4 are stored in variables n1 and n2 respectively.
Since the operation is - , the control of the program jumps to
C goto Statement
In this tutorial, you will learn to create the goto statement in C programming.
Also, you will learn when to use a goto statement and when not to use it.
goto label;
... .. ...
... .. ...
label:
statement;
#include <stdio.h>
int main() {
jump:
average = sum / (i - 1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}
Output
1. Enter a number: 3
2. Enter a number: 4.3
3. Enter a number: 9.3
4. Enter a number: -2.9
Sum = 16.60
Average = 5.53