Functions in C Programming – Code Example

For simplicity we divide our code into small pieces / modules known as functions. To use functions in C programming we need to define function prototype, function definition and finally we call the function to perform that functionality. Here we will define a simple sum function, which will accept 2 numbers as an input and will return sum of these numbers. [js] #include <stdio.h> int sum(int x, int y); // this is prototype of our sum function. int main(void){ // for simplicity we divide our code into small pieces / modules known as functions // to use functions in C programming...
forward

How to use do-while loop in C Programming

  Do-while loop is also a type of repetition structure used to repeat statement(s) for certain number of times. The major difference between while and do while loop is that in do while loop the loop termination condition is checked after the execution of loop body whereas in while loop the loop termination condition is checked before the execution of loop body. Do-while loop at least runs one time no matter if the loop termination condition is true or false. Following simple program explains how to use do-while repetition structure (also called as do-while loop) to print numbers from 1-10....
forward

How to use for loop in C Programming

  For loop is also a type of repetition structure. This repetition structure is simpler in syntax as compared with other 2 loops because all four loop conditions can be defined in single line. Following simple program explains how to use for loop to print numbers from 1-10. [js] #include <stdio.h> int main(void){ // for loop is a type of repetition structure in C programming. In any loop you need to fulfill following 4 conditions for(int i=1; i<=10; i++){ // here in one line we defined control variable i (condition 1), //assigned initial value to control variable i (condition 2),...
forward

How to use while loop in C Programming

  Sometimes in C program we need to repeat statements for certain number of times. So in order to repeat statements for number of times we use repetition structure in C programming. There are 3 types of repetition structures available in C programming namely while, do-while and for. Following simple program explains how to use while repetition structure (also called as while loop) to print numbers from 1-10. [js] #include <stdio.h> int main(void){ // while loop is a type of repetition structure in C programming. In any loop you need to //fulfill following 4 conditions int i; // 1st condition:...
forward

How to get value from user in C Programming

Sometimes in a program we need to get values from user, process the values entered by user and print / output the results of that processing on the screen. In order to get values from user we use 'scanf' function which is part of the stdio.h library. Following simple program explains how to use scanf function to get 2 numbers from user, sum the 2 entered by the user and print the sum of 2 numbers on the output screen. [js] #include <stdio.h> int main(void){ // defining variables, computing their sum and printing sum on the screen int number1; //defined...
forward

How to define variables in c programming

In our previous post we discussed about how to get values from user. Sometimes we need to define variables and assign values to those variables. This simple program explains how to define variables and how to assign values to the variables in c programming. [js] #include <stdio.h> int main(void){ // defining variables, computing their sum and printing sum on the screen int number1; //defined variable named as number1 with data type integer int number2; // defined variable named as number2 with data type integer int sum; // defined variable named as sum with data type integer number1 = 5; // assigned...
forward

How to use if statement in C Programming

  If statement is called as selection structure in C Programming. If statement checks a condition and proceed further depending upon the fact that whether the condition being checked is true or false. In simple words it says "Do something when this condition is true". Following simple program explains how to use if statement to compare 2 numbers entered by the user. [js] #include <stdio.h> int main(void){ // defining variables, computing their sum and printing sum on the screen int number1; //defined variable named as number1 int number2; // defined variable named as number2 printf("Please Enter First Integer Number:n"); scanf("%d",...
forward

How to use if-else statement in C Programming

In previous post we discussed about if statement in C Programming. If-else statement called as double selection structure in C Programming. In simple words it says "Do something when a condition is true, else (when condition is false) do some other thing". Following simple c program explains how to use if-else statement in C Programming. [js] #include <stdio.h> int main(void){ int number1; //defined variable named as number1 int number2; // defined variable named as number2 printf("Please Enter First Integer Number:n"); scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1 printf("n"); printf("Please Enter Second...
forward

How to use switch statement in C Programming

  Switch statement is also called as multiple selection structure in C Programming. Switch selection structure is similar to if-else selection structure. The difference is instead of using multiple if-else statement we use 'cases' to check for multiple conditions. The 'default' case will run when no condition is true. The following simple c program explains how to use switch statement in c programming. [js] #include <stdio.h> int main(void){ int number1; //defined variable named as number1 printf("Please Enter Integer Number:n"); scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1 printf("n"); switch(number1){ // expression to...
forward

How to Output Text/String in C Programming

In C programming we use printf function (which is part of stdio.h library) to print / output some text or variable on the screen. Following simple C program outputs a text on the screen. It also explains how to move to the new line and how to add tabbed space between words. [js] #include <stdio.h> int main( void ){ // lines starting with double slashes are called as comments. //Comments are used to explain code and they are not executed by compiler printf("Welcome to C!n"); // n moves the cursor to the new line printf("Hello My Name is Junaid Hassant...
forward