Compiling contains four steps, preprocessing, compiling, assembling and linking.
First convert a source code into required functions, like for instance in c, read hashtags and copy and past files.
Second step is compiling, convert source code into assembling codes.
Third step is assembling, converting assembling codes into machine codes, 1s and 0s.
Last, link all files into 0s and 1s.
Programmers don’t like to be tedious all the times, instead use powerful tools to assist them when programming. From cs50 sandbox, we will now use cs50 ide(integrated development environment)
Terminal commands
Ls – shows list of files in current location
Cd – redirect current location(cd .. for back, cd filename to go to that directory)
Rm – to remove a file
Mkdir – makes folder
Rmdir – removes directory
Make – creates a runnable code
./ - runs the source code
Touch – creates a file
Utilize the power of a debugger! Will save you minutes and hours with a problem.
String is actually an array of characters with null at the back.
0x0 is null! Could be seen at debugger
Data type can only receive certain size of a byte. String returns memory location.
brian returns 100th value in memory while veronica returns 900th value in memory.
String is a synonym of char *. * means an address of a something to its left, char * will mean character’s address. A pointer is an address.
When nothing is specified in the main function, it automatically returns 0 if it runs successfully.
Segmentation fault means user touched memory that user should not have. Returning one if null like above helps to prevent memory problems.
Difference in size of Char:
Since C only supports ASCII (English), 1 byte is enough for char (256 characters; 0 to 255). Since Java supports more than 18 international languages with over 3200 characters, a char variable is assigned a space of 2 bytes (2^16 = 65536 characters).
CS50 IDE
1. comparing strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include <cs50.h>
#include <stdio.h>
#include <string.h>
bool compare_strings(char *a, char *b);
int main(void){
char *i = get_string("enter i : ");
if(i == NULL) return 1;
char *j = get_string("enter j : ");
//null and exclamation point is the same thing
if(!j) return 1;
if(compare_strings(i,j)) printf("same\n");
else printf("not same\n");
//strcmp returns 0 if it's equal + if i comes before j
//- if j comes before i, a function provided by string.h
if(strcmp(i,j) == 0) printf("itworks!\n");
return 0;
}
bool compare_strings(char *a, char *b){
int alength = strlen(a);
int blength = strlen(b);
if(alength != blength) return false;
for(int i = 0; i < alength; i++){
if(a[i] != b[i]) return false;
}
return true;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
compares strings if it is same or not. The key point is the run about pointers and how * works.
2. String? address?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
//receive user input and capitalize the first letter of a given word
int main(void){
/*char *c = get_string("type your word : ");
for(int i = 0; i < strlen(c); i++){
printf("%c : %i\n", c[i], c[i]);
}
if(c[0] >= 'a' && c[0] <= 'z') c[0] = c[0] - ('a' - 'A');
printf("%s\n",c);*/
string s = get_string("type your word : ");
string t = s;
if(strlen(t) > 0) t[0] = toupper(t[0]);
printf("s : %s\n", s);
printf("t : %s\n", t);
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
~/ $ cd memory
~/memory/ $ make copy0
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow copy0.c -lcrypt -lcs50 -lm -o copy0
~/memory/ $ ./copy0
type your word : taewon
s : Taewon
t : Taewon
~/memory/ $
I only changed t but is applied to s as well. Learning about address concept.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
//receive user input and capitalize the first letter of a given word
int main(void){
char *s = get_string("type your word : ");
//malloc stands for memory allocate
//malloc creates a room and have to specify how many byte is required
//for the given room
//get the size of s, if it is david the size is 5.
//+1 is required because compiler stops reading the memory until \0 appears
//now for each space how many byte is required.
//char is 1byte in C, ONLY in C because english was only used
//since char is 1byte, sizeof is really not necessary but wrote it
//for specification
char *t = malloc((strlen(s) + 1) * sizeof(char));
strcpy(t, s);
/* for(int i = 0, n = strlen(s); i <= n; i++){
t[i] = s[i];
}*/
if(strlen(t) > 0) t[0] = toupper(t[0]);
printf("s : %s\n", s);
printf("t : %s\n", t);
//to avoid memory leak!
free(t);
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
To copy and change the value, malloc is used. Malloc means memory allocate, first we need to assign a memory size to a given variable. Since string is value + null, string length + 1 is needed to used first and we need to multiple by the data type size. Char in c is 1 byte, unlike 2 byte in java because it only supports english. since multiplying by 1 is returning itself, sizeof char is not really needed for this situation but will include it since it is more precise.
Copying a value to another string(char *), we could use a for loop or a function c provides with string, a strcpy.
When t is only used here once, we free the t to avoid memory leak.
3. scanning
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <stdio.h>
int main(void){
int x;
printf("x : ");
//scanf requires the address of the variable so ampersand is used &
//ampersand means the address of that variable
//if it's a string(char*), ampersand is not used because already an
//address is given
scanf("%i", &x);
printf("x : %i\n", x);
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
ampersand gets the address while star uses that address!
4. let us see address!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <cs50.h>
#include <stdio.h>
int main(void){
char *s = get_string("s: ");
char *t = get_string("t: ");
printf("s: %p\n", s);
printf("t: %p\n", t);
//0x17686b0
//0x means anything that comes after 0x is hexadecimal
//17686b0 is hexadecimal, represented for memory location
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
~/memory/ $ ./addressess
s: taewon
t: weeee
s: 0x865670
t: 0x8656b0
~/memory/ $
5. swapping? use stars to apply address!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <stdio.h>
void swap(int *a, int *b);
int main(void){
int a = 1;
int b = 2;
printf("a is %i, b is %i\n", a, b);
swap(&a, &b);
printf("a is %i, b is %i\n", a, b);
}
//* means address!!
//& means give me address of that value, * means go to that address of that value! different
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'IT > ETC' 카테고리의 다른 글
Arrays(week 2, problem set) (0) | 2019.12.30 |
---|---|
C Programming Language(week1, problem set) (0) | 2019.12.30 |
Arrays and Sorting Algorithms (week2) (0) | 2019.12.26 |
C Programming Language(week 1) (0) | 2019.12.25 |
Computational Thinking & Scratch – Intro to Computer Science(week 0) (0) | 2019.12.25 |