printf("%d", n);
scanf("%d", &n);
| Format | Argument Type | Description |
|---|---|---|
| %d, %i | int | Decimal integer |
| %u | unsigned int | Unsigned decimal |
| %x | unsigned int | Hexadecimal |
| %#x | unsigned int | Hexadecimal with prefix 0x |
| %f | double | Floating point |
| %Lf | long double | Long double |
| %e, %E | double | Scientific notation |
| %c | int | To print a character |
| %s | char * | String (character array ended with '\0') |
| %p | void * | Print memory address |
| %g, %G | double | %f or %e depending on the length |
| Format | Argument Type | Description |
|---|---|---|
| %d | int * | &n, store the input integer in n |
| %ld | long * | Read long |
| %lld | long long * | Read long long |
| %u | unsigned int * | Read unsigned int |
| %f | float * | Read float |
| %lf | double * | Read double |
| %Lf | long double * | Read long double |
| %c | char * | Read characters (e.g. Read 3 characters : %3c) |
| %s | char * | Read a string until whitespace |
| %n | int * | with %s, to get string length |
| Category | Operators |
|---|---|
| Logical | ! && || |
| Comparison | == != > < >= <= |
| Arithmetic | + - * / % |
int a[5][5];
| a[0][0] | a[0][1] | a[0][2] | a[0][3] | a[0][4] |
| a[1][0] | a[1][1] | a[1][2] | a[1][3] | a[1][4] |
| a[2][0] | a[2][1] | a[2][2] | a[2][3] | a[2][4] |
| a[3][0] | a[3][1] | a[3][2] | a[3][3] | a[3][4] |
| a[4][0] | a[4][1] | a[4][2] | a[4][3] | a[4][4] |
*(a+i) is equivalent to a[i]
(a+i) is equivalent to &a[i]
1 2 3 4 5 e
#include <stdio.h>
int main(void)
{
int x;
while (scanf("%d", &x) == 1) {
printf("x=%d\n", x);
}
return 0;
}
2
L 5 2
D 5 3
#include <stdio.h>
int main(void)
{
char ch;
int i, n, row, col;
scanf("%d", &n);
for (i=0; i<n; i++) {
while(getchar()!='\n');
scanf("%c%d%d", &ch, &row, &col);
}
return 0;
}
for(i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%5d", A[i][j]);
}
printf("\n");
}
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-1-i; j++)
{
if (ap[j] > ap[j + 1])
{
temp = ap[j];
ap[j] = ap[j + 1];
ap[j + 1] = temp;
}
}
}
#include <string.h>
char str[10];
scanf("%s", str);
fgets(str, 10, stdin);
gets(str);
| Function | Description |
|---|---|
strlen(str) |
to get the string length |
strcmp(str1, str2) == 0 if equal |
to compare two strings |
strncmp(str1, str2, n) == 0 if equal |
to compare the first n chars of two strings |
strcpy(str1, str2) |
to copy str2 to str1 |
strcat(str1, str2) |
to append a copy of str2 to str1 |
strncpy(str1, str2, n) |
to copy the first n chars of str2 to str1 (remember to add '\0' to str1) |
strtok(str1, delim) |
to get the first token in str1 splitted by delim, and use loop to get next token |
strstr(str1, str2) |
to check if str2 is str1's substring, return pointer to the first character of the substring in str1 |
isspace(ch) → check if ch is whitespace (space, tab, newline, etc.)
islower(ch) → check if ch is a lowercase letter
isupper(ch) → check if ch is an uppercase letter
isdigit(ch) → check if ch is a digit (0–9)
isalpha(ch) → check if ch is an alphabet letter (a–z, A–Z)
toupper(ch) → convert ch to uppercase
tolower(ch) → convert ch to lowercase
int arr[n];). Keep the array size fixed (e.g. int arr[1000];).