logo
💻 编程语言

C

C Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 编程语言🧭 Markdown 速查🏷️ 2 个标签
#c#systems
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

hello.c
C
滚动查看更多
#include <stdio.h>

int main(void) {
  printf("Hello World!\n");

  return 0;
}

Compile hello.c file with gcc

BASH
滚动查看更多
$ gcc -Wall -g hello.c -o hello

Run the compiled binary hello

BASH
滚动查看更多
$ ./hello

Output => Hello World!

Variables
C
滚动查看更多
int myNum = 15;

int myNum2; // do not assign, then assign
myNum2 = 15;

int myNum3 = 15; // myNum3 is 15
myNum3 = 10;     // myNum3 is now 10

float myFloat = 5.99; // floating point number
char myLetter = 'D';  // character

int x = 5;
int y = 6;
int sum = x + y; // add variables to sum

// declare multiple variables
int a = 5, b = 6, c = 50;
Constants
C
滚动查看更多
const int minutesPerHour = 60;
const float PI = 3.14;

Best Practices

C
滚动查看更多
const int BIRTHYEAR = 1980;
Comment
C
滚动查看更多
// this is a comment
printf("Hello World!\n"); // Can comment anywhere in file

/*Multi-line comment, print Hello World!
to the screen, it's awesome */
Print text
C
滚动查看更多
printf("I am learning C.\n");
int testInteger = 5;
printf("Number = %d\n", testInteger);

float f = 5.99; // floating point number
printf("Value = %f\n", f);

short a = 0b1010110; // binary number
int b = 02713; // octal number
long c = 0X1DAB83; // hexadecimal number

// output in octal form
printf("a=%ho, b=%o, c=%lo\n", a, b, c);
// output => a=126, b=2713, c=7325603

// Output in decimal form
printf("a=%hd, b=%d, c=%ld\n", a, b, c);
// output => a=86, b=1483, c=1944451

// output in hexadecimal form (letter lowercase)
printf("a=%hx, b=%x, c=%lx\n", a, b, c);
// output => a=56, b=5cb, c=1dab83

// Output in hexadecimal (capital letters)
printf("a=%hX, b=%X, c=%lX\n", a, b, c);
// output => a=56, b=5CB, c=1DAB83
Control the number of spaces
C
滚动查看更多
int a1 = 20, a2 = 345, a3 = 700;
int b1 = 56720, b2 = 9999, b3 = 20098;
int c1 = 233, c2 = 205, c3 = 1;
int d1 = 34, d2 = 0, d3 = 23;

printf("%-9d %-9d %-9d\n", a1, a2, a3);
printf("%-9d %-9d %-9d\n", b1, b2, b3);
printf("%-9d %-9d %-9d\n", c1, c2, c3);
printf("%-9d %-9d %-9d\n", d1, d2, d3);

output result

BASH
滚动查看更多
20        345       700
56720     9999      20098
233       205       1
34        0         23

In %-9d, d means to output in 10 base, 9 means to occupy at least 9 characters width, and the width is not enough to fill with spaces, - means left alignment

Strings
C
滚动查看更多
char greetings[] = "Hello World!";
printf("%s", greetings);

Access string

C
滚动查看更多
char greetings[] = "Hello World!";
printf("%c", greetings[0]);

Modify string

C
滚动查看更多
char greetings[] = "Hello World!";
greetings[0] = 'J';

printf("%s", greetings);
// prints "Jello World!"

Another way to create a string

C
滚动查看更多
char greetings[] = {'H','e','l','l','\0'};

printf("%s", greetings);
// print "Hell!"

Creating String using character pointer (String Literals)

C
滚动查看更多
char *greetings = "Hello";
printf("%s", greetings);
// print "Hello!"

NOTE: String literals might be stored in read-only section of memory. Modifying a string literal invokes undefined behavior. You can't modify it!

C does not have a String type, use char type and create an array of characters

Condition
C
滚动查看更多
int time = 20;
if (time < 18) {
  printf("Goodbye!\n");
} else {
  printf("Good evening!\n");
}
// Output -> "Good evening!"
int time = 22;
if (time < 10) {
  printf("Good morning!\n");
} else if (time < 20) {
  printf("Goodbye!\n");
} else {
  printf("Good evening!\n");
}
// Output -> "Good evening!"
Ternary operator
C
滚动查看更多
int age = 20;
(age > 19) ? printf("Adult\n") : printf("Teenager\n");
Switch
C
滚动查看更多
int day = 4;

switch (day) {
  case 3: printf("Wednesday\n"); break;
  case 4: printf("Thursday\n"); break;
  default:
    printf("Weekend!\n");
}
// output -> "Thursday" (day 4)
While Loop
C
滚动查看更多
int i = 0;

while (i < 5) {
  printf("%d\n", i);
  i++;
}

NOTE: Don't forget to increment the variable used in the condition, otherwise the loop will never end and become an "infinite loop"!

Do/While Loop
C
滚动查看更多
int i = 0;

do {
  printf("%d\n", i);
  i++;
} while (i < 5);
For Loop
C
滚动查看更多
for (int i = 0; i < 5; i++) {
  printf("%d\n", i);
}
Break out of the loop Break/Continue
C
滚动查看更多
for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  printf("%d\n", i);
}

Break out of the loop when i is equal to 4

C
滚动查看更多
for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  printf("%d\n", i);
}

Example to skip the value of 4

While Break Example
C
滚动查看更多
int i = 0;

while (i < 10) {
  if (i == 4) {
    break;
  }
  printf("%d\n", i);

  i++;
}
While continue example
C
滚动查看更多
int i = 0;

while (i < 10) {
  i++;

  if (i == 4) {
    continue;
  }
  printf("%d\n", i);
}
Arrays
C
滚动查看更多
int myNumbers[] = {25, 50, 75, 100};

printf("%d", myNumbers[0]);
// output 25

Change array elements

C
滚动查看更多
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

Loop through the array

C
滚动查看更多
int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf("%d\n", myNumbers[i]);
}

Set array size

C
滚动查看更多
// Declare an array of four integers:
int myNumbers[4];

// add element
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
Enumeration Enum
C
滚动查看更多
enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun };

Define enum variable

C
滚动查看更多
enum week a, b, c;
enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a, b, c;

With an enumeration variable, you can assign the value in the list to it

C
滚动查看更多
enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun };
enum week a = Mon, b = Wed, c = Sat;
// or
enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a = Mon, b = Wed, c = Sat;
Enumerate sample applications
C
滚动查看更多
enum week {Mon = 1, Tues, Wed, Thurs} day;

scanf("%d", &day);

switch(day) {
  case Mon: puts("Monday"); break;
  case Tues: puts("Tuesday"); break;
  case Wed: puts("Wednesday"); break;
  case Thurs: puts("Thursday"); break;
  default: puts("Error!");
}
User input
C
滚动查看更多
// Create an integer variable to store the number we got from the user
int myNum;

// Ask the user to enter a number
printf("Enter a number: ");

// Get and save the number entered by the user
scanf("%d", &myNum);

// Output the number entered by the user
printf("The number you entered: %d\n", myNum);
User input string
C
滚动查看更多
// create a string
char firstName[30];
// Ask the user to enter some text
printf("Enter your name: ");
// get and save the text
scanf("%s", &firstName);
// output text
printf("Hello %s.\n", firstName);
memory address

When a variable is created, it is assigned a memory address

C
滚动查看更多
int myAge = 43;

printf("%p", &myAge);
// Output: 0x7ffe5367e044

To access it, use the reference operator (&)

create pointer
C
滚动查看更多
int myAge = 43; // an int variable
printf("%d\n", myAge); // output the value of myAge(43)

// Output the memory address of myAge (0x7ffe5367e044)
printf("%p\n", &myAge);
pointer variable
C
滚动查看更多
int myAge = 43; // an int variable
int*ptr = &myAge; // pointer variable named ptr, used to store the address of myAge

printf("%d\n", myAge); // print the value of myAge (43)

printf("%p\n", &myAge); // output the memory address of myAge (0x7ffe5367e044)
printf("%p\n", ptr); // use the pointer (0x7ffe5367e044) to output the memory address of myAge
Dereference
C
滚动查看更多
int myAge = 43; // variable declaration
int*ptr = &myAge; // pointer declaration

// Reference: output myAge with a pointer
// memory address (0x7ffe5367e044)
printf("%p\n", ptr);
// dereference: output the value of myAge with a pointer (43)
printf("%d\n", *ptr);

Operators

Arithmetic Operators
C
滚动查看更多
int myNum = 100 + 50;
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)

OperatorNameExample
+Addx + y
-Subtractx - y
*Multiplyx * y
/Dividex / y
%Modulox % y
++Increment++x
--Decrement--x
Assignment operator
ExampleAs
x = 5x = 5
x += 3x = x + 3
x -= 3x = x - 3
x *= 3x = x * 3
x /= 3x = x / 3
x %= 3x = x % 3
x &= 3x = x & 3
x <code>|=</code> 3x = x <code>|</code> 3
x ^= 3x = x ^ 3
x >>= 3x = x >> 3
x <<= 3x = x << 3
Comparison Operators
C
滚动查看更多
int x = 5;
int y = 3;

printf("%d", x > y);
// returns 1 (true) because 5 is greater than 3

SymbolNameExample
==equalsx == y
!=not equal tox != y
>greater thanx > y
<less thanx < y
>=greater than or equal tox >= y
<=less than or equal tox <= y

Comparison operators are used to compare two values

Logical Operators
SymbolNameDescriptionExample
&&and logicalreturns true if both statements are truex < 5 && x < 10
<code>||</code>or logicalreturns true if one of the statements is true<code>x < 5 || x < 4</code>
!not logicalInvert result, return false if true!(x < 5 && x < 10)

{.show-header}

Operator Examples
C
滚动查看更多
unsigned int a = 60; /*60 = 0011 1100 */
unsigned int b = 13; /*13 = 0000 1101 */
int c = 0;

c = a & b; /*12 = 0000 1100 */
printf("Line 1 -the value of c is %d\n", c);

c = a | b; /*61 = 0011 1101 */
printf("Line 2 -the value of c is %d\n", c);
c = a ^ b; /*49 = 0011 0001 */
printf("Line 3 -the value of c is %d\n", c);
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 -The value of c is %d\n", c);
c = a << 2; /*240 = 1111 0000 */
printf("Line 5 -the value of c is %d\n", c);
c = a >> 2; /*15 = 0000 1111 */
printf("Line 6 -The value of c is %d\n", c);
Bitwise operators
OperatorDescriptionInstance
&Bitwise AND operation, "AND" operation by binary digits(A & B) will get 12 which is 0000 1100
<code>|</code>Bitwise OR operator, "or" operation by binary digit<code>(A | B)</code> will get61 which is 0011 1101
^XOR operator, perform "XOR" operation by binary digits(A ^ B) will get 49 which is 0011 0001
~Inversion operator, perform "inversion" operation by binary bit(~A) will get -61 which is 1100 0011
<<binary left shift operatorA << 2 will get 240 which is 1111 0000
>>binary right shift operatorA >> 2 will get 15 which is 0000 1111

{.show-header}

Data Types

Basic data types
Data TypeSizeRangeDescription
char1 byte−128 ~ 127single character/alphanumeric/ASCII
signed char1 byte−128 ~ 127
unsigned char1 byte0 ~ 255
int2 to 4 bytes−32,768 ~ 32,767store integers
signed int2 bytes−32,768 ~ 32,767
unsigned int2 bytes0 ~ 65,535
short int2 bytes−32,768 ~ 32,767
signed short int2 bytes−32,768 ~ 32,767
unsigned short int2 bytes0 ~ 65,535
long int4 bytes-2,147,483,648 ~ 2,147,483,647
signed long int4 bytes-2,147,483,648 ~ 2,147,483,647
unsigned long int4 bytes0 ~ 4,294,967,295
float4 bytes3.4E-38 ~ 3.4E+38
double8 bytes1.7E-308 ~ 1.7E+308
long double10 bytes3.4E-4932 ~ 1.1E+4932

{.show-header}

Data types
C
滚动查看更多
// create variables
int myNum = 5; // integer
float myFloatNum = 5.99; // floating point number
char myLetter = 'D'; // string
// High precision floating point data or numbers
double myDouble = 3.2325467;
// print output variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
printf("%lf\n", myDouble);

Data TypeDescription
charcharacter type
shortshort integer
intinteger type
longlong integer
floatsingle-precision floating-point type
doubledouble-precision floating-point type
voidno type
Basic format specifiers
Format SpecifierData Type
%d or %iint integer
%ffloat single-precision decimal type
%lfdouble high precision floating point data or number
%cchar character
%sfor strings strings

{.show-header}

Separate base format specifiers
FormatShortIntLong
Octal%ho%o%lo
Decimal%hd%d%ld
Hexadecimal%hx / %hX%x / %X%lx / %lX

{.show-header}

Data format example
C
滚动查看更多
int myNum = 5;
float myFloatNum = 5.99; // floating point number
char myLetter = 'D';     // string
// print output variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);

C Preprocessor

Preprocessor Directives
DirectiveDescription
#definedefine a macro
#includeinclude a source code file
#undefundefined macro
#ifdefReturns true if the macro is defined
#ifndefReturns true if the macro is not defined
#ifCompile the following code if the given condition is true
#elseAlternative to #if
#elifIf the #if condition is false, the current condition is true
#endifEnd a #if...#else conditional compilation block
#errorPrint an error message when standard error is encountered
#pragmaIssue special commands to the compiler using the standardized method

{.show-header}

C
滚动查看更多
// replace all MAX_ARRAY_LENGTH with 20
#define MAX_ARRAY_LENGTH 20
// Get stdio.h from the system library
#include <stdio.h>
// Get myheader.h in the local directory
#include "myheader.h"
#undef FILE_SIZE
#define FILE_SIZE 42 // undefine and define to 42
Predefined macros
MacroDescription
__DATE__The current date, a character constant in the format "MMM DD YYYY"
__TIME__The current time, a character constant in the format "HH:MM:SS"
__FILE__This will contain the current filename, a string constant
__LINE__This will contain the current line number, a decimal constant
__STDC__Defined as 1 when the compiler compiles against the ANSI standard

{.show-header}

ANSI C defines a number of macros that you can use, but you cannot directly modify these predefined macros

Predefined macro example

C
滚动查看更多
#include <stdio.h>

int main(void) {
  printf("File: %s\n", __FILE__);
  printf("Date: %s\n", __DATE__);
  printf("Time: %s\n", __TIME__);
  printf("Line: %d\n", __LINE__);
  printf("ANSI: %d\n", __STDC__);
}
Macro continuation operator (\\)

A macro is usually written on a single line.

C
滚动查看更多
#define message_for(a, b) \
    printf(#a " and " #b ": We love you!\n")

If the macro is too long to fit on a single line, use the macro continuation operator \

String Constantization Operator (#)
C
滚动查看更多
#include <stdio.h>

#define message_for(a, b) \
  printf(#a " and " #b ": We love you!\n")

int main(void) {
  message_for(Carole, Debra);

  return 0;
}

When the above code is compiled and executed, it produces the following result:

CODE
滚动查看更多
Carole and Debra: We love you!

When you need to convert a macro parameter to a string constant, use the string constant operator #

tag paste operator (##)
C
滚动查看更多
#include <stdio.h>

#define tokenpaster(n) printf ("Token " #n " = %d\n", token##n)

int main(void) {
  int token34 = 40;
  tokenpaster(34);

  return 0;
}
defined() operator
C
滚动查看更多
#include <stdio.h>

#if !defined (MESSAGE)
   #define MESSAGE "You wish!"
#endif

int main(void) {
  printf("Here is the message: %s\n", MESSAGE);

  return 0;
}
Parameterized macros
C
滚动查看更多
int square(int x) {
  return x * x;
}

The macro rewrites the above code as follows:

C
滚动查看更多
#define square(x) ( (x) * (x) )

No spaces are allowed between the macro name and the opening parenthesis

C
滚动查看更多
#include <stdio.h>
#define MAX(x,y) ( (x) > (y) ? (x) : (y) )

int main(void) {
  printf("Max between 20 and 10 is %d\n", MAX(10, 20));

  return 0;
}

C Function

Function declaration and definition
C
滚动查看更多
int main(void) {
  printf("Hello World!\n");

  return 0;
}

The function consists of two parts

C
滚动查看更多
void myFunction() { // declaration declaration
  // function body (code to be executed) (definition)
}

  • Declaration declares the function name, return type and parameters (if any)
  • Definition function body (code to execute)

C
滚动查看更多
// function declaration
void myFunction();
// main method
int main() {
  myFunction(); // --> call the function

  return 0;
}

void myFunction() {// Function definition
  printf("Good evening!\n");
}
Call function
C
滚动查看更多
// create function
void myFunction() {
  printf("Good evening!\n");
}

int main() {
  myFunction(); // call the function
  myFunction(); // can be called multiple times

  return 0;
}
// Output -> "Good evening!"
// Output -> "Good evening!"
Function parameters
C
滚动查看更多
void myFunction(char name[]) {
  printf("Hello %s\n", name);
}

int main() {
  myFunction("Liam");
  myFunction("Jenny");

  return 0;
}
// Hello Liam
// Hello Jenny
Multiple parameters
C
滚动查看更多
void myFunction(char name[], int age) {
  printf("Hi %s, you are %d years old.\n",name,age);
}
int main() {
  myFunction("Liam", 3);
  myFunction("Jenny", 14);

  return 0;
}
// Hi Liam you are 3 years old.
// Hi Jenny you are 14 years old.
Return value
C
滚动查看更多
int myFunction(int x) {
  return 5 + x;
}

int main() {
  printf("Result: %d\n", myFunction(3));
  return 0;
}
// output 8 (5 + 3)

Two parameters

C
滚动查看更多
int myFunction(int x, int y) {
  return x + y;
}

int main() {
  printf("Result: %d\n", myFunction(5, 3));
  // store the result in a variable
  int result = myFunction(5, 3);
  printf("Result = %d\n", result);

  return 0;
}
// result: 8 (5 + 3)
// result = 8 (5 + 3)
Recursive example
C
滚动查看更多
int sum(int k);

int main() {
  int result = sum(10);
  printf("%d\n", result);

  return 0;
}

int sum(int k) {
  if (k > 0) {
    return k + sum(k -1);
  } else {
    return 0;
  }
}
Mathematical functions
C
滚动查看更多
#include <math.h>

void main(void) {
  printf("%f\n", sqrt(16)); // square root
  printf("%f\n", ceil(1.4)); // round up (round)
  printf("%f\n", floor(1.4)); // round down (round)
  printf("%f\n", pow(4, 3)); // x(4) to the power of y(3)
}

  • abs(x) absolute value
  • acos(x) arc cosine value
  • asin(x) arc sine
  • atan(x) arc tangent
  • cbrt(x) cube root
  • cos(x) cosine
  • the value of exp(x) Ex
  • sin(x) the sine of x
  • tangent of tan(x) angle

C Structures

Create structure
C
滚动查看更多
struct MyStructure { // structure declaration
  int myNum; // member (int variable)
  char myLetter; // member (char variable)
}; // end the structure with a semicolon

Create a struct variable called s1

C{7}
滚动查看更多
struct myStructure {
  int myNum;
  char myLetter;
};

int main() {
  struct myStructure s1;

  return 0;
}
Strings in the structure
C{9}
滚动查看更多
struct myStructure {
  int myNum;
  char myLetter;
  char myString[30]; // String
};

int main() {
  struct myStructure s1;
  strcpy(s1. myString, "Some text");
  // print value
  printf("My string: %s\n", s1.myString);

  return 0;
}

Assigning values to strings using the strcpy function

Accessing structure members
C{11,12,16}
滚动查看更多
// create a structure called myStructure
struct myStructure {
  int myNum;
  char myLetter;
};

int main() {
  // Create a structure variable called myStructure called s1
  struct myStructure s1;
  // Assign values ​​to the members of s1
  s1.myNum = 13;
  s1.myLetter = 'B';

  // Create a structure variable of myStructure called s2
  // and assign it a value
  struct myStructure s2 = {13, 'B'};
  // print value
  printf("My number: %d\n", s1.myNum);
  printf("My letter: %c\n", s1.myLetter);

  return 0;
}

Create different structure variables

C
滚动查看更多
struct myStructure s1;
struct myStructure s2;
// Assign values ​​to different structure variables
s1.myNum = 13;
s1.myLetter = 'B';

s2.myNum = 20;
s2.myLetter = 'C';
Copy structure
C{6}
滚动查看更多
struct myStructure s1 = {
  13, 'B', "Some text"
};

struct myStructure s2;
s2 = s1;

In the example, the value of s1 is copied to s2

Modify value
C{6,7}
滚动查看更多
// Create a struct variable and assign it a value
struct myStructure s1 = {
  13, 'B'
};
// modify the value
s1.myNum = 30;
s1.myLetter = 'C';
// print value
printf("%d %c",
    s1.myNum,
    s1.myLetter);

File Processing

File processing function
FunctionDescription
fopen()open a new or existing file
fprintf()write data to file
fscanf()read data from a file
fputc()write a character to file
fgetc()read a character from a file
fclose()close the file
fseek()set the file pointer to the given position
fputw()Write an integer to a file
fgetw()read an integer from a file
ftell()returns the current position
rewind()set the file pointer to the beginning of the file

{.show-header}

There are many functions in the C library to open/read/write/search and close files

Open mode parameter
ModeDescription
rOpen a text file in read mode, allowing the file to be read
wOpen a text file in write mode, allowing writing to the file
aOpen a text file in append mode<br /><small>If the file does not exist, a new one will be created</small>
r+Open a text file in read-write mode, allowing reading and writing of the file
w+Open a text file in read-write mode, allowing reading and writing of the file
a+Open a text file in read-write mode, allowing reading and writing of the file
rbOpen a binary file in read mode
wbOpen binary file in write mode
abOpen a binary file in append mode
rb+open binary file in read-write mode
wb+Open binary file in read-write mode
ab+open binary file in read-write mode

{.show-header}

Open the file: fopen()
C{6}
滚动查看更多
#include <stdio.h>

void main() {
  FILE *fp;
  char ch;

  fp = fopen("file_handle.c", "r");

  while (1) {
    ch = fgetc(fp);
    if (ch == EOF)
      break;
    printf("%c", ch);
  }
  fclose(fp);
}

After performing all operations on the file, the file must be closed with fclose()

Write to file: fprintf()
C{7}
滚动查看更多
#include <stdio.h>

void main() {
  FILE *fp;
  fp = fopen("file.txt", "w"); // open the file

  // write data to file
  fprintf(fp, "Hello file for fprintf..\n");
  fclose(fp); // close the file
}
Read the file: fscanf()
C{6}
滚动查看更多
#include <stdio.h>

void main() {
  FILE *fp;

  char buff[255]; // Create a char array to store file data
  fp = fopen("file.txt", "r");

  while(fscanf(fp, "%s", buff) != EOF) {
    printf("%s ", buff);
  }
  fclose(fp);
}
Write to file: fputc()
C{6}
滚动查看更多
#include <stdio.h>

void main() {
  FILE *fp;
  fp = fopen("file1.txt", "w"); // open the file
  fputc('a',fp); // write a single character to the file
  fclose(fp); // close the file
}
Read the file: fgetc()
C{8}
滚动查看更多
#include <stdio.h>
#include <conio.h>

void main() {
  FILE *fp;
  char c;

  clrscr();

  fp = fopen("myfile.txt", "r");

  while( (c = fgetc(fp) ) != EOF) {
    printf("%c", c);
  }
  fclose(fp);

  getch();
}
Write to file: fputs()
C
滚动查看更多
#include<stdio.h>
#include<conio.h>

void main() {
  FILE *fp;

  clrscr();

  fp = fopen("myfile2.txt","w");
  fputs("hello c programming",fp);
  fclose(fp);

  getch();
}
Read files: fgets()
C
滚动查看更多
#include<stdio.h>
#include<conio.h>

void main() {
  FILE *fp;
  char text[300];

  clrscr();

  fp = fopen("myfile2.txt", "r");
  printf("%s", fgets(text, 200, fp));
  fclose(fp);

  getch();
}
fseek()
C{8}
滚动查看更多
#include <stdio.h>

void main(void) {
  FILE *fp;

  fp = fopen("myfile.txt","w+");
  fputs("This is Book", fp);

  // Set file pointer to the given position
  fseek(fp, 7, SEEK_SET);

  fputs("Kenny Wong", fp);
  fclose(fp);
}

Set the file pointer to the given position

rewind()
C{11}
滚动查看更多
#include <stdio.h>
#include <conio.h>

void main() {
  FILE *fp;
  char c;

  clrscr();

  fp = fopen("file.txt", "r");

  while( (c = fgetc(fp) ) != EOF) {
    printf("%c", c);
  }

  rewind(fp); // move the file pointer to the beginning of the file

  while( (c = fgetc(fp) ) != EOF) {
    printf("%c", c);
  }
  fclose(fp);

  getch();
}
// output
// Hello World! Hello World!
ftell()
C{11}
滚动查看更多
#include <stdio.h>
#include <conio.h>

void main () {
   FILE *fp;
   int length;

   clrscr();

   fp = fopen("file.txt", "r");

   fseek(fp, 0, SEEK_END);
   length = ftell(fp); // return current position
   fclose(fp);

   printf("File size: %d bytes", length);

   getch();
}
// output
// file size: 18 bytes

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572