for(initialization; condition; increment/decrement)
{
body of the loop;
}
All these initialization, condition, increment/decrement and statements are usual C expressions. So the general form can be re-written as:
for(expression; expression; expression)
{
set of expressions;
}
#include<stdio.h>int main(){int k;for(k=1;k<=5;k++)printf("%d\n", k);return 0;}
while(condition)
{
body of the loop;
}
#include<stdio.h>int main(){int k;k=1;while(k<=5){printf("%d\n", k);k++;}}
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time. Possibly why C seems so popular is because it is reliable, simple and easy to use. Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. However, there is a close analogy between learning English language and learning C language. The classical method of learning English is to first learn the alphabets used in the language, then learn to combine these alphabets to form words, which in turn are combined to form sentences and sentences are combined to form paragraphs. Learning C is similar and easier. Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed, and finally how are these combined to form an instruction.
But, before digging these topics deeper, we have to take a short look on a sample program in C, how it looks like. What are the general components and how it will work to get familiar with programming in C, all these can be seen from below program which prints “Hello World!” on screen.
C Program:-
#include<stdio.h>
int main()
{
printf("Hello World!");
//This is a comment. Above line will just print Hello World! on the screen.
return 0;
/* This is an example of multiline comment,
instead of one line you can write multi-line comment which
is used to provide readability of the code.
These must start end with proper constructs as shown here. */
}
Output of above C Program:-
After writing your program you must compile it before executing. We can compile the programs using compilers, there are a lot of compilers for C language today like Turbo C, gcc etc. gcc (GNU Compiler Collection) is the most popular compiler now a days. It is free and open source so you can download and use it freely.