C LANGUAGE

C Language Online Tutorial

C is a general-purpose programming language, developed in 1972, and still quite popular. C is very powerful; it has been used to develop operating systems, databases, applications, etc.

What is C?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why Learn C?

• It is one of the most popular programming language in the world
• If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar
• C is very fast, compared to other programming languages, like Java and Python
• C is very versatile; it can be used in both applications and technologies

Difference between C and C++

  1. C++ was developed as an extension of C, and both languages have almost the same syntax.
  2. The main difference between C and C++ is that C++ support classes and objects, while C does not.
  3. C is POP is based language while C++ is based on OOPS. which is based on Class and Objects.

Syntax

Syntax

You have already seen the following code a couple of times in the first chapters. Let’s break it down to understand it better:

Example

#include <stdio.h>

int main() {
  printf(“Hello Students”);
  return 0;
}

Example explained

Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions, such as printf() (used in line 4). Header files add functionality to C programs.

Don’t worry if you don’t understand how  #include <stdio.h> works. Just think of it as something that (almost) always appears in your program.

Line 2: A blank line. C ignores white space. But we use it to make the code more readable.

Line 3: Another thing that always appear in a C program, is main(). This is called a function. Any code inside its curly brackets {} will be executed.

Line 4: printf() is a function used to output/print text to the screen. In our example it will output “Hello World”.

Note that: Every C statement ends with a semicolon ;

Note: The body of intmain() could also been written as:
int main(){printf(“Hello World!”);return 0;}

Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.

Line 5: return 0 ends the main() function.

Line 6: Do not forget to add the closing curly bracket } to actually end the main function.

Step 1) Download Binary release

Go to http://www.codeblocks.org/downloads and click Binary Release.

Step 2) Select the installer with GCC for Windows compiler

Choose the installer with GCC Compiler, e.g., codeblocks-17.12mingw-setup.exe which includes MinGW’s GNU GCC compiler download and GNU GDB debugger with Code::Blocks source files.

Step 3) Start installation

Run the downloaded installer and accept the default options to install GCC Windows file.

Step 4) Accept the terms and conditions

Accept the Agreement

Step 5) Keep default component selection

Keep the component selection default and click Next.

Step 6) Locate the installation path

You may change the installation folder and click Next.

Step 7) Find and double-click on the CodeBlocks icon

To launch Code::Blocks double click on the icon.

Step 8) Let it detect the compiler itself

It will detect the GCC compiler for Windows automatically, set it as default.

Step 9) Open the IDE and start using

You will see the IDE Home screen.

 

Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Comments can be singled-lined or multi-lined.

Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

This example uses a single-line comment before a line of code:

Example

// This is a comment
printf(“Hello Students!”);

C Multi-line Comments

Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by the compiler:

Example

/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf(“Hello World!”);

Variables is a Container which holds data or data type.

  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • float – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes.

Creating Variables

specify the type and assign it a value

Syntax:

type variableName = value;

“type” can be int or float or char

Example:

int VariableName = 123;

You can also declare a variable without assigning the value, and assign the value later:

Example:

int mahesh;
mahesh = 15;

Note: If you assign a new value to an existing variable, it will overwrite the previous value:

int age = 15;  // age is 15
age = 10;  // Now age is 10

Operators are used to perform operations on variables and values. +, -, *, / is known as operators. But these sign also used in Arithmatic Operators.

C Operators are of four types, which are given below:

  1. Arithmatic Operator: Arithmatic Operators are the operators which is used to add(+), Subtract(-), multiply(*), divide(/) multiple variables.
  2. Assignment Operator: Assignment Operator is used to add(+=), Subtract(-=), multiply(*=), divide(/=) with one variable or same variable.
  3. Comparison Operator: Comparison operator is used to caompare one variable to another variable or value. Greater than(>), Less than(<), Equal to (==), Not Equal to(!=), Greater than or Equal to(>=), Less than or Equal to(<=) is used to compare two variables.
  4. Logical Operators: and(&&), or(||), Not(!) are the keywords which is known as Logical Operators.

And(&&) : When the all conditions are True then the answer will be True in “and” condition.

Or(||): When one condion is True, answer will be True.

Not(!): When answer is True, Using “Not” will give you false Answer.

C has the following conditional statements:
  1. Use if to specify a block of code to be executed, if a specified condition is true
  2. Use else to specify a block of code to be executed, if the same condition is false
  3. Use else if to specify a new condition to test, if the first condition is false
  4. Use switch to specify many alternative blocks of code to be executed.
if Statement:

if Statement is of 3 types:

  1. if Statement: it is used for 1 conditions.
  2. if Else: it is used for 2 conditions.
  3. Else if: it is used for 3 or more than 3 conditions.
Syntax for If Statement:

if (condition) {
  // block of code
}

Syntax for If Else Statement:

if (condition) {
  // block of code
} else {
  // block of code
}

Syntax for Else if Statement:

if (condition1) {
  // block of code
} else if (condition2) {
  // block of code

} else if (condition2) {
  // block of code

} else if (condition3) {
  // block of code
} else {
  // block of code
}

 

Loops can execute a block of code as long as a specified condition is reached. Loops are handy because they save time, reduce errors, and they make code more readable.

While Loop:

The while loop loops through a block of code as long as a specified condition is true:

Syntax:

while (condition) {
  // code block
}

Do/While Loop:

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax:

do {
  // code block
}

while (condition);

For Loop:

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

for (statement 1; statement 2; statement 3) {
  // code block
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.