Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Overview of C

Introduction

‘Basic Combined Programming Language’ (BCPL), called B (1960’s)

C, modified B – Dennis Ritchie (1972, Bell Laboratories)

UNIX operating system was coded almost entirely in C

C is well suited for writing both system software and business packages

Rich set of built-in functions and keywords

C is highly portable
Why Study C?
Format of simple C programs

int main() Function name


{ Start of program
….
…. Program statements
….
return 0
} End of program
Format of simple C programs
A collection of functions

Every program must have exactly one special function named as main ()

Program always starts from there

The empty pair of parentheses immediately following main() indicates no arguments

All the statements between the two braces form the function body

Every statement should end with a semicolon (;) mark

Statements are executed one by one


Sample Program

#include<stdio.h>

int main()
{
//......1st Print Line.......
printf("\n hello world");
//......2nd Print Line.......
printf("\n welcome");
return 0;
}
Sample Program

#include<stdio.h>

int main()
{
//......1st Print Line.......
printf("\n hello world, \n welcome");
return 0;
}
Some Commonly Used Terms
Some important points

C is a free-form language

C does make a distinction between uppercase and lowercase letters

In C, everything is written in lowercase letters

Uppercase letters are used to represent constants

Braces group program statements together

A proper indentation of braces and statements would make a program easier to read and
debug
Another Program
//Program Addition
#include<stdio.h>

int main()
{
int number; Declarations
float amount;
number=100; Assignment
amount=30.75+75.35;
printf("\n number=%d", number);
printf("\n amount=%5.2f",amount);
return 0;
}
Some important points

In C, all the variables should be declared before its use, i.e. to tell the compiler about the
data type of those variables

In printf(), %d, %f - format specifier, specifies the type of value

%5.2f – the output must be in floating point, with five places in all and two places to the
right of decimal point
Desirable Programming Style
Clarity
The program should be clearly written.
It should be easy to follow the program logic

Meaningful variable names


Make variable/constant names meaningful to enhance program clarity

Program documentation
Insert comments in the program to make it easy to understand
Never use too many comments

Program indentation
Use proper indentation
Structure of the program should be immediately visible
Indentation Example: Good Style
Indentation Example: Bad Style
Executing a ‘C’ Program
Creating or writing the program

Compile the program

Link the program with functions that are needed from C library

Execute the program


Interpreter vs. Compiler
Some Terminologies
Flow Chart: Basic Symbols
Example 1: Adding three numbers
Example 2: Larger of two numbers
Example 3: Largest of three numbers
Example 3: Largest of three numbers
Example 4: Sum of 1st N natural numbers
Example 4: Sum of 1st N natural numbers
Example 5: SUM = 12 + 22 + 32+…+N2
Example 6: Computing factorial
Example 6: Computing factorial
x
Example 7: Computing e series up to N terms
Home Work

Computing ex series up to 4 decimal places


Home Work: Solution
Home Work: Grade Computation
Home Work: Roots of a quadratic equation
Steps to be followed in UNIX/ LINUX system

The file is created by either ‘gedit’ or ‘vi’ command as follows:


gedit filename/ vi filename
filename can consist of letters, digits and special characters, followed by (.c)
The compilation command under LINUX is: gcc filename
Linking is the process of putting together other program files and functions that are
required by the program
Under LINUX, linking is automatically done when gcc command is used
The compiled and linked program is called the executable object code and the default file
name is a.out
The command to execute the program is: ./a.out
Some systems use different compilation for linking mathematical functions:
gcc filename -lm

You might also like