Did You Know?
Got a tip? Let us know

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

Hands On ‘C’ Programming Language

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

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.

I am a major in computer science, love to research nix. I love to write codes and scripts, review distros, experiment Foss Technologies, write technical articles, Hack, of course Ethically. I am working as System Administrator (nix) for a NGO.

Linux Services & Free WordPress Setup

Our post is simply ‘DIY’ aka ‘Do It Yourself, still you may find difficulties and want us to help you out. We offer wide range of Linux and Web Hosting Solutions at fair minimum rates. Please submit your orders by Clicking Here.

« Previous Post

Next Post »

14 Responses

  1. Jef peeraer says:

    where is the indentation? I presume this just an html formatting matter…

  2. bretzel says:

    …”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 …

  3. Ken Jennings says:

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

    C IS a case sensitive programming language.

    i.e. sprintf != SPRINTF

  4. gerato says:

    Please indent your code :)

  5. Avishek says:

    Yup! Turboc (Another compiler for C) is not case sensitive, however gcc is. Sorry for inconvenience

    • Keith Thompson says:

      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.

  6. Raja says:

    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

    • Jason Froebe says:

      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. :)

  7. Bongo says:

    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?

  8. mistere says:

    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

  9. pathological says:

    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.

  10. Aditya says:

    Very nice intro. Was looking for something like this.

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.