answersLogoWhite

0


Best Answer

Yes, but it cannot be done in C as intuitively as it can be done in C++:

struct X {

X (const int i): m {i} {} // converts an int to an X [C++ only]

X& operator= (const int i) { m=i; return *this; } // assign an int to an X [C++ only]

int m;

};

Usage:

X x {42}; // construct X from int

x = 0; // assign an int to an X

In C we must use a conversion function:

struct X convert (const int i) { // convert an int to an X

struct X;

x.m = i;

return x;

}

Usage:

struct X x;

x = convert (42);

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

If you want record a file with specific information, for example: Imagine that you want save a file with information about students, so, imagine what type information you will need keep in your file, you don't need think a lot to imagine something such as: ID, name, address, phone, email. So the ideal use of a defined data in C is through the structure, you can put your data in your structure type and save at file. For example:

#include <stdio.h>

#include <string.h>

#define LEN_REC 38

struct students {

char id[2],

name[10],

address[15],

phone[10],

end;

};

int main()

{

FILE *p;

char Str1[ LEN_REC + 1 ];

struct students Student[3];

int i;

memcpy( (void*) Student[0].id,"01",2);

memcpy( (void*) Student[0].name,"Marcos Bre",10);

memcpy( (void*) Student[0].address,"Queen Street,50",15);

memcpy( (void*) Student[0].phone,"6409940349",10);

Student[0].end = '\0';

memcpy((void*) Student[1].id,"02",2);

memcpy((void*) Student[1].name,"Peter Berl",10);

memcpy((void*) Student[1].address,"Queen Street,10",15);

memcpy((void*) Student[1].phone,"6409940344",10);

Student[1].end = '\0';

memcpy((void*) Student[2].id,"03",2);

memcpy((void*) Student[2].name,"James Carr",10);

memcpy((void*) Student[2].address,"Queen Street,30",15);

memcpy((void*) Student[2].phone,"6409940344",10);

Student[2].end = '\0';

if((p=fopen("Students.dat","w"))==NULL){

printf("Error: Open file !\n");

exit(0);

}else{

fprintf(p,"%s",(char*) &Student[0] );

fprintf(p,"%s",(char*) &Student[1] );

fprintf(p,"%s",(char*) &Student[2] );

fclose(p);

}

/* to list your Students */

if((p=fopen("Students.dat","r"))==NULL){

printf("Error: Open file !\n");

exit(0);

}else{

/* List the Students from the file */

while( fread( (void*) Str1 ,1, LEN_REC , p )){

Str1[ LEN_REC + 1 ] ='\0';

printf("%s\n", Str1 );

}

fclose(p);

}

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

No. C is not an object-oriented language so there's no way to provide a conversion constructor for a user-defined type.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can a basic data type be converted into user defined data type in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

Is string is primitive or user defined data type?

String - is primitive data typestring - is user defined data type


Which data type can store any data?

There is no such data type. However, when we use user-defined data types of our own type, then that type of data can be stored in a variable. So as a term, you may say that user-defined data type can store any data. As the data-type used in any variable will be depending upon us.


What is data type of class in oops?

user defined


What is the difference between class data type and basic type in c plus plus .How can someone know which one is class type and which one is basic type.?

Basic types (primitive data types) have no methods associated with them.


Difference between built in data type and user-defined datatype?

A built in data type is a framework's native data type. By default you'd probably have some built in generic data types, such as integer, string, boolean and so on. In the other hand sometimes you can extend the framework's data types, by programminga user-defined data type. In this data type you have to define it's behaviour and structure, and once defined, you can use it the same way you use the default data types. In PostgreSQL or Oracle, you can define data types. You can read about it on their webs.


What is the need for operator overloading?

1. Most fundamental data types have pre-defined operators associated with them. For example, the C++ data type int, together with the operators +, -, *, and /, provides an implementation of the mathematical concepts of an integer. To make a user-defined data type as natural as a fundamental data type, the user-defined data type must be associated with the appropriate set of operators. 2. Increases user readabitily.


Is button a data type in Java?

In Java, just about anything is defined as a class; a class is a data type, so yes.


What are the basic data type and subtype used by C compiler .explain properly?

Basically data types are divided into three types namely, 1. primary data type -&gt; this is sub divide into int, float, char, void. 2. derived data type -&gt; this is sub divide into array, pointer. 3. user defined data type -&gt; this is sub divide into struct, union, enum, typedef. by, k.p.sruthi


What is categorical data?

Categorical data is the statistical data type consisting of categorical variables or of data that has been converted into that form, for example as grouped data.


What are user defined data types?

Actually user defined data type made by user like array,structure,union,pointer. the data type which is define by user or programer according to his need is called user define data type and also called the programer define data type........


Application of array in data structure?

That rather depends on the underlying type. For any given type T, an array of type T is user-defined if T is user-defined, otherwise it is built-in. For example: #include&lt;string&gt; // required to use std::string std::string s[42]; // user-defined array int i[42]; // built-in array Here, s is a user-defined array because std::string is a user-defined type, whereas i is a built-in array because int is a built-in type.


What is the default data type for Visual Basic?

A variable has a data type such as integer, string, double. A data type tells the variable to only store values that are a particular data type, so you can only store numbers without decimal points in an integer variable, and only characters such as "ABCD" in a string variable.