SlideShare a Scribd company logo
1 of 74
Introduction
to C
Programming
ALGOL 1960 International Group (Europe)
BCPL (Basic Programming Combined
Lang.)
1967 Martin Richards
B 1970 Ken Thompson( first version of
UNIX)
C 1972 Dennis Ritchie (C)
History of C
1
History of C --- contd..
K&R C 1978 Kernighan & Ritchie
ANSI C 1989 ANSI committee
(American National Standards Institute)
ANSI/ISO C 1990 ISO committee
(International Standards Organization)
1
WHY LEARN
C?
C is easy to learn
C is portable
C is middle level language
Characteristics of C
• C Compiler Combines the features of assembly language and high-level
language
• Best suited for writing programs for system software and business
Characteristics
• High level language- Not worry about the machine code
• Small Size – C has 32 keywords
• Extensive use of function calls
• Well suited for Structed programming language
• Supports loose typing
Characteristics of C
• Stable language – ANSI C was created in 1983
and since then no revision
• Quick language
• Facilitates low level programming
• Core Language – C++, Java, Perl
• C is portable language
• C is an extensible language
USES of C
•Used System programming
•End – user application programming
STRUCTURE
: C
PROGRAM
ILLUSTRATED VERSION OF A PROGRAM
Writing the
first C
Program
# Include <stdio.h>
Int main()
{
printf(“n welcome to C
programming”);
}
Parts of a Program
#include <stdio.h>
int x;
int main () {
int y;
printf("Enter x and y: ");
scanf(&x,&y);
printf("Sum is %dn",x+y);
}
Preprocessor Directive
Global Declaration
Function
Local Declaration
Statements
Comments
Text between /* and */
Used to “document” the code for the human reader
Ignored by compiler (not part of program)
Have to be careful
comments may cover multiple lines
ends as soon as */ encountered (so no internal comments - /* An /* internal */
comment */)
// single line
Comment Example
#include <stdio.h>
/* This comment covers
* multiple lines
* in the program.
*/
int main () /* The main header */ {
/* No local declarations */
printf(“Too many commentsn”);
} /* end of main */
Files used in a C program
Files in a C program
Source
file
Header
File
Object
file
Executable
file
Source Code File
• Containing the source code of the program
• The file extension of the C source code is .c
• C source code file contains main function and may
be other functions
• The main() is the starting point of the c program
Header file
•Use same subroutine in different programs
•Compile the source code of the subroutine
once, link object file to any other program
•Header file with .h extension
Standard Header Files
Predefined files
String.h – string handling functions
Stdlib.h – Some library function (miscellaneous
functions)
Stdio.h – Standardized input and output functions
Math.h – Mathematical functions
Alloc.h – Dynamic memory allocation
Conio.h – Clearing the screen and character input
Object File
• Object files are generated by the compiler as a result
of processing the source code file
• Object file contain compact binary code of the
function definitions
• Linker uses object files to produce an executable file
by combining the object files together
• Object files - .o or .obj extension
Binary
Executable
File
• Binary executable file is generated by
the linker
• The linker links the various object files
to produce a binary file that can be
directly executed
• .exe extension - windows
C Tokens
Tokens are the basic building blocks in C language
Character set in C
KEY WORDS
• Keywords are system defined identifiers.
• Keywords are reserved words.
• They have specific meaning in the language and cannot
be used by the programmer as a variable or constant
name.
• C is case sensitive.
• 32 Keywords.
CLASSIFICATION :DATA TYPE
Basic Data Types
Data type Keywords Used Size in bytes Range Use
Character char 1 -128 to 127 To store character
Integer int 2 -32768 to 32767 To store integer number
Floating
Point
float 4 3.4E-38 to 3.4E+38 To store decimal number
Double double 8 1.7E-308 to 1.7E308 To store big floating-point number
Valueless void 0 Valueless -
Examples - Character
#include <stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c n", c);
}
Example - Integer
#include <stdio.h>
void main()
{
int i = 5;
printf("The integer value is: %d n", i);
}
Examples - Float
#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f n", f);
}
Example -
Double
#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf n", d);
}
Variables Types
Variables
Numeric Variable Character Variables
VARIABLES
A variable is a container(storage area) to hold
data.
All variables have three important attributes:
 data type
 name
 value
Eg: int a = 10;
float b = 2.5;
char c = ‘f’;
CONSTANT
A constant is a value or an identifier whose value cannot be
altered in a program. For example: 1, 2.5
An identifier also can be defined as a constant. eg. const double
PI = 3.14
Here, value of PI is a constant.
Constant
Declaration
● To declare a constant, precede the normal variable
declaration with const keyword and assign it a value.
● For example,
const float pi = 3.14;
● Another way to designate a constant is to use the
pre-processor command define.
#define PI 3.14159
When the preprocessor reformats the program to be
compiled by the compiler, it replaces each defined
name with its corresponding value wherever it is
found in the source program.
TYPES OF CONSTANTS
Integer constant eg: 1
Floating-point constant eg: 2.5
Character constant eg: ’a’
String constant eg: ”good”
Input/Output in C
STREAMS
● A stream acts in two ways. It is the source of data as well as the destination of data.
● C programs input data and output data from a stream. Streams are associated with a physical device such as
the monitor or with a file stored on the secondary memory.
● In a text stream, sequence of characters is divided into lines with each line being terminated with a new-line
character (n). On the other hand, a binary stream contains data values using their memory representation.
● Although, we can do input/output from the keyboard/monitor or from any.
Streams in a C program
Text Stream
Binary Stream
Keyboard Data
Monitor Data
Input text stream
Input text stream
Data I/O
1. Formatted Input/output functions
2. Unformatted Input/output functions
Formatted Input and Output Functions
scanf() - input function printf() - output function
printf()
Output function
General form:
printf(“control_string or format
specifier”,variable1,variable2,
variable3,...);
Example:
printf(“The numbers are %d and
%d”,number1,number2);
Format Specifiers
Parts of conversion specifier field for
printf()
The percent sign and conversion code
are required
Other modifiers such as width and
precision are optional.
width specifies the total number of
characters used to display the value
precision indicates the number of
characters used after the decimal
point. The precision option is
only used with floats or strings.
Default precision value is 6
Size modifiers used in printf()
Examples
printf(“number=%3dn”, 10);
printf(“number=%2dn”, 10);
printf(“number=%1dn”, 10);
printf(“number=%7.2fn”, 5.4321);
printf(“number=%.2fn”, 5.4391);
printf(“number=%.9fn”, 5.4321);
printf(“number=%fn”, 5.4321);
Example
# include <stdio.h>
int main()
{
printf(“number=%3dn”, 10);
printf(“number=%2dn”, 10);
printf(“number=%1dn”, 10);
printf(“number=%7.2fn”, 5.4321);
printf(“number=%.2fn”, 5.4391);
printf(“number=%.9fn”, 5.4321);
printf(“number=%fn”, 5.4321);
}
Flag characters used in printf()
Flag Meaning
- Left justify the display
+ Display positive or negative sign of value
Space Display space if there is no sign
0 Pad with leading zeros
# Use alternate form of specifier
Example
printf(“number=%06.1fn”, 5.5); printf(“%-+6.1f=numbern”, 5.5);
Example with strings
#include<stdio.h>
int main()
{
printf(“%s”,“hello”);
printf(“n%3s”,“hello”);
printf(“n%10s”,“hello”);
printf(“n%-10s”,“hello”);
printf(“n%10.3s”,“hello”);
return 0; }
scanf()
General form:
scanf(“control_string”, variable1_address,
variable2_address,...);
scanf() returns the number of input fields
successfully scanned, converted, and stored
Example:
scanf(“%d%d”,&num1,&num2);
2. Unformatted Input/output functions
Input functions
gets()
getchar()
getch()
getche()
Output functions
puts()
putchar()
putch()
puts()
•The I/O library functions are listed in the
“header” file <stdio.h>.
•Types:
•Unformatted Input/output functions
•Formatted Input/output functions
•
1. Data Input and Output
getchar()
Reads a single character from the input data
stream; but does not return the character to
the program until the ‘n’ ( or ) enter key is
pressed.
Syntax:-
variable = getchar();
Example:-
char c;
c = getchar();
Program
#include<stdio.h>
void main()
{
char c;
printf(“Enter a character”);
c=getchar();
printf(“c = %c ”,c);
}
Output
Enter a character k
c = k
getch() and getche()
These functions read any alphanumeric character from
the standard input device
The character entered is not displayed by the getch()
function until enter is pressed
The getche() accepts and displays the character.
The getch() accepts but does not display the character.
Syntax:
getche();
getch();
getch() and getche() - Program
#include<stdio.h>
void main()
{
printf(“Enter two alphabets:”);
getche();
getch();
}
Output
Enter two alphabets a b
a
putchar()
This function prints one character on the
screen at a time which is read by standard
input.
Syntax:
putchar( variable name);
Example:
char c= ‘c’;
putchar(c);
#include<stdio.h>
void main()
{
char ch;
printf(“Enter a character: ”);
scanf(“%c”, &ch);
putchar(ch);
}
Output
Enter a character: r
r
putch()
This function prints any
alphanumeric character taken
by the standard input device
Syntax:
putch( variable name);
Example:
char c= ‘c’;
putch(c);
Program
#include<stdio.h>
void main()
{
char ch;
printf(“Press any key to
continue”);
ch = getch();
printf(“ you pressed:”);
putch(ch);
gets()
String I/O
This function is used for accepting any
string( stream of characters) until enter
key is pressed
Syntax
char str[length of string in number];
gets(str);
Example:
char name[15];
gets(name);
Program
#include<stdio.h>
void main()
{
char ch[30];
printf(“Enter the string:”);
gets(ch);
printf(“Entered string: %s”, ch);
}
Output:
Enter the string: Use of data!
Entered string: Use of data!
puts()
This function prints the string or
character array.
It is opposite to gets()
Syntax
char str[length of string in number];
gets(str);
puts(str);

More Related Content

Similar to c_pro_introduction.pptx

C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
kapil078
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 

Similar to c_pro_introduction.pptx (20)

C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Introduction to C
Introduction to CIntroduction to C
Introduction to C
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
Introduction of c programming unit-ii ppt
Introduction of  c programming unit-ii pptIntroduction of  c programming unit-ii ppt
Introduction of c programming unit-ii ppt
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
C tutorial
C tutorialC tutorial
C tutorial
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Basic c
Basic cBasic c
Basic c
 
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
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
 
Cnotes
CnotesCnotes
Cnotes
 
C Theory
C TheoryC Theory
C Theory
 
c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 

More from RohitRaj744272 (6)

projectors.ppt
projectors.pptprojectors.ppt
projectors.ppt
 
stealthtechnologysupreeth.pptx
stealthtechnologysupreeth.pptxstealthtechnologysupreeth.pptx
stealthtechnologysupreeth.pptx
 
formofacprogram-180106145347.pptx
formofacprogram-180106145347.pptxformofacprogram-180106145347.pptx
formofacprogram-180106145347.pptx
 
M1. Introducing Computers Part A .pdf
M1. Introducing Computers Part A .pdfM1. Introducing Computers Part A .pdf
M1. Introducing Computers Part A .pdf
 
pop2.pdf
pop2.pdfpop2.pdf
pop2.pdf
 
pop1.pdf
pop1.pdfpop1.pdf
pop1.pdf
 

Recently uploaded

Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
hublikarsn
 

Recently uploaded (20)

Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 

c_pro_introduction.pptx

  • 2. ALGOL 1960 International Group (Europe) BCPL (Basic Programming Combined Lang.) 1967 Martin Richards B 1970 Ken Thompson( first version of UNIX) C 1972 Dennis Ritchie (C) History of C 1
  • 3. History of C --- contd.. K&R C 1978 Kernighan & Ritchie ANSI C 1989 ANSI committee (American National Standards Institute) ANSI/ISO C 1990 ISO committee (International Standards Organization) 1
  • 4. WHY LEARN C? C is easy to learn C is portable C is middle level language
  • 5. Characteristics of C • C Compiler Combines the features of assembly language and high-level language • Best suited for writing programs for system software and business Characteristics • High level language- Not worry about the machine code • Small Size – C has 32 keywords • Extensive use of function calls • Well suited for Structed programming language • Supports loose typing
  • 6. Characteristics of C • Stable language – ANSI C was created in 1983 and since then no revision • Quick language • Facilitates low level programming • Core Language – C++, Java, Perl • C is portable language • C is an extensible language
  • 7. USES of C •Used System programming •End – user application programming
  • 8.
  • 11. Writing the first C Program # Include <stdio.h> Int main() { printf(“n welcome to C programming”); }
  • 12. Parts of a Program #include <stdio.h> int x; int main () { int y; printf("Enter x and y: "); scanf(&x,&y); printf("Sum is %dn",x+y); } Preprocessor Directive Global Declaration Function Local Declaration Statements
  • 13. Comments Text between /* and */ Used to “document” the code for the human reader Ignored by compiler (not part of program) Have to be careful comments may cover multiple lines ends as soon as */ encountered (so no internal comments - /* An /* internal */ comment */) // single line
  • 14. Comment Example #include <stdio.h> /* This comment covers * multiple lines * in the program. */ int main () /* The main header */ { /* No local declarations */ printf(“Too many commentsn”); } /* end of main */
  • 15. Files used in a C program Files in a C program Source file Header File Object file Executable file
  • 16. Source Code File • Containing the source code of the program • The file extension of the C source code is .c • C source code file contains main function and may be other functions • The main() is the starting point of the c program
  • 17. Header file •Use same subroutine in different programs •Compile the source code of the subroutine once, link object file to any other program •Header file with .h extension
  • 18. Standard Header Files Predefined files String.h – string handling functions Stdlib.h – Some library function (miscellaneous functions) Stdio.h – Standardized input and output functions Math.h – Mathematical functions Alloc.h – Dynamic memory allocation Conio.h – Clearing the screen and character input
  • 19. Object File • Object files are generated by the compiler as a result of processing the source code file • Object file contain compact binary code of the function definitions • Linker uses object files to produce an executable file by combining the object files together • Object files - .o or .obj extension
  • 20. Binary Executable File • Binary executable file is generated by the linker • The linker links the various object files to produce a binary file that can be directly executed • .exe extension - windows
  • 21. C Tokens Tokens are the basic building blocks in C language
  • 22.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. KEY WORDS • Keywords are system defined identifiers. • Keywords are reserved words. • They have specific meaning in the language and cannot be used by the programmer as a variable or constant name. • C is case sensitive. • 32 Keywords.
  • 34.
  • 35.
  • 36.
  • 37.
  • 39. Basic Data Types Data type Keywords Used Size in bytes Range Use Character char 1 -128 to 127 To store character Integer int 2 -32768 to 32767 To store integer number Floating Point float 4 3.4E-38 to 3.4E+38 To store decimal number Double double 8 1.7E-308 to 1.7E308 To store big floating-point number Valueless void 0 Valueless -
  • 40. Examples - Character #include <stdio.h> void main() { char c = 'b'; printf("The character value is: %c n", c); }
  • 41. Example - Integer #include <stdio.h> void main() { int i = 5; printf("The integer value is: %d n", i); }
  • 42. Examples - Float #include <stdio.h> void main() { float f = 7.2357; printf("The float value is: %f n", f); }
  • 43. Example - Double #include <stdio.h> void main() { double d = 71.2357455; printf("The double value is: %lf n", d); }
  • 44.
  • 45.
  • 46.
  • 48. VARIABLES A variable is a container(storage area) to hold data. All variables have three important attributes:  data type  name  value Eg: int a = 10; float b = 2.5; char c = ‘f’;
  • 49.
  • 50. CONSTANT A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5 An identifier also can be defined as a constant. eg. const double PI = 3.14 Here, value of PI is a constant.
  • 51. Constant Declaration ● To declare a constant, precede the normal variable declaration with const keyword and assign it a value. ● For example, const float pi = 3.14; ● Another way to designate a constant is to use the pre-processor command define. #define PI 3.14159 When the preprocessor reformats the program to be compiled by the compiler, it replaces each defined name with its corresponding value wherever it is found in the source program.
  • 52. TYPES OF CONSTANTS Integer constant eg: 1 Floating-point constant eg: 2.5 Character constant eg: ’a’ String constant eg: ”good”
  • 54. STREAMS ● A stream acts in two ways. It is the source of data as well as the destination of data. ● C programs input data and output data from a stream. Streams are associated with a physical device such as the monitor or with a file stored on the secondary memory. ● In a text stream, sequence of characters is divided into lines with each line being terminated with a new-line character (n). On the other hand, a binary stream contains data values using their memory representation. ● Although, we can do input/output from the keyboard/monitor or from any. Streams in a C program Text Stream Binary Stream Keyboard Data Monitor Data Input text stream Input text stream
  • 55. Data I/O 1. Formatted Input/output functions 2. Unformatted Input/output functions
  • 56. Formatted Input and Output Functions scanf() - input function printf() - output function
  • 57. printf() Output function General form: printf(“control_string or format specifier”,variable1,variable2, variable3,...); Example: printf(“The numbers are %d and %d”,number1,number2);
  • 59. Parts of conversion specifier field for printf() The percent sign and conversion code are required Other modifiers such as width and precision are optional. width specifies the total number of characters used to display the value precision indicates the number of characters used after the decimal point. The precision option is only used with floats or strings. Default precision value is 6
  • 60. Size modifiers used in printf()
  • 61. Examples printf(“number=%3dn”, 10); printf(“number=%2dn”, 10); printf(“number=%1dn”, 10); printf(“number=%7.2fn”, 5.4321); printf(“number=%.2fn”, 5.4391); printf(“number=%.9fn”, 5.4321); printf(“number=%fn”, 5.4321);
  • 62. Example # include <stdio.h> int main() { printf(“number=%3dn”, 10); printf(“number=%2dn”, 10); printf(“number=%1dn”, 10); printf(“number=%7.2fn”, 5.4321); printf(“number=%.2fn”, 5.4391); printf(“number=%.9fn”, 5.4321); printf(“number=%fn”, 5.4321); }
  • 63. Flag characters used in printf() Flag Meaning - Left justify the display + Display positive or negative sign of value Space Display space if there is no sign 0 Pad with leading zeros # Use alternate form of specifier
  • 65. Example with strings #include<stdio.h> int main() { printf(“%s”,“hello”); printf(“n%3s”,“hello”); printf(“n%10s”,“hello”); printf(“n%-10s”,“hello”); printf(“n%10.3s”,“hello”); return 0; }
  • 66. scanf() General form: scanf(“control_string”, variable1_address, variable2_address,...); scanf() returns the number of input fields successfully scanned, converted, and stored Example: scanf(“%d%d”,&num1,&num2);
  • 67. 2. Unformatted Input/output functions Input functions gets() getchar() getch() getche() Output functions puts() putchar() putch() puts() •The I/O library functions are listed in the “header” file <stdio.h>. •Types: •Unformatted Input/output functions •Formatted Input/output functions • 1. Data Input and Output
  • 68. getchar() Reads a single character from the input data stream; but does not return the character to the program until the ‘n’ ( or ) enter key is pressed. Syntax:- variable = getchar(); Example:- char c; c = getchar(); Program #include<stdio.h> void main() { char c; printf(“Enter a character”); c=getchar(); printf(“c = %c ”,c); } Output Enter a character k c = k
  • 69. getch() and getche() These functions read any alphanumeric character from the standard input device The character entered is not displayed by the getch() function until enter is pressed The getche() accepts and displays the character. The getch() accepts but does not display the character. Syntax: getche(); getch();
  • 70. getch() and getche() - Program #include<stdio.h> void main() { printf(“Enter two alphabets:”); getche(); getch(); } Output Enter two alphabets a b a
  • 71. putchar() This function prints one character on the screen at a time which is read by standard input. Syntax: putchar( variable name); Example: char c= ‘c’; putchar(c); #include<stdio.h> void main() { char ch; printf(“Enter a character: ”); scanf(“%c”, &ch); putchar(ch); } Output Enter a character: r r
  • 72. putch() This function prints any alphanumeric character taken by the standard input device Syntax: putch( variable name); Example: char c= ‘c’; putch(c); Program #include<stdio.h> void main() { char ch; printf(“Press any key to continue”); ch = getch(); printf(“ you pressed:”); putch(ch);
  • 73. gets() String I/O This function is used for accepting any string( stream of characters) until enter key is pressed Syntax char str[length of string in number]; gets(str); Example: char name[15]; gets(name); Program #include<stdio.h> void main() { char ch[30]; printf(“Enter the string:”); gets(ch); printf(“Entered string: %s”, ch); } Output: Enter the string: Use of data! Entered string: Use of data!
  • 74. puts() This function prints the string or character array. It is opposite to gets() Syntax char str[length of string in number]; gets(str); puts(str);