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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Avishek
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

14 Comments

Leave a Reply
  1. If C wasnt around they would have coded the linux kernal in Cobol or Lisp as they are still really great. C was used as it was invented as a systems programming language and was largely untested until unix was made and improved over time. C matured as a result of unix large scale adoption. Lisp was already more fully featured and had and still has more features than C. A kernel in lisp would have been better actually.

    Reply
  2. Hey Avishek,

    The great Dennis Ritchie is Dennis, not Dennise (Please alter your graphic so that he doesnt sound like the mother of C and UNIX) :)

    Raja: Most if not all those languages you mention are written in C and are interpreted and so by definition slower than ‘C’. If you are writing CGI the C is probably NOT suitable.
    Kind regards
    mistere

    Reply
  3. Pascal has all of the advantage of C, and is far more readable :)
    Is there anything that can match Free Pascal’s ‘Lazarus’ IDE in the C world?

    Reply
  4. Hi Dude,

    You told without “C” we can’t imagine anything then why we are use this much of programming languages Perl,Python,Bash,JAVA ..etc.

    Can you explain what are the difference between “C” these type languages(Perl,Python .etc) and clearly tell me which one is best programming language.

    Regards,
    Raja

    Reply
    • Raja,

      The “best” language is usually very subjective.

      Some languages are better suited to certain tasks, e.g. SQL for interacting with a relational database and Javascript for web browsers.

      Try different languages and specialize in the one that YOU like the best. :)

      Reply
    • Turbo C is not case sensitive? I don’t believe that’s correct.

      Older versions of the C standard permit (but do not require) external identifiers to be case-insensitive, but C in general has always been case sensitive, and I’m fairly sure Turbo C implemented that correctly.

      And please *please* indent your code.

      Reply
  5. “Note: C is not case sensitive, programming language.”

    C IS a case sensitive programming language.

    i.e. sprintf != SPRINTF

    Reply
  6. …”Note: C is not case sensitive, programming language. ” …

    Uh ?

    Maybe the ‘C/c” letter to name the language – but C-syntax constructs, language – IS – case sensitive …

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.