C Programming

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

C programming

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.

Dev C++
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).

 

Header file

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.

#include<stdio.h>

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.

#include<conio.h>

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.

Conio.h

Functions

•clrscr
•delline
•getch
•getche
•gotoxy
•kbhit
•wherex
•wherey
•textcolor
•textbackground

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
3. Addition
4. Odd or Even
5. Add, subtract, multiply and divide
6. Check vowel
7. Leap year
8. Add digits
9. Factorial
10. HCF and LCM
11. Decimal to binary conversion
12. ncR and nPr
13. Add n numbers
14. Swapping
15. Reverse number
16. Palindrome number
17. Print Pattern
18. Diamond
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
56. Anagrams
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*/
#include
main()
{
printf(“Hello World\n”);
getch();
}
Output of above program:
“Hello World”

Example 2 – c program to take input from user using scanf.
#include
main()
{
int number;
printf(“Enter an integer\n”);
scanf(“%d”,&number);
printf(“Integer entered by you is %d\n”, number);
getch();
}
Output:
Enter a number
5
Number entered by you is 5
Example 3 – using if else control instructions
#include
main()
{
int x = 1;
if ( x == 1 )
printf(“x is equal to one.\n”);
else
printf(“For comparison use == as = is the assignment operator.\n”);
getch();
}
Output:
x is equal to one.
Example 4 – loop example
#include
main()
{
int value = 1;
while(value<=3)
{
printf(“Value is %d\n”, value);
value++;
}
getch();
}
Output:
Value is 1
Value is 2
Value is 3
Example 5 – c program for prime number
#include
main()
{
int n, c;
printf(“Enter a number\n”);
scanf(“%d”, &n);
if ( n == 2 )
printf(“Prime number.\n”);
else
{
for ( c = 2 ; c <= n – 1 ; c++ )
{
if ( n % c == 0 )
break;
}
if ( c != n )
printf(“Not prime.\n”);
else
printf(“Prime number.\n”);
}
getch();
}
Example 6 – command line arguments
#include
main(int argc, char *argv[])
{
int c;
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]);
getch();
}
Above c program prints the number and all arguments which are passed to it.
Example 7 – Array program
#include
main()
{
int array[100], n, c;
printf(“Enter the number of elements in array\n”);
scanf(“%d”, &n);
printf(“Enter %d elements\n”, n);
for ( c = 0 ; c < n ; c++ )
scanf(“%d”, &array[c]);
printf(“Array elements entered by you are:\n”);
for ( c = 0 ; c < n ; c++ )
printf(“array[%d] = %d\n”, c, array[c]);
getch();
}
Example 8 – function program
#include
void my_function();
main()
{
printf(“Main function.\n”);
my_function();
printf(“Back in function main.\n”);
getch();
}
void my_function()
{
printf(“Welcome to my function. Feel at home.\n”);
}
Example 9 – Using comments in a program
#include
main()
{
// 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”);
getch();
}

Example 10 – using structures in c programming
#include
struct programming
{
float constant;
char *pointer;
};
main()
{
struct programming variable;
char string[] = “Programming in Software Development.”;
variable.constant = 1.23;
variable.pointer = string;
printf(“%f\n”, variable.constant);
printf(“%s\n”, variable.pointer);
getch();
}

Example 11 – c program for Fibonacci series
#include
main()
{
int n, first = 0, second = 1, next, c;
printf(“Enter the number of terms\n”);
scanf(“%d”,&n);
printf(“First %d terms of Fibonacci series are :-\n”,n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf(“%d\n”,next);
}
getch();
}
Example 12 – c graphics programming
#include
#include
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,”C:\\TC\\BGI”);
outtextxy(10,20, “Graphics source code example.”);
circle(200, 200, 50);
setcolor(BLUE);
line(350, 250, 450, 50);
getch();
closegraph( );
getch();
}
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
#include
#include
int main()
{
int c;
for ( c = 1 ; c <= 10 ; c++ )
printf(“%d\n”, c);
getch();
getch();
}
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
#include
int main()
{
int c;
/* for loop */
for ( c = 1 ; c <= 10 ; c++ )
printf(“%d\n”, c);
getch();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑