Hands On ‘C’ Programming Language

C‘ is a General Purpose Programming Language developed by Dennis Ritchie at AT&T Bell Labs. It was designed to be Structured programming Language. ‘CProgramming language was developed out of B programming language, which initially was developed from BCPL (Basic CPL or Basic Combined Programming Language). ‘CProgramming language was designed for specific purpose – to Design UNIX operating system and to be useful to allow busy programmers to get things done. ‘C‘ went so popular that it spread widely out of Bell Labs and programmers all over the world start using this language to write program of every kind. ‘C‘ is neither Low-Level Language nor it is High-Level Language, it lies somewhere in between and to be true – “C is a Middle Level Language.”

Learn C Programming

Hands On C Programming

In today’s world with so many High-Level Programming Language to choose from like Perl, PHP, Java, etc why should one choose ‘C’? OK the reason of choosing ‘C‘ programming Language over other Programming languages are its –

  1. Robust.
  2. Rich set of built-in functions.
  3. Provides ground for ‘Low Level Programming‘ with features of ‘High-Level Language‘.
  4. Suitable for writing System Software, Application Software, Business or any other kind of software’s.
  5. Programs written in ‘C‘ are efficient and fast, with the availability of variety of data types and powerful operators.
  6. Popular among professional Programmers with the availability of a number of compilers for almost all the architecture and platforms.
  7. Portability.
  8. Program written in ‘C‘ is simple easy to understand and extensible with the availability of various function supported by ‘C‘ library.
  9. C‘ has influenced a number of computer programming languages including C#, Java, JavaScript, Perl, PHP, Python, etc.

Perhaps by now, you would have learnt why programming courses start with ‘C‘ language irrespective of what programming language you opted for learning.

You know that 90% of the world’s supercomputer are running Linux. Linux is running in space, on your phone and wrist watch, desktop and every other known machine. Most of the UNIX/Linux kernel consist of codes written in C programming Language. And the Linux 3.2 release had more than 15 million lines of codes. can you imagine how powerful, ‘C‘ actually is?

A Single ounce of practical, weights more than tons of Theory, and best way to learn code is to start programming yourself. (Don’t copy and paste codes, write it yourself, learn for mistakes…)

Anatomy

#includes : It tells the compiler where to look for other bits of codes that don’t lies in the program. They are normally “.h” or header files that contain function prototypes. Literally the content of #include is copied into the program file before compilation.

#include <file> (System Defined)
#include "file" (User Defined)

The main function is literally the main part of the code. There can only be one main function in the final compiled program. The code inside the main function is executed sequentially, one line at a time.

 int main(void) 
        {..your code here..}

Fine! Now we will be writing a simple program to add 3 numbers.

#include <stdio.h>

int main()

{

int a,b,c,add;

printf("Enter the first Number");

scanf("%d",&a);

printf("Enter the second Number");

scanf("%d",&b);

printf("Enter the third number");

scanf("%d",&c);

add=a+b+c;

printf("%d + %d + %d = %d",a,b,c,add);

return 0;

}

Save it as first_prog .c and on Linux compile it as.

# gcc -o first_prog first_prog.c

Run it as.

# ./first_prog

Note: C is not case sensitive, programming language. For More information on how to compile a C program refer:

  1. How to Compile a C Program – (See Command :38)

In the above program

  1. int a,b,c,add – are the variables.
  2. Printf – prints anything and everything within quotes as it is.
  3. Scanf – Accepts input from user and store the value to memory location.
  4. %d – signifies integer data type.

Now you can write programs capable of addition, subtraction, multiplication, and division for any number. Yes you have to use “%f” for float value and not “%d“.

If you get successful in implementing both the integer and float values you can program complex mathematical problems.

Calculate Power of 2

Compile and Run it as described above.

#include <stdio.h>

#define N 16

#define N 16

int main(void) {

int n; /* The current exponent */

int val = 1; /* The current power of 2 */

printf("\t n \t 2^n\n");

printf("\t================\n");

for (n=0; n<=N; n++) {

printf("\t%3d \t %6d\n", n, val);

val = 2*val;

}

return 0;

}
Finding the Factors of a Number
#include <stdio.h>

int main(void) {

int n,

lcv,

flag; /* flag initially is 1 and becomes 0 if we determine that n

is not a prime */

printf("Enter value of N > ");

scanf("%d", &n);

for (lcv=2, flag=1; lcv <= (n / 2); lcv++) {

if ((n % lcv) == 0) {

if (flag)

printf("The non-trivial factors of %d are: \n", n);

flag = 0;

printf("\t%d\n", lcv);

}

}

if (flag)

printf("%d is prime\n", n);

}
Fibonacci Series
#include <stdio.h>

int main(void) {

int n;

int i;

int current;

int next;

int twoaway;

printf("How many Fibonacci numbers do you want to compute? ");

scanf("%d", &n);

if (n<=0)

printf("The number should be positive.\n");

else {

printf("\n\n\tI \t Fibonacci(I) \n\t=====================\n");

next = current = 1;

for (i=1; i<=n; i++) {

printf("\t%d \t %d\n", i, current);

twoaway = current+next;

current = next;

next = twoaway;

}

}

}
What if there wouldn’t have been ‘C’

Just think of the scenario. If there would not have ‘C‘ been existed, perhaps there would not be any Linux, nor Mac neither Windows, no IPhones, no Remotes, no Android, no Microprocessor, no Computer, ohhh you just can’t image…

This is not an end. You should write codes of all kind to learn programming. Conceive an idea and code it, if You lands into any trouble and needs my help you can always buzz me. We (Tecmint) always try to provide you with latest and accurate information. Like and share us to help us spread.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

14 Responses

  1. Aditya says:

    Very nice intro. Was looking for something like this.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.