Lec 04
Lec 04
2
Recap
Every C program’s entry point
(program’s execution starts Tells C compiler to include the standard
here) is the main function input/output library stdio.h (collection of
with return type integer functions such as printf, scanf, etc)
# include <stdio.h>
int main () {
int a = 1; Can’t assign a value to c
since it has not been
int b = 2; declared yet
c = a + b;
printf(“Result is %d”, c);
return 0;
} Will NOT Compile
5
What’s Wrong Here?
# include <stdio.h>
int main () {
int a; Can’t use variables a and b in this
int b; assignment operation since a and
b have not been assigned a value
int c; (initialized) in this program
c = a + b;
printf(“Result is %d”, c);
return 0;
} Will Compile but
will print garbage
6
What’s Wrong Here?
9
Recap: Alphabet and Keywords of C
C programs can be written using the following
alphabet
A B .... Z Keywords in C
a b .... z
0 1 .... 9
Space . , : ; ‘ $ “
# % & ! _ { } [ ] ( ) |
+ - * / =
10
Naming Convention for Variables and
Functions
We have seen variables and their usage in programs
We have seen the main and printf function (and will see
various other standard functions and user-defined function
later)
Need to follow some rules for naming of variables and
functions
Names can only contain A-Z, a-z, 0-9, and underscore _
Can’t begin a variable’s or function’s name with a number
A_3, abcDS2, this_variable are some valid names
321, 5_r, dfd@dhr, this variable, no-entry are some not
Contains special Contains hyphen
valid names symbol @
Start with number Contains space
character (“dash”) character -
11
Variables and Function Names: Some
Suggestions
Should prefer short, meaningful names. Don’t use C
keywords.
Multi-word name allows, e.g., firstNumber,
first_number
• Advice: Use capital letters for constants Yes,(e.g.,
but not advisable.
NUMBER_DAYS_JAN)
#include <stdio.h> May make mistakes,
confuse others
• intAdvice:
main(){ Use small letters for variables (e.g., radius,
So this program is fine?
intvolume)
temp, TEMP, Temp, TeMp;
For me, the names temp,
return 0;
Temp, TEMP, TeMp are all
} different variable names
12
Recap: printf and its use Note: In some cases,
there will be no such list.
Example: printf(“Hello”);
13
Reading Inputs: The scanf function
Programs that don’t take inputs from user can be boring
We saw programs to add two numbers but both had to be written into code
#include <stdio.h>
int main(){ 3 8
int a, b;
scanf(“%d”,
Thanks. Let me a
&a); b
get back to work
scanf(“%d”, &b);
11
printf(“%d”,
Please givea + b);
me input
return 0; 11
}
scanf: Some Words of Caution
In Prutor, input has to be specified before “Execute”
scanf: Some Words of Caution
In Prutor,
Yes, input
in printf they has tobutbe
are different Both work!
in specified before “Execute”
scanf, both look like whitespaces to me Experiment!
Please be very careful about this common mistake
Space, Tab, Newline are called whitespace
scanf(“%d”,a); scanf(“%d”,&a);
characters since they are invisible
Huh! What is a
whitespace?
Taking Multiple Inputs using a Single scanf
#include <stdio.h>
int main(){ 3 8
int a, b; Help!!!
scanf(“%d%d”,
You
All entered
look the
a
&a, &b);
b
only
same oneto integer
me a + b);
printf(“%d”, 11
return 0;
11
}
Input please Thanks Okay okay
How does scanf work ?
HOW WE MUST SPEAK TO MR. HOW WE USUALLY SPEAK TO A
COMPILER HUMAN
Try breaking up
I have no idea the problem into
what is going smaller pieces
wrong here!
26
A Useful Tip While Solving Problems
Equals 0
A Useful Tip While Solving Problems