본문 바로가기

코-딩/C & C++7

C언어 - scanf와 \n scanf("%d", &num);의 작동은 입력값 1개를 받고 공백문자가 들어가면 종료되나 scanf("%d \n", &num);의 작동은 입력값 1개를 받고 공백문자를 넣어도 종료가 되지 않고 추가적인 입력값을 기다린다. 형식(format) 문자열에 있는 공백문자(white-space)는 그 다음에 공백 문자가 아닌 문자(non-white-space)가 올때까지 입력버퍼(stdin)에서 읽어들이라는 뜻이기 때문이다. 그리고 읽어들인 문자는 버퍼에 저장되어 이후에 추가 입력을 받으면 기존에 있던 버퍼의 내용을 가지고 오므로 문제가 발생할 수 있다. ex)int num;scanf("%d\n", &num); > 10, 20 입력printf("%d\n", num); > 10출력scanf("%d", &num);.. 2025. 4. 7.
asprintf : print로 찍은 내용을 변수에 담기 #include#include int main() {    std::vector stringVector;    stringVector.push_back("hello");    stringVector.push_back("bye");    char *combined;    asprintf(&combined, "awcUrl : %s://%s\n", stringVector[0].c_str(), stringVector[1].c_str());    std::count     free(combined);    return 0;} 하면 combined에 %s에 출력되는 string을 담을 수 있다. 2024. 10. 2.
C & C++ - 구조체 typedef struct alpha_s { string fruit = ""; string vegetable = ""; } alpha_t; typedef struct beta_s { string fruit = ""; string vegetable = ""; } beta_t; 구조체 배열에서 구조체 배열로 값을 넘겨줄 때 - for문은 int i를 선언하는 형태를 사용 - 구조체의 주소를 받는 포인터를 선언해서 값을 담기 2023. 3. 23.
C & C++ - string을 char*으로 변환 string example = "This is example how to change string to char*"; std::vector toCharStar( example.begin(), example.end() ); toCharStar.push_back('\0'); char* result = &toCharStar[0]; example >>> result로 형변환을 하였다. 출처 & 참고 : std::string을 const char*나 char*로 바꾸는 방법 std::string을 const char*나 char*로 바꾸는 방법 좀 알려주세요 hashcode.co.kr 2022. 10. 12.