0% found this document useful (0 votes)
106 views17 pages

Chapter 1: A Tutorial Introduction Chapter 1: A Tutorial Introduction

This document summarizes the key steps in compiling and running a C program from source code to executable file. It introduces basic C programming concepts like data types, variables, arithmetic expressions, control flow statements like if-else and while loops, and input/output functions like printf, getchar, and putchar. It provides examples to demonstrate formatting output using format specifiers in printf, and reading/writing characters with getchar and putchar.

Uploaded by

swh601
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views17 pages

Chapter 1: A Tutorial Introduction Chapter 1: A Tutorial Introduction

This document summarizes the key steps in compiling and running a C program from source code to executable file. It introduces basic C programming concepts like data types, variables, arithmetic expressions, control flow statements like if-else and while loops, and input/output functions like printf, getchar, and putchar. It provides examples to demonstrate formatting output using format specifiers in printf, and reading/writing characters with getchar and putchar.

Uploaded by

swh601
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 1: A Tutorial Introduction

 E
Executable
t bl fil
file

Editor
So rce code: hello.c
Source helloc

#include <stdio.h>

Assembler
main()
{
Object file: hello.obj
printf(“Hello world₩n”);
Compiler
}

Linker Executable file: hello.exe


Variables and arithmetic expression
#include <stdio.h>
 Example: to print a table of
/* print Fahrenheit-Celsius table Fahrenheit temperatures and
for fahr =0, 20, ..., 300 */ its Celsius equivalent

main ()

C   F  32 
{
int fahr, celsius;  5 
int lower, upper, step;
9
lower=0;
upper=300;
step=20;
fahr=lower;
while(fahr
( <=upper)) {
celsius = 5*(fahr-32)/9;
printf("%d₩t%d₩n", fahr,celsius);
f h f h t
fahr=fahr+step;
}
}
Basic data type

 Integer: “int”
16-bit ints ( from -32768 to +32767), 32-bits ints
 Floating point: “float”
-38 38
Fractional part (32-bit ): 10 ~10
 Character –a single byte: “char”
 Short integer: “short”
 Long integer: “long”
 Double-precision floating point:”double”
Overflow
 G
Guess what
h values
l are printed
dbby ffollowing
ll program

#include <stdio.h>

main ()
{
int i;

I = 32767;

printf("%d
printf( %d %d %d ₩n
₩n", i,i+1,i+2);
i i+1 i+2);

}
#include <stdio
<stdio.h>
h>

/* print Fahrenheit-Celsius table


for fahr =0
=0, 20
20, ..., 300 */

void main (void)


{
float fahr, celsius;
int lower, upper, step;

lower=0;
upper=300;
step=20;
p
fahr=lower;
while(fahr <=upper) {
celsius = (5./9.)*(fahr-32.0);
printf("%3.0f %6.1f₩n", fahr,celsius);
fahr=fahr+step;
}
}
Format specifier in “printf”
printf

 %d print as decimal integer


 %6d print as decimal integer, at least characters wide
 %f print as floating point
 %6f print as floating point, at least 6 characters wide
 % 2f
%.2f print as floating point
point, 2 characters after decimal
point
 %6.2f print as floating point, at least 6 wide and 2 after
decimal point
Example 1

#include <stdio.h>

main()
{
printf( /%d/₩n , 336);
printf("/%d/₩n",
printf("/%2d/₩n",336);
printf("/%10d/₩n",336);
p
printf("/%-10d/₩n",336);
,
}
Example2
#include <stdio.h>

main()
{
printf("/%f/
i tf("/%f/ ₩n",1234.56);
₩ " 1234 56)
printf("/%e/ ₩n",1234.56);
printf("/%4.2f/ ₩n",1234.56);
printf("/%3
printf( /%3.1f/
1f/ ₩n
₩n",1234.56);
1234 56);
printf("/%10.3f/ ₩n",1234.56);
printf("/%10.3e/ ₩n",1234.56);
}
The For statement
#include <stdio.h>

/* print Fahrenheit-Celsius table*/

void main (void)


{
int fahr;

for (fahr=0;fahr<=300; fahr=fahr+20)


printf(“%3d %6.1f₩n”, fahr,(5.0/9.0)*(fahr-32));
}
 The choice between ‘while’ and ‘for’ is arbitrary, based on
which seems clearer. The ‘for’ is usually appropriate for loops
in which the initialization and increment are single statement
Symbolic Constants
 Syntex: #define name replacment next
#include <stdio.h>

#define LOWER 0 // lower limit of table


#define UPPER 300 // upper limit
#define STEP 20 // step size

/* print Fahrenheit-Celsius table */


main()
{
int fahr;

for(fahr=LOWER; fahr<=UPPER; fahr =fahr +STEP)


printf("%3d %6.1f₩n", fahr, (5./9.)*(fahr-32));
}
Character Input and Output
 New line character ‘\n’
 Simplest functions in the standard library
 “getchar() and putchar()”

 c=getchar() ;
 p
putchar(c);
( );
Exercise
 Write a code for
Read a character
While (character is not end-of-file indicator)
output the character just read
read character
First verson
#include <stdio.h>

//*copy
copy input to output: 1st version *//

main()
{
int c;
c=getchar();

while (c != EOF) {

putchar(c);
c=getchar();
}
}
Second version
#include <stdio.h>

//*copy
copy input to output: 1st version *//

main()
{
int c;

while ( (c=getchar()) != EOF) { *precedence

putchar(c); // c = getchar() !=
! EOF
c=getchar();
}
}
Character counting program
#include <stdio.h>

// count characters in input

main()
{
long nc;
nc=0;
while ( getchar()
() !=EOF))
++nc;
// line counting
printf(" %ld₩n", nc);
} // if(c==
if(c ‘/n’)
// ++nc
Word counting
#include <stdio.h>

#define IN 1 // inside a word


#define OUT 0 // outside a word

main()
{
int c,nl,nw,nc,state;

state=OUT;
nl=nw=nc=0;
while ( (c=getchar()) != EOF) {
++nc;
if ( c
c=='₩n')
₩n )
++nl;
if( c==' ' || c== '₩n' || c=='₩t')
state =OUT;
else if (state
( == OUT)) {
state =IN;
++nw;
}
}
printf("%d %d %d₩n", nl,nw,nc);
}
Home work
 E
Exercise
i 1 1-1
1 to
t 1-12
1 12

You might also like