Reputation: 21
This is my first question on this site.
How do i assign string of one variable to other variable. What am i doing wrong here?
#include<stdio.h>
#include<string.h>
main(){
char a[30],b[30];
scanf("%s",a);
b[30]=a[30];
printf("%s",b);
}
Upvotes: 2
Views: 1133
Reputation: 162
#include<stdio.h>
#include<string.h>
main(){
char a[30],b[30];
scanf("%s", a);
strcpy(b, a); //header file <string.h>
//strcpy(destination, source)
printf("%s",b);
}
The strcpy() function will copy the content of string a in string b.
Upvotes: 1
Reputation: 311058
Use the standard C function strcpy
declared in the header <string.h>
. For example
strcpy( b, a );
Arrays do not have the assignment operator.
As for your statement
b[30]=a[30];
then b[30]
and a[30]
are undefined objects of the type char that are beyond the arrays.
Upvotes: 5