Programming C: Posts 3 – Enter made in c
Content
In the previous post, we became acquainted with a number of programs that print to the screen some information. This article we will learn more about how to enter production in C.
1. Format string data
Prior to the introduction, data for the variables I will talk about some format to import and export. Here are the signs described format:
- %c : Single character
- %with : Chain
- %d : Integer Us 10 marked
- %f : Number of floating (CEO 5.54 will print out 5.540000)
- %and : Number of floating (with exponential notation)
- %g : Number of floating (CEO 5.54 printing will print out 5.54)
- %x : Hex unsigned integer (Contacts 16)
- %the : Unsigned octal integer (Contacts 8)
- the : The prefix used with% d, %x, %o to indicate long integer (eg% ld)
2. Export data using printf
We use the function printf to export the data to the console (from the print means print). Combined with the above format string, we consider a simple example follows.
Example 1:
#include <stdio.h> int main() { int a = 12; float b = 13.5; char c = 'Q'; long d = 3454; char* s = "nguyenvanquan7826"; // khai bao kieu chuoi printf("Vi du ve su dung lenh printf\n"); printf("Tong cua %d va %f la %f \n", a, b, a+b); printf("Tich cua %d va %ld la %ld \n", a, d, a*d); printf("Ky tu c la: %c \n", c); printf("Chuoi s la: %s \n", s); printf("Dinh dang so mu cua b la %e \n", b); printf("So he 16 va he 8 cua %d la %x va %o \n", a, a, a); printf("Ma ASCII cua %c la %d", c, c); return 0; }
Above I have used some control characters (\n – down the line) mentioned in the previous post, you can review if not Me. You run the program and draw your own comment.
His explanation 1 statement to clarify the publication of our.
The format string is placed in quotes: “”. Corresponding to each format is a variable of type respectively, if another type will result in errors.
You also note that for integers and characters have with each other via the ASCII code so we can print the character code in the format% d and also in character numeric code through the format% c. However, the nature of the variables do not change. In Vd on command in ASCII code of c will integer c nature but still is a variable of type char.
Example 2:
#include <stdio.h> int main() { int a = 12; float b = 13.5; char c = 'Q'; long d = 3454; char s[] = "nguyenvanquan7826"; // khai bao kieu chuoi printf("%6d %5.3f %.3f \n", a, b, a+b); printf("%-5d %5ld %5ld \n", a, d, a*d); printf("%5c \n", c); printf("%30s \n", s); return 0; }
You see this example we use the format% d, %f but insert in the middle of a% 6d, %5.3f. The meaning of it is as follows:
- %5c : Export character width 5
- %5d : Width integer 5
- %20with : Export chain width 20
- %5.3f : Export the actual width 5 including 3 number after comma
- %-5d : Width integer 5 but aligns the left
You run the program, view to see more results. TH if the breadth of our smaller size, the length of the number of stars? For example, we have some int a = 1234 but only in the% 2d, clear for 2 the space is less than a can 4 digits but the result is still in full. You can try.
Example 3:
Put the problem is assuming we have the variables shown any birth date (eg right = 12, thang = 8, male = 1992). Please print out the format is dd / mm / yyyy. Means that we have to print out the results 12/08/1992.
#include <stdio.h> int main() { int ngay = 12; int thang = 8; int nam = 1992; printf("KQ: %02d/%02d/%04d\n", ngay, thang, nam); return 0; }
Above, You notice we used %02d ie stars? Nhiá is the number printed will be 2 number, if the original number with less than 2 will add 0 front to enough 2 number. Example No. 8 will succeed 08. If you use% 05d will be 00008.
3. Enter data using scanf
In the previous example, we just declare variables and assign values to variables such as but not flexible because the program only works with the predetermined value, want to work with different data we have to revise the code, so it's not a good program. A software program is made to be for many different sets of values can be used.
Therefore, the data will be included in the software used is specific and simple data entry into the keyboard. We use the command scanf to import data from the keyboard (from scanning means scanned and the data we use to scan lệu from keyboard).
Example 1:
#include <stdio.h> int main() { int a; float b; printf("Nhap so nguyen a = "); scanf("%d", &a); printf("Nhap so thuc b = "); scanf("%f", &b); printf("a = %d \t b = %.3f", a, b); return 0; }
The use of command scanf combined with the string format for data entry will be similar to the data by the command printf.
Note Do not forget to character & before each variable entering. Without going wrong.
Example 2:
/* Tinh dien tich hinh chu nhat */ #include <stdio.h> int main() { int a, b; printf("Nhap chieu dai, chieu rong: \n"); scanf("%d%d", &a, &b); printf("Dien tich HCN: %d\n", a * b); return 0; }
In the example above, You see we can use 1 scanf command to enter data for several variables, each corresponding variable 1 certain format.
4. Enter the string in C
4.1 Error importing string scanf
If you use the scanf function to enter the string you see that the chain can not enter spaces before or if you do not enter then enter the string again. If you do not believe you can try to run the program after:
#include <stdio.h> int main() { int tuoi = 0; // khai bao chuoi co toi da 30 ky tu char ten[30], tenNguoiYeu[30]; printf("Ho va ten cua ban ten la gi? "); scanf("%s", ten); // nhap chuoi khong can dau & printf("Ban bao nhieu tuoi roi? "); scanf("%d", &tuoi); printf("Nguoi yeu cua ban ten la gi? "); scanf("%s", tenNguoiYeu); printf("\n====\n"); printf("Ten: %s \nTuoi:%d \nNY:%s \n", ten, tuoi, tenNguoiYeu); return 0; }
The result is that you will not enter the age and name of love, as shown below.
The reason is that only read data scanf no spaces (how to play, tabs, enter, ...) and the distance will be stored in the keyboard buffer so you only get the first string before the match only way (word Nguyen), after each space, the next value if consistent with the data type of the next turn, it will always assigned to them and you will not be entered again. Of your type of material should not receive, tenNguoiYeu will receive the next value in the value received is the word From.
4.2 The phenomenon of floating command
Phenomena such phenomenon is called drift command. If you now make to enter the string before and immediately after that this phenomenon also occurs because scanf just read the number in the correct format that is unreadable when you press the enter key when entered the number (enter characters or also can be considered a chain), it is stored in the buffer and when reading input values to string it finds in the buffer see characters enter the string so it is always assigned to the string.
To enter the string with spaces (spaces) we use the function gets.
To enter commands when not washed before and after the string we need to clear the keyboard buffer with the command fflush(stdin); immediately after entering the.
#include <stdio.h> int main() { int tuoi = 0; // khai bao chuoi co toi da 30 ky tu char ten[30], tenNguoiYeu[30]; printf("Ho va ten cua ban ten la gi? "); gets(ten); // nhap chuoi khong can dau & printf("Ban bao nhieu tuoi roi? "); scanf("%d", &tuoi); fflush(stdin); printf("Nguoi yeu cua ban ten la gi? "); gets(tenNguoiYeu); printf("\n====\n"); printf("Ten: %s \nTuoi:%d \nNY:%s \n", ten, tuoi, tenNguoiYeu); return 0; }
If you use Linux, the fflush(stdin); will not work, you can fix by entering 1 temporary sequence immediately after entering the. This temporary chain entry only to remove characters, excess string in buffer as follows (this way can also be used when you do on windows).
#include <stdio.h> int main() { int tuoi = 0; // khai bao chuoi co toi da 30 ky tu char ten[30], tenNguoiYeu[30], temp[255]; printf("Ho va ten cua ban ten la gi? "); gets(ten); // nhap chuoi khong can dau & printf("Ban bao nhieu tuoi roi? "); scanf("%d", &tuoi); //fflush(stdin); gets(temp); printf("Nguoi yeu cua ban ten la gi? "); gets(tenNguoiYeu); printf("\n====\n"); printf("Ten: %s \nTuoi:%d \nNY:%s \n", ten, tuoi, tenNguoiYeu); return 0; }
5. Explain a little bit about printf, scanf and stdio.h
5.1 Printf and scanf in the letter f
As you all know print mean in, scan was sweep or is called in to enter. So why do they have letters f back to form printf and scanf ?
Word f This means that format (Format). As you can see we are importing or exporting the values of the variables are formatted % Something eg% d is integer, %f are real numbers,… and letters f This is such meaningful.
5.2 Library stdio.h ?
In all the program ever, we always have #include <stdio.h>
, So what is it?
#include
ie meant to include our program will declare use something, but here is the use of the library stdio.h
So stdio.h what? std stands for standard , in stands for input, the stands for output, h stands for header (header – head, top – because it is declared in the beginning of the program). So it means standard input output – Import standard output. We understand it is a library service for the import and export of program standards. Enter the standard output is entered from the keyboard and output to the screen. There are also many sources as import from the file import and export, Import from cursor,… output to printer, output file,… but they considered keyboard and screen is a standard system of import and export.
Exercise
- Write a program in some any and print a value a2, the3, the4
- Write a program that reads from the keyboard 3 perform integer day, month, and output to the screen in the form of "dd / mm / yyyy".
- Read and write program 2 integer and print out the result of the (+), Subtraction (-), multiplication (*), division (/). Reviews results divided 2 integer.
- Write a program to the radius of the sphere, calculate and print area, volume of the sphere which.
Guide: S = 2 and V = ^ 4πR (4/3)πR^3.- Enter a number is the number of seconds, Change the number of seconds the minute and hour format manufactured under gio:phut:Shine, each component 2 number. Example 3661 = 01:01:01.
long integer size does need new prefix to come sir l, I try to run the program in such 2 then delete the letter l to% 5d% 5d but not faulty j, all 2 KQ program are the same, by the way his great post sir, very easy to understand for beginners like me j unknown! =)
When your data type is the dragon, the prefix in front l.
I also wonder dag
Why, when you run it again has stopped working CT Za?? T_T
#include
int main()
{
char Ten[30],Dia_chi[50],So_thich[100];
int right[31],ladder[12],male;
printf(“He ten “);
gets(Ten);
printf(“Dia who “);
gets(Dia_chi);
printf(“date of birth “);scanf(“%d”,&right);
printf(“elevator birth “);scanf(“%d”,&ladder);
printf(“Male birth “);scanf(“%d”,&male);
fflush(stdin);
printf(“like “);gets(So_thich);
printf(“\a ============== n n”);
printf(“Ten: %s these gods: %s neven birth:%d t% d t% d t NSO thich: %with”,Ten,Dia_chi,So_thich,right,ladder,male);
return 0;
}
I fix the DC then it stopped working but the reason he explained to me sir households @@, Here is the source file you just fix again:
#include
int main()
{
char Ten[30],Dia_chi[50],So_thich[100];
int right,ladder,male;
printf(“He ten “);
gets(Ten);
printf(“Dia who “);
gets(Dia_chi);
printf(“date of birth “);scanf(“%d”,&right);
printf(“elevator birth “);scanf(“%d”,&ladder);
printf(“Male birth “);scanf(“%d”,&male);
fflush(stdin);
printf(“like “);gets(So_thich);
printf(“\a ============== n n”);
printf(“Ten: %s these gods: %sn”,Ten,Dia_chi);
printf(“Date of birth:%d/%d/%dn”,right,ladder,male);
printf(“like: %with”,So_thich);
return 0;
}
He said in a comment before I got you. Do you declare array severance.
Because you declare variables and variable date month incorrect. date is the number, I immediately declare[31] mean 1 array 31 Like the name of[30] was 1 Plate 30 characters (merge them into chains). Immediately changed to int, ladder, male. That is
darling a can for facebook to e e please ask some things not you. You are students that are learning about language section C. Due to communication problems at school so much e still do not know. Hope he can only add sir teachers. Thank you
You can send your mail too: nguyenvanquan7826@gmail.com nhé.
Can you tell me please post some code section 5 OK?
#include
#include
#include
//———————————————————————-
int main() {
float so;
printf(“\n n tNhap 1 so: “); scanf(“%f”, &so); fflush(stdin);
printf(“\n Mean phuong than just entered was:\tt%7.2f”, pow(so, 2));
printf(“\n Set an phuong than just entered was:\tt%7.2f”, pow(so, 3));
printf(“\nnnnnnnnnnn”);
return 0;
}
//———————————————————————
int main() {
int right, ladder, male;
printf(“Click the right, Month and year: \n”); scanf(“%d% d% d”, &right, &ladder, &male);
system(“cls”);
printf(“Right, Month and year (dd/mm/yyyy) just entered as% 02d /% 02d /% 4d”, right, ladder, male);
printf(“\nnnnnnnnnnn”);
return 0;
}
//———————————————————————
int main() {
int a, b;
printf(“\n n tNhap 2 so nguyen a, b: “); scanf(“%d%d”, &the, &b); fflush(stdin);
system(“cls”);
printf(“\n n Tvoi know to co gia tri the% d, compared b has a value of% d exam:”, the,b);
printf(“\n n take + b =% d n n would – b =% d n n would * b =% d n n would / b = %d”, the + b, the – b, the * b, the / b);
if (the % b == 0) {
printf(“\n n TSO a divisible by b”);
}
else {
printf(“\n n TSO a not divisible than b, with the% d / %d% d Duoc, you% d”, the, b, the / b, the % b);
printf(“\nnnnnnnnnnn”);
}
return 0;
}
//———————————————————————
int main() {
float r;
printf(“\n n tNhap radius r: “); scanf(“%f”, &r); fflush(stdin);
system(“cls”);
printf(“\n n tVoi radius r =% .2f, area of the sphere S la: %.2f”, r, 4 * 3.14 * pow(r, 2));
printf(“, the volume V of the sphere la: %.2f”, 4 * 3.14 * pow(r, 3) / 3);
printf(“\nnnnnnnnnnn”);
return 0;
}
//———————————————————————
int main() {
int Shine;
printf(“\n n tNhap on total number of seconds: “); scanf(“%d”, &Shine); fflush(stdin);
system(“cls”);
printf(“\n n tVoi Total% d seconds for, It is formatted according to hh:mm:ss la: %02d:%02d:%02d”, Shine, Shine / 3600, (Shine % 3600) / 60, Shine % 60);
printf(“\nnnnnnnnnnn”);
return 0;
}
Note:
– Was ordered to spend the screen erase system(“cls”), in addition you need to add #include #include .
– Similarly, to spend pow function for calculating the power of some, you have #include to nhé. CEO: to exponentiation 4 a figure that represented a turning, then we knock pow(the, 4).
– To understand more deeply, you ask Mr. Quan nhé.
You go online code.cachhoc.net dot code to submit code nhé. will know you made the right two false.
#include
int main()
{
int a,hh,pp,ss;
a=3666;
hh = a / 3600;
pp=(a600)/60;
ss=(a600)%60;
printf(“0%d:0%d:0%d”,hh, pp, ss);
return 0;
}
There are no ad video tutorials?
I ask you in the example 2 the area of the rectangle when the length enter the width value in that match is an area long string of numbers that sir
Can import or export your work to be false, it prints out a very large number. You can visit https://chamcode.net to test and do light exercises.
Can not convert a char value * in VD 1 (2. Export data using printf).
In the data say about the% d% f% c so I asked her to find these formats for different data types (eg : double, long int, unsigned char, …) Where sir ?
You can find more on google nhé. Or consult this link: https://codeforwin.org/2015/05/list-of-all-format-specifiers-in-c-programming.html
when entering their length or width age, they need to measure conditions of greater 0 but sir..
Right, but here you do not focus on it much.
I ask you in the example 1 char * vs different char sir ntn?
char is a character type, tank * the string or character pointer.
=(( no way for these young people did not know about C, but can not you code is. You do each exercise 2, Far from the other post I do not know, saw some sure you want to calculate a hat 2,3,4 must use that in all em pow pow cha see where. If only he knew about it some new do , also you can not do =(( very sad
a^2 = a*a, a^3 = a*a*a. how to use pow ?
thank you, New Kids learn by grade 9 should…. =))
#include
int main()
{
int Thu, phut, Shine;
printf(“nhap so gio now:\nNhap so phut now:\nNhap so giay now:\n “);
scanf(“%d% d% d”, &gio, &phut, &Shine);
printf(“KQ: %dgio %dphut %dgiay”, gio, phut, Shine);
return 0;
}
You can visit https://chamcode.net to submit nhé.
Please code 5 see
So do it yourself.
// time to family room:phut:Shine
#include
int main()
{
int a;
printf(“Enter the number of rules:\n”);
printf(“Compare shoes: “);
scanf(“%d”, &the);
int b = a-(a/3600)*3600;
int c = b-(b/60)*60;
printf(“\r ========= End result ========= n”);
printf(“Time after decision: %02d:%02d:%02d”, a/3600, b/60, c);
return 0;
}
You can come in https://chamcode.net to submit nhé, have dots system.
Teacher how to calculate the root of 1 number so teacher
using sqrt(x) in the math.h library.
If you want to get a real number sequence from the labview about the microcontroller then the CCS program should be used: scanf , fgets , gets . IF????/
I don't know this because I don't do microcontroller.
A dear to e asked, I just learned c only if in the lesson 3 and chia 2 the integer then e splits into a new variable in the real type,I use cthuc Thuong = a / b; is it ok?? And if I do, it makes any difference if I add the library function? Because 2 results i see k different? Hope you help
When divided 2 Integers for each other to get the result of a real number need to be pressed. You see Posts 4 about math, operator to know more.
very nice
Class time again
#include
int main()
{
int h,m,with,the;
printf(“Enter the shoe number n”);
scanf(“%d”, &the);
h=a/3600;
m=(a-h*3600)/60;
s=a-(m*60+h*3600);
printf(“%02d:%02d:%02d”,h,m,with);
return 0;
}