SlideShare a Scribd company logo
1 of 50
In this session, you will learn to:
Identify the benefits and features of C language
Use the data types available in C language
Identify the structure of C functions
Use input-output functions
Use constructs
Objectives
Identifying the Benefits and Features of C Language
Ken Thompson developed a new language called B.
B language was interpreter-based, hence it was slow.
Dennis Ritchie modified B language and made it a
compiler-based language.
The modified compiler-based B language is named as C.
C language:
Possesses powerful low-level features of second generation
languages.
Provides loops and constructs available in third generation
languages.
Is very powerful and flexible.
C as a Second and Third Generation Language
3
C language:
Offers all essentials of structured programming.
Has functions that work along with other user-developed
functions and can be used as building blocks for advanced
functions.
Offers only a handful of functions, which form the core of the
language.
Has rest of the functions available in libraries. These functions
are developed using the core functions.
Block Structured Language - An Advantage for Modular
Programming
4
Features of the C Language
The features that make C a widely-used language are:
Pointers: Allows reference to a memory location by a name.
Memory Allocation: Allows static as well as dynamic memory
allocation.
Recursion: Is a process in which a functions calls itself.
Bit Manipulation: Allows manipulation of data in its lowest form
of storage.
5
Using the Data Types Available in C language
The types of data structures provided by C can be classified
under the following categories:
Fundamental data types
Derived data types
6
Fundamental Data Types
Fundamental Data Types:
Are the data types at the lowest level.
Are used for actual data representation in the memory.
Are the base for other data types.
Have machine dependent storage requirement.
Are of the following three types:
char
int
float
7
The storage requirement for fundamental data types can be
represented with the help of the following table.
Data Number of bytes on a
32-byte machine
Minimum Maximum
char 1 -128 127
int 4 -2^31 (2^31) - 1
float 4 6 digits of precision 6 digits of precision
double 8 14 digits of precision 14 digits of precision
Fundamental Data Types (Contd.)
8
Derived Data Types
Derived Data Types:
Are represented in memory as fundamental data type.
Some derived data types are:
short int
long int
double float
9
The storage requirement for derived data types can be
represented with the help of the following table.
Data Number of bytes
on a 32-byte
machine
Minimum Maximum
short int 2 -2^15 (2^15) - 1
long int 4 -2^31 (2^31) - 1
double float 8 12 digits of precision 6 digits of
precision
Derived Data Types (Contd.)
10
Defining Data
The syntax for defining data is:
[data type] [variable name],...;
Declaration is done in the beginning of a function.
Definition for various data types is shown in the following
table.
Data definition Data type Memory defined Size (bytes) Value assigned
char a, c; char a
c
1
1
-
-
char a = 'Z'; char a 1 Z
int count; int count 4 -
int a, count =10; int a
count
4
4
-
10
float fnum; float fnum 4 -
float fnum1,
fnum2 = 93.63;
float fnum1
fnum2
4
4
-
93.63
11
Practice: 1.1
Write the appropriate definitions for defining the following
variables:
1.num to store integers.
2.chr to store a character and assign the character Z to it.
3.num to store a number and assign the value 8.93 to it.
4.i, j to store integers and assign the value 0 to j.
Practice: 1.1 (Contd.)
Solution:
1.int num;
2.char chr=’Z’;
3.float num = 8.93;
4.int i, j=0;
13
Defining Strings:
Syntax:
char (variable) [(number of bytes)];
Here number of bytes is one more than the number of
characters to store.
To define a memory location of 10 bytes or to store 9 valid
characters, the string will be defined as follows:
char string [10];
Defining Data (Contd.)
14
Practice: 1.2
Write the appropriate definitions for defining the following
strings:
1.addrs to store 30 characters.
2.head to store 14 characters.
15
Practice: 1.2 (Contd.)
Solution:
1.char addrs[31];
2.char head[15];
16
Identifying the Structure of C Functions
In C language, the functions can be categorized in the
following categories:
Single-level functions
Multiple-level functions
17
Single Level Functions
Single Level Functions:
Consider the following single-level function:
main()
{
/*print a message*/
printf("Welcome to C");
}
In the preceding function:
main(): Is the first function to be executed.
(): Are used for passing parameters to a function.
{}: Are used to mark the beginning and end of a function. These
are mandatory in all functions.
/* */: Is used for documenting various parts of a function.
18
Semicolon (;): Is used for marking the end of an executable
line.
printf(): Is a C function for printing (displaying) constant or
variable data.
Single Level Functions (Contd.)
19
Practice: 1.3
Identify any erroneous or missing component(s) in the
following functions:
1. man()
{
printf("This function seems to be okay")
}
2. man()
{
/*print a line*/
printf("This function is perfect“;
}
20
Practice: 1.3 (Contd.)
3. main()
}
printf("This has got to be right");
{
4. main()
{
This is a perfect comment line
printf("Is it okay?");
}
21
Solution:
1. man instead of main() and semi-colon missing at the end of
the printf() function.
2. mam instead of main() and ‘)’ missing at the end of the
printf() function.
3. ‘}’ instead of ‘{‘ for marking the beginning of the function and
‘{’ instead of ‘}‘ for marking the end of the function.
4. Comment line should be enclose between /* and */.
Practice: 1.3 (Contd.)
22
Multiple Level Functions
The following example shows functions at multiple
levels - one being called by another:
main ()
{
/* print a message */
printf ("Welcome to C.");
disp_msg ();
printf ("for good learning");
}
disp_msg ()
{
/* print another message */
printf ("All the best");
}
23
The output of the preceding program is:
Welcome to C. All the best for good learning.
In the preceding program:
main(): Is the first function to be executed.
disp_msg(): Is a programmer-defined function that can be
independently called by any other function.
(): Are used for passing values to functions, depending on
whether the receiving function is expecting any parameter.
Semicolon (;): Is used to terminate executable lines.
Multiple Level Functions (Contd.)
Practice: 1.4
Identify any erroneous or missing component(s) in the following
functions:
a. print_msg()
{ main();
printf(“bye”);
}
main()
{ printf(“This is the main function”);}
b. main()
{ /*call another function*/
dis_error();
}
disp_err();
{ printf(“Error in function”);}
25
Solution:
a. main() is always the first function to be executed. Further
execution of the program depends on functions invoked from
main(). Here, after executing printf(), the program
terminates as no other function is invoked. The function
print_msg is not invoked, hence it is not executed.
b. The two functions, dis_error() and disp_error, are not
the same because the function names are different.
Practice: 1.4 (Contd.)
26
Using the Input-Output Functions
The C environment and the input and output operations are
shown in the following figure.
C Environment
Standard Error Device (stderr)
Standard Input Device (stdin) Standard Output Device (stdout)
27
Using the Input-Output Functions (Contd.)
These are assumed to be always linked to the C
environment:
stdin - refers to keyboard
stdin - refers to keyboard
stdout - refers to VDU
stderr - refers to VDU
Input and output takes place as a stream of characters.
Each device is linked to a buffer through which the flow of
characters takes place.
After an input operation from the standard input device, care
must be taken to clear input buffer.
Character-Based Input-Output Functions
Character-Based Input-Output Functions are:
getc()
putc()
getchar()
putchar()
The following example uses the getc() and putc() functions:
# include < stdio.h>
/* function to accept and display a character*/
main ()
{char alph;
alph = getc (stdin); /* accept a character */
fflush (stdin); /* clear the stdin buffer*/
putc (alph, stdout); /* display a character*/
}
29
The following example uses the getchar() and
putchar() functions:
# include < stdio.h >
/* function to input and display a character using the
function getchar() */
main () {
char c;
c = getchar ();
fflush (stdin); /* clear the buffer */
putchar (c);
}
Character-Based Input-Output Functions (Contd.)
30
Practice: 1.5
1. Write a function to input a character and display the
character input twice.
31
Practice: 1.5 (Contd.)
Solution:
#include <stdio.h>
void displayTwice(char character)
{
printf("You entered: %c%cn", character, character);
}
int main()
{
char inputCharacter;
printf("Enter a character: ");
scanf("%c", &inputCharacter);
displayTwice(inputCharacter);
return 0;
}
32
Practice: 1.6
1. Write a function to accept and store two characters in
different memory locations, and to display them one after
the other using the functions getchar() and
putchar().
33
Practice: 1.6 (Contd.)
Solution:
/* function to accept and display two characters*/
#include<stdio.h>
main()
{
char a, b;
a=getchar();
fflush(stdin);
b=getchar();
fflush(stdin);
putchar(a);
putchar(b);
}
34
String-Based Input-Output Functions
String-based input-output functions are:
gets()
puts()
The following example uses the gets() and puts() functions:
# include < stdio.h >
/* function to accept and displaying */
main ()
{ char in_str {21}; /* display prompt */
puts ("Enter a String of max 20 characters");
gets (in_str); /* accept string */
fflush (stdin); /* clear the buffer */
puts (in_str); /* display input string */
}
35
Using Constructs
There are two types of constructs in C language:
Conditional constructs
Loop constructs
36
Conditional Constructs
Conditional Constructs:
Requires relation operators as in other programming language
with a slight change in symbols used for relational operators.
The two types of conditional constructs in C are:
if..else construct
switch…case construct
37
The Syntax of the if..else construct is as follows:
if (condition)
{
statement 1 ;
statement 2 ;
:
}
else
{
statement 1 ;
statement 2 ;
:
}
Conditional Constructs (Contd.)
38
Practice: 1.7
1. Write a function that accepts one-character grade code, and
depending on what grade is input, display the HRA
percentage according to the following table.
Grade HRA %
A 45%
B 40%
C 30%
D 25%
39
Practice: 1.8 (Contd.)
Identify errors, if any, in the following function:
#include<stdio.h>
/*function to check if y or n is input*/
main()
{
char yn;
puts("Enter y or n for yes/no");
yn = getchar();
fflush(stdin);
if(yn=’y’)
puts("You entered y");
else if(yn=‘n')
puts("You entered n");
else
puts("Invalid input");
}
40
Syntax of switch…case construct:
switch (variable)
{
case 1 :
statement1 ;
break ;
case 2 :
statement 2 ;
:
:
break;
default :
statement
}
Conditional Constructs (Contd.)
41
Loop Constructs
The two types of conditional constructs in C are:
while loop construct.
do..while construct.
The while loop construct has the following syntax:
while (condition in true)
{
statement 1 ; loop
statement 2 ; body
}
Used to iterate a set of instructions (the loop body) as long as
the specified condition is true.
42
The do..while loop construct:
The do..while loop is similar to the while loop, except that the
condition is checked after execution of the body.
The do..while loop is executed at least once.
The following figure shows the difference between the while loop
and the do...while loop.
Loop Constructs (Contd.)
while
Evaluate
Condition
Execute
Body of
Loop
True
False
do while
Evaluate
Condition
Execute
Body of
Loop
True
False
43
Practice: 1.9
1. Write a function to accept characters from the keyboard until
the character ‘!’ is input, and to display whether the total
number of non-vowel characters entered is more than, less
than, or equal to the total number of vowels entered.
44
Practice: 1.9 (Contd.)
Solution:
#include <stdio.h>
#include <ctype.h>
void compareNonVowelsToVowels() {
char ch;
int nonVowelCount = 0;
int vowelCount = 0;
printf("Enter characters (input '!' to stop):n");
while (1) {
scanf(" %c", &ch); // Note the space before %c to consume leading
whitespace
if (ch == '!') {
break;
}
45
if (isalpha(ch)) {
ch = tolower(ch); // Convert character to lowercase for easier
comparison
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
} else {
nonVowelCount++;
}
}
}
printf("Total non-vowel characters entered: %dn", nonVowelCount);
printf("Total vowel characters entered: %dn", vowelCount);
if (nonVowelCount > vowelCount) {
printf("More non-vowel characters than vowels.n");
} else if (nonVowelCount < vowelCount) {
printf("More vowels than non-vowel characters.n");
} else {
printf("Equal number of non-vowel and vowel characters.n");
}
}
int main() {
compareNonVowelsToVowels();
return 0;
}
46
# include <stdio.h>
main ()
{
char ch; int i = 0;
puts ("Enter a character :");
ch = getchar ();
do
{
putchar (ch);
i = i + 1;
} while (i < 40);
}
Program to print accept a character and display it on the screen.
47
#include <stdio.h>
main()
{
char chr;
int i = 0;
printf ("Enter a character : ");
chr = getchar();
fflush (stdin);
while (i < 10)
{
putchar(chr);
i = i + 1;
}
}
Summary
In this session, you learned that:
C language was developed by Ken Thompson and Dennis
Ritchie.
C language combines the features of second and third
generation languages.
C language is a block structured language.
C language has various features that make it a widely-used
language. Some of the important features are:
Pointers
Memory Allocation
Recursion
Bit-manipulation
Summary (Contd.)
The types of data structures provided by C can be classified
under the following categories:
Fundamental data types: Include the data types, which are used
for actual data representation in the memory.
Derived data types: Are based on fundamental data types.
Fundamental data types:
char, int, and float
Some of the derived data types are:
short int, long int, and double float
Definition of memory for any data, both fundamental and
derived data types, is done in the following format:
[data type] [variable name],...;
49
Summary (Contd.)
In C language, the functions can be categorized in the
following categories:
Single-level functions
Multiple-level functions
For standard input-output operations, the C environment uses
stdin, stdout, and stderr as references for accessing the
devices.
There are two types of constructs in C language:
Conditional constructs
Loop constructs
50

More Related Content

Similar to C Programming ppt for beginners . Introduction

Similar to C Programming ppt for beginners . Introduction (20)

Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Chapter3
Chapter3Chapter3
Chapter3
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Basic c
Basic cBasic c
Basic c
 

Recently uploaded

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

C Programming ppt for beginners . Introduction

  • 1. In this session, you will learn to: Identify the benefits and features of C language Use the data types available in C language Identify the structure of C functions Use input-output functions Use constructs Objectives
  • 2. Identifying the Benefits and Features of C Language Ken Thompson developed a new language called B. B language was interpreter-based, hence it was slow. Dennis Ritchie modified B language and made it a compiler-based language. The modified compiler-based B language is named as C.
  • 3. C language: Possesses powerful low-level features of second generation languages. Provides loops and constructs available in third generation languages. Is very powerful and flexible. C as a Second and Third Generation Language 3
  • 4. C language: Offers all essentials of structured programming. Has functions that work along with other user-developed functions and can be used as building blocks for advanced functions. Offers only a handful of functions, which form the core of the language. Has rest of the functions available in libraries. These functions are developed using the core functions. Block Structured Language - An Advantage for Modular Programming 4
  • 5. Features of the C Language The features that make C a widely-used language are: Pointers: Allows reference to a memory location by a name. Memory Allocation: Allows static as well as dynamic memory allocation. Recursion: Is a process in which a functions calls itself. Bit Manipulation: Allows manipulation of data in its lowest form of storage. 5
  • 6. Using the Data Types Available in C language The types of data structures provided by C can be classified under the following categories: Fundamental data types Derived data types 6
  • 7. Fundamental Data Types Fundamental Data Types: Are the data types at the lowest level. Are used for actual data representation in the memory. Are the base for other data types. Have machine dependent storage requirement. Are of the following three types: char int float 7
  • 8. The storage requirement for fundamental data types can be represented with the help of the following table. Data Number of bytes on a 32-byte machine Minimum Maximum char 1 -128 127 int 4 -2^31 (2^31) - 1 float 4 6 digits of precision 6 digits of precision double 8 14 digits of precision 14 digits of precision Fundamental Data Types (Contd.) 8
  • 9. Derived Data Types Derived Data Types: Are represented in memory as fundamental data type. Some derived data types are: short int long int double float 9
  • 10. The storage requirement for derived data types can be represented with the help of the following table. Data Number of bytes on a 32-byte machine Minimum Maximum short int 2 -2^15 (2^15) - 1 long int 4 -2^31 (2^31) - 1 double float 8 12 digits of precision 6 digits of precision Derived Data Types (Contd.) 10
  • 11. Defining Data The syntax for defining data is: [data type] [variable name],...; Declaration is done in the beginning of a function. Definition for various data types is shown in the following table. Data definition Data type Memory defined Size (bytes) Value assigned char a, c; char a c 1 1 - - char a = 'Z'; char a 1 Z int count; int count 4 - int a, count =10; int a count 4 4 - 10 float fnum; float fnum 4 - float fnum1, fnum2 = 93.63; float fnum1 fnum2 4 4 - 93.63 11
  • 12. Practice: 1.1 Write the appropriate definitions for defining the following variables: 1.num to store integers. 2.chr to store a character and assign the character Z to it. 3.num to store a number and assign the value 8.93 to it. 4.i, j to store integers and assign the value 0 to j.
  • 13. Practice: 1.1 (Contd.) Solution: 1.int num; 2.char chr=’Z’; 3.float num = 8.93; 4.int i, j=0; 13
  • 14. Defining Strings: Syntax: char (variable) [(number of bytes)]; Here number of bytes is one more than the number of characters to store. To define a memory location of 10 bytes or to store 9 valid characters, the string will be defined as follows: char string [10]; Defining Data (Contd.) 14
  • 15. Practice: 1.2 Write the appropriate definitions for defining the following strings: 1.addrs to store 30 characters. 2.head to store 14 characters. 15
  • 16. Practice: 1.2 (Contd.) Solution: 1.char addrs[31]; 2.char head[15]; 16
  • 17. Identifying the Structure of C Functions In C language, the functions can be categorized in the following categories: Single-level functions Multiple-level functions 17
  • 18. Single Level Functions Single Level Functions: Consider the following single-level function: main() { /*print a message*/ printf("Welcome to C"); } In the preceding function: main(): Is the first function to be executed. (): Are used for passing parameters to a function. {}: Are used to mark the beginning and end of a function. These are mandatory in all functions. /* */: Is used for documenting various parts of a function. 18
  • 19. Semicolon (;): Is used for marking the end of an executable line. printf(): Is a C function for printing (displaying) constant or variable data. Single Level Functions (Contd.) 19
  • 20. Practice: 1.3 Identify any erroneous or missing component(s) in the following functions: 1. man() { printf("This function seems to be okay") } 2. man() { /*print a line*/ printf("This function is perfect“; } 20
  • 21. Practice: 1.3 (Contd.) 3. main() } printf("This has got to be right"); { 4. main() { This is a perfect comment line printf("Is it okay?"); } 21
  • 22. Solution: 1. man instead of main() and semi-colon missing at the end of the printf() function. 2. mam instead of main() and ‘)’ missing at the end of the printf() function. 3. ‘}’ instead of ‘{‘ for marking the beginning of the function and ‘{’ instead of ‘}‘ for marking the end of the function. 4. Comment line should be enclose between /* and */. Practice: 1.3 (Contd.) 22
  • 23. Multiple Level Functions The following example shows functions at multiple levels - one being called by another: main () { /* print a message */ printf ("Welcome to C."); disp_msg (); printf ("for good learning"); } disp_msg () { /* print another message */ printf ("All the best"); } 23
  • 24. The output of the preceding program is: Welcome to C. All the best for good learning. In the preceding program: main(): Is the first function to be executed. disp_msg(): Is a programmer-defined function that can be independently called by any other function. (): Are used for passing values to functions, depending on whether the receiving function is expecting any parameter. Semicolon (;): Is used to terminate executable lines. Multiple Level Functions (Contd.)
  • 25. Practice: 1.4 Identify any erroneous or missing component(s) in the following functions: a. print_msg() { main(); printf(“bye”); } main() { printf(“This is the main function”);} b. main() { /*call another function*/ dis_error(); } disp_err(); { printf(“Error in function”);} 25
  • 26. Solution: a. main() is always the first function to be executed. Further execution of the program depends on functions invoked from main(). Here, after executing printf(), the program terminates as no other function is invoked. The function print_msg is not invoked, hence it is not executed. b. The two functions, dis_error() and disp_error, are not the same because the function names are different. Practice: 1.4 (Contd.) 26
  • 27. Using the Input-Output Functions The C environment and the input and output operations are shown in the following figure. C Environment Standard Error Device (stderr) Standard Input Device (stdin) Standard Output Device (stdout) 27
  • 28. Using the Input-Output Functions (Contd.) These are assumed to be always linked to the C environment: stdin - refers to keyboard stdin - refers to keyboard stdout - refers to VDU stderr - refers to VDU Input and output takes place as a stream of characters. Each device is linked to a buffer through which the flow of characters takes place. After an input operation from the standard input device, care must be taken to clear input buffer.
  • 29. Character-Based Input-Output Functions Character-Based Input-Output Functions are: getc() putc() getchar() putchar() The following example uses the getc() and putc() functions: # include < stdio.h> /* function to accept and display a character*/ main () {char alph; alph = getc (stdin); /* accept a character */ fflush (stdin); /* clear the stdin buffer*/ putc (alph, stdout); /* display a character*/ } 29
  • 30. The following example uses the getchar() and putchar() functions: # include < stdio.h > /* function to input and display a character using the function getchar() */ main () { char c; c = getchar (); fflush (stdin); /* clear the buffer */ putchar (c); } Character-Based Input-Output Functions (Contd.) 30
  • 31. Practice: 1.5 1. Write a function to input a character and display the character input twice. 31
  • 32. Practice: 1.5 (Contd.) Solution: #include <stdio.h> void displayTwice(char character) { printf("You entered: %c%cn", character, character); } int main() { char inputCharacter; printf("Enter a character: "); scanf("%c", &inputCharacter); displayTwice(inputCharacter); return 0; } 32
  • 33. Practice: 1.6 1. Write a function to accept and store two characters in different memory locations, and to display them one after the other using the functions getchar() and putchar(). 33
  • 34. Practice: 1.6 (Contd.) Solution: /* function to accept and display two characters*/ #include<stdio.h> main() { char a, b; a=getchar(); fflush(stdin); b=getchar(); fflush(stdin); putchar(a); putchar(b); } 34
  • 35. String-Based Input-Output Functions String-based input-output functions are: gets() puts() The following example uses the gets() and puts() functions: # include < stdio.h > /* function to accept and displaying */ main () { char in_str {21}; /* display prompt */ puts ("Enter a String of max 20 characters"); gets (in_str); /* accept string */ fflush (stdin); /* clear the buffer */ puts (in_str); /* display input string */ } 35
  • 36. Using Constructs There are two types of constructs in C language: Conditional constructs Loop constructs 36
  • 37. Conditional Constructs Conditional Constructs: Requires relation operators as in other programming language with a slight change in symbols used for relational operators. The two types of conditional constructs in C are: if..else construct switch…case construct 37
  • 38. The Syntax of the if..else construct is as follows: if (condition) { statement 1 ; statement 2 ; : } else { statement 1 ; statement 2 ; : } Conditional Constructs (Contd.) 38
  • 39. Practice: 1.7 1. Write a function that accepts one-character grade code, and depending on what grade is input, display the HRA percentage according to the following table. Grade HRA % A 45% B 40% C 30% D 25% 39
  • 40. Practice: 1.8 (Contd.) Identify errors, if any, in the following function: #include<stdio.h> /*function to check if y or n is input*/ main() { char yn; puts("Enter y or n for yes/no"); yn = getchar(); fflush(stdin); if(yn=’y’) puts("You entered y"); else if(yn=‘n') puts("You entered n"); else puts("Invalid input"); } 40
  • 41. Syntax of switch…case construct: switch (variable) { case 1 : statement1 ; break ; case 2 : statement 2 ; : : break; default : statement } Conditional Constructs (Contd.) 41
  • 42. Loop Constructs The two types of conditional constructs in C are: while loop construct. do..while construct. The while loop construct has the following syntax: while (condition in true) { statement 1 ; loop statement 2 ; body } Used to iterate a set of instructions (the loop body) as long as the specified condition is true. 42
  • 43. The do..while loop construct: The do..while loop is similar to the while loop, except that the condition is checked after execution of the body. The do..while loop is executed at least once. The following figure shows the difference between the while loop and the do...while loop. Loop Constructs (Contd.) while Evaluate Condition Execute Body of Loop True False do while Evaluate Condition Execute Body of Loop True False 43
  • 44. Practice: 1.9 1. Write a function to accept characters from the keyboard until the character ‘!’ is input, and to display whether the total number of non-vowel characters entered is more than, less than, or equal to the total number of vowels entered. 44
  • 45. Practice: 1.9 (Contd.) Solution: #include <stdio.h> #include <ctype.h> void compareNonVowelsToVowels() { char ch; int nonVowelCount = 0; int vowelCount = 0; printf("Enter characters (input '!' to stop):n"); while (1) { scanf(" %c", &ch); // Note the space before %c to consume leading whitespace if (ch == '!') { break; } 45 if (isalpha(ch)) { ch = tolower(ch); // Convert character to lowercase for easier comparison if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowelCount++; } else { nonVowelCount++; } } } printf("Total non-vowel characters entered: %dn", nonVowelCount); printf("Total vowel characters entered: %dn", vowelCount); if (nonVowelCount > vowelCount) { printf("More non-vowel characters than vowels.n"); } else if (nonVowelCount < vowelCount) { printf("More vowels than non-vowel characters.n"); } else { printf("Equal number of non-vowel and vowel characters.n"); } } int main() { compareNonVowelsToVowels(); return 0; }
  • 46. 46 # include <stdio.h> main () { char ch; int i = 0; puts ("Enter a character :"); ch = getchar (); do { putchar (ch); i = i + 1; } while (i < 40); } Program to print accept a character and display it on the screen.
  • 47. 47 #include <stdio.h> main() { char chr; int i = 0; printf ("Enter a character : "); chr = getchar(); fflush (stdin); while (i < 10) { putchar(chr); i = i + 1; } }
  • 48. Summary In this session, you learned that: C language was developed by Ken Thompson and Dennis Ritchie. C language combines the features of second and third generation languages. C language is a block structured language. C language has various features that make it a widely-used language. Some of the important features are: Pointers Memory Allocation Recursion Bit-manipulation
  • 49. Summary (Contd.) The types of data structures provided by C can be classified under the following categories: Fundamental data types: Include the data types, which are used for actual data representation in the memory. Derived data types: Are based on fundamental data types. Fundamental data types: char, int, and float Some of the derived data types are: short int, long int, and double float Definition of memory for any data, both fundamental and derived data types, is done in the following format: [data type] [variable name],...; 49
  • 50. Summary (Contd.) In C language, the functions can be categorized in the following categories: Single-level functions Multiple-level functions For standard input-output operations, the C environment uses stdin, stdout, and stderr as references for accessing the devices. There are two types of constructs in C language: Conditional constructs Loop constructs 50