NEW PROGRAMS

Typedef in C

Typedef

#The C programming language provides a keyword called typedef, by using this keyword you can create a user defined name for existing data type. Generally typedef are use to create an alias name (nickname).

Declaration of typedef

Syntax

 
typedef  datatype alias_name;

Example

 
typedef  int Intdata;

Example of typedef

#include<stdio.h>
#include<conio.h>

typedef int Intdata; // Intdata is alias name of int
void main()
{
int a=10;
Integerdata b=20;
typedef Intdata Integerdata; // Integerdata is again alias name of Intdata
Integerdata s;
clrscr();
s=a+b;
printf("\n Sum:= %d",s);
getch();
}

Output

 
Sum: 20

Code Explanation

  • In above program Intdata is an user defined name or alias name for an integer data type.
  • All properties of the integer will be applied on Intdata also.
  • Integerdata is an alias name to existing user defined name called Intdata.
Note: By using typedef only we can create the alias name and it is under control of compiler.
You can in another example, here myint is alias name for integer data and again smallint is alias name for myint.
typedef

Command LIne Arguments

Command Line Argument

#If any input value is passed through command prompt at the time of running of program is known as command line argument. It is a concept to passing the arguments to the main() function by using command prompt.

When Use Command Line Argument

When you need to developing an application for DOS operating system then in that case command line arguments are used. DOS operating system is a command interface operating system so by using command we execute the program. With the help of command line arguments we can create our own commands.
In Command line arguments application main() function will takes two arguments that is;
  • argc
  • argv
argc: argc is an integer type variable and it holds total number of arguments which is passed into main function. It take Number of arguments in the command line including program name.
argv[]: argv[] is a char* type variable, which holds actual arguments which is passed to main function.

Compile and run CMD programs

Command line arguments are not compile and run like normal C programs, these programs are compile and run on command prompt. To Compile and Link Command Line Program we need TCC Command.
  • First open command prompt
  • Follow you directory where your code saved.
  • For compile -> C:/TC/BIN>TCC mycmd.c
  • For run -> C:/TC/BIN>mycmd 10 20
  • Explanation: Here mycmd is your program file name and TCC is a Command. In "mycmd 10 20" statement we pass two arguments.

Example of Command line argument

#include<stdio.h>
#include<conio.h>

void main(int argc, char* argv[])
{
int i;
clrscr();
printf("Total number of arguments: %d",argc);
for(i=0;i< argc;i++)
{
printf("\n %d argument: %s",i,argv[i]);
getch();
}
}

Output

C:/TC/BIN>TCC mycmd.c
C:/TC/BIN>mycmd 10 20
Number of Arguments: 3
0 arguments c:/tc/bin/mycmd.exe
1 arguments: 10
2 arguments: 20
Note: In above output we passed two arguments but is show "Number of Arguments: 3" because argc take Number of arguments in the command line including program name. So here two arguments and one program name (mycmd.exe) total 3 arguments.

Example of Command line argument

#include<stdio.h>
#include<conio.h>

void main(int argc, char* argv[])
{
clrscr();
  printf("\n Program name  : %s \n", argv[0]);
  printf("1st arg  : %s \n", argv[1]);
  printf("2nd arg  : %s \n", argv[2]);
  printf("3rd arg  : %s \n", argv[3]);
  printf("4th arg  : %s \n", argv[4]);
  printf("5th arg  : %s \n", argv[5]);
getch();
}

Output

C:/TC/BIN>TCC mycmd.c
C:/TC/BIN>mycmd this is a program
Program name : c:/tc/bin/mycmd.c
1st arg : this
2nd arg : is
3rd arg : a
4th arg : program
5th arg : (null)
Explanation: In the above example.

Example

argc      =   5
argv[0]   =   "mycmd"
argv[1]   =   "this"
argv[2]   =   "is"
argv[3]   =   "a"
argv[4]   =   "program"
argv[5]   =   NULL

Why command line arguments program not directly run form TC IDE

Command line arguments related programs are not execute directly from TC IDE because arguments can not be passed.

Edit Command Line Argument Program

To Edit the Command Line Argument Program use edit Command.

Syntax

C:/cprogram>edit mycmd.c
  • Whenever the program is compiled and link we will get .exe file and that .exe file itself is command.
  • In above example program name is mycmd.c so executable file is mycmd.exe and command name is mycmd.
  • To load the application in to the memory we required to use program name and command name.

Access data from outside of main

  • argc and agrv are local variables to main function because those are the main function parameters.
  • According to the storage classes of C argc and argv are auto variable to main function, so we can not extend the range of auto variable.
  • By using argc and argv we can not access command from data outside of the main function.
  • In implementation when we required to access command from data outside of the main function then use _argc, _argv variables.
  • _argc and _argv are global variable which is declared in dos.h.

Example

#include<stdio.h>
#include<conio.h>
#include<dos.h>

void abc()
{
int i;
printf("data in abc:");
printf("\n Total no. of arguments: %d",_argc);
for (i=0;i< _argc;i++)
{
printf("\n %d argument: %s",i+1,_argv[i]);
}
}
void main(int argc, char*argv[])
{
int i;
clrscr();
printf("\n data in main:");
printf("\n total no. of arguments: %d",argc);
for(i=0;i< argc;i++)
{
printf("\n %d arguments:%s",i+1,argv[i]);
}
abc();
getch();
}

Type Casting

Type Casting

#Type casting is process to convert a variable from one data type to another data type. For example if we want to store a integer value in a float variable then we need to typecast integer into float.
Type Casting

Example

#include <stdio.h>
#include <conio.h>

void main()
{
int a,b;
float sum;
clrscr();
printf("Enter two no. ");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum: %f",sum);
getch();
}
Output
Type Casting

Pointers

Pointer in C

#A pointer is a variable which contains or hold the address of another variable. We can create pointer variable of any type of variable for example integer type pointer is 'int *ptr'.
In pointer following symbols are use;
SymbolNmeDescription
& (ampersand sign)Address of operatorGive the address of a variable
* (asterisk sign)Indirection operatorGives the contents of an object pointed to by a pointer.

Advantage of pointer

  • Pointer reduces the code and improves the performance, because it direct access the address of variable.
  • Using pointer concept we can return multiple value from any function.
  • Using pointer we can access any memory location from pointer.

Address Of Operator

The address of operator & gives the address of a variable. For display address of variable, we need %u.

Example

#include<stdio.h>
void main()
{
int a=50;
printf("\nValue of a is: %d",n);
printf("\Address of &n is: %u",&n);
}

Output

Value of a is: 50
Address of a is: 1002

Declaring a pointer

In C language for declared pointer we can use * (asterisk symbol).

Syntax

int *p;  //pointer to integer
char *ch; //pointer to character

Example of pointer

In below image pointer variable stores the address of num variable i.e. EEE3. The value of num is 50 and address of pointer prt is CCC4
Pointer

Example

#include<stdio.h>
int main ()
{
 int num=50;
 int  *ptr; // pointer variable
 ptr = # // store address of variable in pointer
 printf("Address of num variable: %x\n", &num);
 /* address stored in pointer variable */
 printf("Address stored in ptr variable: %x\n", ptr );
 /* access the value using the pointer */
 printf("Value of *ptr variable: %d\n", *ptr );
 return 0;
}

Output

Address of num variable: EEE3
Address stored in ptr variable: CCC4
Value of *ptr variable: 50

NULL Pointer

A pointer that is not assigned any value but NULL is known as NULL pointer. This is done at the time of variable declaration. The NULL pointer is a constant which is defined in standard libraries with zero value.

Example

#include<stdio.h>
int main ()
{
 int  *ptr = NULL;
 printf("Value of ptr is: %x", ptr  );
 return 0;
}

Output

Value of ptr is: 0

Enum in C

Enumeration in C

#An enum is a keyword, it is an user defined data type. All properties of integer are applied on Enumeration data type so size of the enumerator data type is 2 byte. It work like the Integer.
It is used for creating an user defined data type of integer. Using enum we can create sequence of integer constant value.

Syntax

enum tagname {value1, value2, value3,....};
  • In above syntax enum is a keyword. It is a user defiend data type.
  • In above syntax tagname is our own variable. tagname is any variable name.
  • value1, value2, value3,.... are create set of enum values.

It is start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list. If constant one value is not initialized then by default sequence will be start from zero and next to generated value should be previous constant value one.

Read carefully

Example of Enumeration in C

enum week {sun, mon, tue, wed, thu, fri, sat};
enum week today;
  • In above code first line is create user defined data type called week.
  • week variable have 7 value which is inside { } braces.
  • today variable is declare as week type which can be initialize any data or value among 7 (sun, mon,....).

Example of Enumeration in C

#include<stdio.h>
#include<conio.h>

enum ABC {x,y,z};
void main()
{
int a;
clrscr();
a=x+y+z; //0+1+2
printf("Sum: %d",a);
getch();
}

Output

Sum: 3

Example of Enumeration in C

#include<stdio.h>
#include<conio.h>

enum week {sun, mon, tue, wed, thu, fri, sat};
void main()
{
 enum week today;
 today=tue;
 printf("%d day",today+1); 
 getch();
}

Output

3 day
Here "enum week" is user defined data type and today is integer type variable which initialize Tuesday. In above code we add "today+1" because enum is start from 0, so to get proper answer we add "1". Other wise it give "2nd day".

Example of Enumeration in C

#include<stdio.h>
#include<conio.h>

enum week {sun, mon, tue, wed, thu, fri, sat};
void main()
{
for(i=sun; i<=sat; i++)
{
 printf("%d ",i); 
} 
getch();
}

Output

1 2 3 4 5 6 7
In above code replace sun, mon, tue,.... with Equivalent numeric value 0, 1, 2,...