Union

The Union is same like  Structure in which all members are stored at the same address. Members of a union can only be accessed one at a time. The union data type was invented to prevent memory fragmentation. The union data type prevents fragmentation by creating a standard size for certain data.

Just like with structures, the members of unions can be accessed with the. and -> operators. When the computer allocates memory for a program, it usually does so in one large block of bytes. Every variable allocated when the program runs occupies a segment of that block. When a variable is getting freed, it leaves a “hole” in the block allocated for the program. If this hole is of an unusual size, the computer may have difficulty allocating another variable to “fill” that hole, thus leading to inefficient memory usage.

Since unions have a standard data size, however, any “hole” left in memory by freeing a union can be filled by another instance of the same type of union. A union works because the space allocated for it is the space taken by its largest member; thus, the small-scale memory inefficiency of allocating space for the worst case leads to memory efficiency on a larger scale.

A union is declared in the same way as a structure. It has a list of members, as in the example below:
union customer
{
char *name;
int number;
};

Declaring union variables is similar to declaring structure variables, if you have not read structure before, please visit our simplified explanation for structure here
union customer customer1;

The difference between union and structure:

Structure Union
We can access all the members of a structure at any time. Only one member of a union can be accessed at any time.
Memory is allocated for all variables. Allocates memory for a variable which variable require more memory.
All members of the structure can be initialized Only the first member of a union can be initialized.

typedef:

The C programming language provides a keyword called typedef, which you can use to define your own data type. Following is an example to define a term myDatatype for int

typedef int myDatatype

After this definition myDatatype can be used for int.

Like this,

myDatatype a = 10;

 

 

Pointers

Pointer is an important concept in C programming Language, you should have a very good understanding of pointers. For most people, it will take some time to fully understand pointers. So be patient. You have to learn pointers because they are used everywhere in the C language.

So why use pointers? Why don’t we use arrays to create data structures?
With an array, you have to declare its maximum size at the beginning. But with a pointer memory allocated at runtime.There is a case, in which you have to use 10 megabytes initially and later 15 and after that 20, If you create an array for this with 20 megabytes size, remaining 10 Mb storage will be wasted. With pointers, you can create dynamic data structures. Instead of claiming the memory up-front, the memory is allocated while the program is running. So the exact amount of memory is claimed and there is no waste.

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.

Declaration:

To declare a pointer you have to put an asterisk * in front of its name. A pointer can be typed or untyped. (A typed pointer points to a particular variable type such as an integer. An untyped pointer points to any data type).

syntax

data_type *variable_name;

int *ptr_A;   // typed pointer

void *ptr_A; // untyped pointer

There are few important operations, which we will do with the help of pointers very frequently.

  •  we define a pointer variable
  • assign the address of a variable to a pointer
  •  finally, access the value at the address available in the pointer variable.

This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations:
#include
int main ()
{
int  normal_var = 100;   /* actual variable declaration */
int  *ptr;        /* pointer variable declaration */
ptr= &normal_var;  /* store address of var in pointer variable*/
printf("Address of normal_var variable: %x\n", &normal_var);
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ptr);
/* access the value using the pointer */
printf("Value of *ptr variable: %d\n", *ptr);
return 0;
}
Address of normal_var variable: accfkk
Address stored in ptrvariable: accfkk
Value of *ptrvariable: 100

Pointer is an important concept, so you have to spend more time with it, and need to study clearly, in this we are giving you only basics in the pointer.we kindly advice you study pointers in detail with the help of the other reference If time and your positive reviews come to our hand, we will give separate tutorial for pointer.Best of Luck.

 

Recursion

Recursion Program in C

Factorial program in c using recursion:

Recursion is a technique in which a function calls itself, for example in above code factorial function is calling itself.

#include

long factorial(int);

int main()
{
int n;
long f;

printf(“Enter an integer to find factorial\n”);
scanf(“%d”, &n);

if (n < 0)
printf(“Negative integers are not allowed.\n”);
else
{
f = factorial(n);
printf(“%d! = %ld\n”, n, f);
}
return 0;
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

Pascal Triangle

Pascal Triangle Program in C

Pascal Triangle:

#include
long factorial(int);
int main()
{
int i, n, c;
printf(“Enter the number of rows you wish to see in pascal triangle\n”);
scanf(“%d”,&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n – i – 2 ) ; c++ )
printf(” “);
for( c = 0 ; c <= i ; c++ )
printf(“%ld “,factorial(i)/(factorial(c)*factorial(i-c)));
printf(“\n”);
}
return 0;
}

long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}

output:

Enter the number of rows you wish to see in pascal triangle
4
    1
1 1
1 2 1
1 3 3 1

Palindrome

Palindrome program in C

Palindrome :

 

Palindrome number in c

#include

main()
{
int n, reverse = 0, temp;

printf(“Enter a number to check if it is a palindrome or not\n”);
scanf(“%d”,&n);

temp = n;

while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}

if ( n == reverse )
printf(“%d is a palindrome number.\n”, n);
else
printf(“%d is not a palindrome number.\n”, n);

return 0;
}

C program for palindrome without using string functions

#include
#include

int main()
{
char text[100];
int begin, middle, end, length = 0;

gets(text);

while ( text[length] != ‘\0’ )
length++;

end = length – 1;
middle = length/2;

for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf(“Not a palindrome.\n”);
break;
}
end–;
}
if( begin == middle )
printf(“Palindrome.\n”);

return 0;
}

C program for palindrome
#include
#include

int main()
{
char a[100], b[100];

printf(“Enter the string to check if it is a palindrome\n”);
gets(a);

strcpy(b,a);
strrev(b);

if( strcmp(a,b) == 0 )
printf(“Entered string is a palindrome.\n”);
else
printf(“Entered string is not a palindrome.\n”);

return 0;
}

Introduction to Swift Programming

   Apple introduced a new programming language for IOS and OS X developers called Swift. Our Swift tutorial for iOS developers & beginners is a simplified best tutorial for learning swift from basics and gives the clear understanding of apple’s Swift language. Despite the new name, Swift is compatible with the root of its predecessors like C, Objective-C, and Cocoa Touch framework.If you have been programming with Objective-C, learning Swift would be a breeze as it has inherited a number of syntaxes that you may already have been familiar with. And if learning Swift is on your to-do list but you haven’t started yet, We are providing simplified tutorials for learning Swift

We will update this tutorial regularly to add/modify contents. Please visit regularly.

Setup:

There are few options to have hands on.

1. XCode:

We have to download and install Xcode 8. Once you have installed it, open and select File from the

menu -> New -> Select Source

on the left under either iOS or OS X -> Playground. Give your playground name. That’s it, you are ready to get started.

2. Terminal:

If we have two or more versions of Xcode installed then you will need to select Xcode 6 and above as our default version. If we are using Xcode 6 and above only, then skip this step and ahead to step 3, otherwise go ahead and run the following line:

sudo xcode-select -s /Applications/Xcode6-Beta.app/Contents/Developer/

Please check your app name in the “Applications” folder to write out the appropriate path when using xcode-select.

3. To start the REPL type:

xcrun swift

3. Online compiler for learning Swift

 Go to the below site, it has an online compiler for swift.

http://www.runswiftlang.com/

Android Application Class


Application class is the base class where you can maintain global variables.
 One can override the Application class by extending his own class  and 
specifying that class in AndroidManifest.xml’s tag.
Application class exists through out the app life cycle.
 Creating a static singletons can serve the purpose instead of extending and overriding Application class.
 But if one needs to manage other memory leaks he can override Applications class methods.import android.R.string;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;

public class myAppClass extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
} @Override
public void onCreate() {
super.onCreate();
}

@Override
public void onLowMemory() {
super.onLowMemory();
}

@Override
public void onTerminate() {
super.onTerminate();

}

}

this is lifecycle code in Android Programming
main advantage of application class is , you can access global variable from anywhere in your project without loss, you may ask that we can accomplish this using activity , but difference is when you move from one activity to another one , there is difficult in that, in that case if u extend Application class, you can do that process as follows
public class myAppClass extends Application {
   
        public static String globalWord = “HI DEVS”;
        private static myAppClass Singleton;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
} @Override
public void onCreate() {
      super.onCreate();
              Singleton = this;   // create singleton
}
public static myAppClass getSingleton()  // access singleton
{
        return Singleton;
}
}


Note:  add this class in  AndroidManifeist file as below
<application
        android:name=”.myAppClass”
        android:icon=”@drawable/ic_launcher”
        android:label=”@string/app_name”
        android:theme=”@style/AppTheme”
        >

       

Feedback and comments expexted as usual