C Programming Examples - Programming Simplified
C Programming Examples - Programming Simplified
C Programming Examples - Programming Simplified
http://www.programmingsimplified.com/c-program-examples
Home
www.Alibaba.com
Find Quality Products from Verified Suppliers. Get a Live Quote Now!
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. Download software you need to develop codes. The first program prints "Hello World" on screen.
1 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
Hello world Print Integer Addition Odd or Even Add, subtract, multiply and divide Check vowel Leap year Add digits Factorial HCF and LCM Decimal to binary conversion ncR and nPr Add n numbers Swapping Reverse number Palindrome number Print Pattern Diamond Prime numbers Find armstrong number Generate armstrong number Fibonacci series Print floyd's triangle Print pascal triangle Addition using pointers Maximum element in array Minimum element in array Linear search Binary search Reverse array Insert element in array Delete element from array
2 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
Merge arrays Bubble sort Insertion sort Selection sort Add matrices Subtract matrices Transpose matrix Multiply two matrices Print string String length Compare strings Copy string Concatenate strings Reverse string Find palindrome String to integer Delete vowels C substring Sort a string Remove spaces Change case Swap strings Character's frequency Anagrams Read file Copy files Merge two files List files in a directory Delete file Random numbers Add complex numbers Print date
3 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
Example 1 - C hello world program /* A very simple c program printing a string on screen*/
#include <stdio.h> main() { printf("Hello World\n"); return 0; }
Output of above program: "Hello World" Example 2 - c program to take input from user using scanf
#include <stdio.h> main() { int number; printf("Enter an integer\n"); scanf("%d",&number); printf("Integer entered by you is %d\n", number); return 0; }
4 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
Output: Value is 1
5 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
6 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
Above c program prints the number and all arguments which are passed to it. Example 7 - Array program
#include <stdio.h> 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]); return 0; }
7 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
printf("Back in function main.\n"); return 0; } void my_function() { printf("Welcome to my function. Feel at home.\n"); }
8 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
{ 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); return 0; }
9 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
#include <graphics.h> #include <conio.h> 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( ); return 0; }
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 <stdio.h> #include <conio.h> int main() { int c; for ( c = 1 ; c <= 10 ; c++ ) printf("%d\n", c); getch(); return 0; }
Above source code includes a header file <conio.h> and uses function getch, but this file is Borland specific so it works in turbo c
10 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
compiler but not in GCC. So the code for GCC should be like
#include <stdio.h> int main() { int c; /* for loop */ for ( c = 1 ; c <= 10 ; c++ ) printf("%d\n", c); return 0; }
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 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 a + b, printf("c program examples") are expressions and a + b; and printf("C is an easy to learn computer programming language."); are statements. 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
11 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
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 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
12 of 13
10/18/2013 9:58 PM
http://www.programmingsimplified.com/c-program-examples
mobogenie.com/download-software
13 of 13
10/18/2013 9:58 PM