Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:22:15+00:00 2026-06-10T11:22:15+00:00

I have 2 questions regarding different declarations of the same function and global variable

  • 0

I have 2 questions regarding different declarations of the same function and global variable in two files in case of C and C++ as well.

  1. Different function declarations

    Consider the following code fragments:

    file_1.c

    void foo(int a);
    
    int main(void)
    {
        foo('A');
    }
    

    file_2.c

    #include <stdio.h>
    
    void foo(char a)
    {
        printf("%c", a); //prints 'A' (gcc)
    }
    

    As we can see, the prototype differs from the definition located in
    file_2.c, however, the function prints expected value.

    If it comes to C++, the above program is invalid due to undefined
    reference to foo(int) at link time. It’s probably caused by
    presence of other function signatures – in comparison with C, where
    a function name doesn’t contain any extra characters indicating the
    type of function arguments.

    But when it comes to C then what? Since the prototypes with the same
    name have the same signature regardless of the number of arguments
    and its types, linker won’t issue an error. But which type
    conversions are performed in here? Does it look like this: 'A' ->
    int -> back to char? Or maybe this behavior is
    undefined/implementation-defined ?

  2. Different declarations of a global variable

    We’ve got two files and two different declarations of the same
    global variable:

    file_1.c

    #include <stdio.h>
    
    extern int a;
    
    int main(void)
    {
        printf("%d", a); //prints 65 (g++ and gcc)
    }
    

    file_2.c

    char a = 'A';
    

    Both in C and C++ the output is 65.

    Though I’d like to know what both standards say about that kind of
    situation.

    In the C11 standard I’ve found the following fragment:

    J.5.11 Multiple external definitions (Annex J.5 Common extensions)
    There may be more than one external definition for the identifier of
    an object, with or without the explicit use of the keyword extern; if
    the definitions disagree, or more than one is initialized, the
    behavior is undefined (6.9.2).

    Notice that it refers to presence of two and more definitions, in
    my code there is only one, so I’m not sure whether this article is a good point of reference in
    this case…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:22:16+00:00Added an answer on June 10, 2026 at 11:22 am

    Q1. According to C99 specification, section 6.5.2.2.9, it is an undefined behavior in C:

    If the function is defined with a type that is not compatible with the type (of the expression) pointed to by the expression that denotes the called function, the behavior is undefined.

    The expression “points to” a function taking an int, while the function is defined as taking a char.

    Q2. The case with variables is also undefined behavior, because you are reading or assigning an int to/from char. Assuming 4-byte integers, this will access three bytes past the memory location where it is valid. You can test this by declaring more variables, like this:

    char a = 'A';
    char b = 'B';
    char c = 'C';
    char d = 'D';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions regarding the following function. Obviously, it is about a chat.
I have seen different questions regarding this, but I still find this topic to
I have looked at the different questions regarding this issue, but couldn't find anything
I have some questions regarding the following css that I found: html, body {
I actually have two questions regarding exception/error handling in the iPhone app that I
I have two questions regarding python libraries: I would like to know if there
I have a couple of conceptual questions: regarding serving static files (media) in Django
I have two questions regarding jsTree: I am generating a jsTree using JSON with
I have two questions regarding a login form I'm styling. I am using onfocus
I have two questions regarding how to create / use the JDO persistence manager

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.