Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Week 1: C

1.Number: int(%i, %d), float(%f),long(%li), double(%f);


2.Text: Char(%c),string(%s).
2.1 New text or any thing: $ Code (text'.c')
3. if ( )
{

}
else
{

}
4. int (i = 0)
while (i < 5) = for (i = 0; i < 5; i++)
i = i + 1;
{
printf("%i\n", i);
}
5. do
(*) int age;
should use (do) do
{ {
age = get_int("Age: ");
} or }
while (age < 15 || age > 20); while(boolean-expr);
for (int i = 1; i <= 5; i++)
{
printf("printf sth")
}
6. What i don't know?
// prompting the user for a starting # of llamas (this is the text for 2clock get
up you)
7.
start += start / 12 or start = start = start + start / 12;
8.
equality: Bang nhau (==)
unequality: Khong bang nhau (!=)
9.
int x;
if (expr)
{
x = 5; int x = (expr) ? 5 : 6; (REMEMMBER)
}
else or
{
x = 6; switch = Langen[x]
}

#include <cs50.h>
#include <stdio.h>

int main(void)
{
string first = get_string(what's your first name?"); "String" First =
get_string; "Int" = get_int (Remmember Chuong);
string last = get_String("what's your last name?");
printf("hello, %s %s\n" first, last);
}

TERMINAL

$make hello
$./hello
What's your first name? Minh
What's your last name? Chuong
hello, Minh Chuong

get ! before %s = hello, Minh Chuong!


* you see.

$Code Internet
Go to the page Internet; Code = Open in C

EX2:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
int x = get_int("What's x?");
int y = get_int("What's y?");

if (x<y)
{
printf("x is less than y\n ")
}

}
TERMINAL
$ make compare
$ ./compare
What's x? 2
What's y? 3
x is less than y

Open anything just (make+value)

EX3:
#include <cs50.h>
#include <stdio.h>
Counter = Counter + 1; spin around
Counter = Counter - 1 or --;
=) repeat +1 or -1
(*)
Int counter = 3 (input)
while (counter > 0) (3 to 0)
{
printf(meow\n);
counter = counter - 1;
}
=) counter = i;
(*)
int i = 3;
while (i>0) =) 0 go to 3 and printf;
{
printf("meow\n");
i = i--; (cach de dem tu tren xuong 3,2,1)
i = i++; (cach de dem tu duoi len 1,2,3)
}
=) this is the hide of language! (counter = i);

(+) Spotify play forevel with code:


#include <cs50.h>
#include <stdio.h>
while (true=forevel)
{
printf(meow\n);
}
TERMINAL
$ make meow
$ ./meow
meow
meow
...

EX4: Do game mario.


#include <cs50.h>
#include <stdio.h>

int main (void)


{
const int n = 3; (const is math just look)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf ("#");
}
printf ("\n");
}

TERMINAL
$ make mario
$ ./mario
###
###
###
$
}
int n = get_int("size: ");
to there

TERMINAL
#./mario
size: 3
###
###
###
$

EX4:
#include <cs50.h>
#inclufe <stdio.h>

int main(void)
{
long x = get_long("x: ");
long y = get_long("y: ");
printf("%li\n", x + y);

float z = (float) x / (float) y; (float la dieu kien mot dang so nguyen de


nhan duoc ket qua cua thap phan chinh xac)
printf("%f\n", z printf("%20f\n, z") =) 0.333333334324121 (du 20 so thap
phan, cho %20f gia tri tuyet doi cua 20)
}

if long is 32 bit
then int is more less
TERMINAL
make caculator
./caculator (long)
x: 2000000
y: 2000000
40000000 (true)

./caculator (int)
x: 20000000
y: 20000000
-2309082498 (wrong)

./caculator (float)
x: 1
y: 3
0.333333... (Chi lay gia tri xap xi)

./caculator (double x, y, z)
x: 1
y: 3
0.3333333333333331483

KL:
Int luon chiem 4 byte = 32 bit bo nho (float);
50% int la (-) va 50% int la (+);
khoang - 2 ty den + 2 ty =) su dung double (64 bit) (chinh xac hon) de tang gia tri
bit > 32 bit;
int number = 23;
char letter = "H";

You might also like