소프트웨어/C

    int * float 의 관계에 대해서

    코딩을 하다가 예상과 다른 값이 나와서 이것 저것 찾아보다 보니..float와 int의 관계에서 문제가 생겼다. float와 int의 곱에서 float의 값을 유지하기 위해서는, (int)(int * int * float)의 형태가 되어야한다.즉, float와 int의 곱을 한 값에서 int로 캐스팅을 하는 것이다. 아래는 그 실험이다. #include void func1(); void func2(); void func3(); int main(int argc, char *argv[]) { func1(); func2(); func3(); return 0; } void func1() { printf("1/2 = %f\n", 1/2); printf("1/2 = %d\n", 1/2); printf("1/2 = ..

    scanf속에 표현식을 넣는 경우, scanf가 무시될 수 있다.

    제목에 적은대로scanf에 %s나 %d가 아닌,%[^\n]s%d %d,등으로 설정한 경우, 입력버퍼에 위 경우가 존재하지 않으면 scanf가 실행되지 않습니다. #include int main(void) { int a, b; printf("\n>>"); scanf("%d %d\n", &a, &b); printf("[%d,%d]\n>>", a, b); scanf("%d %d\n", &a, &b); printf("[%d,%d]\n>>", a, b); return 0; } ( Reference : http://electro-don.tistory.com/entry/scanf-n-%EA%B4%80%EB%A0%A8 )

    파일 입출력, 전체 파일 메모리 복사해놓기

    file IO - reading an entire file into memory return to main index #include Often the exact structure of a document is NOT know at the time it is read. Therefore, a function such as fscanf() cannot be used because the sequence, and type, of the data must be know beforehand! Reading text one line at a time can also be tricky unless the maximum number of characters on a line is also known beforehan..

    struct 선언하면서 할당하기(?) _ struct의 이상한 모양 분석

    #include static struct { int count; int rate; } counts[] = { {100, 100} }; int main() { int arr[] = {{100}, {100}}; printf("%d\n", arr[0]); printf("%d\n", arr[1]); //counts[0].count = 4; printf("%d\n", counts[0].count); } result : 100100100

    [에러 리뷰] error: expected ‘,’ or ‘...’ before ‘this’

    c++에서 컴파일 할때 혹시 error: expected ‘,’ or ‘...’ before ‘this’이런 에러가 나온다면, 그리고 c언어와 cpp의 파일으로 작업하고 있다면 의심해볼 만 하다. 이 문제는 c언어와 cpp의 예약어의 차이에서 오는 것이다.즉, c언어로 만들어서 이상 없는 소스를 cpp로 컴파일 시키면 에러가 나올 수 있는 것이다.예제는 아래와 같다. struct Something { char *value; char class[20]; // Bad code!!}; 위 코드는 c언어로는 이상이 없는 코드지만 cpp에서는 에러를 내뿝는 코드이다. 즉 이런 경우에는 해결방법은 크게 3가지 정도이다. 1. 소스의 컴파일을 c언어로 한다. 2. cpp의 예약어를 사용하지 않는다. 3. 아래와 같은..

    엔터(enter,개행문자) 입력받기.

    1. 개요 엔터 혹은 개행문자라 불리는 '\n'을 입력받아서 처리하는 코드를 작성해봤습니다. 2. 방법 방법은 2가지로 실험해봤습니다.1) gets2) getchar scanf는 엔터와 몇가지 아스키코드는 입력받지 않는 것으로 취급하기 때문에 논외로 하였습니다. 3. Source 1) gets #include int main() { char ch; gets(&ch); printf("%d\n", ch); if (ch == '\n') printf("Detecting Enter\n"); return 0; } 2) getchar #include int main() { int i; i = getchar(); printf("%d\n", i); if (i == '\n') printf("Detecting Enter\n..

    개행문자 입력받기.

    단독직입적으로 말하자면 char str[10]; scanf("%s", str); 의 경우 개행 문자(엔터)를 받지 않는다. (지식인의 설명 바로보기) scanf의 경우 엔터를 delimiter로 판단해서 입력받지 않는다는.. 하지만 scanf("%c" 로 받으면 받어지긴 한다. 그래서 대안은 gets인데, gets는 deprecated가 될거라고 떠서.. 한번 개행문자를 걸러볼까 하고 scanf("%c" 로 이것 저것 만들으려고 하다가... 그냥 말았다. 아래 코드는 수정하다가 말았다. #include bool cscanf(char *str) { // false is stop. scanf("%c", str); if( str[0] == '\n' ) { scanf("%c", str+1); if( str[1]..