Qhov txawv ntawm printf thiab fprintf

Cov txheej txheem:

Qhov txawv ntawm printf thiab fprintf
Qhov txawv ntawm printf thiab fprintf

Video: Qhov txawv ntawm printf thiab fprintf

Video: Qhov txawv ntawm printf thiab fprintf
Video: Qhov txawv ntawm Zaj Tshoob and Txiv Xaiv 2024, Kaum ib hlis
Anonim

Key Difference – printf vs fprintf

Ib txoj haujlwm yog cov lus qhia los ua ib txoj haujlwm tshwj xeeb. Nws tsis tuaj yeem sau tag nrho cov nqe lus hauv tib txoj haujlwm. Yog li ntawd, qhov kev zov me nyuam tau muab faib ua ntau txoj haujlwm. Functions muab code reusability. Hauv cov lus programming xws li C lus, main() yog ib txoj haujlwm. Nws qhia txog qhov pib ntawm qhov kev ua tiav. Muaj built-in functions thiab cov neeg siv-txhais ua haujlwm. Tus programmer tsim cov haujlwm ua haujlwm raws li tus neeg siv. Cov lus muab cov haujlwm ua hauv. Tus programmer tuaj yeem siv lawv yam tsis tau siv txij thaum pib. Ob lub luag haujlwm tseem ceeb hauv C yog printf() thiab fprintf(). Kab lus no tham txog qhov sib txawv ntawm ob txoj haujlwm no. Qhov sib txawv tseem ceeb ntawm kev luam tawm thiab fprintf yog tias printf yog C muaj nuj nqi siv los luam cov kab ntawv tawm mus rau cov qauv tso zis tawm uas yog lub khoos phis tawj lub vijtsam, thaum fprintf yog C muaj nuj nqi los luam cov hlua formatted rau cov ntaub ntawv.

Printf yog dab tsi?

"printf" muaj nuj nqi yog siv los muab cov khoom tso tawm rau hauv txoj hauv kev formatted rau ib qho khoom siv xws li lub computer screen. Lub syntax ntawm printf muaj nuj nqi yog raws li nram no.

printf("txoj hlua hom", "cov npe ntawm cov hloov pauv");

Qhov txawv ntawm printf thiab fprintf
Qhov txawv ntawm printf thiab fprintf
Qhov txawv ntawm printf thiab fprintf
Qhov txawv ntawm printf thiab fprintf

Daim duab 01: printf()

Yog tus neeg siv tsis xav luam ib txoj hlua formatted, nws tuaj yeem luam cov hlua raws li nws yog.

eg. printf("Nyob zoo ntiaj teb");

Txoj kev los luam ib txoj hlua formatted yog raws li hauv qab no. Saib piv txwv hauv qab no. "a" thiab "b" yog cov lej, yog li lawv tau teev nrog %d.

int main(){

int a=10, b=20;

printf("Tus nqi ntawm ib qho yog %d thiab tus nqi ntawm b yog %d\n", a, b);

return 0;

}

Printing floating point tooj yog raws li hauv qab no. Saib cov piv txwv hauv qab no.

int main(){

float area=20.45;

printf("Area is %4.2f", thaj chaw);

return 0;

}

Printing cim muaj raws li hauv qab no.

int main(){

char letter='A';

printf("Lab ntawv yog %c", tsab ntawv);

return 0;

}

Cov hlua luam tawm raws li hauv qab no.

int main(){

char word[6]="nyob zoo";

printf("Lord is %s", lo lus);

return 0;

}

txoj hlua formatted kuj tseem muaj kev khiav tawm. Lawv pib nrog backslash ("\"). Ib txhia yog \n thiab \t.

int main(){

int a=10, b=20;

printf("tus nqi ntawm ib qho yog %d \n tus nqi ntawm b yog %d\n", a, b);

return 0;

}

Qhov no yuav luam "a" thiab "b" qhov tseem ceeb hauv cov kab sib cais.

printf("tus nqi ntawm ib qho yog %d \t tus nqi ntawm b yog %d\n", a, b); yuav muab ib qhov chaw los yog tab ntawm tus nqi ntawm ib tug thiab tus nqi ntawm b.

Tau sau ob nqe lus, tus programmer siv tau raws li hauv qab no.

printf("Kev Kawm \"C \" programming");

fprintf yog dab tsi?

Fprinf muaj nuj nqi yog siv los tso tawm ib txoj hlua formatted rau ib cov ntaub ntawv. Cov syntax rau fprintf yog raws li nram no;

fprintf(file pointer, "format specifier", "list of variables");

Saib cov cai hauv qab no kom nkag siab txog kev ua haujlwm ntawm fprintf ().

nrog

nrog

int main(){

FILE ptr;

char name[5]="Ann";

int id=3;

ptr=fopen(“file1.txt”, “w”);

if (ptr==NULL){

printf("Tsis tuaj yeem qhib cov ntaub ntawv\n");

}

lse{

fprintf(ptr, "%s, %d", npe, id);

printf("Cov ntaub ntawv tau sau tiav rau hauv cov ntaub ntawv");

fclose(ptr);

}

getch();

return 0;

}

"ptr" yog tus taw tes rau cov ntaub ntawv. Cov ntaub ntawv qhib rau hauv hom sau. Yog tias nws tsis qhib, nws yuav ua rau tsis tuaj yeem qhib cov ntaub ntawv yuam kev. Yog tias nws qhib tau zoo, txoj hlua formatted yog luam tawm rau cov ntaub ntawv. Cov ntaub ntawv pointer, txoj hlua formatted thiab cov npe sib txawv tau dhau mus rau fprintf muaj nuj nqi. Thaum kawg, cov ntaub ntawv raug kaw siv fclose(). Txhawm rau ntxiv cov ntaub ntawv rau hauv cov ntaub ntawv, cov nqe lus tuaj yeem hloov pauv raws li hauv qab no.

ptr=fopen(“file1.txt”, “a”);

Dab tsi yog qhov zoo sib xws ntawm printf thiab fprintf?

Ob leeg yog cov haujlwm muab los ntawm C hom lus

Qhov txawv ntawm printf thiab fprintf yog dab tsi?

printf vs fprintf

printf yog C muaj nuj nqi los luam cov kab ntawv formatted rau tus qauv tso zis kwj uas yog lub computer screen. fprintf yog C muaj nuj nqi los luam cov kab ntawv formatted rau ib cov ntaub ntawv.
Syntax
Formatted hlua thiab daim ntawv teev cov tsis tau dhau mus rau printf muaj nuj nqi. e.g. printf(“format”, args); File pointer, formatted hlua thiab daim ntawv teev cov tsis tau dhau mus rau fprintf muaj nuj nqi. e.g. fprintf(File ptr, “format”, args);

Summary – printf vs fprintf

"printf" thiab "fprintf" yog cov haujlwm hauv C. Programmer tsis tas yuav siv cov haujlwm no txij thaum pib. Cov lus C twb muab rau lawv. Qhov txawv ntawm printf thiab fprintf yog tias printf yog siv los luam ib txoj hlua formatted rau cov qauv tso zis uas yog feem ntau ntawm lub computer screen thiab fprintf yog siv los luam ib txoj hlua formatted rau ib cov ntaub ntawv tshwj xeeb. printf thiab fprintf tuaj yeem siv raws li txoj haujlwm.

Download the PDF Version of printf vs fprintf

Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline hom phiaj raws li daim ntawv ceeb toom. Thov download PDF version ntawm no Qhov txawv Ntawm printf thiab fprintf

Pom zoo: