Introduction To C Programming II
Introduction To C Programming II
Introduction to C Programming II
Learning Outcomes
At the end of this session, student will be able to:
• Define element and structure of C programming language (LO1
& LO2)
ch 65 123456
Range of value:
name -128 – 127
value
or:
int x, y, z;
or:
-128 -128 32 8 4 2 1
64 16
Total = -128 Total = -1
COMP6047 - Algorithm and Programming 9
Data Type
• Beside used in function identifier type as no return value,
keyword void also used as data type in variable.
• Void data type: is data type that can be transform into any data
type (will be discussed later in pointer)
casting
#define Pi 3.14
int main()
{
float PHI=3.14;
PHI = 3.1475; //OK (variable)
Pi=3.1475; //Error
return 0;
}
• Example :
sizeof(int) = 4 => Dev-V (Windows)
sizeof(int) = 2 => Turbo C ver 2.0 (DOS)
• Example :
– 3.14 (double)
– 3.14f (float)
– 3.14L (long double)
• Example :
– 174 (integer)
– 174u (unsigned integer)
– 174L (long integer)
– 174ul (unsigned long integer)
• How to deal with the issue? You may use casting or suffix
float x;
x = (float)3.14; // casting
x = 3.14f;// or suffix
int main()
{
printf(“Size of Floating Point Constant :\n");
printf(" – using suffix f = %d\n",sizeof(3.14f));
printf(" – without suffix = %d\n",sizeof(3.14));
printf(" – using suffix L = %d\n",sizeof(3.14L));
getch();
return 0;
}
Output:
Size of Floating Point Constant :
- using suffix f = 4
- without suffix = 8
- using suffix L = 12
COMP6047 - Algorithm and Programming 21
Output Operation
• To show data on the display screen/monitor. Some of standard
library function in C :
printf();
putchar();
puts();
etc.
• Syntax:
printf(const char *format[,argument, …]);
• Example 2
printf (“%10s”, “BINUS”); …..BINUS
printf (“%-10s”, “BINUS”); BINUS…..
printf (“%8.2f”, 3.14159 ); ….3.14
printf (“%-8.3f”, 3.14159 ); 3.141…
123456789012345678901234567890
Selamat Da di Binus
Selamat Datang di Binus
Selamat Datang di Binus
Selamat Datang di Binus
Selamat Datang di Binus
Selamat Datang di Binus
Selamat Da di Binus
Selamat Da di Binus
• Functionality:
– Displaying character on the monitor at cursor position. After
display, cursor will move to the next position
– Return EOF if error, and return the displayed character after
successfully done
– putchar is a macro similar to : putc(c, stdout )
– Header File : stdio.h
• Example :
char ch=’A’;
putchar(ch);
• Functionality :
– Display string to the monitor and move the cursor to new line
– Return non-negative value when successful and EOF if error
– Header file: stdio.h
• Example :
puts(”Welcome”);
puts(”to Binus”);
Output on monitor:
Welcome
to Binus
Answer:
x : 234
&x : 45678
• Example :
int x,y,z,w;
x = scanf("%d %d %d",&y,&z,&w);
#include <stdio.h>
int main()
{
int number; char initial; float money;
scanf(“%d %c %f” ,&number, &initial, &money);
//other statements
• Example :
– Using previous example, if a string “good
morning every one” entered then ss value will
only contain “good”
• Example 1:
char ss[40];
scanf(”%[^\n]”,ss);
• Functionality:
– Return the next ASCII character from keyboard buffer
– Shown on the monitor screen
– Awaiting for ENTER pressed
– Header file: stdio.h
• Example :
char ch;
ch = getchar();
• Functionality :
– read a string from keyboard till find new-line and save in buffer
– new-line will later on replace with null character
– will return NULL if error and return its argument (buffer) if success
• Example :
char buffer[40];
char *ptr;
ptr = gets(buffer);
2. char ss1[40];
char ss2[40];
x=scanf(”%s %s”,ss1,ss2);
c. What is ss1 and ss2, if the input from keyboard is ”Good morning
everyone” ?
d. What is x if the input : ”Class 1PAT” ?
4. char ch;
ch = getchar();
What is ch value, if the input : Binus ?
After entering name and student number, program will exit to prompt.
gender=getchar() seems to be never executed. Explain why?