2
#include <stdio.h>
#include <stdlib.h>

int main()
{

    int agecalc;
    int nextyr;
    int birthday;
    int currentday;
    int agecalcu;
    int randomnumbers;

    birthday = 1987;
    currentday = 2016;
    nextyr = currentday + 1;
    agecalc = currentday - birthday;
    randomnumbers = 7890;

    char My[] = "Sayan Banerjee";

    printf("%s 's birthday is %.3d \n", My , agecalc);

    agecalcu = agecalc + 1;

/* alternatively I can use one int and change the variable stored in agecalc by stating that
agecalc = currentday - birthday +1; But I think that is just messy and disturbs a lot of code. declaring a new int is
better and less intrusive. this way when I may need to use agecalc again, i dont have to worry about the value stored in that
variable. */

    printf("%.5s will turn %.3d on %d.\n \a", My , agecalcu , nextyr);

    printf("The username for %s is %.6d \n", My , randomnumbers);
    // the thing here is that I cannot shorten a value stored in a value unless its in an array
    // but I can express it in other ways like 1 to 01.
    /* The thing is that I cannot store two %s in one argument/code. this freezes the code. better way would be to
    create another variable and then try to edit that.*/

    //this is an experiment to see whether I can take characters from an array and store it in a variable
    int user;


    My [0,1,2,3,4,5] = user;

    printf("%s is also %s", My , user );


   return 0;
}

The main question I have is in the last line. I am a newbie to coding and have just started learning. I was just playing around and noticed that if I put in two %s in the same argument the program kind of crashes. So, I was thinking if I can take certain characters in the array My and store them in a variable and then print it?

Is it possible? or am I just looking at this in the wrong way? Sorry for the messy post. Thanks for all your help.

Zeta
  • 103,620
  • 13
  • 194
  • 236
returnNULL
  • 19
  • 6
  • also, the comments there, are just what I discovered tinkering around. I may be wrong in what I wrote, but I just posted the whole thing. Thanks for your patience! – returnNULL May 12 '16 at 06:22
  • Welcome to StackOverflow. Please read the [page on formatting](https://stackoverflow.com/editing-help), that way your question won't be as "messy". Also keep in mind that you can always [edit] your question. – Zeta May 12 '16 at 06:24
  • By the way, if `x = y` assigns the value of `y` to `x`, does your `My ... = user` makes sense to you? (I don't have time for a full answer at the moment, sorry). – Zeta May 12 '16 at 06:27
  • 1
    `My [0,1,2,3,4,5] = user` what do you expect this line of code to do? – CinCout May 12 '16 at 06:27
  • @Zeta thanks for your answer. What I wanted to do was take the characters stored in the My array ( My[] = "Sayan Banerjee"; ) and take the "Sayan" part and store it in a variable. and then access it later with the ("%s is also %s \n", My, user) part of the code. Sorry. But I am not very expressive in this language. – returnNULL May 12 '16 at 15:05

2 Answers2

1

My [0,1,2,3,4,5] = user;

This line is equivalent to saying

My[5] = user;

That's because of the comma operator

//this is an experiment to see whether I can take characters from an array and store it in a variable

You can. But what you are doing right now is assigning an un-assigned value to the 5th element of the My array. int val = My[5] is perfectly fine to do not the other way round though.

The crash is happening because your code is trying to interpret an integer as a null terminated string because of %s format specifier that you gave

printf("%s is also %d", My , user ); // %d for integers

Your code in current state is in Undefined Behavior land.

To copy certain characters from an existing array into the target array you will need more than one variable. A single variable will just hold a single character.

char user[6];
user[5] = '\0'; // fill the last character as null to be a valid C string
user[0] = My[0];
user[1] = My[1];
user[<any other index you want>] = My[<The index you want>];
 

You can save yourself much energy, time and effort by simply using functions from string.h which has bunch of utilities for string manipulation. Probably what you need for your current problem is strncpy

As an orthogonal suggestion, compile your code with the highest warning level supported by your compiler. The compiler will shout out these issues at compile time.

For eg gcc -Wall filename.c

Community
  • 1
  • 1
bashrc
  • 4,725
  • 1
  • 22
  • 49
  • when i try using int val = My[5] then the output comes out as Sayanpbanerjee is also @. And if I take your other idea then the output is Sayanpbanerjee is also 4200816. Now, I might be wrong but, aren't we able to save "strings or text" in an int variable? Then why is this giving me such a weird output? – returnNULL May 12 '16 at 15:08
  • BTW, English isn't my first language so I kind of trip up while explaining things. The original intent of the code was to take certain letters in the array and then put them into a variable and then try to run it in the code. – returnNULL May 12 '16 at 15:14
  • @SayanBanerjee updated my answer to address your comment. – bashrc May 12 '16 at 15:58
  • thanks for the help. I did look at the answer and while it made some sense, I don't think I have the knowledge to pull it off atm. So, I am going to try this again after attending a few more classes! – returnNULL May 13 '16 at 16:45
  • @SayanBanerjee While this is completely unrelated to the question, but I would suggest that instead of taking more classes try writing more code. I don't know your college/course but I do know that these things are best learned outside classroom. – bashrc May 14 '16 at 04:19
0

Following line does not do what you think:

this:

My [0,1,2,3,4,5] = user;

is actually the same thing as this:

My [5] = user;

Read about the comma operator.

Anyway, you cannot store the whole Myarray in a single int variable.


Here you are using %s for printing user which is not a char* but an int

printf("%s is also %s", My , user );

This results in undefined behaviour, most likely a crash on modern systems.


As for your question it is a bit unclear, you maybe want this:

#include <string.h>
...
char user[20];
strcpy(user, My);

printf("%s is also %s", My , user);

which will print:

Sayan Banerjee is also Sayan Banerjee
Community
  • 1
  • 1
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • First things first. Thanks for all the help! I knew a little about strcpy, I don't know Why I didn't use it myself. well the code works, and I can keep it to 15. So why did you specify 20? It's not what I wanted originally,and editing the byte size in user to anything below 15 doesn't give the result I am looking for. What I am looking for is taking out certain characters from an array : maybe the B, N, J, R, S and put it in the variable. Is there any way to do that? Thanks again for all your patience! – returnNULL May 12 '16 at 15:38