F

C Programming Examples



 These samples illustrate various main elements, key points, concepts such as operators, conditional statements, loops, functions, switch case, single and double dimensional arrays, and also performing operations on strings, operations on files(file handling), pointers etc. Browse the code from simple basic c program to complicated ones, as you are looking for, every one of them is provided with it's own output. You can download C programs with executable files(output file), so that you can save on your computer and run programs without compiling it's programs source-code. All programs are made using C language and Code-blocks, most of these will work under Dev c++ compiler also. Just, Download software you need to develop codes. The first program prints "Hello World" on screen.



Hello world program in c, 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, 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, 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, Get IP address, Shutdown computer & C Graphics Programs.

Ex. 1 - hello world

/* A Basic and simple c program that printing a string on screen*/
#include <stdio.h>
main()
{
printf("Hello World\n");
return 0;
}


Output:
"Hello World"

Ex. 2 - Use of "scanf" function

#include <stdio.h>
main()
{
int number1;
printf("Enter an integer\n");
scanf("%d",&number1);
printf("Integer entered by you is:- %d\n\n", number1);
return 0;
}

Output:
Enter a number
5
Number entered by you is 5


Simple if else control instructions program

#include <stdio.h>
main()
{
int x = 1;
if ( x == 1 )
printf("x is equal to one.");
else
printf("For comparison use == as = is the assignment operator.");
return 0;
}

Output:
x is equal to one.


While-loop

#include <stdio.h>
main()
{
int value = 1;
while(value<=3)
{
printf("Value is %d\n", value);
value++;
}
return 0;
}

Output:
Value is 1
Value is 2
Value is 3


c program prime-number

#include <stdio.h>
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");
}
return 0;
}


Ex. 6 - command line arguments

#include <stdio.h>
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]);
return 0;
}
Above given example prints the number and all arguments which are passed to it.


Ex. 7 - Array program

#include <stdio.h>
main()
{
int array[100], n, c;
printf("Enter the no. of elements in array\n\n");
scanf("%d", &n);
printf("Enter %d elements\n\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Array elements, which is entered by you are:\n\n");
for ( c = 0 ; c < n ; c++ )
printf("Elements: array[%d] = %d\n", c, array[c]);
return 0;
}


Ex. 8 - functions

#include <stdio.h>
void my_function();
main()
{
printf("Main function.\n");
my_function();
printf("Back in function main....@PP\n");
return 0;
}
void my_function()
{
printf("Welcome to my function. Feel at home...@AviD\n");
}


Ex. 9 - Using comments in a program

#include <stdio.h>
main()
{
// Single line comments
printf("Writing comments is very useful and helpful in understanding code.\n");

/*
* Multi line comment syntax
* Comments help us to understand program code later .
* Will you really write comments while developing programs in c?
*/

printf("Good luck c programmers...@AviD\n");
return 0;
}

Ex. 10 - structures in c

#include <stdio.h>
struct programminpad
{
float constant;
char *pointer;
};

main()
{
struct programminpad variable;
char string[] = "ProgramminIn software development(With P.Pad).\n";
variable.constant = 1.23;
variable.pointer = string;
printf("%f\n", variable.constant);
printf("%s\n", variable.pointer);
return 0;
}


Ex. 11 - Fibonacci series

#include <stdio.h>
main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the no. 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);
}
return 0;
}

Ex. 12 - c graphics

#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;
}



For GCC users :

If you are using GCC on Linux operating system then you need to modify programs. For example, just 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 program includes a header-file <conio.h> and uses a function getch, but this file is Borland specific so it works in turbo c compiler but not in GCC. Hence, 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, well.....to compile the program open the terminal and enter command GCC numbers.c(program name), this will compile your program and to execute the program enter command ./a.out .



C tutorials


C program consists of functions and declarations or instructions given to the computer to perform a particular task. And, the process of writing a program involves several factors like designing the algorithm, a flowchart(graphical presentation) may also be drawn and then writing the program source code, after developing your program, you need to test it and debug it if it doesn't meet the requirement. To make a program in c , you need a text simple editor and a compiler. You can use any text editor(like notepad, edit plus..etc) of your choice and a compiler. C compiler converts program source code into machine code which consists of zero and one only and can be directly executed on machine. An IDE (also known asIntegrated Development Environment) provides a text editor, compiler, debugger etc(on the same window). for developing programs or projects. You have to Download codeblocks IDE, which provides us an ideal environment for development. It can import Microsoft Visual C++ projects, extendable as it uses plugins, open-source and cross-platform also.


A c program must have at least one function which is main(like void main( )). A function consists of declaration &statements, a statement is an expression followed by a semicolon, for example a+b, printf("c program Samples") are expressions and a + b; and printf("C is an easy to learn computer understandable  language. You can learn with us"); are statements. Hence, to use a variable you must indicate its type whether it's an integer, double, boolean, float, character.  "C " has many built in data types and we can make our own using structures and unions. Some keywords such as switch, case, default, register (all in cProgrammng) etc are special words with predefined meaning and can't be used for other purposes. And, Memory allocated during compile time or at run time using malloc or calloc. As you know, C language has many features such as recursion, portability, preprocessor, conditional compilation, pointers, multi-threading by using external libraries, and dynamic memory allocation due to which it is used for making portable software programs and applications. Well...!! Networking API's are available using which computer users can communicate and interact with each other, share files etc. (You have know everything in c) .

C standard library offers functions for mathematical operations(for solving complex problems related to maths), character strings and input/output and time. In C language, 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 have to learn c programing basics and start making simple and complex programs. Learning data structures such as structures, unions, stacks, queues, linked lists etc. well, we can say that provides you a greater understanding as you learn everything in detail. Our, 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++ & Java 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 or .NET Plateform.


Useful Books:

Dev c++ compiler
C handbook
Essential c

Download Pdf (in C)

Posted in C Programming Examples by Aviral Dixit. No Comments
Leave a Comment