728x90
반응형
1.main_변수와 상수
#include <stdio.h>
int main(void)
{
//값, 상수, value, rValue, data, 데이터
//아래와 같은 형식은 값이 저장이 될까? -> 된다.
//unsigned int, int
100;
3.14;
"Hello";
'A';
//파일
FILE* my = 0;
fprintf(my, "Hello");
return 0;
}
2.main_함수와 자료구조
#include <stdio.h>
#include <malloc.h>
int member = 234;
int Add(int, int); //declaration
int main(void)
{
//배열은 뭘까? - 자료구조
//배열은 항상int 하나짜리하고 같이 코딩을 한다.
int arr[2] = { 0 };
int count = 0;
arr[count] = 100;
count++;
arr[count++] = 200;
//arr[1] = 200;
{
int a = 100; //지역변수 : 공간을 벗어나면 사라짐
}
int * a = 0;
a = (int*)malloc(4);
*a = 234;
free(a);
//배열
int i = 0;
i = 100;
i = i; //오른쪽 i는 값, 왼쪽 i는 공간
Add(i, i);
Add(100, 100);
i = Add(100, 100);
//printf
return 0;
}
//int a = i, int b = i;
int Add(int a, int b) //definition
{
return a + b;
}
3.main_함수를 보고 변수가 정해진다
//포인터의 사용
//값을 변경시킬 때
//배열을 넘길때
#include <stdio.h>
//int Add(int, int);
void Add(int, int, int*);
int main(void)
{
int number = 0;
Add(1, 2, &number);
printf("%d", number);
return 0;
}
void Add(int a, int b, int* result)
{
*result = a + b;
}
4.main_포인터
// 함수에서 어떤 때에 파라미터로 포인터를 사용할까?
// - 값을 변경할 때, 길이를 알 수 없는 배열같은 데이터를 넘겨줄 때
#include <stdio.h> //standard input / output(표준입력/표준출력) - 키보드/모니터
void MyOpenCar(char* car);
int Add(int, int);
void Plus(int, int, int*);
int main(void)
{
char d = 'G';
MyOpenCar(&d);
printf("d = %c\n", d);
int c = 100;
Plus(1, 2, &c);
printf("c = %d\n", c);
Add(1, 2);
fprintf(stdout, "\nHello Network programming\n\n");
int* p = 0;
int i = 0;
char* str = 0;
char a = 0;
printf("p = %dbyte, i = %dbyte\n", sizeof(p), sizeof(i));
printf("p = %dbyte, i = %dbyte\n", sizeof(int*), sizeof(int));
printf("str = %dbyte, a = %dbyte\n", sizeof(str), sizeof(a));
printf("stre = %dbyte, a = %dbyte\n", sizeof(char*), sizeof(char));
return 0;
}
void MyOpenCar(char* car)
{
*car = 'S';
}
int Add(int a, int b)
{
return a + b; //return을 사용해 값을 반환
}
void Plus(int a, int b, int* result)
{
*result = a + b; //포인터를 사용해 값을 변경
}
5.main_함수에 파라미터로 포인터를 쓰는 2가지 이유
#include <stdio.h>
// 상수 : value, 값, 데이터, rValue, constant, 변하지 않는 것
// 변수 : 공간, 저장하는 곳, lValue
//int i =100; i=i;
// 함수에 파라미터로 포인터를 쓰는 2가지
// 1) 값을 반드시 변경 2) 배열같은 2개 이상의 데이터를 넘길 때
int Add(int, int);
void Plus(int, int, int*);
int total(int*);
int main(void)
{
int arr[10] = { 10 };
int totalResult = total(arr);
printf("totalResult = %d\n", totalResult);
for (int i = 0; i < 10; i++)
{
printf("%d\n", arr[i]);
}
int result = -2;
Plus(10, 20, &result);
int i = 10, j = 20;
i = i;
Add(10, 20);
Add(i, j);
return 0;
} // int a = 10; int b = 20;
// int a = j; int b = j;
int Add(int a, int b)
{
return a + b;
}
void Plus(int a, int b, int* result)
{
// * result = a +b; //상수일까 변수일까?
*result = a + b;
}
int total(int* a)
{
int result = 0;
for (int i = 0; i < 10; i++)
{
a[i] += 1;
result += a[i];
}
return result; // return은 어디에 나오든지 이 함수를 종료한다는 의미이다.
}
6.main_포인터의 복습
#include <stdio.h>
#include <string.h>
// 문자열은 데이터 형 일까? 시스템에서 문자열을 알고 있을까?
//const : 값을 변경하지 않음, reading, 상수까지 포함
int MyStrlen(const char* str);
int main(void)
{
//strlen - 문자열 길이, 문자들 하나하나의 갯수
const int i = 100;
char name[8] = "Potinter";
//name[0] = 'A';
//int count = MyStrlen(name);
//int count = strlen(name);
//int count = strlen("Pointer");
const char* p = "Pointer";
int count = MyStrlen("Pointer");
printf("문자열은 %d개의 문자로 구성되어있다.\n", count);
// if (100) printf("항상 프린트가 된다.");
int length = 10;
printf("%d\n, length=10");
//문자열은 하나의 데이터형 : 시작주소 ~~\0
printf("%s\n", "Pointer");
return 0;
}
int MyStrlen(char* str)
{
int length = 0;
// 제어문 - while, if, for, switch...
9 == 0;
printf("%d\n", length > 0);
//length가 0보다 클 때
if (100);
if (length > 0) printf("length는 0보다 크다."); //0(False)만 아니면 참(True)
//while(* str)
//while(* str! = 0)
//while(* str! = NULL)
while ((*str) != '\0')
{
length++;
str++;
}
return length;
}
7.main_새로운 데이터형은 무한대이다
#include <stdio.h>
#include <string.h>
// 문자열은 데이터 형 일까? 시스템에서 문자열을 알고 있을까?
//const : 값을 변경하지 않음, reading, 상수까지 포함
int MyStrlen(const char* str);
int main(void)
{
//strlen - 문자열 길이, 문자들 하나하나의 갯수
const int i = 100;
char name[8] = "Potinter";
//name[0] = 'A';
//int count = MyStrlen(name);
//int count = strlen(name);
//int count = strlen("Pointer");
const char* p = "Pointer";
int count = MyStrlen("Pointer");
printf("문자열은 %d개의 문자로 구성되어있다.\n", count);
// if (100) printf("항상 프린트가 된다.");
int length = 10;
printf("%d\n, length=10");
//문자열은 하나의 데이터형 : 시작주소 ~~\0
printf("%s\n", "Pointer");
return 0;
}
int MyStrlen(char* str)
{
int length = 0;
// 제어문 - while, if, for, switch...
9 == 0;
printf("%d\n", length > 0);
//length가 0보다 클 때
if (100);
if (length > 0) printf("length는 0보다 크다."); //0(False)만 아니면 참(True)
//while(* str)
//while(* str! = 0)
//while(* str! = NULL)
while ((*str) != '\0')
{
length++;
str++;
}
return length;
}
728x90
반응형