C언어 가변 변수 함수에서 기본 인자 진급

alee의 이미지

C언어에서 다음과 같은 가변 변수 함수를 사용하는 경우 C 표준에 의하면 부동소수점 숫자의 경우 항상
double 형으로 기본 인자 진급이 일어나기 때문에 va_arg()를 사용해 인자를 해석할 때 double형으로
해석해야 합니다.

int main() {
    ...
    my_function(1, (float) 1.0f);
    ...
}
 
void my_function(int N, ...) {
    va_list ap;
    va_start(ap, N);
    float F = va_arg(ap, double); // or va_arg(ap, float)?
    ...
}

그런데, my_function()을 호출할 때 항상 두 번째 argument를 (float)으로 명시적으로 캐스팅하여
넘긴다고 보장할 수 있는 경우 my_function()함수 안의 va_arg()에서 double이 아닌 float으로
해석하는게 가능한지요? 또, 다음과 같이 float형 변수를 포함하고 있는 구조체를 넘기는 경우, 구조체
안의 float 변수에 들어 있는 값도 double로 기본 인자 진급이 일어나는지요?

typedef struct {
    float F;
} float_struct;
 
typedef struct {
    double F;
} double_struct;
 
int main() {
    ...
    float_struct Struct = { 1.0f };
    my_function(1, Struct);
    ...
}
 
void my_function(int N, ...) {
    va_list ap;
    va_start(ap, N);
    float_struct Struct = va_arg(ap, float_struct); // or va_arg(ap, double_struct)?
    ...
}

혹시 C언어 표준을 정확히 이해하고 계신 분이 계시면 답변을 부탁 드립니다.

익명 사용자의 이미지

1. 같은 type 으로의 cast 연산은 무의미함 (1.0f 의 type 은 이미 float)
2. 넘기는 것은 구조체이지 float 이 아님

Anti-Lock의 이미지

인용: "C언어에서 다음과 같은 가변 변수 함수를 사용하는 경우 C 표준에 의하면 부동소수점 숫자의 경우 항상
double 형으로 기본 인자 진급이 일어나기 때문에 va_arg()를 사용해 인자를 해석할 때 double형으로
해석해야 합니다."

말씀하신 대로 라면 double 로의 '인자 진급'은 피할수 없을것 같습니다.
다음과 같겠죠.
double d = (float) 1.0f;

이미 1.0f 가 float 이더라도, float로 명시적으로 캐스팅 하더라도
인용: "부동소수점 숫자의 경우 항상 double 형으로 기본 인자 진급이 일어나기 때문에"
double로 '인자 진급' 되는것을 피할수 없겠죠..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.