0% found this document useful (0 votes)
10 views

Module 1 Part 1

PPT to learn C programming

Uploaded by

Parth Bhad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 1 Part 1

PPT to learn C programming

Uploaded by

Parth Bhad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

Module – I

BASICS OF C
What will you learn?
 What is Programming Language
 Types of Programming Languages
 Introduction to C Language
 Structure of C Program
 Compilation and Execution of C Programs
WHAT IS
PROGRAMMING ? computer
Programming is letting the
to learn
how to do the things.
-------------------------------------
Programming is giving training to computer
to do things
as it is non – intelligent body
------------------------------------
Programming is meant for
solving the problems by the Computer
PROGRAMMING LANGUAGES
 Programming language is a notations, which are used to
create(write) a programs.

 Program languages are used to :

To write(create) programs / software.

To control operations of computer.

To express algorithms in programs


TYPES OF PROGRAMMING
LANGUAGES
To write a program for a computer, we must use a
computer language. Over the years computer languages have
evolved from machine languages to natural languages.

1. 1940’s Machine level Languages

2. 1950’s Symbolic Languages/Assembly Language

3. 1960’s High-Level Languages


MACHINE LANGUAGES
 In the earliest days of computers, the only programming languages
available were machine languages.

 Each computer has its own machine language, which is made of


streams of 0’s and 1’s.

 Instructions in machine language must be of 0’s and1’s because


the computers are made up of electronic components those can be
in one of two states: off or on.

 The off state is represented by 0, the on state is represented by1.

 The only language understood by computer hardware is machine


language.
SYMBOLIC
LANGUAGES:-
In early1950’sAdmiralGraceHopper, A mathematician and
naval officer developed the concept of a special computer
program that would convert programs into machine
language.

 Computer does not understand symbolic language it must


be translated to the machine language.

 A special program called assembler translates symbolic


code into machine language.
HIGH LEVEL
LANGUAGES:-
1. Symbolic languages greatly improved programming
efficiently but they still required programmers to
concentrate on the hardware that they were using.

2. Working with symbolic languages was also very tedious


because each machine instruction has to be individually
coded.

3. The desire to improve programmer efficiency and to change


the focus from the computer to the problem being solved
led to the development of high-level language.
INTRODUCTION TO ‘C’
LANGUAGE
C language facilitates a very efficient approach to the development
and implementation of computer programs. The History of C started in
1972 at the Bell Laboratories, USA, where Dennis M. Ritchie proposed this
language. In 1983 the American National Standards Institute (ANSI)
established committee whose goal was to produce “an unambiguous and
machine independent definition of the language C” which still retaining its
spirit.

C is the programming language most frequently associated with


UNIX. Since the 1970s, the bulk of the UNIX operating system and its
applications have been written in C. Because the C language does not
directly rely on any specific hardware architecture, UNIX was one of the
first portable operating systems. In other words, the majority of the code
that makes up UNIX does not know and does not care which computer it is
actually running on.
CONTINUED.,
C was first designed by Dennis Ritchie for use with UNIX on DEC PDP-11
computers. The language evolved from Martin Richard's BCPL, and one of its earlier
forms was the B language, which was written by Ken Thompson for the DEC PDP-7.
The first book on C was The C Programming Language by Brian Kernighan and
Dennis Ritchie, published in1978.

In 1983, the American National Standards Institute (ANSI) established a


committee to standardize the definition of C. The resulting standard is known as
ANSI C, and it is the recognized standard for the language, grammar, and a core set
of libraries. The syntax is slightly different from the original C language, which is
frequently called K&R for Kernighan and Ritchie. There is also an ISO (International
Standards Organization) standard that is very similar to the ANSI standard.

It appears that there will be yet another ANSI C standard officially dated
1999 or in the early 2000 years; it is currently known as "C99."
Learning Analogy for
C

Alphabets Words Sentences Paragraphs

Grammar
Rules
Learning Analogy for
C

Character Programs
Tokens Statements
Set

Syntax
Rules
PROGRAMMING LANGUAGE
TRANSLATORS
It can classified into 4 types.
1.Compiler
2.Interpreter
3.Assembler
4.Linker
COMPILER:-
The software that reads a program written in high level language and
translates it into an equivalent program in machine language is called as
compiler.

The complier converts the entire source code into machine level
program at a time.

Source Program:-

The program written by the programmer in high level language is called


source program.

Object Program:-

The program generated by the compiler after translation is called as


object program.
INTERPRETER:-
The software that reads a program written in high level
language and translates it into an equivalent program in
machine language is called as compiler.

The interpreter takes 1 statement, translates it,


executes it & then again takes the next statement.
ASSEMBLER:-
The software that reads a program written in assembly language
and translates it into an equivalent program in machine language is called
as assembler.

Linker:-
A linker or link editor is a computer program that takes one or
more object files generated by a compiler and combines them into a
single executable file, library file, or another object file.
BRIEF HISTORY OF C

1. The C programming language is a structure oriented programming


language, developed at Bell Laboratories in 1972 by Dennis Ritchie.
2. C programming language features were derived from an earlier
language called “B” (Basic Combined Programming Language –
BCPL).
3. Many of C’s ideas & principles were derived from the earlier
language B, thereby naming this new language “C”.
4. In 1978, Dennis Ritchie and Brian Kernighan published the first
edition “The C Programming Language” and is commonly known as
K&R C.
TAXONOMY OF C LANGUAGE
WHY IS C POPULAR

1. It is reliable, simple and easy to use.


2. C is a small, block-structured programming language.
3. C is a portable language, which means that C programs
written on one system can be run on other systems with
little or no modification.
4. Executing C has one of the largest assortments of
operators, such as those used for calculations and data
comparisons.
5. Although the programmer has more freedom with data
storage, the languages do not check data type accuracy
for the programmer.
PROGRAM
 Program is a collection(set) of statements, that perform specific task.
or

Program is a sequence of instructions, that perform specific operation.

 Task is a logical unit of work. Ex: c = a + b.

/* A C program to find sum of two no's */


#include<stdio.h>
main()
{
int a, b, c;
a = 23;
b = 32; Statements
c = a + b;
printf(“\n Sum of a + b : %d”, c);
}
WHAT IS STATEMENT?
 A statement in C programming refers to a single line of

code or a group of lines that execute a specific task or


operation.

 It may include expressions, declarations, or control


structures.

Example:-

int a,b,c; // variable declaration statement

int f=20; // variable initialization statement

printf(“welcome to c”); // output statement


PROCEDURAL ORIENTED LANGUAGES
 Procedural oriented languages are oriented around
procedures.
 In procedural oriented programming, large programs can be
divided into subprogram called functions.
 Function is block of statements that perform some operation.
 Procedural Oriented Programming support top-down design
approach.
Top-down design
Main Function

Function 1 Function 2 Function 3

Function 4 Function 5 Function 6 Function 7


OBJECT ORIENTED LANGUAGES(OOP)
 Object oriented languages are oriented around objects.
 An object is run time entity (Ex : Student, Book, Table)
 In object oriented programming, small programs can be
combined into large.
 Object Oriented Programming support Bottom-design design
approach.
 Example : C++,JAVA

Main Function

Function 1 Function 2 Function 3

Function 4 Function 5 Function 6 Function 7

Bottom up
DIFFERENCE BETWEEN PROCEDURAL AND
OBJECT ORIENTED LANGUAGES
 Procedural and Object oriented languages are high-
level languages:
Procedural Oriented Languages Object Oriented Languages
In procedural oriented language, In object oriented language, large
large programs are divided into sub- programs are divided into small parts
programs called “functions “. called “objects” .
It support top-down approach. It support bottom-up approach.
Problems are solved top to bottom. Problems are solved bottom to top.
To add new data and functions not To add new data and functions are so
so easy. easy.
Partially support Object oriented Fully support Object oriented
programming concepts. programming concepts.
Programs are oriented around Programs are oriented around
functions. objects.
Example : C, PASCAL, BASIC Example : C++, JAVA, .NET
CHARECTERESTICS OF A C
PROGRAM
1. Middle level language.

High Level Middle Level Low Level

High level languages Middle level languages don’t Low level languages
provide almost everything provide all the built-in functions provides nothing
that the programmer might found in high level languages, other than access to
need to do as already built but provides all building blocks the machines basic
into the language. that we need to produce the result instruction set.
we want.

Examples: Java,
Examples: C, C++ Examples:
Python Assembler
CHARECTERESTICS OF A C
PROGRAM
1. Small size – has only 32 keywords.
2. Extensive use of function calls- enables the end user to
add their own functions to the C library.
3. Supports loose typing – a character can be treated as an
integer & vice versa.
4. Pointer implementation - extensive use of pointers for
memory, array, structures and functions.
5. It produces efficient programs.
6. It can be compiled on a variety of computers.
CREATING AND RUNNING
PROGRAMS
To create and run the programs the following steps are to
be performed:

1. Writing and editing programs


2. Compiling the program
3. Linking the program with library modules
4. Executing the program
COMPILATION AND EXECUTION

Pre
Source Compil Object Executable
process Linker
File or
er Files Files

Library
Library Files
Files
STRUCTURE OF C
PROGRAM
The program written in C language follows this basic
structure.

1. Documentation section
2. Linking section or Pre process or statements
3. Definition section
4. Global declaration section
5. Main function section
{
Declaration Section
Executable section
}

6. Sub program or function section


DOCUMENT SECTION
 To enhance the readability of the program, programmers can

provide comments about the program in this section.

 Comments can be used any where in the program but too

many comments are avoided. It is useful for documentation.

 Comments can be classified in to 2 types.

1.Single line comments

2.Multi Line Comments

 Single Line comments are represented by //

 Where as Double Line Comments are represented by


/*………….*/
HEADER FILE SECTION
 A header file contains the information required by the

compiler when calls to the library functions used in the


program occur during compilation.

 Each header file by default is extended with .h.

 The file should be included using #include directive.

Example:

#include<stdio.h> or #include “stdio.h”

 In this example file is included i.e. all the definitions and

prototypes of functions defined in this file are available in the


current program.
HEADER FILE SECTION
Some of the C Header files:
 stddef.h – Defines several useful types and macros.

 stdint.h – Defines exact width integer types.

 stdio.h – Defines core input and output functions

 stdlib.h – Defines numeric conversion functions, pseudo-

random number generator, and memory allocation

 string.h – Defines string handling functions

 math.h – Defines common mathematical functions.


RULES OF C
LANGUAGE
 C is made of functions.

 A function is a set of statements which perform a specific task(job).

 Structure of a function definition: A function will have a name.

 A C statements ends with a semi-colon (;) which indicates to the

compiler end of the statement.

 Average of compiler provide 500 functions.

 C language is case-sensitive but not space-sensitive.

 ANSI C has 32 keywords called as reserve words.

Note: Though main () is not a keyword it has to be written in lower


case letter.
C - TOKEN
 C Tokens are basic building blocks of C program.
 C programs can be crated with different set of C tokens :

Token

Keywords Identifiers Variables Constants Operators Strings

float, const r, ar, pi, main r, ar pi, \n


#*= , ; Area of circle

/* A C program to find area of circle */


#include<stdio.h>
main()
{
float r, ar;
const float pi = 3.14;
r = 5.58;
ar = pi*r*r;
printf(“\n Area of circle : %f “, ar)
C KEYWORDS
C has a set of reserved words known as keywords that cannot be used as
identifier. All the keywords are basically a sequence of characters that have a fixed
meaning. All the keywords must be written in small case letters.

Note: The keywords with at least one uppercase letter can be used as an identifier.

The following is the list of C keywords:

auto break case char


const continue default double
else enum extern float
for goto int long
register return short signed
sizeof struct switch typedef
union unsigned void volatile
do if static while
IDENTIFIER
 Identifier refers name of variable, constant, function or
array.
Identifier is a name of program data object / element.
variable

constant

Identifier function
name

array

pointer

 Identifier used to distinguish that data element from


other elements.
LITERAL CONSTANTS
 The A constant is a value that doesn’t change during the execution of a
program. These are generally declared before main function. These fixed values are
also called literals.

 Constants can be of any of the basic data types like an integer literal, a floating literal, a
character literal, or a string literal.

Integer literals

 An integer literal can be a decimal, octal, or hexadecimal constant.

 Examples: 25, +34, -45

Floating-point literals

 A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part. You can represent floating point literals either in decimal form or
exponential form.

 Examples: 3.14159, 314159E-5L


LITERAL CONSTANTS
Character constants

 Character literals are enclosed in single quotes, e.g., 'x' and can be stored in

a simple variable of char type.

 A character literal can be a plain character (e.g., 'x’)

Note:-

 Character constants have integer values known as ASCII values (ASCII-

American Standard code for Information Interchange).

 Since each character represents an integer value, it is also possible to

perform arithmetic operations on character constants.

 Spacebar is also a character constant.


LITERAL CONSTANTS
String literals

 String literals or constants are enclosed in double quotes "". A string contains

characters that are similar to character literals: plain characters, escape

sequences, and universal characters.

 Examples:

"hello, dear"

"hello, dear"
BACKSLASH CHARACTER
CONSTANTS:
 C supports some special backslash character constants that are used in

output functions. These are also known as escape sequences.

 ‘\a’- audible alert(bell) ‘\v’- vertical tab

 ‘\b’- backspace. ‘\’’- single quote.

 ‘\f’- form feed ‘\”’- double quote.

 ‘\n’- new line ‘\?’- question mark

 ‘\r’- carriage return ‘\0’- Null


VARIABLES
 A variable is defined as a meaningful name given to the data storage location
in the computer memory. A variable value can be allowed to change during
the execution of a program.

 A variable to be used in a program must be declared.

 To declare a variable, the following syntax must be followed:

<datatype> <variable_name>;
 Example:

1. int rollno;
Valid Variable Declaration
2. char grade;
3. float temperature;

int 1variable Invalid Variable Declaration


char $name
WHAT IS DECLARATION?
 Declaration of a variable in a computer programming language is a

statement used to specify the variable name and its data type.

 Declaration tells the compiler about the existence of an entity in

the program and its location.

Note:

 All declaration statements must end with a semi-


colon (;)

 The Data type and the Value used to store in the


Variable must match.

Example: int a,b,c;//Declaring 3 variables


RULES OF VARIABLE
DECLARATION
 Variable should always be declared at the beginning of the

functions.

 A variable name should begin with alphabet or underscore. And

the remaining character can be alphabets digits or underscore.

 A variable name can’t exceed 31 characters.

 Special characters are not allowed with in the variable name

including spaces. Expect underscore.

 Keywords cannot be used as variable names.

Note: Uninitialized variables contain junk or garbage values.


TYPES OF INITIALIZATION
 Initialization is the process of assigning the value

to the variable.

 If the value is not assigned to the Variable, then

the process is only called a Declaration.

 Always use the ‘=’ sign to initialize a value to the

Variable.

 Initialization can be classified into 2 types.

1.Static initialization

2.Dynamic initialization
TYPES OF INITIALIZATION
1.Static initialization:-
 In this method, the variable is assigned a value in
advance.

 Here, the values are assigned in the declaration statement.

 Static Initialization is also known as Explicit Initialization.

Example:-

int a=9; //static initialization

float a=0.5,b=9.888; //static initialization


TYPES OF INITIALIZATION
2.Dynamic initialization:-
 In this method, the variable is assigned a value at the run time.

 The value is either assigned by the function in the program or by the


user at the time of running the program.

 The value of these variables can be altered every time the


program runs.

 Dynamic Initialization is also known as Implicit Initialization.

Example:-

int speed; //Variable declaration

printf(“enter the value of speed”);

scanf(“%d”,&speed);
DATA TYPES
A data type consists of the values it represents and the operations
defined upon it.
BASIC DATA TYPES IN
CData types are used to specify two things to the compiler:
1. How much memory has to be allocated for an identifier to
store the data.

2. What type of data item has been stored in an identifier.


Data type Size in bytes Range Format specifier
char 1 -128 to + 127 %c

unsigned char 1 0 to 255 %c

signed char 1 -128 to + 127 %c

Int 2 -32768 to +32767 %d

unsigned int 2 0 to 65535 %u

signed int 2 -32768 to +32767 %d

short int 2 -32768 to +32767 %d

unsigned short int 2 0 to 65535 %d

signed short int 2 -32768 to +32767 %d

long int 4 -2147483648 to +2147483647 %ld

unsigned long int 4 0 to 424967295 %lu

signed long int 4 -2147483648 to +2147483647 %ld

float 4 3.4 E-38 to +3.4E+38 %f

double 8 1.7E-308 to +1.7E+308 %lf

long double 10 3.4E-4932 to 1.1E+4932 %Lf


INPUT AND OUTPUT
STATEMENTSInputIN C
and Output
Statements in c

Formatted Unformatted
Input/output Input/output
statement statement

-getch()
-printf() -getche()
-scanf() -getchar()
-putchar()
-gets()
-puts()
-putch()
FORMATTED I/O STATEMENTS
IN C
 Formatted I/O functions are used to take various inputs from
the user and display multiple outputs to the user.

 These types of I/O functions can help to display the output


to the user in different formats using the format specifiers.

 These I/O supports all data types like int, float, char, and
many more.
FORMAT SPECIFIERS IN C
INPUT AND OUTPUT
STATEMENTS
OUTPUT Statement:
IN C
 The printf ( ) function is used to display the information required by the user
and also prints the values of the variables.

 It is a predefined function that is already declared in the stdio.h(header file).

 The syntax of the printf ( ) function can be given as

printf (“CONTROL STRING”, Variables list);

 This function accepts two arguments or parameters – control string and


variable list.

 Where variable list is list of variables to be displayed and these are displayed
as formatted in the control string.

 Control string may also contain text, captions, identifiers or any other text
that is to be readable.
EXAMPLE FOR
PRINTF():
//C Program to implement printf() function.

#include<stdio.h> Output:- 20

#include<conio.h>

void main()

int a; //Declaring an int type variable

a=20; //Assigning a value in a variable

printf(“%d”,a); //Printing the value of a variable

getch();

Note:

Always c program execution starts from main().


INPUT AND OUTPUT
STATEMENTS
INPUT Statement: IN C
 The function scanf ( ) is used to read the data from the keyboard.
This function takes the text from keyboards and formats the data
according to the format specified in the control string and then stores
the data in the variables.

 The syntax of the scanf ( ) function can be given as

scanf (“Control string”, &arg1, &arg2, ……, argN);

 The control string specified the type and format of the data that has
to be obtained from the keyboard and stored in the memory
locations pointed by the arguments arg1, arg2, arg3,….., argN.

 Arguments are nothing but the variables used in the program.


EXAMPLE FOR
SCANF():
//C Program to implement scanf() function.

#include<stdio.h>

#include<conio.h>

void main()

int a; //Declaring an int type variable

printf(“enter a value”); //printing a message on the output screen

scanf(“%d”,&a); //taking an integer value from keyboard

printf(“you have entered %d”,a); //displaying the entered value

getch();

}
EXAMPLES:
 It is possible to read and write multiple variables at the
same time
char x;
float y;
int z;

 To input a value for x from Keyboard:

scanf(“%c %f %d”, &x, &y, &z);

 To display the value of x onto Screen:

printf(“%c %f %d”, x, y, z);


UNFORMATTED I/O
STATEMENTS IN C
Why they are called unformatted I/O?

 These functions are called unformatted I/O functions


because we cannot use format specifiers in these
functions.

 Unformatted I/O functions are used only for character


data type or character array/string and cannot be used
for any other datatype.

 These functions are used to read single input from the


user at the console and it allows to display the value at the
console.
UNFORMATTED I/O
STATEMENTS IN C
getch():
 The function reads a single character from the keyboard by the
user but doesn’t display that character on the console screen and
immediately returned without pressing enter key.

 This function is declared in conio.h(header file).

Syntax: Note: getch() is also used for hold


the screen.

getch();

or

variable-name = getch();
EXAMPLE FOR
GETCH():
//C Program to implement getch() function.

#include<stdio.h>

#include<conio.h>

void main()

printf(“enter a character”); //Reads a character but not


display

getch();

Output: enter a character


UNFORMATTED I/O
STATEMENTS IN C
getche():
 The function reads a single character from the keyboard by
the user displays it on the console screen and immediately
returned without pressing enter key.

 This function is declared in conio.h(header file).

Syntax:

getche();

or

variable-name = getche();
EXAMPLE FOR
GETCHE():
//C Program to implement getche() function.

#include<stdio.h>

#include<conio.h>

void main()

printf(“enter a character”); //Reads a character but not


display

getche();

Output: enter a character g


UNFORMATTED I/O
STATEMENTS IN C
getchar():
 The function is used to read only a first single character
from the keyboard whether multiple characters is
typed by the user and this function reads one character
at one time until and unless the enter key is pressed.

 This function is declared in stdio.h(header file)

Syntax:

variable-name = getchar();
EXAMPLE FOR
GETCHAR():
//C Program to implement getchar() function.

#include<stdio.h>

#include<conio.h>

void main()

char ch; //Declaring a character type variable

printf(“enter a character”);

ch=getchar(); //Taking the character from the keyboard

printf(“%c”,ch); //Displays the value of ch

getch();

Output: enter a character g

g
UNFORMATTED I/O
STATEMENTS IN C
putchar():
 The function is used to display a single character at a time
by passing that character directly to it or by passing a
variable that has already stored a character.

 This function is declared in stdio.h(header file)

Syntax:

putchar(variable_name);
EXAMPLE FOR
PUTCHAR():
//C Program to implement putchar() function.

#include<stdio.h>

#include<conio.h>

void main()

char ch; //Declaring a character type variable

printf(“enter a character”);

ch=getchar(); //Reads a character

putchar(ch); //Displays the value of ch

getch();

Output: enter a character g

g
UNFORMATTED I/O
STATEMENTS IN C
gets():
 The gets() function reads a group of characters or strings
from the keyboard by the user and these characters get
stored in a character array.

 This function is declared in stdio.h(header file)

Syntax:

char str[length of string in number]; //Declare a char


type variable of any length

gets(str);
EXAMPLE FOR GETS():
//C Program to implement gets() function.

#include<stdio.h>

#include<conio.h>

void main()

char ch[50]; //Declaring a character type array of length 50 characters

printf(“please enter some text ”);

gets(ch); //Reads a string

printf(“you have entered: %s",ch); //Displaying a string

getch();

Output: please enter some text welcome to c

You have entered: welcome to c


UNFORMATTED I/O
STATEMENTS IN C
puts():
 In C programming puts() function is used to display a group
of characters or strings which is already stored in a
character array.

 This function is declared in stdio.h(header file)

Syntax:

puts(identifier_name);

You might also like