SlideShare a Scribd company logo
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
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. Identifying the Benefits and Features of C Language
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
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
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.  Features of the C Language
The types of data structures provided by C can be classified under the following categories: Fundamental data types Derived data types Using the Data Types Available in C language
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 Fundamental Data Types
The storage requirement for fundamental data types can be represented with the help of the following table. Fundamental Data Types (Contd.) 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
Derived Data Types: Are represented in memory as fundamental data type. Some derived data types are: short int long int double float   Derived Data Types
The storage requirement for derived data types can be represented with the help of the following table. Derived Data Types (Contd.) 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
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. Defining Data 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
Write the appropriate definitions for defining the following variables: num  to store integers. chr  to store a character and assign the character  Z  to it. num  to store a number and assign the value 8.93 to it. i, j  to store integers and assign the value 0 to  j . Practice: 1.1
Solution: int num; char chr=’Z’; float num = 8.93; int i, j=0; Practice: 1.1 (Contd.)
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.)
Write the appropriate definitions for defining the following strings: addrs  to store 30 characters. head  to store 14 characters. Practice: 1.2
Solution: char addrs[31]; char head[15];  Practice: 1.2 (Contd.)
In C language, the functions can be categorized in the following categories: Single-level functions Multiple-level functions Identifying the Structure of C 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. Single Level Functions
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.)
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“; } Practice: 1.3
3. main()   }   printf("This has got to be right");   { 4. main()   {   This is a perfect comment line   printf("Is it okay?");   } Practice: 1.3 (Contd.)
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.)
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"); } Multiple Level Functions
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.)
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”);} Practice: 1.4
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.)
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)
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. Using the Input-Output Functions (Contd.)
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*/ } Character-Based Input-Output Functions
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.)
Write a function to input a character and display the character input twice.  Practice: 1.5
Solution: Practice: 1.5 (Contd.)
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() . Practice: 1.6
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); } Practice: 1.6 (Contd.)
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 (&quot;Enter a String of max 20 characters&quot;); gets (in_str);  /* accept string  */ fflush (stdin); /* clear the buffer */ puts (in_str);  /* display input string */ } String-Based Input-Output Functions
1. Write a function that prompts for and accepts a name with a maximum of 25 characters, and displays the following message. Hello. How are you? (name) 2. Write a function that prompts for a name (up to 20 characters) and address (up to 30 characters) and accepts them one at a time. Finally, the name and address are displayed in the following way. Your name is: (name) Your address is: (address) Practice: 1.7
Solution: Practice: 1.7 (Contd.)
There are two types of constructs in C language: Conditional constructs Loop constructs Using 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 Conditional Constructs
The Syntax of the  if..else  construct is as follows: if (condition) { statement 1 ; statement 2 ;   : } else { statement 1 ; statement 2 ;   : } Conditional Constructs (Contd.)
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. Practice: 1.8 Grade HRA % A 45% B 40% C 30% D 25%
Identify errors, if any, in the following function: #include<stdio.h> /*function to check if y or n is input*/  main() { char yn; puts(&quot;Enter y or n for yes/no&quot;); yn = getchar(); fflush(stdin); if(yn=’y’) puts(&quot;You entered y&quot;);  else if(yn=‘n') puts(&quot;You entered n&quot;);  else puts(&quot;Invalid input&quot;); } Practice: 1.8 (Contd.)
Solution: Practice: 1.8 (Contd.)
Syntax of  switch…case  construct: switch (variable) {   case 1 :  statement1 ; break ; case 2 :  statement 2 ;   : : break; default : statement } Conditional Constructs (Contd.)
Write a function to display the following menu and accept a choice number. If an invalid choice is entered then an appropriate error message must be displayed, else the choice number entered must be displayed. Menu 1. Create a directory 2. Delete a directory 3. Show a directory 4. Exit Your choice: Practice: 1.9
Solution: Practice: 1.9 (Contd.)
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. Loop Constructs
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
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. Practice: 1.10
Solution: Practice: 1.10 (Contd.)
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
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],...; 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 Summary (Contd.)

More Related Content

What's hot (20)

PDF
Strings-Computer programming
nmahi96
 
PPTX
C programming language tutorial
javaTpoint s
 
PPTX
Structure of c_program_to_input_output
Anil Dutt
 
PPTX
Computer programming(CP)
nmahi96
 
PDF
C programming & data structure [character strings & string functions]
MomenMostafa
 
PDF
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
PDF
Functions-Computer programming
nmahi96
 
PDF
C programming & data structure [arrays & pointers]
MomenMostafa
 
PPT
02a fundamental c++ types, arithmetic
Manzoor ALam
 
PDF
Pointers-Computer programming
nmahi96
 
PPTX
C Programming Unit-2
Vikram Nandini
 
PDF
C Programming Tutorial - www.infomtec.com
M-TEC Computer Education
 
PDF
C notes.pdf
Durga Padma
 
PPT
Unit 4 Foc
JAYA
 
PDF
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
PDF
Structures
arshpreetkaur07
 
PDF
C programming language
Mahmoud Eladawi
 
PDF
Introduction to c language
RavindraSalunke3
 
DOCX
Theory1&amp;2
Dr.M.Karthika parthasarathy
 
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Strings-Computer programming
nmahi96
 
C programming language tutorial
javaTpoint s
 
Structure of c_program_to_input_output
Anil Dutt
 
Computer programming(CP)
nmahi96
 
C programming & data structure [character strings & string functions]
MomenMostafa
 
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
Functions-Computer programming
nmahi96
 
C programming & data structure [arrays & pointers]
MomenMostafa
 
02a fundamental c++ types, arithmetic
Manzoor ALam
 
Pointers-Computer programming
nmahi96
 
C Programming Unit-2
Vikram Nandini
 
C Programming Tutorial - www.infomtec.com
M-TEC Computer Education
 
C notes.pdf
Durga Padma
 
Unit 4 Foc
JAYA
 
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
Structures
arshpreetkaur07
 
C programming language
Mahmoud Eladawi
 
Introduction to c language
RavindraSalunke3
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 

Viewers also liked (20)

PPS
C programming session 03
Dushmanta Nath
 
PPS
Dacj 1-2 c
Niit Care
 
PPT
Session No1
Saif Ullah Dar
 
PPS
C programming session 07
Dushmanta Nath
 
PPTX
Session no 4
Saif Ullah Dar
 
PPTX
Session no 2
Saif Ullah Dar
 
PPS
C programming session 11
Dushmanta Nath
 
PPTX
Java script session 3
Saif Ullah Dar
 
PPS
C programming session 08
Dushmanta Nath
 
PPTX
Niit
Mahima Narang
 
PPTX
C string
University of Potsdam
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
C programming - String
Achyut Devkota
 
PPSX
C programming function
argusacademy
 
PPSX
C programming array & shorting
argusacademy
 
PPTX
Java script Session No 1
Saif Ullah Dar
 
PPSX
C programming string
argusacademy
 
DOC
String in c
Suneel Dogra
 
PPT
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
PPTX
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
C programming session 03
Dushmanta Nath
 
Dacj 1-2 c
Niit Care
 
Session No1
Saif Ullah Dar
 
C programming session 07
Dushmanta Nath
 
Session no 4
Saif Ullah Dar
 
Session no 2
Saif Ullah Dar
 
C programming session 11
Dushmanta Nath
 
Java script session 3
Saif Ullah Dar
 
C programming session 08
Dushmanta Nath
 
Strings in C
Kamal Acharya
 
C programming - String
Achyut Devkota
 
C programming function
argusacademy
 
C programming array & shorting
argusacademy
 
Java script Session No 1
Saif Ullah Dar
 
C programming string
argusacademy
 
String in c
Suneel Dogra
 
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
Ad

Similar to C programming session 01 (20)

PPTX
C Programming ppt for beginners . Introduction
raghukatagall2
 
PPS
C programming session 01
Vivek Singh
 
PDF
The best every notes on c language is here check it out
rajatryadav22
 
PPTX
C language
Priya698357
 
PPTX
c_pro_introduction.pptx
RohitRaj744272
 
PPT
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
PPTX
Introduction to C Unit 1
Dr. SURBHI SAROHA
 
PPTX
First c program
NageswaraRao Gogisetti
 
PPTX
unit 1 cpds.pptx
madhurij54
 
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
PPTX
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
ssuser71a90c
 
PPT
Ch2 introduction to c
Hattori Sidek
 
PPTX
2. Introduction to 'C' Language (1).pptx
AkshatMuke1
 
PPTX
C language
Robo India
 
PPTX
Fundamental programming Nota Topic 2.pptx
UmmuNazieha
 
PPTX
CHAPTER 3 STRUCTURED PROGRAMMING.pptx 2024
ALPHONCEKIMUYU
 
PPT
C tutorial
Anurag Sukhija
 
PDF
C programing Tutorial
Mahira Banu
 
PPS
C programming unit 01
Prashanth Shivakumar
 
PPT
Introduction to C Programming
MOHAMAD NOH AHMAD
 
C Programming ppt for beginners . Introduction
raghukatagall2
 
C programming session 01
Vivek Singh
 
The best every notes on c language is here check it out
rajatryadav22
 
C language
Priya698357
 
c_pro_introduction.pptx
RohitRaj744272
 
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
Introduction to C Unit 1
Dr. SURBHI SAROHA
 
First c program
NageswaraRao Gogisetti
 
unit 1 cpds.pptx
madhurij54
 
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
ssuser71a90c
 
Ch2 introduction to c
Hattori Sidek
 
2. Introduction to 'C' Language (1).pptx
AkshatMuke1
 
C language
Robo India
 
Fundamental programming Nota Topic 2.pptx
UmmuNazieha
 
CHAPTER 3 STRUCTURED PROGRAMMING.pptx 2024
ALPHONCEKIMUYU
 
C tutorial
Anurag Sukhija
 
C programing Tutorial
Mahira Banu
 
C programming unit 01
Prashanth Shivakumar
 
Introduction to C Programming
MOHAMAD NOH AHMAD
 
Ad

More from Dushmanta Nath (6)

DOC
Global Warming Project
Dushmanta Nath
 
DOC
DBMS Practical File
Dushmanta Nath
 
DOC
Manufacturing Practice (MP) Training Project
Dushmanta Nath
 
DOC
BSNL Training Project
Dushmanta Nath
 
DOC
IT- 328 Web Administration (Practicals)
Dushmanta Nath
 
DOC
IT-314 MIS (Practicals)
Dushmanta Nath
 
Global Warming Project
Dushmanta Nath
 
DBMS Practical File
Dushmanta Nath
 
Manufacturing Practice (MP) Training Project
Dushmanta Nath
 
BSNL Training Project
Dushmanta Nath
 
IT- 328 Web Administration (Practicals)
Dushmanta Nath
 
IT-314 MIS (Practicals)
Dushmanta Nath
 

Recently uploaded (20)

PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Digital Circuits, important subject in CS
contactparinay1
 

C programming session 01

  • 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. 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. Identifying the Benefits and Features of C Language
  • 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
  • 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
  • 5. 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. Features of the C Language
  • 6. The types of data structures provided by C can be classified under the following categories: Fundamental data types Derived data types Using the Data Types Available in C language
  • 7. 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 Fundamental Data Types
  • 8. The storage requirement for fundamental data types can be represented with the help of the following table. Fundamental Data Types (Contd.) 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
  • 9. Derived Data Types: Are represented in memory as fundamental data type. Some derived data types are: short int long int double float Derived Data Types
  • 10. The storage requirement for derived data types can be represented with the help of the following table. Derived Data Types (Contd.) 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
  • 11. 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. Defining Data 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
  • 12. Write the appropriate definitions for defining the following variables: num to store integers. chr to store a character and assign the character Z to it. num to store a number and assign the value 8.93 to it. i, j to store integers and assign the value 0 to j . Practice: 1.1
  • 13. Solution: int num; char chr=’Z’; float num = 8.93; int i, j=0; Practice: 1.1 (Contd.)
  • 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.)
  • 15. Write the appropriate definitions for defining the following strings: addrs to store 30 characters. head to store 14 characters. Practice: 1.2
  • 16. Solution: char addrs[31]; char head[15]; Practice: 1.2 (Contd.)
  • 17. In C language, the functions can be categorized in the following categories: Single-level functions Multiple-level functions Identifying the Structure of C Functions
  • 18. Single Level Functions: Consider the following single-level function: main() { /*print a message*/ printf(&quot;Welcome to C&quot;); } 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. Single Level Functions
  • 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.)
  • 20. Identify any erroneous or missing component(s) in the following functions: 1. man() { printf(&quot;This function seems to be okay&quot;) } 2. man() { /*print a line*/ printf(&quot;This function is perfect“; } Practice: 1.3
  • 21. 3. main() } printf(&quot;This has got to be right&quot;); { 4. main() { This is a perfect comment line printf(&quot;Is it okay?&quot;); } Practice: 1.3 (Contd.)
  • 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.)
  • 23. The following example shows functions at multiple levels - one being called by another : main () { /* print a message */ printf (&quot;Welcome to C.&quot;); disp_msg (); printf (&quot;for good learning&quot;); } disp_msg () { /* print another message */ printf (&quot;All the best&quot;); } Multiple Level Functions
  • 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. 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”);} Practice: 1.4
  • 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.)
  • 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)
  • 28. 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. Using the Input-Output Functions (Contd.)
  • 29. 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*/ } Character-Based Input-Output Functions
  • 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.)
  • 31. Write a function to input a character and display the character input twice. Practice: 1.5
  • 33. 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() . Practice: 1.6
  • 34. 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); } Practice: 1.6 (Contd.)
  • 35. 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 (&quot;Enter a String of max 20 characters&quot;); gets (in_str); /* accept string */ fflush (stdin); /* clear the buffer */ puts (in_str); /* display input string */ } String-Based Input-Output Functions
  • 36. 1. Write a function that prompts for and accepts a name with a maximum of 25 characters, and displays the following message. Hello. How are you? (name) 2. Write a function that prompts for a name (up to 20 characters) and address (up to 30 characters) and accepts them one at a time. Finally, the name and address are displayed in the following way. Your name is: (name) Your address is: (address) Practice: 1.7
  • 38. There are two types of constructs in C language: Conditional constructs Loop constructs Using Constructs
  • 39. 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 Conditional Constructs
  • 40. The Syntax of the if..else construct is as follows: if (condition) { statement 1 ; statement 2 ; : } else { statement 1 ; statement 2 ; : } Conditional Constructs (Contd.)
  • 41. 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. Practice: 1.8 Grade HRA % A 45% B 40% C 30% D 25%
  • 42. Identify errors, if any, in the following function: #include<stdio.h> /*function to check if y or n is input*/ main() { char yn; puts(&quot;Enter y or n for yes/no&quot;); yn = getchar(); fflush(stdin); if(yn=’y’) puts(&quot;You entered y&quot;); else if(yn=‘n') puts(&quot;You entered n&quot;); else puts(&quot;Invalid input&quot;); } Practice: 1.8 (Contd.)
  • 44. Syntax of switch…case construct: switch (variable) { case 1 : statement1 ; break ; case 2 : statement 2 ; : : break; default : statement } Conditional Constructs (Contd.)
  • 45. Write a function to display the following menu and accept a choice number. If an invalid choice is entered then an appropriate error message must be displayed, else the choice number entered must be displayed. Menu 1. Create a directory 2. Delete a directory 3. Show a directory 4. Exit Your choice: Practice: 1.9
  • 47. 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. Loop Constructs
  • 48. 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
  • 49. 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. Practice: 1.10
  • 51. 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
  • 52. 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],...; Summary (Contd.)
  • 53. 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 Summary (Contd.)

Editor's Notes

  • #2: Begin the session by explaining the objectives of the session.
  • #3: Discuss the history of the C language.
  • #8: The storage requirements for different data types are machine-dependent. There is a subtle difference between declaring and defining variables. The statement: char a; Is used to declare a variable a. Here, only the attributes of the variable are specified, not the contents. In contrast, the statement: char a = ‘A’ ; is used to define the contents of the variable a.
  • #10: Derived data types modify the data according to the specific requirements. For example, if you need to store a integer value above 32,767 or below -32,767, you can use long int instead of int . the derived data type long increases the range of the int data type.
  • #13: Use Slide 12 to test the student’s understanding on defining the variables.
  • #16: Use Slide 15 to test the student’s understanding on defining the variables.
  • #19: A C function is typically made up of blocks. A block is a set of C statements enclosed within braces. Variables are generally declared at the beginning of a function, but may also be declared at the beginning of a block. A C program is modular and structured. This allows for reusability of code.
  • #21: Use Slide 20 to test the student’s understanding on functions.
  • #24: Multiple-level functions are those functions where a function calls another function. Like in the slide, the main() function calls another function disp_msg() .
  • #26: Use Slide 25 to test the student’s understanding on functions.
  • #30: The getc() and getchar() functions are used to take a character from a stream. The getc() functions takes a stream as a parameter while the getchar() function does not take any parameter. Similarly, the putc() and putchar() function outputs a character to a stream. The putc() functions takes two parameters, the character to be output and the stream, while the putchar() function takes only one parameter i.e. the character to be output.
  • #32: Use Slide 31 to test the student’s understanding on character-based input-output functions.
  • #34: Use Slide 33 to test the student’s understanding on the getchar() and putchar() functions.
  • #36: The gets() function is used to get a string from stdin . It collects a string terminated by a new line character from the input stream. It takes a constant string as a parameter. The puts() function copies the null-terminated string, which is passed as a parameter, to the standard output stream.
  • #37: Use Slide 36 to test the student’s understanding on the gets() and puts() functions.
  • #39: Explain the need for conditional constructs and loop constructs by taking appropriate examples.
  • #42: Use Slide 41 to test the student’s understanding on the conditional constructs.
  • #45: Explain the limitations of the switch…case construct. Tell the students that the switch…case construct cannot work with float variable. Also, you cannot specify an expression in the switch…case construct.
  • #46: Use Slide 45 to test the student’s understanding on the conditional constructs.
  • #49: The condition associated with the while loop is checked before entering the loop. Hence, there is a possibility that the loop is not executed at all. In the other case, the condition is checked after executing the loop once and for every interaction thereafter.
  • #50: Use Slide 49 to test the student’s understanding on the loop constructs.
  • #52: Use Slides 51, 52, and 53 to summarize the session.