Category Archives: C Tutorial

Passing array to function

In this tutorial, you will learn passing array to function. You’ll learn to passing both one-dimensional and multidimensional arrays to a function. In C programming, several times we may require passing more than one values to a function parameter. With array we can pass multiple values to function parameter.

Suppose you have a function to sort the given parameters in ascending order. This would require you to pass all values as actual parameters to function. Here, instead of declaring n number of different variables and then passing them to function, we can have an array of required size and pass it to the function. This way we can pass any number of parameters to function using array. This will also resolve the complexity, since function will now work with any number of values. You can pass an array to a function in following two ways:

  1. call by value
  2. call by reference

Passing array to function in c by value

In this method, value of the actual parameter is copied into the function’s formal parameter. And these two types of parameters are stored in different memory locations.

Passing array to function in c by reference

In this method, actual and formal parameters refer to the same memory locations. Any change made into the actual parameter will reflect formal parameters.

Multi-dimensional Array

Multi-dimensional array

Multi-dimensional arrays is nothing just an array of arrays. We can declare a multidimensional array, as below:

Syntax:-

Example:-

Output:

 

C If Else If Ladder Statement

The c if else if ladder statement is an extension of the if-else statement.

The if-else if ladder statement allows us to add an alternative set of test conditions in the if-else statement using the else-if statement.

if-else if ladder statement Syntax

Syntax:-

 

C Tokens

What Is Token

The smallest individual entity used in a c program is known as a token. Tokens are the various elements in a program; identified by the compiler. It can be anything that is meaningful to the compiler. Compiler parses the program and extracts the individual tokens. The tokens are the important building block of the C programming language. We can not create a c program without using c tokens.

Types of Token In C

C supports the following six types of tokens:

  • Keywords
  • Identifiers
  • Strings
  • Operators
  • Constants
  • Special Symbols

c-tokens-type

Keywords

There is a set of reserved words that you cannot use as an identifier. These words are known as “reserved words” or “Keywords”. Keywords are standard identifiers and their function and behavior are predefined by the compiler. There are 32 keywords in the C language.

Example:-

Identifiers

An identifier is a name given to program elements such as functions, variables, structures, unions, arrays, etc. It is a sequence of letters, digits, and underscores. The keywords can not be used as identifiers. The identifier must differ in spelling and case from keywords.

Example:-

Strings

String refers to a series or sequence of characters such as letters, numbers, and special characters. In C programming, string is enclosed in double-quotes(“”) and characters are enclosed in single quotes(”).

Example:-

Operators

The operator is a special symbol that is used to carry out some specific operation on its operand. In c programming, we have a rich set of built-in operators to carry out a different typess of operations. There are operators for assignment, arithmetic operations, logical operations, comparison operations, etc.

Example:-

Constants

Constants refer to immutable values. These are basically literals whose values cannot be modified or changed during the execution of the program.

Example:-

Special Symbols

The special symbols have some special meaning associated with them. They cannot be used for some other purpose except a special purpose attached to them.

Example:-

C Comments

C Comments

In c programming, comments are a set of statements that are not executed by the compiler. The use of comments makes it easy for humans to understand the source code. Usually, comments give you an inside or explanation about the variable, method, class, or any statement that exists in the source code. The comment statements are ignored during the execution of the program. In this tutorial I’ll explain you how to use comments properly with syntax and examples.

Types of Comments In C

In C programming, there are 2 types of comments.

  • Single Line Comment
  • Multi-Line Comment

types-of-comments-in-c

It is recommended to choose a commenting style and use it consistently throughout your source code. Doing this makes your code more readable.

Single line Comment

A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character. This can be used to comment out everything until a line break. Let’s see an example of a single line comment in C.

Syntax:-

General syntax of single line comments is as follows:

Example:-

Simple example to demonstrate the use of single line comments is as follows:

Output:-

Now when we run the above program we see the following output, here the comment is completely ignored by compiler.

Multi-line Comment

You can also create a comment that spans multiple lines. If you want to comment multiple lines then you can do it using /* and */. The compiler ignores everything from /* to */. Let’s see an example of a multi-line comment in C.

Syntax:-

General syntax of multi-line comments is as follows:

Example:-

Simple example to demonstrate the use of multi-line comments is as follows:

Output:-

Now when we run the above program we see the following output, here the comment is completely ignored by compiler.

Uses of multi-line comments are as follows:

  • Useful for commenting out a section of code
  • Cannot be nested within other multi-line comments

C Go To Statement

C goto statement

The c goto statement is used to alter the normal execution of a program and transfer control to a labeled statement in the same program. We can have multiple goto and label statements in a c program. The goto statement is followed by a label name. The label is an identifier, which can be any plain text. It can be set anywhere in a c program. The label can be defined above or below to go to the statement. When a goto statement is encountered the compiler transfers the control to a label: specified with a goto statement. Then compiler begins execution from there.

GoTo Statement Syntax

Below is the general syntax of the goto statement in c programming:

Syntax:-

Program Structure:-

goto Statement Example

Below is a simple example to demonstrate the use of the goto statement in c programming:

Example:-

Output:-

c-goto-statement

C Continue Statement

C Continue Statement

It will give you a way to skip over the current iteration of any loop. When a continuous statement is encountered in a loop, the rest of the statements in the loop body for the current iteration ends and returns the program execution to the very first statement in the loop body. It does not terminate the loop rather continues with the next iteration.

Continue Statement Flow Diagram

c_continue_flow_diagram

Continue Statement Syntax

Below is the general syntax of the continue statement in c programming:

Syntax:-

Continue Statement Example

Below is a simple example to demonstrate the use of the continue statement in c programming:

Example:-

When we run the above C program, we will see the following output –

Output:-

c-continue-statement

As you can see when ctr == 5, the continue statement is executed which causes the current iteration to end, and the control moves on to the next iteration.

C Break Statement

The c break statement inside any loop gives you a way to break or terminate the execution of the loop containing it and transfers the execution to the next statement following the loop. It is always used with a c if-else statement.

Break Statement Flowchart In C

c-break-statement-flowchart

Break Statement Inside Loops

C Break Statement Flow Diagram

Syntax Of Break Statement

Below is the general syntax of break statement in c programming:

Syntax:-

Break Statement Example

Below is a simple example to demonstrate the use of break statement in c programming:

Example:-

In the above program, the variable count is initialized as 0. Then a while loop is executed as long as the variable count is less than 10. Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1). Next, we have an if statement that checks the variable count is equal to 5, if it returns TRUE causes the loop to break or terminate. Within the loop, there is a printf statement that will execute with each iteration of the while loop until the loop breaks. Then, there is a final printf statement outside of the while loop. When we above c program, our output will be as follows –

Output:-

c-break-statement

C Nested Loop

C Nested Loop

In c programming, loop within a loop is called a nested loop. In this tutorial, we will learn about nested loops in c programming with examples.

What Is Nested Loop

When there is a loop statement inside another loop statement then it is known as a nested loop statement. It is possible to have a loop statement as a part of another loop statement in c programming. There can be any number of loops inside a loop. It is not necessary that the loop must be nested inside the same type of loop. There can be any type of loop nested inside the body of another loop, such as a while loop or for a loop. For example, we can have an inner for loop inside the outer while loop.

Syntax:-

How nested loop works

For every pass of the outer loop triggers the inner loop. This repeats until the outer loop completes all iterations. For each iteration of the outer loop inner loop execute all its iteration. It is possible to have a break statement within either the inner or outer loop to interrupt this process. A total number of iterations is equal to the number of iterations of the outer loop multiplied by the number of iterations in an inner loop.

Nested for Loop

When there is for loop inside another for loop statement then it is known as a nested for loop statement. It is possible to have a for loop statement as a part of another for loop statement. Below is the general syntax of nested for loop statements in c programming:

Syntax:-

Below is a simple example to demonstrate the use of nested for loop statement in c programming. In this example, we are using a for loop inside a for a loop.

Example:-

Output:-

c-nested-for-loop-example

Nested while Loop

When there is a while loop inside another while loop statement then it is known as a nested while loop statement. It is possible to have a while loop statement as a part of another while loop statement. Below is the general syntax of nested while loop statement in c programming:

Syntax:-

Below is a simple example to demonstrate the use of a nested while loop statement in c programming:

Example:-

Output:-

c-nested-while-loop-example

Nested do-while Loop

When there is a do-while loop inside another do while loop statement then it is known as a nested do-while loop statement. It is possible to have a do-while loop statement as a part of another do while loop statement. Below is the general syntax of nested do…while loop statement in c programming:

Syntax:-

Below is a simple example to demonstrate the use of a nested do-while loop statement in c programming:

Example:-

Output:-

c-nested-do-while-loop-example

When To Use a Nested Loop

Nested loops are handy when you want to loop through multi-dimensional data. For example, it is easy to traverse a multi-dimensional array using nested loops.

Break In Nested loop

The break statement is used to exit out of the loop. If the break statement is used inside a nested loop, it will terminate the innermost loop.

Continue In the Nested loop

The continue statement is used to skip the current iteration and move to the next iteration. When a continue statement is encountered inside a loop, it skips all the statements below it and jumps the control to the next iteration.

C Do While Loop

C do-while Loop

In c programming, the do-while loop executes a block of statements inside the loop body first and then tests the condition. The next iteration executes only if the test condition is true.

The do-while loop is much similar to the while loop with one major difference, in the do-while loop block of statements inside the loop body executes at least once.

Why We Need a do-while loop

The main difference between a while loop and a do-while loop is that a while loop is entry-controlled loop. On the other hand, the do-while loop is an exit-controlled loop. In a while loop, if the test condition becomes false in the first iteration then the loop body is not executed at all. But the do-while loop is somewhat different from the while loop. The do-while loop executes the loop body at least once even if the test condition is false for the first iteration. The do-while loop is mainly used where we want to execute the loop at least once.

C do while Loop Flowchart

The following flowchart image illustrates the working of the do-while loop in C programming:

c-do-while-loop-flowchart-diagram

Do while Loop Syntax

Below is the general syntax of the do-while loop in c programming:

Syntax:-

Here, the loop executes a block of statements inside the loop body first and then tests the condition provided along with the while keyword for the next iteration and executes next only if the condition is true. Condition is a Boolean expression that results in either True or False, if it results in True then statements inside the loop body are executed and the condition is evaluated again. This process is repeated until the condition is evaluated as False. If the condition results in False then the loop is terminated and control is transferred to the next statement.

Do while Loop Example

Below is a simple example to demonstrate the use of a do-while loop in c programming:

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have loop body inside the pair of curly brackets {} after do keyword, statements inside loop body are executed first then control is passed to the condition ctr <= maxCtr provided along with while keyword for next iteration. If the test condition returns True, then control is passed again to the loop body for the next iteration. If the condition results in False then the loop is terminated and control is transferred to the next statement. Inside the loop body, we have incremented the ctr value by one for each iteration. When we run the above c program, we see the following output –

Output:-

c-do-while-loop

Important Difference Between a while and do-while Loop

The main difference between a while loop and a do-while loop is that a while loop is an entry-controlled loop. On the other hand, the do-while loop is an exit-controlled loop. The while loop checks the test condition before executing the loop body. While the do-while loop checks the test condition after executing the statements inside the loop body. Here, we have listed some of the important Difference Between a while and do-while Loops.

# while do-while
Syntax while ( condition) {
statements; //body of loop
}
do{
.
statements; // body of loop.
.
} while( Condition );
Condition In the ‘while’ loop the test condition appears at the start of the loop. In the ‘do-while’ loop the test condition appears at the end of the loop.
Iterations The iterations do not occur if, the test condition for the first iteration is false. The iteration occurs at least once even if the test condition is false for the first iteration.
Type Entry-controlled loop Exit-controlled loop
Semi-colon Not used Used at the end of the loop

C while Loop

C While Loop

The c while loop will execute a block of statements as long as a test expression is true. While loop is useful when the number of iterations can not be predicted beforehand. The while loop evaluates test expression at beginning of each pass.

The while loop is an entry-controlled loop. In a while loop, the test condition is evaluated before processing the loop body. If a condition is true then and only then the body of a loop is executed. Once the loop body is executed then control goes back to the beginning, and the condition is checked again if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

C While Loop Flowchart

The following flowchart image illustrates the working of the while loop in C programming:

c-while-loop-flowchart-diagram

While Loop Syntax

Below is the general syntax of while loop in c programming:

Syntax:-

Working Of While Loop

Here, while loop firstly evaluates the Condition inside parentheses (). The Condition is a Boolean expression that results in either True or False. If the condition results in True then statements inside the loop body are executed and the condition is evaluated again. This process is repeated until the condition is evaluated as False. If the condition results in False then execution is skipped from the loop body and the while loop is terminated.

After exiting the loop, the control goes to the statements immediately after the loop. The loop body can contain one or more statements. If it contains only one statement, then the curly braces are not compulsory. It is a good practice to use the curly braces even we have a single statement inside the loop body.

While Loop Example

Below is a simple example to demonstrate the use of the while loop in c programming:

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have a while loop, that checks the condition ctr <= maxCtr for each of the iterations. If the test condition returns True, the statements inside the loop body are executed and if the test condition returns False then the while loop is terminated. Inside the loop body, we have incremented the ctr value by one for each iteration. When we run the above C program, we see the following output –

Output:-

c-while-loop

Properties of while loop

  • A conditional expression is used to check the condition. The statements defined inside the while loop will repeatedly execute until the given condition fails.
  • The condition will be true if it returns 0. The condition will be false if it returns any non-zero number.
  • In a while loop, the condition expression is compulsory.
  • Running a while loop without a body is possible.
  • We can have more than one conditional expression in the while loop.
  • If the loop body contains only one statement, then the braces are optional.

C for Loop

C for Loop

The for loop executes a block of code repeatedly until a given condition evaluates to false. The C for loop is similar to the Java and C++ for loop. The for loop is used to repeat a block of code known number of times. When we want to execute a block of code for fixed number of times, it is recommended to use for loop. The for loop takes a variable as iterator and assign it with an initial value. It iterate through the loop body as long as the test condition is true. Once the loop statements are executed for current iteration, the iterator is updated with new value. If the test condition is still valid, we loop another time. When test condition return a false value, then the for loop is ended.

For Loop flowchart

The flowchart image illustrates the working of for loop statement:

c-for-loop-flow-chart-diagram

for Loop Syntax

The general for loop syntax in c programming is as follows:

Syntax :-

Above is the general syntax of a for loop. We just initialize iterator, check condition and then increment or decrement iterator value. The for loop has three components separated by semicolons.

initialization :- In this section is used to declare and initialize the loop iterator. This is the first statement to be executed and executed only once at the beginning.

condition :- Next, the test condition is evaluated before every iteration, and if it returns a true value then the loop body is executed for current iteration. If it returns a false value then loop is terminated. Then control jumps to the next statement just after the for loop.

incr/decr :- Once, the loop body is executed for current iteration then the incr/decr statement is executed to update the iterator value and if condition is evaluated again. If it returns a true value then the loop body is executed another time. If test condition returns a false value then loop is terminated and control jumps to the next statement just after the for loop.

How for Loop Works In C

The for loop is an entry-controlled loop. In for loop, test condition is evaluated before processing the loop body. If a condition is true then and only then the body of a loop is executed. Once the loop body is executed then control goes back to the beginning, and the condition is checked again and if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

Why We Need For Loop?

  • Like any other loop “for loop” can also be used to execute the block of code over and over again.
  • The advantage of a for loop is that we know exactly how many times the loop will be executed before the loop starts.

For Loop Example In c

Below is the simple for loop example to demonstrate the use of for loop in c programming:

Example:-

Here, we have initialized ctr with initial value of 1, in the next we checks the condition ctr <= 5 for each of the iteration. If the test condition returns True, the statements inside loop body are executed and if test condition returns False then the for loop is terminated. In incr/decr section we have incremented the ctr value by one for each if the iteration. When we run the above C program, we see the following output –

Output:-

c-for-loop

Various Forms of for loop In C

Let’s understand the various possible forms of for loop in c programming. Various forms of for loop are as follows:

for loop without initialization

Initialization can be skipped from a for loop. Even though we can skip initialization part but semicolon (;) before test condition is must. If you missed semicolon (;), then you will get a compilation error.

Example:-

for loop without increment or decrement

It is also possible to skip the increment or decrement part in for loop. In this case semicolon (;) is must after test condition. In this case the increment or decrement part is done inside the loop body.

Example:-

for loop without initialization and without increment

In this for loop form, the loop variable can be initialized before the loop and can be increment or decremented inside the loop.

Example:-

Multiple Initialization In for Loop

It is also possible to initialize multiple variables in for loop.

Example:-

Multiple test conditions can be joined with logical AND (&&) and/or OR (||) operators. It has two variables in increment part. Should be separated by comma.