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

  • Home
  • SEARCH
  • 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 6835311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:13:03+00:00 2026-05-26T23:13:03+00:00

Why is it that I can assign a string to a variable of type

  • 0

Why is it that I can assign a string to a variable of type int? Eg. the following code compiles correctly:

int main(int argv, char** argc){
    int a="Hello World";
    printf(a);
}

Also, the program doesn’t compile when I assign a string to a variable of a different type, namely double and char.

I suppose that what is actually going on is that the compiler executes int* a = "Hello World"; and when I write double a="Hello World";, it executes that line of code as it is.

Is this correct?

  • 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-05-26T23:13:03+00:00Added an answer on May 26, 2026 at 11:13 pm

    In fact, that assignment is a constraint violation, requiring a diagnostic (possibly just a warning) from any conforming C implementation. The C language standard does not define the behavior of the program.

    EDIT : The constraint is in section 6.5.16.1 of the C99 standard, which describes the allowed operands for a simple assignment. The older C90 standard has essentially the same rules. Pre-ANSI C (as described in K&R1, published in 1978) did allow this particular kind of implicit conversion, but it’s been invalid since the 1989 version of the language.

    What probably happens if it does compile is that

    int a="Hello World";
    

    is treated as it if were

    int a = (int)"Hello World";
    

    The cast takes a pointer value and converts to int. The meaning of such a conversion is implementation-defined; if char* and int are of different sizes, it can lose information.

    Some conversions may be done explicitly, for example between different arithmetic types. This one may not.

    Your compiler should complain about this. Crank up the warning levels until it does. (Tell us what compiler you’re using, and we can tell you how to do that.)

    EDIT :

    The printf call:

    printf(a);
    

    has undefined behavior in C90, and is a constraint violation, requiring a diagnostic, in C99, because you’re calling a variadic function with no visible prototype. If you want to call printf, you must have a

    #include <stdio.h>
    

    (In some circumstances, the compiler won’t tell you abut this, but it’s still incorrect.) And given a visible declaration, since printf‘s first parameter is of type char* and you’re passing it an int.

    And if your compiler doesn’t complain about

    double a="Hello World";
    

    you should get a better compiler. That (probably) tries to convert a pointer value to type double, which doesn’t make any sense at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code, is there a shorthand that I can use to assign a
I am running an asp.net code-behind that creates a string variable which holds a
I have the following code wich is raising an EConvertError - can not assign
Is there a simple attribute or data contract that I can assign to a
The following code does not compile: #include <iostream> class Foo { std::string s; public:
I would like to assign a multi-line string to a variable in R so
Im trying to assign a piece of a string, to a new string variable.
I have a object 'currentVersion' of type Data and string variable of 'version',i need
I have the following code that I am using to parse out a test
Anyone that can help me out will be very much appreciated. I seem to

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.