Learn C language in 20 minutes

Dennis Ritchie developed the C Programming language in the 1970s at AT&T Ball laboratories. C language is created in B language. B language also develops by Dennis Ritchie with Ken Thompson. We will also be surprised after knowing the UNIX operating system is also designed in C language. ANSI standardize the C language. So you can predict how much the c language was famous.

Prerequisite

  1. Need text editors like Visual Studio, Atom, Turbo C, and Borland C.
  2. If you are using a mac or Linux-based system then GCC will already be available.

Here we will use the GCC compiler.

How to use C

  1. Write code and save filename with .c extension. Like main.c
  2. Open a terminal and reach the file path.
  3. After saving the file compiles the file. Like gcc main.c.
  4. After compilation we get compiled file then we need to run that file. Like ./main.

Hello world program in C

It’s a basic program and an important problem in C. It does not only verify all environment settings are perfectly working and as well as you know how to print in C. It’s done by using a printf function. But before going further we need to understand this basic program.


/*
  It's a multiline comment
  Hello, world program in C
*/

#include <stdio.h>
int main() {
  // value inside "" is a string or text
  printf("Welcome to C Programming Language"); // Output:- "Welcome to C Programming Language"
  return 0;
}

Here we have a basic structure of the C program. Let us understand how this program will work.

First, we should know about the fact that the C language does not provide any functionality of input-output so we need to use the Standard Input and Output library that’s why we are using #include code. that reusable we are including standard Input and Output header files in our program. that indicates how our program will take input from the keyboard and save it into memory and if we need that data then how it will take from memory and print our output on the screen.

The execution of every C program always starts with the main function. So one main function should be in our program. By default main function returns an integer value to the operating system. If we will not use the int return type here then it will return some garbage value. So you can see we are using the int return type in the main function and it is returning a 0 value.

// This symbols indicate a single line comment and /**/ this symbol indicates a multiline comment. Comments are used to increase the readability of the program we can explain the purpose of a code snippet that can help another programmer to understand your code but the compiler ignores that code.

Now we are using the printf function and we have a message inside ” double quotes we are wrapping this inside the printf function which means the printf function will print this message on the output screen.


  char msg[] = "Welcome to C Programming Language"; // Hard coded message
  printf("%s", msg); // Output:- "Welcome to C Programming Language"

Here we are using the same code using variable but in this case inside the printf, we are using the %s symbol (conversion specification) with the variable name. Which specify the type and size of data and it will start from the % symbol. This symbol also uses inside the scanf function that reads input from the keyboard.


  int age;
  printf("What is your age?\n");
  scanf("%d", &age);
  printf("Your age is: %d", age);

  /*
    What is your age?
    30
    Your age is: 30
  */

Here we are using an integer variable and in our previous example, we are using a character type variable. After that, we are printing a message on the screen. Then our program asks age inside the scanf function. Here we are using %d for integer and we are also using & symbol that indicates to store keyboard value inside age variable memory. after that, we are just printing our output.

What is a Variable?

We have used the variable term a lot of time but what exactly variable means? For me, it’s just a Bucket. You can put something inside it, can carry it and also retrieve it when you need it. Now next thing is our type of variable. which tells us the kind of data that can store inside a variable. These could be integers, decimals, or characters. So don’t forget in the C language variable must have a type and name.


  int age; // %d - int take 4byte inside memory
  double height; // lf% - double take 8byte inside memory
  char name[40]; // %c or %s - char take 1 byte inside memory

Conditionals

In decision control statements we have if, if…else, nested if and all these statements represent the direction of code. Like

If


  int age = 30;
  if ( age > 20 ) {
    printf("Now you are an adult");
  }

if…else


  int no1 = 12, no2 = 30;
  if ( no1 > no2 ) {
     printf("%d is Big number.", no1);
  } else {
     printf("%d is Big number.", no2);	
  }

Ternary operator


  int num = 6, isA;
  isA = ( num % 2 == 0 ) ? 1 : 0; // 1 for even, 0 for odd
  printf("%d is a: %d", num, isA);

if…else Ladder Or Nested if…else


  int no1 = 40, no2 = 40;
  if ( no1 > no2 ) {
    printf("%d is Big number.", no1);
  } else if (no1 < no2) {
    printf("%d is Big number.", no2);
  } else {
    printf("Both number are equal");
  }

Switch


  int num  = 1;
  switch ( num ) {
    case 1:
    printf("First: %d", num);
    break;
    case 2:
    printf("Second: %d", num);
    break;
    default:
    printf("Default");
  }

Array


  int arr[5] = { 2, 4, 6, 8, 10 };
  printf("Array third index is: %d", arr[3]); // Array index start from 0

Loop in C


  int I;
  int len = 3;
  char arr[]  = { 'f', 's', 't', '\0' };

  for ( i = 0; i < len; i++ ) {
    printf("%d ", i); // 0, 1, 2
  }
  printf("\n");

  // Loop with array
  for ( i = 0; i < len; i++ ) { // for loop
    printf("%c ", arr[i]); // f, s, t
  }
  printf("\n");

  i = 0;
  while ( i < len ) { // while loop
    printf("%c ", arr[i++]); // f, s, t
  }
  printf("\n");

  i = 0;
  do { // do while loop
    printf("%c ", arr[i++]); // f, s, t
  } while ( i < len );

Break in C

We can prevent iterating all elements using a break keyword or stopping on the desired condition.


  // break with for loop
  for ( int i = 0, len = 5; i < len; i++ ) {
    printf("%d ", i); // 0, 1, 2
      if ( i == 2 ) {
        break;
      }
  }

Continue in C

Continue can skip over items.


  // continue with for loop
  for ( int i = 0, len = 5; i < len; i++ ) {
    if ( i == 2 ) {
      continue;
    } else {
      printf("%d ", i); // 0, 1, 3, 4
   }
}

Functions

Reusable block of code.


  // Define function by declaration
  int add( a, b ) {
    return a + b;
  }
  printf("Your some is: %d", add(3, 4));

Pointer


  int num = 123;
  int *pnum = &num;
  printf("%p", pnum); // %p for address of num that store using &num

Structure


struct student {
  char name[20];
  int age;
};

struct student st1 = { "John", 26 };
printf("My name is %s and my age is %d.", st1.name, st1.age); // My name is John and my age is 26.


About the Author: Pankaj Bisht