Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can you use variables in strings?

char string1[20]=my_age;

my_age, seperated with an underline(full variable) can you use that in a string or it will get mixed up and the compiler will get confused? then make another string and do something and use strcat onto the two variables? or using variables with strings will become error??


Comments

  • : char string1[20]=my_age;

    : my_age, seperated with an underline(full variable) can you use that in a string or it will get mixed up and the compiler will get confused? then make another string and do something and use strcat onto the two variables? or using variables with strings will become error??


    What the compiler will do with your line will depend on what my_age is defined as. The only way it is legal is if my_age is a character, and then it will assign the character that is at index 20 of the array to the value of the character my_age.


    If my_age is any other type your line will not work.


    If your purpose is to simply muck around with the innards of the C string (setting characters individually) then you're along the right lines.


    If your purpose is to turn the value of my_age into its decimal text equivalent, then I suggest you look up the function 'sprintf' in C or the ostrstream class in C++. Neither language allows you to 'automatically' convert a number into its textual format.


  • Shawn CarterShawn Carter Member Posts: 0

    _ || http://forcoder.org || free ebooks and video tutorials about [ Perl, JavaScript, C, Swift, C#, Ruby, MATLAB, R, C++, Delphi, Visual Basic, Python, Assembly, Go, Visual Basic .NET, PL/SQL, Scratch, Java, Objective-C, PHP COBOL, Kotlin, FoxPro, F#, Rust, Alice, SAS, Dart, Prolog, Ada, Fortran, Lua, Logo, VBScript, Transact-SQL, Erlang, Scheme, Hack, LabVIEW, D, Julia, Scala, ABAP, Clojure, Awk, Bash, Crystal, Apex, Lisp, ML ] ____________

Sign In or Register to comment.