C_chapter_02
C_chapter_02
&
printf and scanf Functions
2020
1
What is a program
A sequence of instructions that a computer can
interpret and execute;
If I tell you the way from Chib Plaza to Executive Block … I
will tell sequence of instructions…. Any wrong instruction
leads to a undesired result.
A program is something that runs on your computer. In
case of MS Windows program is of .EXE or .COM
extensions
MS Word, Power point, Excel are all computer
programs
Writing C Programs
A programmer uses a text editor to create or modify
files containing C code.
Code is also known as source code.
A file containing source code is called a source file.
After a C source file has been created, the programmer
must invoke the C compiler before the program can
be executed (run).
#include <stdio.h>
/* The simplest C Program */
Writing and Running Programs
int main(int argc, char **argv) 1. Write text of program (source code) using an editor such
{ as Netbeans for C/C++ Development, Code::Blocks or
printf(“Hello World\n”); Eclipse CDT
return 0; 2. save as file e.g. my_program.c
}
3. Run the compiler to convert program from source to an
“executable” or “binary”:
$ gcc –Wall –g my_program.c –o my_program
$ gcc -Wall –g my_program.c –o my_program
tt.c: In function `main':
tt.c:6: parse error before `x'
tt.c:5: parm types given both in parmlist and separately
tt.c:8: `x' undeclared (first use in this function)
tt.c:8: (Each undeclared identifier is reported only once
4-N. Compiler gives errors and warnings; edit source file,
tt.c:8: for each function it appears in.)
tt.c:10: warning: control reaches end of non-void function
fix it, and re-compile
tt.c: At top level:
tt.c:11: parse error before `return'
#include <stdio.h>
The main() function is always
/* The simplest C Program */ where your program starts
int main(int argc, char **argv) running.
{
Blocks of code (“lexical
printf(“Hello World\n”); scopes”) are marked by { … }
return 0;
}
Return ‘0’ from this function Print out a message. ‘\n’ means “new line”.
5
A Quick Digression About the Compiler
#include <stdio.h>
/* The simplest C Program */
Compilation occurs in two steps:
int main(int argc, char **argv)
{
Preprocess
printf(“Hello World\n”);
return 0;
“Preprocessing” and “Compiling”
}
6
What is a Variable?
symbol table?
7
Types of variable
• We must declare the type of every variable we use
in C.
• Every variable has a type (e.g. int) and a name.
• We already saw int, double and float.
• This prevents some bugs caused by spelling errors
(misspelling variable names).
• Declarations of types should always be together at
the top of main or a function (see later).
• Other types are char, signed, unsigned,
long, short and const.
Naming variables
• Variables in C can be given any name made from
numbers, letters and underlines which is not a
keyword and does not begin with a number.
• A good name for your variables is important
int a,b; int start_time;
double d; int no_students;
/* This is double course_mark;
a bit cryptic */ /* This is a bit better */
1 + 2 * 2 1 + 4 5
(1 + 2) * 2 3 * 2 6
17
Assignment Operators
Don’t confuse = and ==! The compiler will warn “suggest parens”.
int x=5; int x=5;
if (x==6) /* false */ if (x=6) /* always true */
{ {
recommendation
/* ... */ /* x is now 6 */
} }
/* x is still 5 */ /* ... */ 18
Example-1
Add Two Numbers
Ask to the user to enter two numbers and place the addition of the
numbers in third variable of the same data type and print the third variable
#include <stdio.h>
#include <stdlib.h>
19
stdin, stdout, stderr
printf("%4.2f\n", 1.0);
printf("%4.3f\n", 12.232432442);
printf("%2.1f\n", 12.232432442);
printf("%8.3f\n", 123.22);
int num1=12345;
int num2=96;
printf("%8d\n",num1);
printf("%08d\n",num1);
printf("%-8d%d\n",num1,num2);
Formatted Output Example
12 1234567890 12345678901234567890
1 1.000000 1.000000000000000
2 1.414214 1.414213562373095
3 1.732051 1.732050807568877
4 2.000000 2.000000000000000
Output Produced on Screen
C command using printf () with
Position: 1 1 1 1 1 1 1 1 1 1 2
various conversion specifiers:
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
printf ("%3c",'A'); A X X X X X X X X X X X X X X X X X
printf ("%-3c",'A'); A X X X X X X X X X X X X X X X X X
printf ("%8s","ABCD"); A B C D X X X X X X X X X X X X
printf ("%d",52); 5 2 X X X X X X X X X X X X X X X X X X
printf ("%8d",52); 5 2 X X X X X X X X X X X X
printf ("%-8d",52); 5 2 X X X X X X X X X X X X
printf ("%f",123.456); 1 2 3 . 4 5 6 0 0 0 X X X X X X X X X X
printf ("%10f",123.456); 1 2 3 . 4 5 6 0 0 0 X X X X X X X X X X
printf ("%10.2f",123.456); 1 2 3 . 4 6 X X X X X X X X X X
printf ("%-10.2f",123.456); 1 2 3 . 4 6 X X X X X X X X X X
printf ("%.2f",123.456); 1 2 3 . 4 6 X X X X X X X X X X X X X X
printf ("%10.3f",-45.8); - 4 5 . 8 0 0 X X X X X X X X X X
printf ("%10f",0.00085); 0 . 0 0 0 8 5 0 X X X X X X X X X X
printf ("%10.2e",123.89); 1 . 2 4 e + 0 0 2 X X X X X X X X X X
Keyboard Input
• In C, keyboard input is accomplished using the scanf( ) function.
• The arguments are the addresses of the variables into which the input
is store.
scanf( ) format string
The scanf( ) format string usually contains
conversion specifications that tell scanf( ) how
to interpret the next “input field”. An input field is
a string of non-whitespace characters.
The format string usually contains
– Blanks or tabs which are ignored
– Ordinary characters which are expected to match the
next (non-whitespace) character input by the user
– Conversion specifications usually consisting
• % character indicating the beginning of the conversion
• An optional h, l (ell) or L
• A conversion character which indicates how the input field
is to be interpreted.
Common scanf( ) conversions
• %d -- a decimal (integer) number
• %u - an unsigned decimal (integer) number
• %x -- a hexadecimal number
– The matching argument is the address of an int
– May be preceded by h to indicate that the argument is the address of a short or
by l (ell) to indicate that the argument is the address of a long rather than an
int
• %f, %e -- a floating point number with optional sign, optional decimal
point, and optional exponent
– The matching argument is the address of a float
– May be preceded by l (ell) to indicate the argument is of the address of a
double rather than a float
• %s -- a word (a string delimited by white space, not an entire line)
– The matching argument is the address of a char or the name of a char array
– The caller must insure the array is large enough to for the input string and the
terminating \0 character
– More on this later
• %c - a single character
– The matching arguments is the address of a char
– Does not skip over white-space
– More on this later
scanf( ) examples
int age;
printf("How old are you? ");
scanf("%d", &age);
printf("OK, you are %d years old\n", age)
In this example,
• The format string “%d” is used, meaning that the program wants an integer
value from the keyboard.
• The value the user types will be stored in the age variable.
Here is possible output from the program when this code is executed (user input is in bold):
‘$’
‘4’
String Constants
A sequence characters surrounded by double
quotation marks.
Considered a single item.
Examples:
“UMBC”
“I like ice cream.”
“123”
“CAR”
“car”
Punctuation
Semicolons, colons, commas, apostrophes,
quotation marks, braces, brackets, and
parentheses.
; : , ‘ “ [ ] { } ( )
Keywords
Sometimes called reserved words.
Are defined as a part of the C language.
Can not be used for anything else!
Examples: Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Examples-1
return 0;
}