Cμλ μ¬λ¬ μλ£νμ΄ μκ³ κ°κ°μ μλ£νμ μλ‘ λ€λ₯Έ ν¬κΈ°μ λ©λͺ¨λ¦¬λ₯Ό μ°¨μ§νλ€. char νμ μ λ³μλ₯Ό μμ±νλ©΄ λ©λͺ¨λ¦¬μ ν μ¬κ°ν μμ κ·Έ λ³μμ κ°μ΄ μ μ₯λλ€.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int score1 = 72;
int score2 = 73;
int score3 = 33;
printf("Average: %i\n", (score1 + score2 + score3)/3);
}
μΈ κ°μ μ μλ₯Ό μ μ₯νκ³ νκ· μ μΆλ ₯νλ νλ‘κ·Έλ¨μ΄λ€.
νμ§λ§ μ΄ μ½λκ° μ΅μ μ λμμΈμ΄ μλλ€. κ·Έ μ΄μ λ μ μκ° 3κ°μ§λ‘ κ³ μ μ μ΄κ³ μμμ μ λν λμλ μλ€. λ°λ³΅μ μΈ μμ
μ΄ λμ 보μΈλ€.
λ°λΌμ κ°μ μ μ¬μ§κ° μλ νλ‘κ·Έλ¨μΈ κ²μ΄λ€.
μΌλ¨ μ μμ κ°μκ° λ λ§μμ§λ μ¬μ©ν μ μλ κ²μ΄ λ°°μ΄μ΄λ€.
λ°°μ΄μ λμΌν μλ£νμ λ°μ΄ν°λ₯Ό λ©λͺ¨λ¦¬μμ μ°μ΄μ΄μ μ μ₯νκ³ μ΄λ₯Ό νλμ λ³μλ‘ κ΄λ¦¬νκΈ° μν΄ μ¬μ©λλ€.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int scores[3]; // ν¬ν€κ° 3μΈ λ°°μ΄μ λ§λ€κ³
scores[0] = 72; // ν΄λΉ indexμ κ°μ ν λΉ
scores[1] = 73;
scores[2] = 33;
printf("Average: %i\n", (scores[0] + scores[1] + scores[2])/3);
}
νμ§λ§ μ΄ μ½λμμλ μ μμ κ°μκ° κ³ μ λλ€λ λ¬Έμ μ μ΄ μ¬μ ν μ‘΄μ¬νλ€.
λ μ μκ° μ
λ ₯μ λ°λκ² μλ μ½λ λ΄λΆμ μ΄λ―Έ μ§μ μ΄ λμ΄μλ€. κ·Έλ¦¬κ³ κ°μ κ°λ₯ν λ°λ³΅λ€λ μ¬μ ν μ‘΄μ¬νλ€.
#include <stdio.h>
#include <cs50.h>
float average (int length, int array[]);
int main(void)
{
int n = get_int("Number of scores: ");
int scores[n];
for (int i = 0; i < n; i++)
{
scores[i] = get_int("Score %i: ", i + 1);
}
printf("Average: %.1f\n", average(n, scores));
}
float average (int length, int array[])
{
int sum =0;
for (int i = 0; i < length; i++)
{
sum += array[i];
}
return (float) sum / (float) length;
}
μ μμ κ°μλ₯Ό μ
λ ₯ λ°κ³ κ·Έ κ°μ μ΄μ©ν΄ nκ°
μ μ¬μ΄μ¦λ₯Ό κ°μ§ λ°°μ΄μ λ§λ€κ³
nλ²
μ μ μ
λ ₯μ λ°λλ€. λ average
ν¨μλ₯Ό λ§λ€μ΄μ λ°°μ΄μ κΈΈμ΄μ λ°°μ΄μ μΈμκ°μΌλ‘ μ λ¬ν΄ νκ· κ°μ ꡬν΄μ€κ±Έ μΆλ ₯νλ€.
stringμ char μλ£νμ λ°μ΄ν°λ€μ λ°°μ΄μ΄λ€.
string = "HI!";
λ μλ κ·Έλ¦Όκ³Ό κ°μ΄ μ μ₯λλ€.
λ¬Έμμ΄μ λ°λ‘ κΈΈμ΄λ₯Ό μ§μ νμ§ μκΈ° λλ¬Έμ μ»΄ν¨ν°κ° λ¬Έμμ΄μ΄ μ’ λ£λλ μ§μ μ μκΈ°μν΄μ λ¬Έμμ΄μ λμ νμ null μ’ λ¨ λ¬Έμ \0μ΄ λ€μ΄κ°λ€.
string names[4];
names[0] = "EMMA";
names[1] = "RODRIGO";
names[2] = "BRIAN";
names[3] = "DAVID";
printf("%s\n", names[0]);
printf("%c%c%c%c\n", names[0][0], names[0][1], names[0][2], names[0][3]);
μ΄λ¬ν μ½λκ° μλ€λ©΄ μ€μ λ©λͺ¨λ¦¬ μμλ μλμ κ°μ΄ μ μ₯λλ€.
λ¬Έμμ΄ κΈΈμ΄ νμνλ λ°©λ²μ 첫λ²μ§Έλ‘ λ°λ³΅λ¬Έμ μ΄μ©ν΄ λ¬Έμμ΄μ μννλ©° \0
κ³Ό μΌμΉνλμ§ κ²μ¬νλ κ²μ΄λ€. νμ§λ§ strlen
ν¨μλ₯Ό μ΄μ©νλ©΄ μ½κ² μ»μ μ μλ€.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("input: ");
printf("Output:\n");
for (int i =0, n = strlen(s); i < n; i++)
{
printf("%c\n",s[i]);
}
}
μ
λ ₯λ°μ λ¬Έμμ΄μ λλ¬Έμλ‘ λ°κΏμ£Όλ λ°©λ²μ ν¬κ² 2κ°μ§κ° μλ€.
μμ€ν€ μ½λλ₯Ό μ°Έκ³ ν΄ λ³κ²½ν΄μ£Όλ λ°©λ²μ΄ μλ€.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
{
printf("%c", s[i] - 32);
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
}
λ ctype
λΌμ΄λΈλ¬λ¦¬μ μλ toupper
ν¨μλ₯Ό μ¬μ©νλ€.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c", toupper(s[i]));
}
printf("\n");
}
CS50 Manual Pages λ₯Ό μ°Έμ‘°ν΄μ λ€μν λΌμ΄λΈλ¬λ¦¬μ ν¨μλ₯Ό νμΈν μ μλ€.
μ»΄νμΌ ν μ μ₯νκ³ μ νλ νμΌλͺ
κ³Ό κ°μ΄ μΆκ°μ μΈ μ 보λ₯Ό ν¨κ» μ€ μ μλ€.
μ΄λ¬ν μ 보λ₯Ό λͺ
λ Ήν μΈμλΌκ³ νλ€.
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("hello, %s\n", argv[1]);
}
else
{
printf("hello, world\n");
}
}
첫λ²μ§Έ 맀κ°λ³μ argc
λ main
ν¨μκ° λ°κ² λ μ
λ ₯μ κ°μμ΄λ€. κ·Έλ¦¬κ³ argv[]
λ κ·Έ μ
λ ₯μ΄ ν¬ν¨λμ΄ μλ λ°°μ΄μ΄λ€. argv[0]
μ κΈ°λ³Έμ μΌλ‘ νλ‘κ·Έλ¨ μ΄λ¦μΌλ‘ μ μ₯λλ€.
μλ₯Ό λ€μ΄ μ νλ‘κ·Έλ¨μ arg.c
λ‘ μ μ₯νκ³ μ»΄νμΌ ν νμ ./argc Kim
μΌλ‘ μ€ννλ©΄ hello, Kim
μ κ°μ΄ μΆλ ₯μ΄ λλ€.
argc == 2, argv[1] == Kim