C Language

The C language is a basic programming language and it is a very popular language, particularly used in game programming. It was developed by Dennis Ritchie between 1969 and 1973 at Bell Labs and used to re-implement the unix operating system.
A program that prints out ‘hello world!’ when executed.

This is a program written in C. When executed, the program will print out hello world!

hello world!

Lets dissect these lines of codes to understand how it works:

#include
It is called a preprocessor. Preprocessor as the name suggests, it pre-processes before the main lines of code. It is compiled first and then the program is executed.
<stdio.h>
It is a library or a header file. The files that has .h extensions(h for header, if you didn’t guess) are libraries(or header file). Library is a collection of prewritten code.
#include <stdio.h>
This is a statement which tells the compiler to insert the contents of stdio at that particular place, so that we might use it later on in our program.
<cs50.h>
This is another library that includes another set of functions(or codes).
int main(void){
printf("hello world!\n");
}
This here is the main function. It is the point where the execution gets started. So the output of this program will depend on the content inside this main function.
printf("hello world!\n");
printf() is a function. As you might have guessed, it prints out the content inside the parentheses. In this case, hello world!
And about the \n: It is one of the escape sequences. It is a newline character. Meaning it will insert a new line after the output.
This function is not in-built in C. printf() is defined in stdio.h. We extract the codes for it and execute it. So now you know why did we include stdio.h?
The semicolon at the end of this statement is compulsory. It is used to terminate a statement like this.

Now that you have a bit of an idea about how a C program works, lets explore a bit more:

How to declare variables in C?

  1. Specify the type of the variable.
  2. Name of the variable.
int m; //declaration
m = 12; //assignment

or you can directly assign the value:

int m = 12; //initialization

Here, the type of the variable is int as in integer and the name is m.

That’s all you need to do to declare a variable. That simple.

Datatypes in C:

Basic datatypes:

  1. Int
  2. Char
  3. Float
  4. Double
Basic datatypes.

Enumerated datatypes:

  1. Enum
An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.
enum compare{ const1, const2, ..., constN };
Here, name of the enumeration is compare.
And, const1, const2,…., constN are values of type compare.
Enumerated Types are a special way of creating your own Type in C.

Derived datatypes:

  1. Pointer
  2. Array
  3. Structure
  4. Union

Pointer

A pointer is a variable which stores the address of another variable, i.e, the direct address of the memory location.
You must declare a pointer before using it to store any variable address.
Declaring a pointer:
type *var-name;
The asterisk is used to indicate its a pointer.
int *int_p;
float *float_p;
double *d_p;
char *ch;
Example:
int num = 20;
int *ip; //declaring pointer
ip = &num;
//storing the address of num, adding '&' in front of the //variable name gives the address of the variable.
//& means the address-of

Array

Arrays are a fundamental data structure.
Arrays hold values of the same type at contiguous memory location.
Declaration:
type name[size];
Example:
int numbers[10] = {1,2,3,4,5,6,7,8,9,10};

Structure

To define a structure, you must use the struct statement.
The struct statement defines a new data type, with more than one member. The format of the struct statement:
struct [structure tag] {

element description;
element description;
...
element description;
} [one or more structure variables];
Example:
struct students{
char name[40];
int class;
int roll_no.;
}

Union:

A union is a special data type available in C that allows to store different data types in the same memory location.
To define a union, you must use the union statement in the same way as you did while defining a structure.
union [union tag] {
element description;
element description;
...
element description;
} [one or more union variables];
Example:
union info{
int number;
char name[40];
float weight;
}

Void datatytpes:

  1. Void
Void is considered a data type, but it is basically a keyword to use as a placeholder where you would put a data type, to represent “no data”.
Example:
void myBrain();
myBrain() is a function, which returns no data.

Conditional Statements

  1. if statement
 if(condition){
statement;
}
statement;
If the given condition is true, the statement inside the curly braces is executed. Otherwise, the if statement is skipped and the statement afterwards is considered.
if(5>2){
printf("five is greater than two\n");
}
printf("five is not greater than two\n");

Output

five is greater than two

2. if-else statement

if(condition){
statement;
}
else {
statement;
}
if the condition given is true, the statement inside the if clause is executed. Otherwise the else-statement is executed.
if(5<2){
printf("five is less than two\n");
else{
printf("five is greater than two\n");
}
The condition given in this example is false. So which statement would be executed?
Output
five is greater than two

3. nested if-else statement

if(condition) {
if(condition) {
statement;
else(condition) {
statement;
}
}
else {
statement;
}

4. switch statement

switch(expression){
case1: statement;
break;
case2: statement;
break;
case3: statement;
break;
case4: statement;
break;
case5: statement;
break;
case6: statement;
break;
Switch statement is used when you have multiple option type problem.
One of the given statements is executed if the corresponding case if equal to the expression given.
And the break statement terminates the switch clause and skips the rest of the switch statements.
An example of switch statement.

Loops.

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

  1. While loop
variable initialization ;
while (condition)
{
statements ;
variable increment or decrement ;
}
Example:
int x = 0;
while(x<5){
printf("Less than 5\n");
x++;
}
The condition given here is (x<5). That is the statement printf(“Less than 5\n”); will be executed until x<5 is true.
The last line x++ meaning the same as x = x+1. It increments the value of x. It becomes greater by one each time the statement is evaluated. And it will be executed as long as x<5.
Step by step execution of while loop.
First, the value of x is 0, which is less than 5. So the statement will be executed satisfying the condition.
After printing the statement, the variable is incremented. x becomes x+1. Now x=1. The condition is again verified and it is true [1<5]. So the statement is executed again.
And this process will continue until x is not less than 5. (It will be executed 5 times — 0,1,2,3,4)
When the variable is incremented after printing the statement for the fifth time, x will be equal to 5. Now if you check the condition[5<5], it is not valid and the loop will be terminated.
Less than 5
Less than 5
Less than 5
Less than 5
Less than 5
The output will be like this.

2. For loop

for(initialization; condition; increment/decrement){
statements;
}
In for loop there is exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.
int x;
for(x=1; x<=10; x++)
{
printf("%i\t",x);
}
Output:
1 2 3 4 5 6 7 8 9 10

3. Do while loop

do
{
statements;
}
while(condition)
Do statement evaluates the body of the loop first and at the end, the condition is checked.
int a,i;
a=5;
i=1;
do
{
printf("%d\t",a*i);
i++;
}
while(i <= 10);
Output:
5 10 15 20 25 20 35 40 45 50