C Programming Introduction

Importance of Subject 


  • ‘C is the base language of any​ other programming language.​
  •  To be a Good Programmer one ​must know fundamentals of C​ programming language.

History of ‘C’


  • ​Root of the morden language is ​ALGOL 1960. It’s first ​computer language to use block ​ structure.​ It gave concept of structured​ programming.​
  • In 1967, Martin Richards developed ​a language, BCPL (Basic Combined Programming Language)
  • In 1970,by Ken Thompson created a language called as ‘B’.​ It used to create early version of Unix.
  • In 1972,by Dennis Ritchie introduced new language called as ‘C’ .​


Features Of ‘C’


  • It is a robust language.​
  • Programs written in ‘C’ are efficient and fast.​(Because of variety of data types and powerful ​operators)​
  • Highly Portable. (related to OS)​
  • Well suited for structured programming.​
  • Ability to extend itself. ​

Program & Programming Language

Program:- A Set of instructions which ​carried out by processor for some ​Specific input, generates specific output. ​​

Programming language:- A specific ​manner of writing a program with some ​Predefined rules, symbols & their use as ​a part of language.​  i.e. Pascal, C, C++, VC++, JAVA, VB.


Basic structure of ‘C’


[1] Documentation Section 

  • It has set of comment lines(name of program, author details).​

What is Comment line??​

  • To guide a programmer. To write a note for ​function,operation,logic in between a program.​
  • Non-executable statement.​
  • Can’t be nested.

 e.g.    /* Hello /* abc */ Hi */      ​

[2] Link Section

  • It provides instructions to the compiler to link ​function from the system library.
  • # include  Directive
    • To access the functions which are stored in the​ library, it is necessary to tell the compiler , ​about the file to be accessed.​
    • Syntax:-​ #include<stdio.h> // stdio.h is header file

[3] Definition Section

  • It defines all symbolic constants.​
  •  #define instuction defines value to a symbolic constant.​
  •  #define:-​
    • It is a preprocessor compiler directive, not a statement.​
    • Therefore it should not end with a semicolon.​
    • Generally written in uppercase.​


  • Some variables that are used in more than on function, such variables (global variables) declared in the global declaration section.​
  • It also declares all the user-defined function.

[5] Main() function section

  • Every ‘C’ program must have one main() function section.​
  • It contains two parts​
    • Declaration part:​ It declares all variables used in the executable part.​
    • Executable part:​ It has atleast  one statement.​


Link Section

Definition Section

Global Declaration Section

main() function section


Declaration part

Executable part


 Subprogram section


Function2 …         

User defined function



  • There are two ways to run programs written in a high-level language. ​
  • ​The most common is to compile the program​
  • The other method is to pass the program through an interpreter.


Why compiler is require ?​

As machine (a processor) can operate​ On binary code instruction only…..​

If we use higher level language then …For execution of the program we must ​Convert it to lower level / machine level​ Code means, ​

A program that translates source code into object code.

The compiler derives its name from the way it works, looking at the entire piece of source code and collecting and reorganizing the instructions.​

Advantage of compiler​:

  • Programs produced by compilers run much faster than the same programs executed by an interpreter.


which analyzes and executes each line of source code without looking at the entire program. ​

Advantage of interpreter:​

  • It can execute a program immediately. ​
  • Compilers require some time before an executable program emerges. ​

C  Compiler  ​

  • Checks for syntax errors if any​ – on success  coverts ‘C source code​ into object code form –​ which is nearer to machine

Types of languages


(1) Lower level languages:-​

Languages which are very near to ​machine…. I.e. machine language, ​

Assembly language.​

(2) Higher level languages:-​

Languages which are very near to ​programmer rather than to machine….​

I.e. C++,Visual C++,Visual basic,Java.


Data Types in C


Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:

Following are the examples of some very common data types used in C:

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single precision.
double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Different data types also have different ranges upto which they can store numbers.

These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.



We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:

#include <stdio.h>
int main()
    int a = 1;
    char b ='G';
    double c = 3.14;
    printf("Hello World!\n");
    //printing the variables defined above along with their sizes
    printf("Hello! I am a character. My value is %c and "
           "my size is %lu byte.\n", b,sizeof(char));
    //can use sizeof(b) above as well
    printf("Hello! I am an integer. My value is %d and "
           "my size is %lu  bytes.\n", a,sizeof(int));
    //can use sizeof(a) above as well
    printf("Hello! I am a double floating point variable."
           " My value is %lf and my size is %lu bytes.\n",c,sizeof(double));
    //can use sizeof(c) above as well
    printf("Bye! See you soon. :)\n");
    return 0;


Hello World!
Hello! I am a character. My value is G and my size is 1 byte.
Hello! I am an integer. My value is 1 and my size is 4 bytes.
Hello! I am a double floating point variable. My value is 3.140000 and my size i
s 8 bytes.
Bye! See you soon. 🙂




  • C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.
  • The value of the C variable may get change in the program.
  • C variable might be belonging to any of the data type like int, float, char etc.


Rules for Variable name

  • Variable name must begin with letter or underscore.
  • Variables are case sensitive
  • They can be constructed with digits, letters.
  • No special symbols are allowed other than underscore.
  • sum, height, _value are some examples for variable name


  • Variables should be declared in the C program before to use.
  • Memory space is not allocated for a variable while declaration. It happens only on variable definition.
  • Variable initialization means assigning a value to the variable.
Variable declaration
data_type variable_name;
Example: int x, y, z; char flat, ch;
Variable initialization
data_type variable_name = value;
Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;


  1. Local variable
  2. Global variable
  3. Environment variable


Variable declaration
Variable definition
Declaration tells the compiler about data type and size of the variable.
Definition allocates memory for the variable.
Variable can be declared many times in a program.
It can happen only one time for a variable in a program.
The assignment of properties and identification to a variable.
Assignments of storage space to a variable.

Input and Output



  • scanf(“%d”,&a);
  • Gets an integer value from the user and stored it under the name a (variable)


  • printf(“%d”,a);
  • Prints the value present in variable a on the screen


C Programming Operators


An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.

C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Conditional Operators
  • Bitwise Operators
  • Special Operators


C Arithmetic Operators


An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).

Operator Meaning of Operator
+ addition or unary plus
subtraction or unary minus
* multiplication
/ division
% remainder after division( modulo division)


Example #1: Arithmetic Operators

// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
    int a = 9,b = 4, c;
    c = a+b;
    printf("a+b = %d \n",c);

    c = a-b;
    printf("a-b = %d \n",c);
    c = a*b;
    printf("a*b = %d \n",c);
    printf("a/b = %d \n",c);
    printf("Remainder when a divided by b = %d \n",c);
    return 0;


Increment and decrement operators



C programming has two operators increment ++ and decrement — to change the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement — decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.


Example #2: Increment and Decrement Operators

// C Program to demonstrate the working of increment and decrement operators
#include <stdio.h>
int main()
    int a = 10, b = 100;
    float c = 10.5, d = 100.5;

    printf("++a = %d \n", ++a);

    printf("--b = %d \n", --b);

    printf("++c = %f \n", ++c);

    printf("--d = %f \n", --d);

    return 0;


++a = 11
--b = 99
++c = 11.500000
++d = 99.500000

C Assignment Operators


An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

Example #3: Assignment Operators


// C Program to demonstrate the working of assignment operators
#include <stdio.h>
int main()
    int a = 5, c;

    c = a;
    printf("c = %d \n", c);

    c += a; // c = c+a
    printf("c = %d \n", c);

    c -= a; // c = c-a
    printf("c = %d \n", c);

    c *= a; // c = c*a
    printf("c = %d \n", c);

    c /= a; // c = c/a
    printf("c = %d \n", c);

    c %= a; // c = c%a
    printf("c = %d \n", c);

    return 0;


c = 5 
c = 10 
c = 5 
c = 25 
c = 5 
c = 0

C Relational Operators


A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops

Operator Meaning of Operator Example
== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 return 0

Example #4: Relational Operators


// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
    int a = 5, b = 5, c = 10;

    printf("%d == %d = %d \n", a, b, a == b); // true
    printf("%d == %d = %d \n", a, c, a == c); // false

    printf("%d > %d = %d \n", a, b, a > b); //false
    printf("%d > %d = %d \n", a, c, a > c); //false

    printf("%d < %d = %d \n", a, b, a < b); //false
    printf("%d < %d = %d \n", a, c, a < c); //true

    printf("%d != %d = %d \n", a, b, a != b); //false
    printf("%d != %d = %d \n", a, c, a != c); //true

    printf("%d >= %d = %d \n", a, b, a >= b); //true
    printf("%d >= %d = %d \n", a, c, a >= c); //false

    printf("%d <= %d = %d \n", a, b, a <= b); //true
    printf("%d <= %d = %d \n", a, c, a <= c); //true

    return 0;



5 == 5 = 1
5 == 10 = 0
5 > 5 = 0
5 > 10 = 0
5 < 5 = 0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1

C Logical Operators


An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making and loop

Operator Meaning of Operator Example
&& Logial AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c == 5) && (d > 5)) equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c == 5) || (d > 5)) equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression ! (c == 5) equals to 0.

Example #5: Logical Operators


// C Program to demonstrate the working of logical operators

#include <stdio.h>
int main()
    int a = 5, b = 5, c = 10, result;

    result = (a = b) && (c > b);
    printf("(a = b) && (c > b) equals to %d \n", result);

    result = (a = b) && (c < b);
    printf("(a = b) && (c < b) equals to %d \n", result);

    result = (a = b) || (c < b);
    printf("(a = b) || (c < b) equals to %d \n", result);

    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) equals to %d \n", result);

    result = !(a != b);
    printf("!(a == b) equals to %d \n", result);

    result = !(a == b);
    printf("!(a == b) equals to %d \n", result);

    return 0;


(a = b) && (c > b) equals to 1 
(a = b) && (c < b) equals to 0 
(a = b) || (c < b) equals to 1 
(a != b) || (c < b) equals to 0 
!(a != b) equals to 1 
!(a == b) equals to 0


Explanation of logical operator program


  • (a = b) && (c > 5) evaluates to 1 because both operands (a = b) and (c > b) is 1 (true).
  • (a = b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
  • (a = b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).


Bitwise Operators


During computation, mathematical operations like: addition, subtraction, addition and division are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right



Other Operators


Comma Operator


Comma operators are used to link related expressions together. For example:

int a, c = 5, d;

The sizeof operator


The sizeof is an unary operator which returns the size of data (constant, variables, array, structure etc).

Example #6: sizeof Operator

#include <stdio.h>
int main()
    int a, e[10];
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));
    printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
    return 0;


Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Size of integer type array having 10 elements = 40 bytesC Ternary Operator (?:)

A conditional operator is a ternary operator, that is, it works on 3 operands.

Conditional Operator Syntax


conditionalExpression ? expression1 : expression2

The conditional operator works as follows:

  • The first expression conditionalExpression is evaluated at first. This expression evaluates to 1 if it’s and evaluates to 0 if it’s false.
  • If conditionalExpression is true, expression1 is evaluated.
  • If conditionalExpression is false, expression2 is evaluated.

Example #7: C conditional Operator

#include <stdio.h>
int main(){
   char February;
   int days;
   printf("If this year is leap year, enter 1. If not enter any integer: ");

   // If test condition (February == 'l') is true, days equal to 29.
   // If test condition (February =='l') is false, days equal to 28. 
   days = (February == '1') ? 29 : 28;

   printf("Number of days in February = %d",days);
   return 0;


If this year is leap year, enter 1. If not enter any integer: 1
Number of days in February = 29

C if, if…else and Nested if…else Statement

C if statement

if (testExpression) 
   // statements

The if statement evaluates the test expression inside the parenthesis.

If the test expression is evaluated to true (nonzero), statements inside the body of if is executed.

If the test expression is evaluated to false (0), statements inside the body of if is skipped from execution.


Example #1: C if statement

// Program to display a number if user enters negative number
// If user enters positive number, that number won't be displayed

#include <stdio.h>
int main()
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // Test expression is true if number is less than 0
    if (number < 0)
        printf("You entered %d.\n", number);

    printf("The if statement is easy.");

    return 0;

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

When user enters -2, the test expression (number < 0) becomes true. Hence, You entered -2 is displayed on the screen.

Output 2

Enter an integer: 5
The if statement in C programming is easy.

When user enters 5, the test expression (number < 0) becomes false and the statement inside the body of if is skipped.

C if…else statement


The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).

Syntax of if…else

if (testExpression) {
    // codes inside the body of if
else {
    // codes inside the body of else


C Programming for Loop


Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:

  • for loop
  • while loop
  • do…while loop

for Loop

The syntax of for loop is:

for (initializationStatement; testExpression; updateStatement)
       // codes 

for loop flowchart


while loop

The syntax of a while loop is:

while (testExpression) 

where, testExpression checks the condition is true or false before each loop.


do…while loop

The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once.

do…while loop Syntax

   // codes
while (testExpression);


C Programming break and continue Statement

t is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.

In such cases, break and continue statements are used.

break Statement


The break statement terminates the loop immediately when it is encountered. The break statement is used with decision making statement such as if…else.

Syntax of break statement


The simple code above is the syntax for break statement.

Flowchart of break statement



How break statement works?


continue Statement


The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if…else.

Syntax of continue Statement


Flowchart of continue Statement


How continue statement works?


C switch…case Statement


The if..else..if ladder allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in if...else...if, it is better to use switchstatement.

The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.

Syntax of switch…case

switch (n)
    case constant1:
        // code to be executed if n is equal to constant1;

    case constant2:
        // code to be executed if n is equal to constant2;
        // code to be executed if n doesn't match any constant

When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.


C goto Statement



he goto statement is used to alter the normal sequence of a C program.

Syntax of goto statement

goto label;
... .. ...
... .. ...
... .. ...

The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.

How goto statement works?

int main()

    const int maxInput = 5;
    int i;
    double number, average, sum=0.0;
    for(i=1; i<=maxInput; ++i)
        printf("%d. Enter a number: ", i);

    // If user enters negative number, flow of program moves to label jump
        if(number < 0.0)
            goto jump;

        sum += number; // sum = sum+number;


    printf("Sum = %.2f\n", sum);
    printf("Average = %.2f", average);

    return 0;

Reasons to avoid goto statement

The use of goto statement may lead to code that is buggy and hard to follow. For example:

for (i = 0; i < number; ++i)
    test += i;
    goto two;
if (test > 5) {
  goto three;
... .. ...

Also, goto statement allows you to do bad stuff such as jump out of scope.

That being said, goto statement can be useful sometimes. For example: to break from nested loops.

Should I or shouldn’t I use goto statement?

If you think the use of goto statement simplifies your program. By all means use it. The goal here is to create code that your fellow programmers can understand easily.

C Programming Functions

“A function is a block of code that performs a specific task”

Dividing complex problem into small components makes program easy to understand and use.

There are two types of functions in C programming:

  • Standard Library functions
  • User Defined functions

Standard library functions

The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.

These functions are defined in the header file. When you include the header file, these functions are available for use. For example:

The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file.

User-defined functions

As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions.

Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want.

How user-defined function works?

#include <stdio.h>
void functionName()
    ... .. ...
    ... .. ...

int main()
    ... .. ...
    ... .. ...

    ... .. ...
    ... .. ...

The execution of a C program begins from the main() function.

When the compiler encounters functionName(); inside the main function, control of the program jumps to

 void functionName()

And, the compiler starts executing the codes inside the user-defined function.

The control of the program jumps to statement next to functionName(); once all the codes inside the function definition are executed


Remember, function name is an identifier and should be unique.

Function prototype

A function prototype is simply the declaration of a function that specifies function’s name, parameters and return type. It doesn’t contain function body.

A function prototype gives information to the compiler that the function may later be used in the program.

Syntax of function prototype

returnType functionName(type1 argument1, type2 argument2,...);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s