C Language


C is a structured programming language used to create system applications. It directly interacts with the hardware devices such as kernels. It is a middle level language because a user can use C to do system programming as well as application programming. 


Features of C language


  • It is rich in built-in functions and operators that can be used to write any complex program.
  • C is a portable language or machine independent i.e. once written, it can be run on any other machines with little or no modifications.
  • It allows us to create our own functions and add them to C library.
  • It is a mid-level language.
  • It supports dynamic memory allocation. We can free allocated memory at any time using free() function.
  • Fast compilation and execution time.
  • It provides code reusability.


A brief history


C language came into existance after the following languages.

ALGOL, the first structured computer language, was developed in 1960s.

BCPL (Basic Combined programming Language), was developed by Martin Richards in 1967.

B language, based on BCPL, was developed by Ken Thomson in 1970.


C language was developed by Dennis Ritchie in 1972 at the Bell Laboratories in USA. It is a middle level language which has features of both high and low level languages.  


Characteristics of C


Characteristics are the fundamental components required to develop a C program. 

  1. Operators: It is a special kind of symbol which performs a specific task. For example, *, +, -, /, ==, ++, etc. 
  2. Keywords: These are reserved words whose meanings are automatically recognized by the compiler. There are 32 keywords in the C language.  For example, if, else, while, do, break etc.
  3.  Separators: Separators are used to separate an individual unit of a program called as tokens. Separators include comma (,), curly braces ({ }), semi colon (;), spaces etc. 
  4. Constants: Constants are fixed values which never change during execution of program. For example, 2, -3, 0.5 etc.
  5.  Pre-defined functions: These are built-in functions which are available along with the compiler. We call a pre-defined function when we perform any specific task. It includes scanf(), printf() etc. 
  6. Variables/Identifiers: They are combination of letters and digits and must be declared before using. They can have different values. A variable name can start with underscore (_) but not with digits. Variables include _smash, smash, etc.


Syntax rules:


C is a case sensitive language. Hence, words written in capital and small letters are different. All statements in C must be terminated with a semicolon (;). White spaces should be provided between variables and keywords.


Data Types in C


A datatype describes the variables i.e. the set of values a variable can store along with operations that can be performed on it. Data types are of three types:

  1. Primitive datatypes: These are pre-defined types. They are directly used to declare variables. For example, int, float, char, void etc.
  2. Derived datatypes: They are derived from the built-in types. Example- array, references, pointers etc.
  3. User defined datatypes: These are defined by users themselves such as structure, union, enum etc.


Structure of a C program


A basic C program consists of following parts:


Header Files inclusion


A header file is a file with extension .h which contains functions (predefined programs) declarations and definitions. The header file is included using the command #include (known as preprocessor).


Syntax:


#include<stdio.h>


Here, stdio.h means standard input output. It allows us to perform input (accepting data from user) and output (displaying result on screen).

 

main() function


It is the compulsory part of any C program. There must be one main function for a program.


Syntax:

int main()

{

  ...return 0;

}

The curly braces enclose the body of main() function. The main() function is of int type hence, an integer value is returned. The return value is the the exit status of the main function. return 0 tells the caller that your program is successful and the the caller can proceed further with the program.


Comments


Comments are optional. They are written in order to explain the codes which we write and are ignored by the compiler i.e. they are  non-executable. Single line comment starts with \\. Multiline comments start with /* and end with */.

 

Basic output function


It allows us to display any message on the standard output screen.

Example Explained:

printf("Hey")   //1 argument

printf("%d", var)    //2 arguments

Explanation:

The first argument is always in double inverted comma (" ") because it is always a string constant.

%d is placeholder for variable var. 'd' is used for decimal. Similarly, 'f' for float, 'c' for character and so on.


The scanf() function


It stands for Scan Formatted string. It is used to accept data from the user using standard input device- keyboard. It also uses format specifiers like printf.


Example:


int a;

scanf("%d", &a);


Example Explained:

&a means address of a. Ampersand (&), also called as address-of operator, is used to know the memory location of varibale 'a' to store it while scanning the input data.


Now, let's see few basic examples in C language to understand the concept of C programming.



Q 1. Write a program to print any message on the screen.

//C program to display any message on the screen

#include<stdio.h>

int main()

{

  printf("Welcome to Coding Smash!");

  return 0;

}

 OUTPUT:




Q 2. Write a program to accept two inputs from user and find their product.

//C program to find product by accepting input from user

#include<stdio.h>

int main()

{

  int a, b;

  printf("Enter 1st number: " );

  scanf("%d", &a);

  printf("Enter 2nd number: ");

  scanf("%d", &b);

  printf("%d * %d = %d", a, b, a*b);

  return 0;

}

  OUTPUT:






Q 3. Find the output of the below program.


#include<stdio.h>

int main()

{

int a, b, c;


  printf("Enter 1st number:");
  
scanf("%d"&a);


  printf("Enter 2nd number:");

scanf("%d"&b);


 printf("Enter 3rd number:");

  scanf("%d"&c);


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

  return 0;

}

Try it yourself...