C programming language
To easy learn c you must start making programs in it. As you may already know that to develop programs you need a text editor and a compiler to translate source program into machine code which can be executed directly on machine. Dev C++ IDE is a good choice, so if you are not having it installed on your computer then Download Dev c++ compiler
Now you have the tool to create programs, if you wish to look at some example codes then see c programming examples. Below I explain you to how to compile and run your programs. You may be using different compilers or operating system. I will explain you how to use Dev C++, GCC and turbo c compiler.
1. Create new file (Ctrl + N).
2. Write the code
3. Press Ctrl+F9 to compile.
4. Press Ctrl+F10 to run.
Compiling and running c programs in GCC compiler
1. Save the code in a file say “hello.c” (quotes for clarity).
2. To compile open terminal and type “gcc hello.c”.
3. To execute type “./a.out”.
You can also specify the output file name as follows:
gcc hello.c -o hello.out
hello.out is name of output file.
Turbo C compiler:-
1. Write your code in a file and save it.
2. Compile by pressing Alt + F9.
3. Execute by pressing Ctrl + F9
4. For output of this program, press (Alt + F5).
Load the file SIMPLEIO.C for our first look at a file with standard I/O. Standard I/O refers to the most usual places where data is either read from, the keyboard, or written to, the video monitor. Since they are used so much, they are used as the default I/O devices and do not need to be named in the Input/Output instructions. This will make more sense when we actually start to use them so lets look at the file in front of you.
The first thing you will notice is the second line of the file, the #include “stdio.h” line. This is very much like the #define we have already studied, except that instead of a simple substitution, an entire file is read in at this point. The system will find the file named “stdio.h” and read its entire contents in, replacing this statement. Obviously then, the file named “stdio.h” must contain valid C source statements that can be compiled as part of a program. This particular file is composed of several standard #defines to define some of the standard I/O operations. The file is called a header file and you will find several different header files on the source disks that came with your C compiler. Each of the header files has a specific purpose and any or all of them can be included in any program.
Your C compiler uses the double quote marks to indicate that the search for the “include” file will begin in the current directory, and if it not found there, the search will continue in the “include” directory as set up in the environment. It also uses the “less than” and “greater than” signs to indicate that the file search should begin in the directory specified in the environment. Most of the programs in this tutorial have the double quotes in the “include” statements. The next program uses the “” to illustrate the usage. Note that this will result is a slightly faster (but probably unnoticeable) compilation because the system will not bother to search the current directory.
conio.h header used in c programming contains functions for console input/output. Some of the most commonly used functions of conio.h are clrscr, getch, getche, kbhit etc. Functions of conio.h can be used to clear screen, change color of text and background, move text, check if a key is pressed or not and many more. conio.h file is provided by Borland turbo c compiler and GCC compiler doesn’t support it. Beginner c/c++ programmers and some books use this file but it is not recommended to use it in your software/application. To write portable c language programs do not use this file. There are also many other header files similar to this one.
C programming basics
Computer programming means giving instruction to the computer and to interact with the computer we need a language to communicate with it, there are many languages such as C, C++, Java, Python each having their own features. Let’s discuss first why we need programming suppose you are given ten numbers and asked to arrange them in ascending order then you can do it easily, but what if the numbers are say ten thousand it will be a difficult task and may take a long time and result may also contain errors, to simplify we can write a program which does this task and if the algorithm is correct then we will get result accurately and quickly, this is where programming is helpful.
If using GCC then save the code in a file say “numbers.c”, to compile the program open the terminal and enter command “gcc numbers.c”, this will compile the program and to execute the program enter command “./a.out”, do not use quotes while executing commands.
C programming tutorial
C program consists of functions and declarations or instructions given to the computer to perform a particular task. The process of writing a program involves designing the algorithm, a flowchart may also be drawn, and then writing the source code, after developing the program you need to test it and debug it if it does not meet the requirement. To make a program you need a text editor and a compiler. You can use any text editor of your choice and a compiler. C compiler converts source code into machine code that consists of zero and one only and directly executed on machine. An IDE or Integrated Development Environment provides a text editor, compiler, debugger etc. for developing programs or projects. Download Codeblocks IDE it provides an ideal environment for development. It can import Microsoft Visual C++ projects, extendable as it uses plug-ins, open source and cross platform.
A c program must have at least one function which is main, function consists of declaration and statements,
a statement is an expression followed by a semicolon, for example :-
printf(“c program examples”); //statement.
a + b; //Expression.
printf(“C is an easy to learn computer programming language.”);//statement.
To use a variable we must indicate its type whether it is an integer, float, character. C language has many built in data types and we can make our own using structures and unions. Every data type has its own size that may depend on machine for example an integer may be of 2 or 4 Bytes. Data is stored in binary form i.e. group of bits where each bit may be ‘0’ or ‘1’. Keywords such as switch, case, default, register etc. are special words with predefined meaning and can’t be used for other purposes. Memory can be allocated during compile time or at run time using malloc or calloc. C language has many features such as recursion, preprocessor, conditional compilation, portability, pointers, multi threading by using external libraries, dynamic memory allocation due to which it is used for making portable software programs and applications. Networking API’s are available using which computer users can communicate and interact with each other, share files etc. C standard library offers functions for mathematical operations, character strings and input/output and time. The process of making programs which is known as coding requires knowledge of programming language and logic to achieve the desired output. So you should learn c programming basics and start making programs. Learning data structures such as stacks, queues, linked lists etc. using c programming provides you a greater understanding as you learn everything in detail. General belief is to go for other high level languages but it’s a good idea to learn c before learning C++ or Java. C++ programming language is object oriented and it contains all the features of c language so learning c first will help you to easily learn C++ and then you can go for Java programming.
C programming PDF downloads and other software
• Dev c++ compiler
• C handbook
• Essential c
C programming books
If you are a beginner then buy anyone of first two books mentioned below and if you have previous programming experience or you know basics of c language then you can buy third one.
• Let Us C By Yashavant Kanetkar
• PROGRAMMING WITH C By Byron Gottfried, Jitender Chhabra
• The C Programming By Brian Kernighan and Dennis Ritchie
C programming examples
These programs illustrate various programming elements, concepts such as using operators, loops, functions, single and double dimensional arrays, performing operations on strings, files, pointers etc. Browse the code from simple c program to complicated ones you are looking for, every one of them is provided with output. C program download with executable files, so that you save on your computer and run programs without compiling the source code. All programs are made using c programming language and Codeblocks, most of these will work under Dev C++ compiler also.The first program prints “Hello World” on screen.
C programming codes
1. Hello world
2. Print Integer
4. Odd or Even
5. Add, subtract, multiply and divide
6. Check vowel
7. Leap year
8. Add digits
10. HCF and LCM
11. Decimal to binary conversion
12. ncR and nPr
13. Add n numbers
15. Reverse number
16. Palindrome number
17. Print Pattern
19. Prime numbers
20. Find armstrong number
21. Generate armstrong number
22. Fibonacci series
23. Print floyd’s triangle
24. Print pascal triangle
25. Addition using pointers
26. Maximum element in array
27. Minimum element in array
28. Linear search
29. Binary search
30. Reverse array
31. Insert element in array
32. Delete element from array
33. Merge arrays
34. Bubble sort
35. Insertion sort
36. Selection sort
37. Add matrices
38. Subtract matrices
39. Transpose matrix
40. Multiply two matrices
41. Print string
42. String length
43. Compare strings
44. Copy string
45. Concatenate strings
46. Reverse string
47. Find palindrome
48. String to integer
49. Delete vowels
50. C substring
51. Sort a string
52. Remove spaces
53. Change case
54. Swap strings
55. Character’s frequency
57. Read file
58. Copy files
59. Merge two files
60. List files in a directory
61. Delete file
62. Random numbers
63. Add complex numbers
64. Print date
65. Get IP address
66. Shutdown computer
C program examples
Example 1 – C hello world program
/* A very simple c program printing a string on screen*/
Output of above program:
Example 2 – c program to take input from user using scanf.
printf(“Enter an integer\n”);
printf(“Integer entered by you is %d\n”, number);
Enter a number
Number entered by you is 5
Example 3 – using if else control instructions
int x = 1;
if ( x == 1 )
printf(“x is equal to one.\n”);
printf(“For comparison use == as = is the assignment operator.\n”);
x is equal to one.
Example 4 – loop example
int value = 1;
printf(“Value is %d\n”, value);
Value is 1
Value is 2
Value is 3
Example 5 – c program for prime number
int n, c;
printf(“Enter a number\n”);
if ( n == 2 )
for ( c = 2 ; c <= n – 1 ; c++ )
if ( n % c == 0 )
if ( c != n )
Example 6 – command line arguments
main(int argc, char *argv)
printf(“Number of command line arguments passed: %d\n”, argc);
for ( c = 0 ; c < argc ; c++)
printf(“%d. Command line argument passed is %s\n”, c+1, argv[c]);
Above c program prints the number and all arguments which are passed to it.
Example 7 – Array program
int array, n, c;
printf(“Enter the number of elements in array\n”);
printf(“Enter %d elements\n”, n);
for ( c = 0 ; c < n ; c++ )
printf(“Array elements entered by you are:\n”);
for ( c = 0 ; c < n ; c++ )
printf(“array[%d] = %d\n”, c, array[c]);
Example 8 – function program
printf(“Back in function main.\n”);
printf(“Welcome to my function. Feel at home.\n”);
Example 9 – Using comments in a program
// Single line comment in c source code
printf(“Writing comments is very useful.\n”);
* Multi line comment syntax
* Comments help us to understand code later easily.
* Will you write comments while developing programs ?
printf(“Good luck c programmer.\n”);
Example 10 – using structures in c programming
struct programming variable;
char string = “Programming in Software Development.”;
variable.constant = 1.23;
variable.pointer = string;
Example 11 – c program for Fibonacci series
int n, first = 0, second = 1, next, c;
printf(“Enter the number of terms\n”);
printf(“First %d terms of Fibonacci series are :-\n”,n);
for ( c = 0 ; c < n ; c++ )
if ( c <= 1 )
next = c;
next = first + second;
first = second;
second = next;
Example 12 – c graphics programming
int gd = DETECT, gm;
outtextxy(10,20, “Graphics source code example.”);
circle(200, 200, 50);
line(350, 250, 450, 50);
For GCC users
If you are using GCC on Linux operating system then you need to modify programs. For example consider the following program which prints first ten natural numbers
for ( c = 1 ; c <= 10 ; c++ )
Above source code includes a header file and uses function getch, but this file is Borland specific so it works in turbo c compiler but not in GCC. So the code for GCC should be like
/* for loop */
for ( c = 1 ; c <= 10 ; c++ )