Skip to main content

C Programmng





Dennis Ritchie - founder of C languageHistory of C Language

History of C language is interesting to know. Here we are going to discuss brief history of c language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A.
Dennis Ritchie is known as the founder of c language.
It was developed to overcome the problems of previous languages such as B, BCPL etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.

LanguageYearDeveloped By
Algol1960International Group
BCPL1967Martin Richard
B1970Ken Thompson
Traditional C1972Dennis Ritchie
K & R C1978Kernighan & Dennis Ritchie
ANSI C1989ANSI Committee
ANSI/ISO C1990ISO Committee
C991999Standardization Committee




Features of C Language

C is the widely used language. It provides a lot of features that are given below.
  1. Simple
  2. Machine Independent or Portable
  3. Mid-level programming language
  4. structured programming language
  5. Rich Library
  6. Memory Management
  7. Fast Speed
  8. Pointers
  9. Recursion
  10. Extensible

1) Simple

C is a simple language in the sense that it provides structured approach (to break the problem into parts), rich set of library functionsdata types etc.

2) Machine Independent or Portable

Unlike assembly language, c programs can be executed in many machines with little bit or no change. But it is not platform-independent.

3) Mid-level prorgramming language

C is also used to do low level programming. It is used to develop system applications such as kernel, driver etc. It also supports the feature of high level language. That is why it is known as mid-level language.

4) Structured prorgramming language

C is a structured programming language in the sense that we can break the program into parts using functions. So, it is easy to understand and modify.

5) Rich Library

provides a lot of inbuilt functions that makes the development fast.

6) Memory Management

It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at any time by calling the free() function.

7) Speed

The compilation and execution time of C language is fast.

8) Pointer

C provides the feature of pointers. We can directly interact with the memory by using the pointers. We can use pointers for memory, structures, functions, array etc.

9) Recursion

In c, we can call the function within the function. It provides code reusability for every function.

10) Extensible

C language is extensible because it can easily adopt new features.


First C Program

Before starting the abcd of C language, you need to learn how to write, compile and run the first c program.
To write the first c program, open the C console and write the following code:
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main(){  
  4. printf("Hello C Language");  
  5.   
  6. getch();  
  7. }  
#include <stdio.h> includes the standard input output library functions. The printf() function is defined in stdio.h .
#include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.
void main() The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value.
printf() The printf() function is used to print data on the console.
getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.
c program

How to compile and run the c program

There are 2 ways to compile and run the c program, by menu and by shortcut.

By menu

Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.

By shortcut

Or, press ctrl+f9 keys compile and run the program directly.
You will see the following output on user screen.
c program output
You can view the user screen any time by pressing the alt+f5 keys.
Now press Esc to return to the turbo c++ console.

clear screen by clrscr() function

If you run the c program many times, it will append the output in previous output. But, you can call clrscr() functionto clear the screen. So it will be better for you to call clrscr() function after the main method as given below:
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main(){  
  4. clrscr();  
  5. printf("Hello C Language");  
  6.   
  7. getch();  
  8. }  



Flow of C Program

The C program follows many steps in execution. To understand the flow of C program well, let us see a simple program first.
File: simple.c
  1. #include <stdio.h>  
  2. void main(){  
  3. printf("Hello C Language");  
  4. }  
Let's try to understand the flow of above program by the figure given below.
C program flow
1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert preprocessor directives into their respective values. The preprocessor generates an expanded source code.
2) Expanded source code is sent to compiler which compiles the code and converts it into assembly code.
3) The assembly code is sent to assembler which assembles the code and converts it into object code. Now a simple.obj file is generated.
4) The object code is sent to linker which links it to the library such as header files. Then it is converted into executable code. A simple.exe file is generated.
5) The executable code is sent to loader which loads it into memory and then it is executed. After execution, output is sent to console.


printf scanf in C

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.
The syntax of printf() function is given below:
  1. printf("format string",argument_list);  
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the console.
  1. scanf("format string",argument_list);  

Variables in C

variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
  1. type variable_list;  
The example of declaring variable is given below:
  1. int a;  
  2. float b;  
  3. char c;  
Here, a, b, c are variables and int,float,char are data types.
We can also provide values while declaring the variables as given below:
  1. int a=10,b=20;//declaring 2 variable of integer type  
  2. float f=20.8;  
  3. char c='A';  

Data Types in C

A data type specifies the type of data that a variable can store such as integer, floating, character etc.




There are 4 types of data types in C language.
TypesData Types
Basic Data Typeint, char, float, double
Derived Data Typearray, pointer, structure, union
Enumeration Data Typeenum
Void Data Typevoid


C if else Statement

The if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.
There are many ways to use if statement in C language:
  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if

If Statement

The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below:
  1. if(expression){  
  2. //code to be executed  
  3. }  

Flowchart of if statement in C

if statement in c
Let's see a simple example of c language if statement.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. if(number%2==0){  
  11. printf("%d is even number",number);  
  12. }  
  13.   
  14. getch();  
  15. }  

Output

enter a number:4
4 is even number
enter a number:5

If-else Statement

The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below:
  1. if(expression){  
  2. //code to be executed if condition is true  
  3. }else{  
  4. //code to be executed if condition is false  
  5. }  

Flowchart of if-else statement in C

if-else statement in c
Let's see the simple example of even and odd number using if-else statement in C language.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. if(number%2==0){  
  11. printf("%d is even number",number);  
  12. }  
  13. else{  
  14. printf("%d is odd number",number);  
  15. }  
  16. getch();  
  17. }  

Output

enter a number:4
4 is even number
enter a number:5
5 is odd number

If else-if ladder Statement

The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:
  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }  

Flowchart of else-if ladder statement in C

if-else-if ladder statement in c
The example of if-else-if statement in C language is given below.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. if(number==10){  
  11. printf("number is equals to 10");  
  12. }  
  13. else if(number==50){  
  14. printf("number is equal to 50");  
  15. }  
  16. else if(number==100){  
  17. printf("number is equal to 100");  
  18. }  
  19. else{  
  20. printf("number is not equal to 10, 50 or 100");  
  21. }  
  22. getch();  
  23. }  

Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50



C Switch Statement

The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
  1. switch(expression){    
  2. case value1:    
  3.  //code to be executed;    
  4.  break;  //optional  
  5. case value2:    
  6.  //code to be executed;    
  7.  break;  //optional  
  8. ......    
  9.     
  10. default:     
  11.  code to be executed if all cases are not matched;    
  12. }    


Rules for switch statement in C language

1) The switch expression must be of integer or character type.
2) The case value must be integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
  1. int x,y,z;  
  2. char a,b;  
  3. float f;  
Valid SwitchInvalid SwitchValid CaseInvalid Case
switch(x)switch(f)case 3;case 2.5;
switch(x>y)switch(x+2.5)case 'a';case x;
switch(a+b-2)case 1+2;case x+2;
switch(func(x,y))case 'x'>'y';case 1,2,3;

Flowchart of switch statement in C

flow of switch statement in c
Let's see a simple example of c language switch statement.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. switch(number){  
  11. case 10:  
  12. printf("number is equals to 10");  
  13. break;  
  14. case 50:  
  15. printf("number is equal to 50");  
  16. break;  
  17. case 100:  
  18. printf("number is equal to 100");  
  19. break;  
  20. default:  
  21. printf("number is not equal to 10, 50 or 100");  
  22. }  
  23. getch();  
  24. }  

Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

C Switch statement is fall-through

In C language, switch statement is fall through, it means if you don't use break statement in switch case, all the case after matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given below.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. switch(number){  
  11. case 10:  
  12. printf("number is equals to 10\n");  
  13. case 50:  
  14. printf("number is equal to 50\n");  
  15. case 100:  
  16. printf("number is equal to 100\n");  
  17. default:  
  18. printf("number is not equal to 10, 50 or 100");  
  19. }  
  20. getch();  
  21. }  

Output

enter a number:10
number is equals to 10
number is equals to 50
number is equals to 100
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
number is equals to 100
number is not equal to 10, 50 or 100

C Loops

The loops in C language are used to execute a block of code or a part of the program several times.
In other words, it iterates a code or group of code many times.

Why use loops in C language?

Suppose that you have to print table of 2, then you need to write 10 lines of code.
By using the loop statement, you can do it by 2 or 3 lines of code only.

Advantage of loops in C

1) It saves code.
2) It helps to traverse the elements of array (which is covered in next pages).


Types of C Loops

There are three types of loops in C language that is given below:
  1. do while
  2. while
  3. for

do-while loop in C

It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.
It is better if you have to execute the code at least once.
The syntax of do-while loop in c language is given below:
  1. do{  
  2. //code to be executed  
  3. }while(condition);  
Flowchart and Example of do-while loop

while loop in C

Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.
It is better if number of iteration is not known by the user.
The syntax of while loop in c language is given below:
  1. while(condition){  
  2. //code to be executed  
  3. }  
Flowchart and Example of while loop

for loop in C

Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.
It is good if number of iteration is known by the user.
The syntax of for loop in c language is given below:
  1. for(initialization;condition;incr/decr){  
  2. //code to be executed  
  3. }  


do while loop in C

To execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true.
In do while loop, statement is given before the condition, so statement or code will be executed at lease one time. In other words, we can say it is executed 1 or more times.
It is better if you have to execute the code at least once.

do while loop syntax

The syntax of C language do-while loop is given below:
  1. do{  
  2. //code to be executed  
  3. }while(condition);  

Flowchart of do while loop

flowchart of do while loop in c language


do while example

There is given the simple program of c language do while loop where we are printing the table of 1.
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1;  
  5. clrscr();    
  6.   
  7. do{  
  8. printf("%d \n",i);  
  9. i++;  
  10. }while(i<=10);  
  11.     
  12. getch();    
  13. }    

Output

1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using do while loop

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,number=0;  
  5. clrscr();    
  6.   
  7. printf("Enter a number: ");  
  8. scanf("%d",&number);  
  9.   
  10. do{  
  11. printf("%d \n",(number*i));  
  12. i++;  
  13. }while(i<=10);  
  14.     
  15. getch();    
  16. }    

Output

Enter a number: 5
5
10
15
20
25
30
35
40
45
50
Enter a number: 10
10
20
30
40
50
60
70
80
90
100

Infinitive do while loop

If you pass 1 as a expression in do while loop, it will run infinite number of times.
  1. do{  
  2. //statement  
  3. }while(1);  

















Comments

Popular posts from this blog

CSS Tutorial step by step

CSS   Introduction Cascading Style Sheets   (CSS) is a   style sheet language   used for describing the   look and formatting   of a document written in a markup language . While most often used to style   web pages   and user interfaces written in   HTML   and   XHTML . CSS is a cornerstone specification of   the web   and almost all web pages use CSS style sheets to describe their presentation. CSS is designed primarily to enable   the separation of document content from document presentation , including elements such as the   layout ,   colors , and   fonts .   This separation can improve content   accessibility , provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content (such as by allowing for   table less web design ). It   obvia...

TypingPad

TypingPad is a simple  text editor  for  Microsoft Windows  and a basic text-editing program which enables computer users to create documents. It was first released  in 2018, and has been included in  versions of  Microsoft Windows .  Features TypingPad  is a common text-only ( plain text ) editor. The resulting files—typically saved with the  .txt  extension—have no format tags or styles, making the program suitable for editing system files to use in a  DOS  environment and, occasionally, source code for later  compilation or  execution , usually through a  command prompt . It is also useful for its negligible use of system resources; making for quick load time and processing time, especially on under-powered hardware. TypingPad supports both left-to-right and right-to-left based languages. Historically,  TypinfPad offers only the most basic text manipulation functions, such as finding text. ...

XML Engine Tutorial

Overview • XML basics and Libname engine introduction • Effectively reading XML files • Introduction to the XML Mapper •Writing XML files using SAS • Common problems processing XML files What is XML? • XML is a set of rules used for defining &modeling structures • XML is extensible & customizable  Its greatest strength  Its greatest weakness XML Basics- Well Formed Files Document has a single root element • Elements nest properly • No tag omission (close what you open) • Attributes must be quoted • Special characters < > and & must always be escaped XML Basics- Well Formed Files • XML is case sensitive Anatomy of an XML file <?xml version="1.0"?> <workorder priority="high"datedue="09/30/2001"> <submitter> <name first="Jennifer" last="Kyrnin" /> <email>html.guide@about.com</email> <account number="11001100" /> Container or root X...