Introduction to C
Introduction
C is a general-purpose, high-level programming language that can be used to develop software like operating systems, databases, compilers and so on. Computers don’t understand programs written in C. The code needs to be compiled and converted into machine(binary) code before the program can be executed.
Review of C Programming Concept
Features of C
- Supports structured programming
- Faster and efficient
- Portability
- Extendibility
Some basic syntax rules for C program
- ‘C’ is a case-sensitive language.
- Keywords cannot be used as variable or function name.
- All C instructions must be end with semicolon(;) sign.
- Whitespace is required between keywords and identifiers.
Control Statement in C
Control statement defines the flow of control within the program. Below are the statements:
- Simple if statement: It evaluates the content only if expression is true.
Syntax
if (Condition)
Statement;
Or,
if(Condition)
{
Statement;
.......
}
- if-else statement: It verifies the given condition and executes only one out of two blocks of statements based on the condition result.
Syntax
if(Condition)
Statement;
else
Statement;
Or,
if(Condition)
{
True block of statement;
}
else
{
False block of statement;
}
- if-else-if statement: It is also known as if-else-if ladder. This statement is used when there are more than two possible actions based on different conditions.
Syntax
if(Condition1)
{
Statement1;
}
else if(Condition2)
{
Statement2;
}
.........
.........
else if(Condition N)
{
Statement N;
}
else
{
Default_Statement;
}
- Nested if statement: It means an if statement inside another if statement.
Syntax
if(Condition1)
{
if(Condition2)
{
Statement1;
}
else
{
Statement2;
}
}
else
{
if(Condition3)
{
Statement3;
}
else
{
Statement4;
}
}
- Switch statement: It is a multi-way decision maker that tests the value of an expression against a list of integers or character constants. When a match is found, the statements associated with that constant are executed.
Syntax
switch(expression)
{
case constant1:
statement1;
break;
case constant2:
statement2;
break;
case constant3:
statement3;
break;
Default:
statement;
}