I. Hàm printf
- Hàm printf là hàm dùng để xuất dữ liệu được định dạng
- Cú pháp:
printf("control string", argument list);
- Ví dụ:
+ printf("Xin chao");
+ int x;
printf("%d", x);
II. Hàm scanf
- Hàm scanf là hàm dùng để định dạng khi nhập dữ liệu.
- Cú pháp:
scanf("control string", argument list);
- Ví dụ:
+ int x;
printf("Nhap x:");
scanf("%d", x);
III. Bài tập:
Bài 1: Use the printf( ) statement and do the following
a) Print out the value of the integer variable sum
b) Print out the text string "Welcome", followed by a new line.
c) Print out the character variable letter
d) Print out the float variable discount
e) Print out the float variable dump using two decimal places
****************************************************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int sum=17;
printf("the value of the integer variable = %d",sum);
printf("\nWelcom\n");
printf("the character variable: letter");
float discount=1,51;
printf("\nthe float variable discount=%f",discount);
float dump=1,41;
printf("\nthe float variable dump using two decimal places=%0.2f",dump);
return 0;
}
****************************************************************************
Bài 2: Use the scanf( ) statement and do the following:
a) To read a decimal value from the keyboard, into the integer variable sum
b) To read a float variable into the variable discount_rate
****************************************************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
// phan a
float sum;
printf("\nNhap mot so thap phan:");
scanf("%f", &sum);
// phan b
float discount_rate;
printf("\nNhap mot so:");
scanf("%f", &discount_rate);
return 0;
}
****************************************************************************
Bài 3: Write a program which takes name, basic , daper ( ie, percentage of D.A), bonper (ie, percentage bonus) and loandet ( loan amount to be debited) for an employee. Calculate the salary using the following relation:
salary = basic + basic * daper /100 +bonper
* basic/100 - loandet
Data is :
name
|
basic
|
daper
|
bonper
|
loandet
|
MARK
|
2500
|
55
|
33.33
|
250.00
|
Calculate salary and then print the result under the following headings.
(Salary to be printed to the nearest dollar.)
Name Basic Salary
****************************************************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int basic=2500, daper=55;
float bonper=33.33, loandet=250.00;
float salary = basic+basic*daper/100+bonper*basic/100-loandet;
printf("name:Mark\n");
printf("basic=%d\n",basic);
printf("salary = %0.2f USD",salary);
return 0;
}
****************************************************************************
Bài 4: Write a program that asks for your first name and last name, and then prints the names in the format last name, first name.
****************************************************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char a[30];
char b[30];
printf("Your first name:");
scanf("%s", a);
printf("Your last name:");
scanf("%s", b);
printf("\nYour name is:%s %s", a,b);
return 0;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char a[30];
char b[30];
printf("Your first name:");
scanf("%s", a);
printf("Your last name:");
scanf("%s", b);
printf("\nYour name is:%s %s", a,b);
return 0;
}
****************************************************************************
Lưu ý: Ví dụ nhập:
Your fist name: Le
Your last name: Tuan Anh
thì sẽ xuất hiện lỗi, tên hiển thị ra chỉ là Your name is Le Tuan
Bởi vì lệnh scanf sẽ kết thúc khi nó gặp khoảng trắng, nên nó chỉ đọc được Tuan mà không đọc được Tuan Anh.
Để khắc phục chúng ta sẽ sử dụng hàm gets thay cho scanf:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char a[20];
char b[20];
printf("Your first name:");
scanf("%s", a); fflush(stdin);
printf("Your last name:");
gets(b);
printf("\nYour name is:%s %s", a,b);
return 0;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char a[20];
char b[20];
printf("Your first name:");
scanf("%s", a); fflush(stdin);
printf("Your last name:");
gets(b);
printf("\nYour name is:%s %s", a,b);
return 0;
}
như cái đb`
Trả lờiXóa