Reputation: 19
#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.
Upvotes: 2
Views: 113
Reputation: 4835
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
Upvotes: 1
Reputation: 50778
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 My
array 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
Upvotes: 0