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

Programming in C

Session 1
Agenda
• Overview of C
Introduction to C-Language
History of ‘C’ Language
• Edit Compile Link Execute- A Cyclic Process
• My First C Program
• Dissecting a C Program
• Structure of C Programs
• Constants, Variables and Data Types in C
• Escape Sequence
• Arrays: Single and Multidimensional
Introduction to C-Language
• C is a general-purpose language which has been closely
associated with the UNIX operating system
• C is often called a "Middle Level" programming language
• Currently, the most commonly-used language for
embedded systems
• Embedded Systems- Handheld Electronic Devices
• “High Level assembly”
• Very portable: compilers exist for virtually every processor
• Easy-to-understand compilation
• Produces efficient code- Memory Efficient
• Designed for systems programming
• Operating systems
• Utility programs
• Compilers
History of C-Language
• Developed between 1969 and 1973 along with Unix
• Due mostly to Dennis Ritchie, an employee from Bell Labs (AT&T)
• Few key ideas of C are borrowed from BCPL, developed by Martin
Richards
• Influenced by BCPL, though indirectly through the language B,
which was written by Ken Thompson in 1970 at Bell Labs
• BCPL and B are "type less" languages where as C has Data Types
• Dennis Ritchie wrote C in 1972 at Bell Labs
• Later in 1978, a publication The C Programming Language written
by Kernighan & Ritchie enlightened and created revolution
• In 1983, the American National Standards Institute (ANSI) formed
a committee to provide a modern, comprehensive definition of C
• Finally in 1988, ANSI standard, or "ANSI C", was completed late
1988.
Edit-Compile-Link-Execute A Cyclic Process

EDIT COMPILE

 Process of developing a  Validating the correctness


Program of the program
 Correcting a Program  Syntax check, Error check
 Storing the Program  Translating a program so
 Enhancing the Program that it can be understood
by the Computer
EXECUTE LINK
 Programming Languages
 Executable files perform a
have built-in functions
desired operation or an
 Functions make our work
activity
easier
 Functions are defined and
stored in Library Files
 Compiler read the definition
of function from library
file(s) and finally creates an
executable file
My First C Program
#include <stdio.h>

void main()
{
printf(“Hi
There!!!\n”);
}
Analysing a C Program-”Hi
There!!!”
Header File which
#include <stdio.h> stores definition of
printf function
Program mostly a
void main() collection of
functions
{ “main” function
printf(“Hi There!!! special: the entry
point
n”); “void” qualifier
indicates function
} does not return
anything
I/O performed by a
library function: not
included in the
language
Structure of C Programs
• Layout of C Programs
Pre-processor directives
Global declaration
main()
{
Local variable declaration
List of statements
}

Function1()
{
List of statement
}
……
FunctionN()
{
List of Statement
}
Structure of C Programs
• Layout of C Programs
Pre-processor directives
Global declaration
main()
{
Local variable declaration
List of statements
}

Function1()
{
List of statement
}
……
FunctionN()
{
List of Statement
}
Constants, Variables and Data
Types in C
C Identifiers and Keywords
• Identifier
Identifier is a alphanumeric character in C
It is a name giver to variables, function and user defined entities
An identifier can be composed of letters (both uppercase and
lowercase letters), digits and underscore '_' only

• Character Set
Character set are the set of alphabets, letters and some special
characters that are
 Uppercase: A B C .................................... X Y Z
 Lowercase: a b c ...................................... x y z
 Digits: 0 1 2 3 4 5 6 7 8 9
 Special Characters: , <> . _();$:%[]#?’&{}”?^/|-\~+ Space
 White space Characters: blank space, new line, horizontal tab, carriage
return and form feed
C Identifiers and Keywords
• Identifier
Identifier is a alphanumeric character in C
It is a name giver to variables, function and user defined entities
An identifier can be composed of letters (both uppercase and
lowercase letters), digits and underscore '_' only

• Character Set
Character set are the set of alphabets, letters and some special
characters that are
 Uppercase: A B C .................................... X Y Z
 Lowercase: a b c ...................................... x y z
 Digits: 0 1 2 3 4 5 6 7 8 9
 Special Characters: , <> . _();$:%[]#?’&{}”?^/|-\~+ Space
 White space Characters: blank space, new line, horizontal tab, carriage
return and form feed
C Identifiers and Keywords
• Identifier
Identifier is a alphanumeric character in C
It is a name giver to variables, function and user defined entities
An identifier can be composed of letters (both uppercase and
lowercase letters), digits and underscore '_' only

• Character Set
Character set are the set of alphabets, letters and some special
characters that are
 Uppercase: A B C .................................... X Y Z
 Lowercase: a b c ...................................... x y z
 Digits: 0 1 2 3 4 5 6 7 8 9
 Special Characters: , <> . _();$:%[]#?’&{}”?^/|-\~+ Space
 White space Characters: blank space, new line, horizontal tab, carriage
return and form feed
C Variables
• Variable
Variables are memory location in computer's
memory to store data
Variable should have unique name
Variables are symbolic representation of a memory
location
Example: sum, _Sum, Result, result_data

• Rules for writing variable name in C


Variable name can be composed of letters (both
uppercase and lowercase letters), digits and
underscore '_' only.
First letter should be either alphabet or _
C Constants
• Constants
Constants are the terms that can't be changed
during the execution of a program
They are read only variables
Constants are declared using ‘const’ keyword

Example:
main()
{
const float pi = 3.14;
}
C Data Types
• Fundamental data type
int (signed/unsigned)(2,4Bytes): used to store integers.
float, double(4,8Bytes): used to store a decimal number.
char (1Byte): used to store characters

• Derived Data Types


 struct, union, typedef
 struct and union will be covered in detail in the later
session
C Data Types
• Fundamental data type
int (signed/unsigned)(2,4Bytes): used to store integers.
float, double(4,8Bytes): used to store a decimal number.
char (1Byte): used to store characters

• Derived Data Types


 struct, union, typedef
 struct and union will be covered in detail in the later
session
C Data Types
• Fundamental data type
int (signed/unsigned)(2,4Bytes): used to store integers.
float, double(4,8Bytes): used to store a decimal number.
char (1Byte): used to store characters

• Derived Data Types


 struct, union, typedef
 struct and union will be covered in detail in the later
session
C Escape Sequence
• C provides a way to enter backspace, enter(newline), tab, “,? etc
which either cannot be typed or has special meaning
• Escape Sequence helps us to achieve it by attaching a (\) before
some characters
• The backlash (\) guides the compiler to interpret them in a
special way
Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
C Arrays
• An Arrays can store a fixed-size sequential collection of
elements of the same type
An array is used to store a collection of data
An array as a collection of variables of the same type
All arrays consist of contiguous memory locations
To access a value, one requires Array name and index number
Programming in C
Session 1 Part -5
C Multidimensional Array
• C programming language allows multidimensional arrays
• Declaring a multidimensional array
 data type array_name[size1][size2]….[sizeN];
 An example of 3-Dimensional integer array
int Roll_number[2][2][1]

• 2-dimensional arrays are widely used


• In computer memory, a 2-D array is represented as a matrix with
rows and column
 int Roll_Number[4][2] is a 2-D array with 4 Row and 2 Column
C 2-D Array
• Initialising a 2-D array
 int Roll_Number[4][2]={{1,2},{3,4},{5,6},{7,8}};

 Roll_Number[2][1] will fetch data from 3rd row and second column
i.e. Roll_Number[2][1]=6
C Standard Library

• C programs consist of pieces/modules called functions


• A programmer can create his own functions
• Advantage: the programmer knows exactly how it works
• Disadvantage: time consuming
• Programmers will often use the C library functions
• Use these as building blocks
• Avoid re-inventing the wheel
• If a premade function exists, generally best to use it rather than write
your own
• Library functions carefully written, efficient, and portable
Thank You

You might also like