SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#2
Assignment/Program Statement:
Study basic structure of C program and write C programs using formatted input-
output functions, clrscr() function and getch() function in C.
Learning Objectives:
Students will be able to
- explain basic structure of C program
- explain printf, scanf, clrsrc() and getch() functions in C
- write C code using formatted input functions in C (i.e. scanf)
- write C code using formatted output functions in C (i.e. printf)
Theory:
[I] Basic Structure of C Program
Documentation Section
 This section consists of comment lines which include the name of
programmer, the author and other details like time and date of writing the
program.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
 Documentation section helps anyone to get an overview of the program.
Link Section
 The link section consists of the header files of the functions that are used in
the program.
 It provides instructions to the compiler to link functions from the system
library.
Definition Section
 All the symbolic constants are written in definition section.
 Macros are known as symbolic constants.
Global Declaration Section
 The global variables that can be used anywhere in the program are declared
in global declaration section.
 This section also declares the user defined functions.
main() Function Section
 It is necessary have one main() function section in every C program.
 This section contains two parts, declaration and executable part.
 The declaration part declares all the variables that are used in executable
part.
 These two parts must be written in between the opening and closing braces.
 Each statement in the declaration and executable part must end with a
semicolon (;).
 The execution of program starts at opening braces and ends at closing
braces.
Subprogram Section
 The subprogram section contains all the user defined functions that are used
to perform a specific task.
 These user defined functions are called in the main() function.
[Reference: http://www.thecrazyprogrammer.com/2013/07/explain-basic-structure-of-c-
programs.html ]
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 3
Sample C Program:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello World”);
getch();
}
[II] Basics of Formatted Input/Output in C
(A) Output with printf
 The basic format of a printf function call is:
printf (format_string, list_of_expressions);
where:
 format_string is the layout of what's being printed
 list_of_expressions is a comma-separated list of variables or expressions
yielding results to be inserted into the output
To output string literals, just use one parameter on printf, the string itself
printf("Hello, world!n");
printf("Greetings, Earthlingnn");
Sample C Program:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello World”);
getch();
}
Header files required to use
built-in functions in C
main() function in C-program
C-function to clear screen
C-function to print
statement on screen
C-function to read a
key from keyboard
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
Conversion Specifiers:
A conversion specifier is a symbol that is used as a placeholder in a formatting
string. For integer output (for example), %d is the specifier that holds the place for
integers.
Here are some commonly used conversion specifiers (not a comprehensive list):
%d int (signed decimal integer)
%u unsigned decimal integer
%f floating point values (fixed notation) - float, double
%e floating point values (exponential notation)
%s string
%c character
 To output an integer, use %d in the format string, and an integer expression
in the list of expressions.
int totalStuds = 523;
printf("First Year of Engineering has %d students", totalStuds);
 Use the %f modifer to print floating point values in fixed notation:
double cost = 123.45;
printf("Your total is $%f todayn", cost);
(B) Input with scanf
 To read data in from standard input (keyboard), we call the scanf function.
 The basic format of a scanf function call is:
scanf(format_string, list_of_variable_addresses);
where:
 format_string is the layout of what's being read
 list_ of_variable_addresses is a comma-separated list of addresses variables
which specify space to store incoming data
 If x is a variable, then the expression &x means "address of x"
Example: int month, day;
printf("Please enter your birth month, followed by the day: ");
scanf("%d %d", &month, &day);
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
Conversion Specifiers
 Same as for output but with some small differences
 Use %f for type float, but use %lf for types double and long double
 The data type read, the conversion specifier, and the variable used need to
match in type.
 White space is skipped by default in consecutive numeric reads. But it is not
skipped for character/string inputs.
Example:
#include <stdio.h>
int main()
{
int i;
float f;
char c;
printf("Enter an integer and a float, then Y or Nn> ");
scanf("%d%f%c", &i, &f, &c);
printf("You entered:n");
printf("i = %d, f = %f, c = %cn", i, f, c);
}
(C) Interactive Input
 You can make input more interactive by prompting the user more carefully.
 This can be tedious in some places, but in many occasions, it makes
programs more user-friendly.
Example:
int rollNo, marks;
char answer;
printf("Please enter your Roll No: ");
scanf("%d", &rollNo);
printf("Please enter your marks: ");
scanf("%d",&marks);
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
printf("Do you want to continue (Y/N)? ");
scanf("%c",&answer);
[III] clrscr() function in C:
 It is built-in function in "conio.h" (console input output header file) used to
clear the console screen.
 It is used to clear the data from console (Monitor).
 Use of clrscr() function is always optional.
 This function should be place after variable or function declaration only.
[Reference: www.tutorial4us.com/cprogramming/c-clrscr-and-getch ]
[IV] getch() function in C:
 It is built-in function available under in "conio.h" (console input output
header file) will tell to the console wait for some time until a key is hit given
after running of program.
 This function can be used to read a character directly from the keyboard.
 Generally getch() are placing at end of the program after printing the output
on screen.
[Reference: www.tutorial4us.com/cprogramming/c-clrscr-and-getch ]
Example:
#include<stdio.h>
void main()
{
int rollNo, marks;
clrscr();
printf("Please enter your Roll No: ");
scanf("%d", &rollNo);
printf("Please enter your marks: ");
scanf("%d",&marks);
getch();
}
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
Practice Problem Statements:
[Note: In following programs make use of clrscr() and getch() functions]
1) Write a program demonstrating scanf() function for addition of two numbers.
2) Write a program to read five subjects (Maths, BECP, Chemistry, Basic Civil &
Engineering Graphics) marks and display the average, percentage & total marks in
the following format
----------------------------------------------------------------------------------------
INPUT: Entered Marks
Maths : 85
BECP : 90
Chemistry : 80
Basic Civil : 75
Engineering Graphics : 92
---------------------------------------------------------------------------------------
OUTPUT:
Total Marks : 422
Average : 84.4
Percentage : 84.4%
----------------------------------------------------------------------------------------
Conclusion:
Thus we have studies the basic structure of C program, basic functions in C such as
printf to print and scanf to read. To clear screen, clrsrc() in C and to read key input
from keyboard getch(), we studied.
Learning Outcomes:
At the end of this assignment, students are able to
- explain basic structure of C program
- explain printf, scanf, clrsrc() and getch() functions in C
- write C code using formatted input functions in C (i.e. scanf)
- write C code using formatted output functions in C (i.e. printf)

Contenu connexe

Tendances

Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 

Tendances (20)

CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
C Programming
C ProgrammingC Programming
C Programming
 
What is c
What is cWhat is c
What is c
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Casa lab manual
Casa lab manualCasa lab manual
Casa lab manual
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
C programming
C programmingC programming
C programming
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 

En vedette

Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 

En vedette (20)

Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Apclass
ApclassApclass
Apclass
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Loop c++
Loop c++Loop c++
Loop c++
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Array in c++
Array in c++Array in c++
Array in c++
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 

Similaire à CP Handout#2

Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg Patel
TechNGyan
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx
tarifarmarie
 

Similaire à CP Handout#2 (20)

Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Chapter3
Chapter3Chapter3
Chapter3
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxUnit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg Patel
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
C structure
C structureC structure
C structure
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
C programming
C programmingC programming
C programming
 
C programming course material
C programming course materialC programming course material
C programming course material
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 

Plus de trupti1976 (10)

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
 

Dernier

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Dernier (20)

NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 

CP Handout#2

  • 1. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 1 Handout#2 Assignment/Program Statement: Study basic structure of C program and write C programs using formatted input- output functions, clrscr() function and getch() function in C. Learning Objectives: Students will be able to - explain basic structure of C program - explain printf, scanf, clrsrc() and getch() functions in C - write C code using formatted input functions in C (i.e. scanf) - write C code using formatted output functions in C (i.e. printf) Theory: [I] Basic Structure of C Program Documentation Section  This section consists of comment lines which include the name of programmer, the author and other details like time and date of writing the program.
  • 2. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 2  Documentation section helps anyone to get an overview of the program. Link Section  The link section consists of the header files of the functions that are used in the program.  It provides instructions to the compiler to link functions from the system library. Definition Section  All the symbolic constants are written in definition section.  Macros are known as symbolic constants. Global Declaration Section  The global variables that can be used anywhere in the program are declared in global declaration section.  This section also declares the user defined functions. main() Function Section  It is necessary have one main() function section in every C program.  This section contains two parts, declaration and executable part.  The declaration part declares all the variables that are used in executable part.  These two parts must be written in between the opening and closing braces.  Each statement in the declaration and executable part must end with a semicolon (;).  The execution of program starts at opening braces and ends at closing braces. Subprogram Section  The subprogram section contains all the user defined functions that are used to perform a specific task.  These user defined functions are called in the main() function. [Reference: http://www.thecrazyprogrammer.com/2013/07/explain-basic-structure-of-c- programs.html ]
  • 3. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 3 Sample C Program: #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Hello World”); getch(); } [II] Basics of Formatted Input/Output in C (A) Output with printf  The basic format of a printf function call is: printf (format_string, list_of_expressions); where:  format_string is the layout of what's being printed  list_of_expressions is a comma-separated list of variables or expressions yielding results to be inserted into the output To output string literals, just use one parameter on printf, the string itself printf("Hello, world!n"); printf("Greetings, Earthlingnn"); Sample C Program: #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Hello World”); getch(); } Header files required to use built-in functions in C main() function in C-program C-function to clear screen C-function to print statement on screen C-function to read a key from keyboard
  • 4. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 4 Conversion Specifiers: A conversion specifier is a symbol that is used as a placeholder in a formatting string. For integer output (for example), %d is the specifier that holds the place for integers. Here are some commonly used conversion specifiers (not a comprehensive list): %d int (signed decimal integer) %u unsigned decimal integer %f floating point values (fixed notation) - float, double %e floating point values (exponential notation) %s string %c character  To output an integer, use %d in the format string, and an integer expression in the list of expressions. int totalStuds = 523; printf("First Year of Engineering has %d students", totalStuds);  Use the %f modifer to print floating point values in fixed notation: double cost = 123.45; printf("Your total is $%f todayn", cost); (B) Input with scanf  To read data in from standard input (keyboard), we call the scanf function.  The basic format of a scanf function call is: scanf(format_string, list_of_variable_addresses); where:  format_string is the layout of what's being read  list_ of_variable_addresses is a comma-separated list of addresses variables which specify space to store incoming data  If x is a variable, then the expression &x means "address of x" Example: int month, day; printf("Please enter your birth month, followed by the day: "); scanf("%d %d", &month, &day);
  • 5. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 5 Conversion Specifiers  Same as for output but with some small differences  Use %f for type float, but use %lf for types double and long double  The data type read, the conversion specifier, and the variable used need to match in type.  White space is skipped by default in consecutive numeric reads. But it is not skipped for character/string inputs. Example: #include <stdio.h> int main() { int i; float f; char c; printf("Enter an integer and a float, then Y or Nn> "); scanf("%d%f%c", &i, &f, &c); printf("You entered:n"); printf("i = %d, f = %f, c = %cn", i, f, c); } (C) Interactive Input  You can make input more interactive by prompting the user more carefully.  This can be tedious in some places, but in many occasions, it makes programs more user-friendly. Example: int rollNo, marks; char answer; printf("Please enter your Roll No: "); scanf("%d", &rollNo); printf("Please enter your marks: "); scanf("%d",&marks);
  • 6. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 6 printf("Do you want to continue (Y/N)? "); scanf("%c",&answer); [III] clrscr() function in C:  It is built-in function in "conio.h" (console input output header file) used to clear the console screen.  It is used to clear the data from console (Monitor).  Use of clrscr() function is always optional.  This function should be place after variable or function declaration only. [Reference: www.tutorial4us.com/cprogramming/c-clrscr-and-getch ] [IV] getch() function in C:  It is built-in function available under in "conio.h" (console input output header file) will tell to the console wait for some time until a key is hit given after running of program.  This function can be used to read a character directly from the keyboard.  Generally getch() are placing at end of the program after printing the output on screen. [Reference: www.tutorial4us.com/cprogramming/c-clrscr-and-getch ] Example: #include<stdio.h> void main() { int rollNo, marks; clrscr(); printf("Please enter your Roll No: "); scanf("%d", &rollNo); printf("Please enter your marks: "); scanf("%d",&marks); getch(); }
  • 7. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 7 Practice Problem Statements: [Note: In following programs make use of clrscr() and getch() functions] 1) Write a program demonstrating scanf() function for addition of two numbers. 2) Write a program to read five subjects (Maths, BECP, Chemistry, Basic Civil & Engineering Graphics) marks and display the average, percentage & total marks in the following format ---------------------------------------------------------------------------------------- INPUT: Entered Marks Maths : 85 BECP : 90 Chemistry : 80 Basic Civil : 75 Engineering Graphics : 92 --------------------------------------------------------------------------------------- OUTPUT: Total Marks : 422 Average : 84.4 Percentage : 84.4% ---------------------------------------------------------------------------------------- Conclusion: Thus we have studies the basic structure of C program, basic functions in C such as printf to print and scanf to read. To clear screen, clrsrc() in C and to read key input from keyboard getch(), we studied. Learning Outcomes: At the end of this assignment, students are able to - explain basic structure of C program - explain printf, scanf, clrsrc() and getch() functions in C - write C code using formatted input functions in C (i.e. scanf) - write C code using formatted output functions in C (i.e. printf)