× Back Design Methods What is programming language? Procedural and OOP Introduction to C language Elements of C Structure of C programs C programs
Next Topic → ← Previous Topic

C Programming basics

Design Methods

Top-Down Design

  • Top-Down design method starts from top-level component to lowest level (bottom) component.
  • In this design method, the system is divided into some major components.
  • Then each major component is divided into lower level components.
  • Similarly other components are divided till the lowest level component.
  • 'C' uses top-down approach

Bottom-Up Design

  • Bottom-Up design method is the reverse of Top-Down approach. It starts from the lowest level component to the highest-level component.
  • It first designs the basic components and from these basic components the higher level component is designed.
  • Object-oriented language such as C++ or java uses a bottom-up approach where each object is identified first.

Modular Approach

  • It is better to divide a large system into modules.
  • In terms of programming, module is logically a well-defined part of program.
  • Each module is a separate part of the program.
  • It is easy to modify a program written with modular approach because changes in one module don't affect other modules of program.
  • It is also easy to check bugs in the program in module level programming.

What is programming language?

The programming languages can be classified into two types -
1. Low level languages
2. High level languages

Low level languages

The languages in this category are the Machine level language and Assembly language.

Machine Level Language

  • Computers can understand only digits signals, which are in binary digits i.e. 0 and 1.
  • So the instructions given to the computer can be only in binary codes.
  • The machine language consists of instructions that are in binary 0 or 1.
  • Computers can understand only machine level language.
  • Writing a program in machine level language is a difficult task because it is not easy for programmers to write instructions in binary code.
  • A machine level language program is error-prone and its maintenance is very difficult.
  • Furthermore machine language programs are not portable.
  • Every computer has its own machine instructions, so the programs written for one computer are not valid for other computers.

Assembly Language

  • The difficulties faced in machine level language were reduced to some extent by using a modified form of machine level language called assembly language.
  • In assembly language instructions are given in English like words, such as MOV, ADD, SUB etc. So it is easier-to write and understand assembly programs.
  • Since a computer can understand only machine level language, assembly language program must be translated into machine language.
  • The translator that is used for translating is called 'assembler'.
  • Although writing programs in assembly language is a bit easier, the programmer has to know all the lower level details related with the hardware of a computer.
  • In assembly language, data is stored in computer registers and each computer has diiferent set of registers. Thus the assembly language program is also not portable.
  • Since the low level languages are related with the hardware, the execution of a low-level program is faster.

High level language

  • High level languages are designed keeping in mind the features of portability i.e. these languages are machine independent.
  • These are English like languages, so it is easy to write and understand the programs of high-level language.
  • While programming in a high level language, the programmer is not concerned with the low level details, and so the whole attention can be paid to the logic of the problem being solved.
  • For translating a high-level language program into machine language, compiler or interpreter is used.
  • Every language has its own compiler or interpreter.
  • Some languages in this category are- C, FORTAN, COBOL, BASIC, Pascal etc.

Translators

  • Translators are just computer programs, which accept a program written in high level or low-level language and produce an equivalent machine language program as output.
  • The three types of translators used are-
    -Assembler
    -Compiler
    -Interpreter

Assembler

  • It is used for converting the code of low-level language (assembly language) into machine level language.

Compiler and interpreters

  • Both are used to convert the code of high-level language into machine language.
  • The high level language is known as source program and the corresponding machine language program is known as object program.
  • Although both compilers and interpreters perform the same task, there is a difference in their working.

Difference between compiler and interpreter

  • A compiler searches all the errors of a program and lists them, while an interpreter checks the errors of a program statement by statement.
  • If the program is error-free, the compiler converts the entire code into machine code, which can then be executed by separate commands. In contrast, after checking one statement, the interpreter converts that statement into machine code and then executes it.
  • This process continues for the interpreter until the last statement of the program or an erroneous statement occurs.

What is procedural and object oriented programming?

Introduction to C Language

Facts about C

  • C was invented to write an operating system called UNIX.
  • C is a successor of B languange which was introducted around 1970.
  • The language was formalized in 1988 by the American National Standard Institute (ANSI).
  • By 1973 UNIX OS almost totally written in C.
  • Today C is the most widely used System Programming Language.
  • Most of the state of the art software have been implemented using C

Why to use C?

  • C was initially used for system development work, in particular the programs that make-up the operating system.
  • C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language.
  • Some example of the use of C might be :
    • Operating System
    • Language Compilers
    • Assemblers
    • Text Editors
    • Network Drivers
    • Modern Programs
    • Data Bases
    • Language Interpreters
    • Utilities

Characteristics of 'C'

  • It has a very small size.
  • The major use of function calls in it.
  • It is a structured language.
  • It has the ability to read low level (Bitwise) programming.
  • The large use of pointers for memory, array, structures and functions.
  • High-level constructs.
  • The program written in C is efficient.
  • C can be compiled on a variety of computers.
  • The C programming language is used to write a variety of applications so that we can say that it is a general-purpose programming language. It is a "System programming language".
  • C supports modular programming means a big program can divide into different modules or functions.
  • It is also a standalone programming language. Due to these characteristics of C it is most popular.
  • C has very fast compilation and execution in comparision to other programming languages.
  • C is easy and free available, You just have to download its software, intall it on your system, and start coding.
  • It has many in-built memory management functions malloc(), calloc(), and alloc() to utilize the memory efficiently.

Properties of C language

  • Simple & efficient : The basic syntax style of implementing C language is very simple and easy to learn.
  • Fast : statically typed programming language are faster than dynamic ones.
  • Dynamic Memory Management : One of the most significant features of C language is its support for dynamic memory management (DMA).
  • Function rich libraries : C comes with an extensive set of libraries with several builtin functions that make the life of a programmer easy.

Elements of C

Every language has some basic elements and grammatical rules. Before programming in C, it is must to know the basic elements of the language. These basic elements are character set, variables, datatypes, constants, keywords, variable declaration, expressions, statements etc. All of these are used to construct a C program.

C Character Set

Escape Sequences/Execution Characters

Characters like newline, tab, backspace can't be displayed like normal characters. To display them we use '\'(backslash) and some character from the C character set.
Some escape sequences ↓

Reserved Words / Keywords

Identifiers

Data Types

Constants

Constant is a value that cannot be changed during execution of the program.

Variables

Declaration of Variables

  • Variables must be declared before they are used in program.
  • Declaration of a variable specifies its name and datatype.

Syntax ↓

                            
int x;
float salary;
char grade;
int max;
                            
                        

Initialization of variables

  • When a variable is declared it contains undefined value commonly known as garbage value.
  • Assigning value to a variable is known as initialization of the variable.

For example ↓

                        
int a = 5;
float x = 8.9, y = 10.5;
                        
                    

Structure of C program

                
Header file 

int main()
{
    // statements
}
                
            
                
#include <stdio.h> 

int main()
{
    // code
}
                
            

Some more header files and their functions

Program to print 'hello' on the screen

                        
#include <stdio.h> 

int main()
{
    printf("hello\n"); // here '\n' is a escape sequence which gives a newline
    printf("how are you?")
}
                        
                    

output ↓

                    
hello 
how are you?
                    
                

Program to calculate sum of two numbers

                    
#include <stdio.h>

int main()
{
    int a, b, sum;

    printf("Enter first number : ");
    scanf("%d", &a);

    printf("Enter second number : ");
    scanf("%d", &b);

    sum = a + b;
    
    printf("The sum is : %d", sum);
    
    return 0;
}
                    
                

Program to calculate area of a rectangle

                                        
#include <stdio.h>

int main()
{
    int l, b, area;
    printf("Enter length :");
    scanf("%d", &l);

    printf("Enter breadth :");
    scanf("%d", &b);

    area = l * b;
    
    printf("The area is : %d", area);

    return 0;
}
                    
                

Program to calculate area of a circle

                                       
#include <stdio.h>

int main()
{
    int r;
    float area;
    printf("Enter radius :");
    scanf("%d", &r);

    area = 3.14 * r * r;
    
    printf("The area is : %f", area);

    return 0;
}
                    
                

Types of Errors

There are two types of error logical and syntax error

Warning vs Error

Return type

Programs

Write a C program to swap 2 number using 3rd variable.

                    
#include <stdio.h>
int main()
{
    int a, b, c;

    printf("Enter the value of 'a' before swap : ");
    scanf("%d",&a);
    printf("Enter the value of 'b' before swap : ");
    scanf("%d",&b);

    c = a;
    a = b;
    b = c;

    printf("The value of 'a' and 'b' after swap are : %d \t %d",a,b);
    
    return 0;
}
                    
                

Write a C program to swap two numbers without using 3rd variable

                    
#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter the value of 'a' and 'b' before swap : ");
    scanf("%d %d", &a, &b);
    // above we are only using single statement to input two values

    a = a + b;
    b = a - b;
    a = a - b;

    printf("The value of 'a' and 'b' after swap are : %d \t %d", a, b);

    return 0;
}
                    
                

Write a program to input basic salary of an employee and then calculate net salary on the basis of hra, da, ta, tax.
hra = 5% of basic salary
da = 6% of basic salary
ta = 3% of basic salary and tax = 2% of basic salary
net salary = (basic salary + hra + da + ta) - tax;

                    
#include <stdio.h>

int main()
{

    float bs, hra, da, ta, tax, netS;
    printf("Enter employee's basic salary : ");
    scanf("%f", &bs);

    hra = 0.05 * bs;
    da = 0.06 * bs;
    ta = 0.03 * bs;
    tax = 0.02 * bs;

    netS = (bs + hra + da + ta) - tax;

    printf("Net salary of employee is : %f", netS);

    return 0;
}                            
                    
                

Write a C program to convert temperature from Fahrenheit to celsius.

                    
#include <stdio.h>

int main()
{

    float fahren, celsius;
    printf("Enter the temperature in Fahrenheit : ");
    scanf("%f", &fahren);

    celsius = (fahren - 32) * 1.8;

    printf("Temperature in celsius is : %f", celsius);

    return 0;
}                            
                    
                

Write a C program to calculate simple interest and compound interest

                    
#include <stdio.h>
#include <math.h>

int main()
{
    float P, R, T, SI, CI;
    printf("Enter the value of Principle Amount : ");
    scanf("%f", &P);
    printf("Rate of Ineterest : ");
    scanf("%f", &R);
    printf("Time (in years) : ");
    scanf("%f", &T);

    SI = (P * R * T) / 100;
    CI = pow(P * (1 + (R / 100)), T);
    printf("The Simple Interest is : %f\n", SI);
    printf("The Compound Interest is : %f\n", CI);

    return 0;
}
                    
                

Write a program to check whether any number is positive, negative or zero

                    
#include <stdio.h>

int main()
{
    int num;
    printf("Enter a number : ");
    scanf("%d", &num);
    if (num < 0)
        printf("The entered number is negative");
    else if (num > 0)
        printf("The entered number is positive");
    else
        printf("The entered number is zero");

    return 0;
}
                    
                

Input 4 marks of subject and calculate percentage
and make result according to following
percentage >= 70 → 1st division with honours
60 <= percentage <=70 → 1st division
50 <= percentage <=60 → second division
40 <= percentage <=50 → third division
percentage < 40 → failed

                                        
#include <stdio.h>

int main()
{
    int m1, m2, m3, m4;
    float per;
    printf("Enter m1, m2, m3 and m4 : ");
    scanf("%d%d%d%d", &m1, &m2, &m3, &m4);
    per = (m1 + m2 + m3 + m4) / 4;
    if (per >= 70)
        printf(" 1st division with honours");
    else if (per >= 60)
        printf(" 1st division");
    else if (per >= 50)
        printf(" 2nd division");
    else if (per >= 40)
        printf(" 3rd division ");
    else
        printf("failed\n");
    return 0;
}
                                        
                                    

Note

We cannot use ';' in main function, conditional statement and looping statement.

References