Tuesday, July 14, 2009

Some questions in C programming for string and array?

In C, What is the diffrence between a string and an array?





Is it possible to execute code even after the program exits the main() function?


Is using exit() the same as using return?





What do you mean by binding of data and functions?

Some questions in C programming for string and array?
In c, typically, a string is a type of array (specifically it's an array of characters):





int myArray[100]={12};


char myString[100]="Hello!";





The difference is pretty much the same as the difference between an animal and a squirrel, i.e. One is just a type of the other.





I'm not sure if it's possible in C to have code execute after the end of the main function, but it's certainly possible in C++:





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;





class MyClass


{


~MyClass()


{


printf(" World");


getch(); // Wait for a keypress...


}


};





MyClass c;





int main()


{


printf("Hello");


return 0;


}





What happens is that after the program ends, the global instance of MyClass, c, is destroyed, so it'll call it's destructor. If you run the above code you should see it print out the words "Hello World" (unless I've messed up somehow :) )





exit() is certainly not the same as return. exit() stops the entire program from running, whereas return just ends the current function being called. If you call return from the main() function, then it is pretty much the same though.





When people talk about binding data and functions, they normally mean building classes. Imagine that you have some data about people you want to keep (say, just their age and their name). So you can have two arrays, one of strings, one of numbers, to store the data. Then you can write some functions which use those arrays to print out the names and ages, or whatever. The alternate is to make a class called "Person" that contains a string and a number and has member functions for printing them out, in this way we have bound the data and functionality together.
Reply:I just checked, and I made a mistake in the code above. For it to compile you need to change:





class MyClass


{


~MyClass()





to





class MyClass


{


public:


~MyClass() Report It

Reply:I will try to keep it simple:





1. String is one datatype in which you can store one alphanumeric data of max 256 characters (Integers stores only numbers). Array is a datatype which can be classified as a string or integer or others depending on what kind of data you store in an array. Array is a collection of strings or other form of data. There are single dimention array, two dimensional array and multiple dimention array.





A program can exit the main function to go to another function, however if you exit main function as in end of function you cannot go to any other function. its end of program.





Exit function is not same as return. Return can allow you to come back to main function or go to another function. Exit function terminates the program/function.





Not sure about your last Q.
Reply:in C a string is an array of chars: for example





char* c = "the";


is the same as


char c = ['t','h','e','\0'];








when the main function exits the program will end.


using exit is generally used for errors, while return is generally used for normal execution
Reply:A string is an array of chars, an array can be integers, or any other data type,





It is possibole to execute code after the main function, but extremely difficult in C. You have to push code into memory, and then transfer control over to it, similiar to what you would do in the Assembly language. Most C programmers just use MAIN as a control function, and do all of the work in sub functions.





Binding of data and functions is an attempt at Object Oriented programming before C++.
Reply:check this link


it might help


http://www.google.co.in/url?sa=t%26amp;ct=res%26amp;...





regards


Islam Inamdar


islaminamdar@yahoo.com


inamdarinfotech.com


join new randomizer website


www.allyours.info
Reply:The difference is that a sting holds anything. It can be this "Junk","1234", "junk1234". You can't do calculations. With an array, it's like a place holder. It's usually like this. int stuff[10]. You use it to store information in it.





I don't think you can execute code after the Main(). However you can do a bunch of calls and functions with classes to do stuff in the main to clean it up. The rest i'm not sure.

wedding florist

No comments:

Post a Comment