SlideShare a Scribd company logo
1 of 40
Download to read offline
Lecture 04
Introduction to C Programming
CSE115: Computing Concepts
preprocessor directives
main function heading
{
declarations
executable statements
}
General Form of a C Program
A Simple Program in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!n");
return 0;
}
A Simple Program in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!n");
return 0;
}
standard Library, input-output, header-file
Beginning of program
End of Segment
Start of Segment
Function for printing text
End of statement
Insert a new line
Preprocessor Directives
• A C program begins with # which provides an instruction to the C
preprocessor
• It is executed before the actual compilation is done.
• Two most common directives :
• #include
• #define
• In our example (#include<stdio.h>) identifies the header
file for standard input and output operations.
Function main()
• Identify the start of the program
• Every C program has a main( )
• 'main' is a C keyword. We must not use it for any other
purpose.
• 4 common ways of main declaration
int main(void)
{
return 0;
}
void main(void)
{
}
main(void)
{
}
main( )
{
}
The curly braces { }
• Identify a segment / body of a program
• The start and end of a function
• The start and end of the selection or repetition block.
• Since the opening brace indicates the start of a segment
with the closing brace indicating the end of a segment,
there must be just as many opening braces as closing
braces (this is a common mistake of beginners)
Statement
• Specifying an action to be taken by the computer as the program
executes.
• Each statement in C needs to be terminated with semicolon (;)
• Example:
#include <stdio.h>
int main()
{
printf(“I love programmingn”);
printf(“You will love it too once ”);
printf(“you know the trickn”);
return 0;
}
statement
statement
statement
statement
Statement
• Statement has two parts :
• Declaration
• The part of the program that tells the compiler the names of memory
cells in a program
• Executable statements
• Program lines that are converted to machine language instructions and
executed by the computer
An Example
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
//Get the distance in miles
printf("Enter distance in miles: ");
scanf("%f", &miles);
//Convert the distance to kilometres
kms = KMS_PER_MILE * miles;
//Display the distance in kilometres
printf("That equals %f km.n", kms);
return 0;
}
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
//Get the distance in miles
printf("Enter distance in miles: ");
scanf("%f", &miles);
//Convert the distance to kilometres
kms = KMS_PER_MILE * miles;
//Display the distance in kilometres
printf("That equals %f km.n", kms);
return 0;
}
preprocessor
directives
standard header file
comments
constant
reserved
words
variables
functions
special
symbols
punctuations
An Example
An Example
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
//Get the distance in miles
printf("Enter distance in miles: ");
scanf("%f", &miles);
//Convert the distance to kilometres
kms = KMS_PER_MILE * miles;
//Display the distance in kilometres
printf("That equals %f km.n", kms);
return 0;
}
declarations
Executable
statements
An Example
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
//Get the distance in miles
printf("Enter distance in miles: ");
scanf("%f", &miles);
//Convert the distance to kilometres
kms = KMS_PER_MILE * miles;
//Display the distance in kilometres
printf("That equals %f km.n", kms);
return 0;
}
Sample Run
Enter distance in miles: 10.5
That equals 16.89 km.
At the beginning
memory
MileToKm.exe
miles
?
?
kms
After user enters:
10.5 to
scanf("%f", &miles);
memory
MileToKm.exe
miles
10.5
?
kms
After this line is
executed:
kms = KMS_PER_MILE * miles;
memory
MileToKm.exe
miles
10.5
16.89
kms
 What happens in the computer memory?
Do not assume that
uninitialised variables
contain zero! (Very
common mistake.)
An Example
Variables
• Variable  a name associated with a memory cell whose value can
change
• Variable Declaration: specifies the type of a variable
• Example: int num;
• Variable Definition: assigning a value to the declared variable
• Example: num = 5;
Basic Data Types
• There are 4 basic data types :
• int
• float
• double
• char
• int
• used to declare numeric program variables of integer type
• whole numbers, positive and negative
• keyword: int
int number;
number = 12;
Basic Data Types
• float
• fractional parts, positive and negative
• keyword: float
float height;
height = 1.72;
• double
• used to declare floating point variable of higher precision or higher range
of numbers
• exponential numbers, positive and negative
• keyword: double
double valuebig;
• valuebig = 12E-3; (is equal to 12X10-3)
Basic Data Types
• char
• equivalent to ‘letters’ in English language
• Example of characters:
• Numeric digits: 0 - 9
• Lowercase/uppercase letters: a - z and A - Z
• Space (blank)
• Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < >
etc
• single character
• keyword: char
char my_letter;
my_letter = 'U';
• In addition, there are void, short, long, etc.
The declared character must be
enclosed within a single quote!
A closer look at variables
19
Memory
A closer look at variables
int a = 139, b = -5;
20
Memory
A closer look at variables
int a = 139, b = -5;
21
Memory
a
b
139
-5
A closer look at variables
int a = 139, b = -5;
22
Memory
a
b
139
-5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 32 bits
A closer look at variables
int a = 139, b = -5;
23
Memory
a
b
139
-5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 32 bits
A closer look at variables
char c = ‘H’;
0 1 0 0 1 0 0 0 8 bits
A closer look at variables
char c = ‘H’;
0 1 0 0 1 0 0 0 8 bits
?
A closer look at variables
char c = ‘H’;
10010002 = 7210
0 1 0 0 1 0 0 0 8 bits
?
Input/Output Operations
• Input operation
• an instruction that copies data from an input device into
memory
• Output operation
• an instruction that displays information stored in memory to
the output devices (such as the monitor screen)
Input/Output Functions
• A C function that performs an input or output operation
• A few functions that are pre-defined in the header file
stdio.h such as :
• printf()
• scanf()
• getchar() & putchar()
The printf function
• Used to send data to the standard output (usually the monitor) to
be printed according to specific format.
• General format:
• printf(“string literal”);
• A sequence of any number of characters surrounded by
double quotation marks.
• printf(“format string”, variables);
• Format string is a combination of text, conversion specifier
and escape sequence.
The printf function
• Example:
• printf(“Thank you”);
• printf (“Total sum is: %dn”, sum);
• %d is a placeholder (conversion specifier)
• marks the display position for a type
integer variable
• n is an escape sequence
• moves the cursor to the new line
No Conversion
Specifier
Output Type Output Example
1 %d Signed decimal integer 76
2 %i Signed decimal integer 76
3 %o Unsigned octal integer 134
4 %u Unsigned decimal integer 76
5 %x Unsigned hexadecimal (small letter) 9c
6 %X Unsigned hexadecimal (capital letter) 9C
7 %f Integer including decimal point 76.0000
8 %e Signed floating point (using e
notation)
7.6000e+01
9 %E Signed floating point (using E
notation)
7.6000E+01
10 %g The shorter between %f and %e 76
11 %G The shorter between %f and %E 76
12 %c Character ‘7’
13 %s String ‘76'
Placeholder / Conversion Specifier
Escape Sequence
Escape Sequence Effect
a Beep sound
b Backspace
f Formfeed (for printing)
n New line
r Carriage return
t Tab
v Vertical tab
 Backslash
” “ sign
o Octal decimal
x Hexadecimal
O NULL
Formatting output
int meters = 21, feet = 68 , inches =
11;
printf("Results: %3d meters = %4d ft.
%2d in.n", meters, feet, inches);
printf("Results: %03d meters = %04d ft.
%02d in.n", meters, feet, inches);
R e s u l t s : 2 1 m e t e r s = 6 8 f t . 1 1 i n .
R e s u l t s : 0 2 1 m e t e r s = 0 0 6 8 f t . 1 1 i n .
Formatting output
Formatting output
• Displaying x Using Format String Placeholder %6.2f
The scanf function
• Read data from the standard input device (usually keyboard) and
store it in a variable.
• General format:
• scanf(“Format string”, &variable);
• Notice ampersand (&) operator :
• C address of operator
• it passes the address of the variable instead of the variable itself
• tells the scanf() where to find the variable to store the new
value
The scanf function
• Example :
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
• Common Conversion Identifier used in printf and scanf
functions.
printf scanf
int %d %d
float %f %f
double %lf %lf
char %c %c
string %s %s
The scanf function
• If you want the user to enter more than one value, you
serialize the inputs.
• Example:
float height, weight;
printf(“Please enter your height and weight:”);
scanf(“%f%f”, &height, &weight);

More Related Content

What's hot (20)

C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures
 
What is c
What is cWhat is c
What is c
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabus
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
 
c++
 c++  c++
c++
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
C++ PROGRAMMING BASICS
C++ PROGRAMMING BASICSC++ PROGRAMMING BASICS
C++ PROGRAMMING BASICS
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
 
C language basics
C language basicsC language basics
C language basics
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 

Similar to Cse115 lecture04introtoc programming

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 Functionimtiazalijoono
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of Ceducationfront
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxvanshhans21102005
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg PatelTechNGyan
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
2.1 input and output in c
2.1 input and output in c2.1 input and output in c
2.1 input and output in cJawad Khan
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.pptUdhayaKumar175069
 

Similar to Cse115 lecture04introtoc programming (20)

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
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Cpu
CpuCpu
Cpu
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg Patel
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
2.1 input and output in c
2.1 input and output in c2.1 input and output in c
2.1 input and output in c
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
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
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 

Cse115 lecture04introtoc programming

  • 1. Lecture 04 Introduction to C Programming CSE115: Computing Concepts
  • 2. preprocessor directives main function heading { declarations executable statements } General Form of a C Program
  • 3. A Simple Program in C #include <stdio.h> #include <stdlib.h> int main() { printf("Hello world!n"); return 0; }
  • 4. A Simple Program in C #include <stdio.h> #include <stdlib.h> int main() { printf("Hello world!n"); return 0; } standard Library, input-output, header-file Beginning of program End of Segment Start of Segment Function for printing text End of statement Insert a new line
  • 5. Preprocessor Directives • A C program begins with # which provides an instruction to the C preprocessor • It is executed before the actual compilation is done. • Two most common directives : • #include • #define • In our example (#include<stdio.h>) identifies the header file for standard input and output operations.
  • 6. Function main() • Identify the start of the program • Every C program has a main( ) • 'main' is a C keyword. We must not use it for any other purpose. • 4 common ways of main declaration int main(void) { return 0; } void main(void) { } main(void) { } main( ) { }
  • 7. The curly braces { } • Identify a segment / body of a program • The start and end of a function • The start and end of the selection or repetition block. • Since the opening brace indicates the start of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces (this is a common mistake of beginners)
  • 8. Statement • Specifying an action to be taken by the computer as the program executes. • Each statement in C needs to be terminated with semicolon (;) • Example: #include <stdio.h> int main() { printf(“I love programmingn”); printf(“You will love it too once ”); printf(“you know the trickn”); return 0; } statement statement statement statement
  • 9. Statement • Statement has two parts : • Declaration • The part of the program that tells the compiler the names of memory cells in a program • Executable statements • Program lines that are converted to machine language instructions and executed by the computer
  • 10. An Example /* Converts distance in miles to kilometres. */ #include <stdio.h> //printf, scanf definitions #define KMS_PER_MILE 1.609 //conversion constant int main(void) { float miles, // input – distance in miles kms; // output – distance in kilometres //Get the distance in miles printf("Enter distance in miles: "); scanf("%f", &miles); //Convert the distance to kilometres kms = KMS_PER_MILE * miles; //Display the distance in kilometres printf("That equals %f km.n", kms); return 0; }
  • 11. /* Converts distance in miles to kilometres. */ #include <stdio.h> //printf, scanf definitions #define KMS_PER_MILE 1.609 //conversion constant int main(void) { float miles, // input – distance in miles kms; // output – distance in kilometres //Get the distance in miles printf("Enter distance in miles: "); scanf("%f", &miles); //Convert the distance to kilometres kms = KMS_PER_MILE * miles; //Display the distance in kilometres printf("That equals %f km.n", kms); return 0; } preprocessor directives standard header file comments constant reserved words variables functions special symbols punctuations An Example
  • 12. An Example /* Converts distance in miles to kilometres. */ #include <stdio.h> //printf, scanf definitions #define KMS_PER_MILE 1.609 //conversion constant int main(void) { float miles, // input – distance in miles kms; // output – distance in kilometres //Get the distance in miles printf("Enter distance in miles: "); scanf("%f", &miles); //Convert the distance to kilometres kms = KMS_PER_MILE * miles; //Display the distance in kilometres printf("That equals %f km.n", kms); return 0; } declarations Executable statements
  • 13. An Example /* Converts distance in miles to kilometres. */ #include <stdio.h> //printf, scanf definitions #define KMS_PER_MILE 1.609 //conversion constant int main(void) { float miles, // input – distance in miles kms; // output – distance in kilometres //Get the distance in miles printf("Enter distance in miles: "); scanf("%f", &miles); //Convert the distance to kilometres kms = KMS_PER_MILE * miles; //Display the distance in kilometres printf("That equals %f km.n", kms); return 0; } Sample Run Enter distance in miles: 10.5 That equals 16.89 km.
  • 14. At the beginning memory MileToKm.exe miles ? ? kms After user enters: 10.5 to scanf("%f", &miles); memory MileToKm.exe miles 10.5 ? kms After this line is executed: kms = KMS_PER_MILE * miles; memory MileToKm.exe miles 10.5 16.89 kms  What happens in the computer memory? Do not assume that uninitialised variables contain zero! (Very common mistake.) An Example
  • 15. Variables • Variable  a name associated with a memory cell whose value can change • Variable Declaration: specifies the type of a variable • Example: int num; • Variable Definition: assigning a value to the declared variable • Example: num = 5;
  • 16. Basic Data Types • There are 4 basic data types : • int • float • double • char • int • used to declare numeric program variables of integer type • whole numbers, positive and negative • keyword: int int number; number = 12;
  • 17. Basic Data Types • float • fractional parts, positive and negative • keyword: float float height; height = 1.72; • double • used to declare floating point variable of higher precision or higher range of numbers • exponential numbers, positive and negative • keyword: double double valuebig; • valuebig = 12E-3; (is equal to 12X10-3)
  • 18. Basic Data Types • char • equivalent to ‘letters’ in English language • Example of characters: • Numeric digits: 0 - 9 • Lowercase/uppercase letters: a - z and A - Z • Space (blank) • Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc • single character • keyword: char char my_letter; my_letter = 'U'; • In addition, there are void, short, long, etc. The declared character must be enclosed within a single quote!
  • 19. A closer look at variables 19 Memory
  • 20. A closer look at variables int a = 139, b = -5; 20 Memory
  • 21. A closer look at variables int a = 139, b = -5; 21 Memory a b 139 -5
  • 22. A closer look at variables int a = 139, b = -5; 22 Memory a b 139 -5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 32 bits
  • 23. A closer look at variables int a = 139, b = -5; 23 Memory a b 139 -5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 32 bits
  • 24. A closer look at variables char c = ‘H’; 0 1 0 0 1 0 0 0 8 bits
  • 25. A closer look at variables char c = ‘H’; 0 1 0 0 1 0 0 0 8 bits ?
  • 26.
  • 27.
  • 28. A closer look at variables char c = ‘H’; 10010002 = 7210 0 1 0 0 1 0 0 0 8 bits ?
  • 29. Input/Output Operations • Input operation • an instruction that copies data from an input device into memory • Output operation • an instruction that displays information stored in memory to the output devices (such as the monitor screen)
  • 30. Input/Output Functions • A C function that performs an input or output operation • A few functions that are pre-defined in the header file stdio.h such as : • printf() • scanf() • getchar() & putchar()
  • 31. The printf function • Used to send data to the standard output (usually the monitor) to be printed according to specific format. • General format: • printf(“string literal”); • A sequence of any number of characters surrounded by double quotation marks. • printf(“format string”, variables); • Format string is a combination of text, conversion specifier and escape sequence.
  • 32. The printf function • Example: • printf(“Thank you”); • printf (“Total sum is: %dn”, sum); • %d is a placeholder (conversion specifier) • marks the display position for a type integer variable • n is an escape sequence • moves the cursor to the new line
  • 33. No Conversion Specifier Output Type Output Example 1 %d Signed decimal integer 76 2 %i Signed decimal integer 76 3 %o Unsigned octal integer 134 4 %u Unsigned decimal integer 76 5 %x Unsigned hexadecimal (small letter) 9c 6 %X Unsigned hexadecimal (capital letter) 9C 7 %f Integer including decimal point 76.0000 8 %e Signed floating point (using e notation) 7.6000e+01 9 %E Signed floating point (using E notation) 7.6000E+01 10 %g The shorter between %f and %e 76 11 %G The shorter between %f and %E 76 12 %c Character ‘7’ 13 %s String ‘76' Placeholder / Conversion Specifier
  • 34. Escape Sequence Escape Sequence Effect a Beep sound b Backspace f Formfeed (for printing) n New line r Carriage return t Tab v Vertical tab Backslash ” “ sign o Octal decimal x Hexadecimal O NULL
  • 35. Formatting output int meters = 21, feet = 68 , inches = 11; printf("Results: %3d meters = %4d ft. %2d in.n", meters, feet, inches); printf("Results: %03d meters = %04d ft. %02d in.n", meters, feet, inches); R e s u l t s : 2 1 m e t e r s = 6 8 f t . 1 1 i n . R e s u l t s : 0 2 1 m e t e r s = 0 0 6 8 f t . 1 1 i n .
  • 37. Formatting output • Displaying x Using Format String Placeholder %6.2f
  • 38. The scanf function • Read data from the standard input device (usually keyboard) and store it in a variable. • General format: • scanf(“Format string”, &variable); • Notice ampersand (&) operator : • C address of operator • it passes the address of the variable instead of the variable itself • tells the scanf() where to find the variable to store the new value
  • 39. The scanf function • Example : int age; printf(“Enter your age: “); scanf(“%d”, &age); • Common Conversion Identifier used in printf and scanf functions. printf scanf int %d %d float %f %f double %lf %lf char %c %c string %s %s
  • 40. The scanf function • If you want the user to enter more than one value, you serialize the inputs. • Example: float height, weight; printf(“Please enter your height and weight:”); scanf(“%f%f”, &height, &weight);