SlideShare a Scribd company logo
1 of 21
ESC101: Fundamentals of Computing
Basics of C Syntax,
Printing Outputs (printf)
Nisheeth
1
Syntax
• The rules that decide how to combine symbols to form
sentences
– Subject-verb-object (SVO) languages, e.g. English, write sentences
like, “the subject performed some verb upon the object”
– Subject-object-verb (SOV) languages, e.g. Hindi, write sentences like,
“subject ne object par kuchh verb kiya”
2
Syntax vs. standard
• If violating a rule makes the sentence nonsensical, you have
violated a syntactic rule
• If violating a rule makes the sentence look unconventional, but
still understandable, you have violated a standard
3
Syntax vs. standard
• main() vs int main()
– Some compilers will accept just using main, some will not
– Some compilers will accept main functions that don’t have a return
statement
• We will follow the C11 standard in this course
– Has been superseded by the C18 standard, but differences are minor
and don’t show up in the material covered in this course
• We may sometimes ask you questions for which the answer
would be ‘depends on the compiler version’
4
C Syntax: The “Alphabet” of C
 C programs can be written using the following alphabet
A B .... Z
a b .... z
0 1 .... 9
Space . , : ; ‘ $ “
# % & ! _ { } [ ] ( ) |
+ - * / =
5
C Syntax: Variables and Constants
 Most C programs consist of variables or constants with some name
 The value of each variables or constant is stored at some location in the
computer’s main memory (RAM)
 A variable’s value can be changed during execution of the program
 A constant’s value cannot be changed during execution of the program
firstName, age, height, streetAddress, valueOfPi
More on naming
conventions/rules later
6
C Syntax: C Keywords (root words)
 C language has a set of 32 keywords
 These keywords have special meaning
 Can’t use keywords for other purposes (e.g., can’t use them to declare
variable names, constant values, or function names)
Seen so far
Prutor shows keywords in a different color
7
C Syntax: Keywords Usage
# include <stdio.h>
int main(){
int main = 3;
printf(“%d”, main);
return 0;
}
 This WILL work
 Reason: main is not a keyword
 But not recommended
# include <stdio.h>
int main(){
int return = 3;
printf(“%d”, return);
return 0;
}
 This will NOT work
 Reason: return is a keyword
8
Advice: Try to Write Code that looks good
#include<stdio.h>
int main(){
int a = 1;
int b = 2;
int c;
c = a + b;
printf(“Result is %d”,c);
return 0;
}
#include<stdio.h>
int main(){
int a = 1;
int b = 2;
int c;
c = a + b;
printf(“Result is %d”,c);
return 0;
}
#include<stdio.h>
int main(){
int a = 1;
int b = 2;
int c;
c = a + b;
printf(“Result is %d”,c);
return 0;
}
 Very important in industry - large groups collaborate
 Important even for solo projects - maintenance
 Will learn several good coding habits over time (such as Commenting,
Indentation, Code-structuring)
This is good indentation
Prutor does it automatically
when you write your code
(try writing code in Prutor)
9
The printf Function
 A function used for printing the outputs of the C program
 Prints the outputs in a format specified by us
 We have already seen some simple examples of usage of printf
printf(“Welcome to ESC101”);
printf(“Result is %d”, c);
An integer
%d is used to print the
value of an integer 10
Introducing Mr. C (or Mr. Compiler)
 Will sometimes use this fictional character to refer to the C
compiler (or our Prutor system)
 Sometimes it will mean the screen of the computer that
shows us the program’s output
11
True Power of printf
We have seen how to make Mr. C
Say things like “Welcome to ESC101”
Tell us the value of an integer variable
Can he speak
only once?
No, you can
make him speak
again and again
#include<stdio.h>
int main(){
int a = 5, b = 4;
printf(“Hello”);
printf(“%d”,a);
printf(“%d”,b);
return 0;
}
Hello54
Don’t be afraid
to experiment
Hello 54
12
True Power of printf
We have seen how to make Mr. C
Say things like “Welcome to ESC101”
Tell us the value of an integer variable
Any
shorthands?
Yes, very
powerful ones!
#include<stdio.h>
int main(){
int a = 5, b = 4;
printf(“Hello %d %d”,a,b);
return 0;
}
Hello 5 4
I am confused

Okay, lets see
in detail
13
How printf Works?
HOW WE MUST SPEAK TO MR. COMPILER HOW WE USUALLY SPEAK TO A HUMAN
printf(“Hello %d %d”,a,b); Please write the English word Hello,
followed by a space, followed by the
value of an integer, followed by a
space followed by the value of another
integer.
By the way, the first integer to be
written is a and the second integer to
be written is b.
Mr. C likes to be told beforehand what all we are going to ask
him to do!
printf follows this exact same rule while telling Mr. C what to print
Format string
tells me how to
print things, and
then I am told
what to print
Format string
14
True Power of printf
int main(){
int a = 3;
int b = 2;
printf(“Sum of a and b = %d”, a+b);
return 0;
}
Can also use printf
to directly print the
value of an
expression
15
Summary: The syntax of printf
printf(format string, list of things to print);
printf(“Hello %d %d”, a,b);
Important: Format string must have format specifiers for all things we wish to
print in the exact same order as those things appear in the list of things
Example (already seen)
Can Mr. C print
integers only?
Of course NOT.
Wait for next
week’s lectures
16
Note: In some cases,
there will be no such list.
Example: printf(“Hello”);
Some Fun with printf
Can I print different things on separate lines?
What if I wish to print the character “ (inverted quotes)?
What if I wish to print the character % (percentage sign)?
Hello
5
4
printf(“Hellon%dn%d”,a,b);
printf(““Hello““); “Hello”
If I see ”, I
will print “
printf(“90%% marks“); 90% marks
If I see
%%, I will
print %
If I see n, I will
start printing
on a new line
17
Some Fun with printf
To print the character  (backslash) use 
To print a tab character (a longer space) use t
Allows us to print very nicely formatted output 
More examples in labs – till then, have fun on your own
printf(“To print on new line, use n”);
To print on new line, use n
printf(“VerytNice“); Very Nice
n, ” called escape
sequences since
they “escape” the
normal rules
Experiment with
them to get
comfortable
18
Fun with Integers
Operation C Code a b c
Addition c = a + b; 5 4 9
Subtraction c = a – b; 4 5 -1
Multiplication c = a * b; -2 -4 8
Division c = a / b; 7 2 3
Remainder c = a % b; 7 2 1
Be careful: in math we
often write z = 2xy
Mr C will not like it.
He will want z = 2 * x * y;
Also be careful about
division and remainder
7 / 2 is actually 3.5 but since c is
an integer variable, it just stores
3. Remainder is 1
Experiment on your own –
will revisit these very soon
Oh! So Mr. C allows
us to use constants
too in expressions?
Yes. The command
c = 2 + b;
makes sense to me
printf(“%d %d”,a,10);
is fine too 
Come to the lab
and give it a try!
Not everything needs to
be stored in a variable
19
Life beyond Integers
 Lots of fun possible with integers alone
 However, the box storing integers is actually not very big
 Can only store integers between -2,147,483,648 and 2,147,483,647
 Also, what about real numbers (fractions etc)
 How to ask Mr C to work with a real number?
 How to ask Mr C to print a real number?
 Later classes: long, float, double, long double
 C designers were really nice with names
20
ESC101: Fundamentals of
Computing
Emoticons from Flaticon, designed by Twitter
https://www.flaticon.com/packs/smileys-and-people-9
Licensed under CC BY 3.0

More Related Content

Similar to INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx

the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184Sumit Saini
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing TutorialMahira Banu
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdfziyadaslanbey
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and BeyondComicSansMS
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 

Similar to INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx (20)

the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
C rules
C rulesC rules
C rules
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
C Programming
C ProgrammingC Programming
C Programming
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and Beyond
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
2 debugging-c
2 debugging-c2 debugging-c
2 debugging-c
 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programming
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
14 Jo P Feb 08
14 Jo P Feb 0814 Jo P Feb 08
14 Jo P Feb 08
 

More from AbhimanyuChaure

INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxAbhimanyuChaure
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxAbhimanyuChaure
 
b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...
b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...
b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...AbhimanyuChaure
 
915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...
915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...
915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...AbhimanyuChaure
 
28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...
28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...
28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...AbhimanyuChaure
 
2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...
2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...
2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...AbhimanyuChaure
 
IITK ESC 111M Lec02.pptx .
IITK ESC 111M Lec02.pptx               .IITK ESC 111M Lec02.pptx               .
IITK ESC 111M Lec02.pptx .AbhimanyuChaure
 

More from AbhimanyuChaure (7)

INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...
b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...
b5f1d7dac0b7d87e4f41a5509c1bf602e0d18aae0f9177cb406348a0edf59507_hello iitk L...
 
915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...
915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...
915d511043d1509957e5b45ed827a5a9652f6759581a3682f5aabb6b0bccc5bd_Helloiitk Le...
 
28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...
28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...
28f7f76ecd0fa8c132d87392a65507f78661379cfef5faa7ab8f733625de2723_Helloiit Lec...
 
2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...
2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...
2480b48c99ef4e3b94038d2680df02fe96bc196b029703c1929ec2575bed8eb9_Helloiitk Le...
 
IITK ESC 111M Lec02.pptx .
IITK ESC 111M Lec02.pptx               .IITK ESC 111M Lec02.pptx               .
IITK ESC 111M Lec02.pptx .
 

Recently uploaded

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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
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
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
(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
 
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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
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
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
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
 
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
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
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
 

Recently uploaded (20)

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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
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, ...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
(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...
 
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
 
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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(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...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
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
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
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
 
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
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.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
 

INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx

  • 1. ESC101: Fundamentals of Computing Basics of C Syntax, Printing Outputs (printf) Nisheeth 1
  • 2. Syntax • The rules that decide how to combine symbols to form sentences – Subject-verb-object (SVO) languages, e.g. English, write sentences like, “the subject performed some verb upon the object” – Subject-object-verb (SOV) languages, e.g. Hindi, write sentences like, “subject ne object par kuchh verb kiya” 2
  • 3. Syntax vs. standard • If violating a rule makes the sentence nonsensical, you have violated a syntactic rule • If violating a rule makes the sentence look unconventional, but still understandable, you have violated a standard 3
  • 4. Syntax vs. standard • main() vs int main() – Some compilers will accept just using main, some will not – Some compilers will accept main functions that don’t have a return statement • We will follow the C11 standard in this course – Has been superseded by the C18 standard, but differences are minor and don’t show up in the material covered in this course • We may sometimes ask you questions for which the answer would be ‘depends on the compiler version’ 4
  • 5. C Syntax: The “Alphabet” of C  C programs can be written using the following alphabet A B .... Z a b .... z 0 1 .... 9 Space . , : ; ‘ $ “ # % & ! _ { } [ ] ( ) | + - * / = 5
  • 6. C Syntax: Variables and Constants  Most C programs consist of variables or constants with some name  The value of each variables or constant is stored at some location in the computer’s main memory (RAM)  A variable’s value can be changed during execution of the program  A constant’s value cannot be changed during execution of the program firstName, age, height, streetAddress, valueOfPi More on naming conventions/rules later 6
  • 7. C Syntax: C Keywords (root words)  C language has a set of 32 keywords  These keywords have special meaning  Can’t use keywords for other purposes (e.g., can’t use them to declare variable names, constant values, or function names) Seen so far Prutor shows keywords in a different color 7
  • 8. C Syntax: Keywords Usage # include <stdio.h> int main(){ int main = 3; printf(“%d”, main); return 0; }  This WILL work  Reason: main is not a keyword  But not recommended # include <stdio.h> int main(){ int return = 3; printf(“%d”, return); return 0; }  This will NOT work  Reason: return is a keyword 8
  • 9. Advice: Try to Write Code that looks good #include<stdio.h> int main(){ int a = 1; int b = 2; int c; c = a + b; printf(“Result is %d”,c); return 0; } #include<stdio.h> int main(){ int a = 1; int b = 2; int c; c = a + b; printf(“Result is %d”,c); return 0; } #include<stdio.h> int main(){ int a = 1; int b = 2; int c; c = a + b; printf(“Result is %d”,c); return 0; }  Very important in industry - large groups collaborate  Important even for solo projects - maintenance  Will learn several good coding habits over time (such as Commenting, Indentation, Code-structuring) This is good indentation Prutor does it automatically when you write your code (try writing code in Prutor) 9
  • 10. The printf Function  A function used for printing the outputs of the C program  Prints the outputs in a format specified by us  We have already seen some simple examples of usage of printf printf(“Welcome to ESC101”); printf(“Result is %d”, c); An integer %d is used to print the value of an integer 10
  • 11. Introducing Mr. C (or Mr. Compiler)  Will sometimes use this fictional character to refer to the C compiler (or our Prutor system)  Sometimes it will mean the screen of the computer that shows us the program’s output 11
  • 12. True Power of printf We have seen how to make Mr. C Say things like “Welcome to ESC101” Tell us the value of an integer variable Can he speak only once? No, you can make him speak again and again #include<stdio.h> int main(){ int a = 5, b = 4; printf(“Hello”); printf(“%d”,a); printf(“%d”,b); return 0; } Hello54 Don’t be afraid to experiment Hello 54 12
  • 13. True Power of printf We have seen how to make Mr. C Say things like “Welcome to ESC101” Tell us the value of an integer variable Any shorthands? Yes, very powerful ones! #include<stdio.h> int main(){ int a = 5, b = 4; printf(“Hello %d %d”,a,b); return 0; } Hello 5 4 I am confused  Okay, lets see in detail 13
  • 14. How printf Works? HOW WE MUST SPEAK TO MR. COMPILER HOW WE USUALLY SPEAK TO A HUMAN printf(“Hello %d %d”,a,b); Please write the English word Hello, followed by a space, followed by the value of an integer, followed by a space followed by the value of another integer. By the way, the first integer to be written is a and the second integer to be written is b. Mr. C likes to be told beforehand what all we are going to ask him to do! printf follows this exact same rule while telling Mr. C what to print Format string tells me how to print things, and then I am told what to print Format string 14
  • 15. True Power of printf int main(){ int a = 3; int b = 2; printf(“Sum of a and b = %d”, a+b); return 0; } Can also use printf to directly print the value of an expression 15
  • 16. Summary: The syntax of printf printf(format string, list of things to print); printf(“Hello %d %d”, a,b); Important: Format string must have format specifiers for all things we wish to print in the exact same order as those things appear in the list of things Example (already seen) Can Mr. C print integers only? Of course NOT. Wait for next week’s lectures 16 Note: In some cases, there will be no such list. Example: printf(“Hello”);
  • 17. Some Fun with printf Can I print different things on separate lines? What if I wish to print the character “ (inverted quotes)? What if I wish to print the character % (percentage sign)? Hello 5 4 printf(“Hellon%dn%d”,a,b); printf(““Hello““); “Hello” If I see ”, I will print “ printf(“90%% marks“); 90% marks If I see %%, I will print % If I see n, I will start printing on a new line 17
  • 18. Some Fun with printf To print the character (backslash) use To print a tab character (a longer space) use t Allows us to print very nicely formatted output  More examples in labs – till then, have fun on your own printf(“To print on new line, use n”); To print on new line, use n printf(“VerytNice“); Very Nice n, ” called escape sequences since they “escape” the normal rules Experiment with them to get comfortable 18
  • 19. Fun with Integers Operation C Code a b c Addition c = a + b; 5 4 9 Subtraction c = a – b; 4 5 -1 Multiplication c = a * b; -2 -4 8 Division c = a / b; 7 2 3 Remainder c = a % b; 7 2 1 Be careful: in math we often write z = 2xy Mr C will not like it. He will want z = 2 * x * y; Also be careful about division and remainder 7 / 2 is actually 3.5 but since c is an integer variable, it just stores 3. Remainder is 1 Experiment on your own – will revisit these very soon Oh! So Mr. C allows us to use constants too in expressions? Yes. The command c = 2 + b; makes sense to me printf(“%d %d”,a,10); is fine too  Come to the lab and give it a try! Not everything needs to be stored in a variable 19
  • 20. Life beyond Integers  Lots of fun possible with integers alone  However, the box storing integers is actually not very big  Can only store integers between -2,147,483,648 and 2,147,483,647  Also, what about real numbers (fractions etc)  How to ask Mr C to work with a real number?  How to ask Mr C to print a real number?  Later classes: long, float, double, long double  C designers were really nice with names 20
  • 21. ESC101: Fundamentals of Computing Emoticons from Flaticon, designed by Twitter https://www.flaticon.com/packs/smileys-and-people-9 Licensed under CC BY 3.0