Kuppam Engineering College

Welcome

5 Rules for Constructing Variable Names in C Language



A variable is an entity whose value keeps on changing throughout the program execution. As we all know, data is stored in the memory of the computer. Actually, data is not stored in the variable. A variable is the name given to the memory location. A variable name is an entity that points to a particular memory location.
Variable can be of different types.

Rules for constructing variable names
1) A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to your typing effort.
2) The first character of the variable name must either be alphabet or underscore. It should not start with the digit.
3) No commas and blanks are allowed in the variable name.
4) No special symbols other than underscore are allowed in the variable name.

We need to declare the type of the variable name before making use of that name in the program. Type declaration can be done as follows:
To declare a variable as integer, follow the below syntax:
int variable_name;
Here int is the type of the variable named variable_name. ‘int’ denotes integer type.
Following are the examples of type declaration statements:
E.g.: int p, n;
float r;
[Read More...]


1 How to Install Turbo C++ Version 3.0, Compile and Run C Program



In this article, you will get answers to the following questions:

  • How to Install Turbo C++ Version 3.0?
  • How to create a new C Program using Turbo C++ Version 3.0?
  • How to run a C Program using Turbo C++ Version 3.0?
How to Install Turbo C++ Version 3.0?
Installing Turbo C++ Version 3.0 is very easy and effortless. Follow below mentioned easy steps to install Tourbo C:
Congratulation... You are done with installation of Turbo C++ Version 3.0.

How to create a new C Program using Turbo C++ Version 3.0?
  • Open the bin folder ("C:\TC\BIN")
  • Click on "TC" icon as shown below 
  • TC Icon
  • You will see the following screen:
  • Tourbo C Screen
  • Now, Click on File->New. Please find image below for your reference
  • Create new file
  • Write your C Program. Press F2 (or File->Save) to save your program. On pressing F2, pop window will open (as shown below). You need to specify the name of the program.
    Note:
    For C Program, use .C as extension.
    For C++ Program, use .CPP as extension.
  • Save C Program

How to run a C Program using Turbo C++ Version 3.0?
  • Installing Tourbo C is very simple and effortless. Similarly, compiling and running C Program is very simple and effortless.
    Please make a note that, we can compile and run C Programs without saving it.
  • To compile a C Program you can either press Alt+F9 or Compile->Compile. After you compile your C Program you will see the following screen.
  • Compile C Program
  • To run a C Program you can either press Ctrl+F9 or Run->Run. After you run your C Program you will see the output screen as shown below.
  • Run C Program

These are the steps you need to follow to install, create and a run a C Program successfully. In case you have any queries related to installation then kindly post a comment. I will definitely reply to your queries.
[Read More...]


0 The C Preprocessor



The job of the C Preprocessor is to process the source code before it is passed to the compiler. The preprocessor command is also known as directive.

The C Preprocessor

The C Program is often called as source code. Before the program is compiled, the source code goes through one process called preprocessor.

The preprocessor gets the source code (pr.C file) as input and creates expanded source code (pr.I file). This expanded source code is then passed to the compiler for compilation.

Following are the preprocessor directives:
  1. Macro expansion
  2. File inclusion
  3. Conditional Compilation
  4. Miscellaneous directives
Lets discuss these preprocessor directive one by one.

Macro expansion
Let us understand the macro expansion using an example.

Example:
#include<stdio.h>
#define TEN 10
void main()
{
 int a = 10;
 if (a == TEN)
 {
  printf("The value of a is 10");
 }
}
If we run the above example, we will get the output as "The value of a is 10".
In the above example, we have used macro definition. As we learnt before, before compilation process the Source code goes through preprocessing process. During preprocessing, the program is scanned from top to bottom. Every occurrence of TEN in the source code is replaced by 10.

Thus, after preprocessing  the program looks like give below,
#include<stdio.h>
void main()
{
 int a = 10;
 if (a == 10)
 {
  printf("The value of a is 10");
 }
}
The preprocessor directive ‘#define’ can also be used to define operators as shown below.
#define AND &&
#define OR ||

After the definition of these operators we can use AND instead of &&  and OR instead of || within the program. This increases the readability of the program.

The above explained macro is called as ‘Simple macro’. There is another form of macro called ‘Macros with Arguments’.
Let us understand ‘Macros with Arguments’ with the help of an example.

Example:
#include<stdio.h>
#define AREA (3.14  * a * a)
void main()
{
 int ar = AREA(10);
 printf("Area = %f", ar);
}
If we run the above example, we will get the output as “AREA = 314.000000”.

In the above example, we have used macros with arguments. As we learnt, before compilation process the Source code goes through preprocessing process. During preprocessing, the program is scanned from top to bottom. Every occurrence of AREA(10) in the source code is replaced by (3.14 * 10 * 10).
Thus, after preprocessing the program looks like give below.
#include<stdio.h>
void main()
{
 int ar = 3.14 * 10 * 10;
 printf("Area = %f", ar);
}
Macros can be split into multiple lines using back slash (‘\’) as shown below.
#define DISPLAY for(i=0;i<10;i++) \
   printf("This is a macro split into multiple lines");

File Inclusion
File inclusion directive causes one file to be included in another. We have already used file inclusion directive before. The preprocessor command for file inclusion looks like this:

#include “File name”
OR
#include <file name>

E.g. #include<stdio.h>

The above statement in C will include the file “stdio.h” in the program.

If the filename is enclosed within angle brackets, the file is searched for in the standard compiler include paths. If the filename is enclosed within double quotes, the search path is expanded to include the current source directory.

Conditional Compilation
The #if, #ifdef, #ifndef, #else, #elif and #endif directives can be used for conditional compilation.
If the macroname has been #defined, the block of code will be processed as usual.

If we use #if, #ifdef, #ifndef, #else, #elif or #endif directives, it will not be processed as usual. Using these directives we can have compiler to skip over part of a source code.

Let us understand the conditional compilation directives using an example.

Example:
#include<stdio.h>
void main()
{
 #ifdef YES
  Statement 1;
  Statement 2;
 #endif
 Statement 3;
}
In the above example, statement 1 and statement 2 would be processed only if we define macro ‘YES’ else it won’t be processed.
As seen in the above program, macro ‘YES’ has not been defined. Hence, only statement 3 would be processed.

Example:
#include<stdio.h>
#define YES
void main()
{
 #ifdef YES
  Statement 1;
  Statement 2;
 #endif
 Statement 3;
}
In the above example, statement 1, Statement 2 and Statement 3 would be processed as we have defined macro ‘YES’.

Example:
#include<stdio.h>
#define YES
void main()
{
 #ifdef YES
  Statement 1;
  Statement 2;
 #else
  Statement 3;
 #endif
}
The above example can be read as follows:
If macro ‘YES’ is defined then
Process Statement 1 and Statement 2
Else
Process Statement 3
The above example will process Statement 1 and Statement 2.

#ifndef (if not defined) is exactly opposite to #ifdef.
Example:
#include<stdio.h>
#define YES
void main()
{
 #ifndef YES
  Statement 1;
  Statement 2;
 #else
  Statement 3;
 #endif
}
The above example can be read as follows:
If macro ‘YES’ is not defined then
Process Statement 1 and Statement 2
Else
Process Statement 3
The above example will process Statement 3.

The #if directive can be used to test whether an expression evaluates to non zero or not. If the expression evaluates to non zero then the subsequent lines upto #else, #elif or #endif are compiled, otherwise they are skipped.

Example:
#include<stdio.h>
#define VALUE 5
void main()
{
 #if VALUE == 5
  Statement 1;
 #elif VALUE <=10
  Statement 2;
 #else 
  Statement 3;
 #endif 
}
In the above program, if VALUE == 5 returns true then Statement 1 would be processed, else if VALUE <= 10 returns true then Statement 2 would be processed else Statement 3 would be processed.

Miscellaneous directive
#undef directive causes a defined name to become undefined. In order to undefined the macro that has been defined earlier, the directive, #undef macro name can be used. Thus, #undef VALUE would cause the definition of VALUE to be removed from the system.
#pragma directive is another very special and useful directive. This directive is used to specify diverse options to the compiler. These options are specific for the platform and the compiler you use.
If the compiler does not support a specific argument for #pragma, it is ignored - no error is generated. #pragma startup and #pragma exit are most commonly used pragma directives.
#pragma startup allows you to specify a particular function that are called upon program startup (before the execution of main()).
#pragma exit allows you to specify a function that can be called just before the program terminates.

Example:
void display1();
void display2();
#pragma startup display1
#pragma exit display2
void main()
{
 printf(“I am in main”); 
}
void display1()
{
 printf(“I am in function1”);
}
void display2()
{
 printf(“I am in function2”);
}

Output:

I am in function1
I am in main
I am in function2

Please provide your valuable comments about this article or anything related to this website. It would really help me in improving the website.
[Read More...]


0 Structures in C Programming Language



The Structure is a special type of C data type. A structure contains a number of data types grouped together. Structure in C is one of the excellent functionality provided in C. Structure makes C Programming language easy and simpler up to certain extent. Structure is most widely used in Programming.

Structure in C allows multiple data types to be grouped together. As a programmer I have used structures in C a lot and find this feature interesting too. Just go through this article and you too will find structures in C interesting.

Let us suppose you want to store basic information about animal such as age, name, gender and birth place. There are two approaches that can be followed.
1)    Use one array each to store properties of animal as shown below.
int age[10];
char name[10][20] ;
char gender[10];
char bPlace[10][20];

This approach is very tedious as number of characteristics related to animal might increase. Also, we need to keep the track of index of each and every variable. Hence, it becomes very difficult to handle such C programs.

2)    Second approach is using structures in C.
At the end of this article you will understand the ease of using structures and its importance.
Declaring a Structure in C:
Structure in C is declared using the keyword ‘struct’.

Syntax:
struct <structure name>
{
    Element 1;
    Element 2;
    Element 3;
    .
    .
    .
    Element n;   
};
Please don’t forget to include semicolon (;) at the end of structure declaration as shown above.

For example, we can declare structure as follows:
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};

Here,
animal is the name given to structure.
age, name, gender and bPlace are the elements of the structure animal. We can call it as properties of the structure animal.

Declaring Structure variables:
Once the new structure data type has been defined, we can declare one or more variables of that type as shown below:

struct animal a1, a2;

This statement set aside space in memory. 

Following are the ways in which we can declare structure variables.
1)   
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
}a1,a2;

2)
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};
struct animal a1, a2;

3)
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};
struct animal a1={12,”Sam”,’M’,”North America”};
struct animal a2={18,”Maxie”,’M’,”Amsterdam”};

Accessing structure Elements:
Now is the time to access structure elements after declaration of structure type and structure variables.

Syntax:
    <structure variable>.<structure element>

Consider,
struct animal
{
    int age;
    char *name ;
    char gender;
    char *bPlace;
}a1,a2;

Now, element ‘age’ for the variable ‘a1’ can be accessed using the statement,
a1.age;
Similarly, element ‘name’ for the variable ‘a1’ can be accessed using the statement,
a1.name;

Array of Structures:
We can create array of structure variables as follows:
struct animal
{
    int age;
    char *name ;
    char gender;
    char *bPlace;
}a[100];
   
The above declaration can store information of not more than 100 animals.
The values can be accessed as follows:
a[0].age =10;
The above statement will assign value 10 to the element ‘age’ of structure variable a[0].
Similarly,
We can assign values to the elements of other structure variables too.
a[1].age=12;

We can print the values as follows:
printf(“%d”,a[1].age);

The above statement will display 12.

Also See:
[Read More...]


0 Storage Classes in C Programming Language



A storage class is an attribute that tells us where the variable would be stored, what will be the initial value of the variable if no value is assigned to that variable, life time of the variable and scope of the variable.

There are four storage classes in C:
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class

Automatic storage class:
The keyword used for Automatic storage class is 'auto'.
The variable declared as auto is stored in the memory.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Example:
#include<stdio.h>
#include<conio.h>
Void main(){
          auto int a;
          printf(“%d”,a)
}
Output:
1285
As seen above, the output is garbage value.

Register storage class:
The keyword used for Register storage class is 'register'.
The variable declared as register is stored in the CPU register.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Main difference between auto and register is that variable declared as auto is stored in memory whereas variable declared as register is stored in CPU register. Since the variable is stored in CPU register, it takes very less time to access that variable. Hence it becomes very time efficient.
It is not necessary that variable declared as register would be stored in CPU registers. The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as automatic variable.

Example:
#include<stdio.h>
#include<conio.h>
Void main(){
          register int a;
          printf(“%d”,a)
}
Output:
4587
As seen above, the output is garbage value.

Static storage class:
The keyword used for Static storage class is 'static'.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is local to the block in which the variable is defined.
Life of variable persists between different function calls.

External storage class:
The keyword used for External storage class is 'extern'.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is global.
Variable is alive as long as the program’s execution doesn’t come to an end.

External variable can be declared outside all the functions or inside function using 'extern' keyword.
Example:
#include<stdio.h>
#include<conio.h>
int a;
Void main(){
          extern int b;
          printf(“%d %d”,a,b)
}
int b=10;
Output:
0 10
[Read More...]


0 sscanf and sprintf functions




sscanf() function is used to extract strings from the given string. 

Consider,
Char *str = "Learn C Online";
If we want to extract "Learn", "C" and "Online" in a different variable then it can be done using sscanf function.

Syntax:
sscanf(characterArray, "Conversion specifier", address of variables);

This will extract the data from the character array according to the conversion specifier and store into the respective variables.
sscanf() will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab).

Let us understand this using an example.
char *str = "Learn C Online";
char *first, *second, *third;
sscanf(str, "%s %s %s",first,second,third);

In the above example,
"Learn" will get stored in variable first
"C" will get stored in variable second
"Online" will get stored in variable third


sprintf() function is exactly opposite to sscanf() function. Sprint() function writes the formatted text to a character array.

Syntax:
sprintf  (CharacterArray,"Conversion Specifier", variables);

Let us understand this using an example.
char *str;
char *first = "Learn", *second = "C", *third = "Online";
sprintf(str, "%s %s %s",first,second,third);

In the above example, “Learn C Online” will get stored in the character array str
[Read More...]


0 String Handling Functions



Following are some of the useful string handling functions supported by C.
1) strlen()
2) strcpy()
3) strncpy()
4) strcat()
5) strncat()
6) strcmp()
7) strncmp()
8) strcmpi()
9) strncmpi()

These functions are defined in string.h header file. Hence you need to include this header file whenever you use these string handling functions in your program.
All these functions take either character pointer or character arrays as arguments.

strlen()
strlen() function returns the length of the string. strlen() function returns integer value.
Example:
char *str = "Learn C Online";
int strLength;
strLength = strlen(str);    //strLength contains the length of the string i.e. 14

strcpy()
strcpy() function is used to copy one string to another. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strcpy(Destination_String,Source_String);
Example:
char *Destination_String;
char *Source_String = "Learn C Online";
strcpy(Destination_String,Source_String);
printf("%s", Destination_String);
Output:
Learn C Online

strncpy()
strncpy() is used to copy only the left most  n characters from source to destination. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strncpy(Destination_String, Source_String,no_of_characters);

strcat()
strcat() is used to concatenate two strings.
The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strcat(Destination_String, Source_String);
Example:
char *Destination_String ="Learn ";
char *Source_String = "C Online";
strcat(Destination_String, Source_String);
puts( Destination_String);
Output:
Learn C Online


strncat()
strncat() is used to concatenate only the leftmost n characters from source with the destination string.
The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strncat(Destination_String, Source_String,no_of_characters);
Example:
char *Destination_String="Visit ";
char *Source_String = "Learn C Online is a great site";
strncat(Destination_String, Source_String,14);
puts( Destination_String);
Output:
Visit Learn C Online

strcmp()

strcmp() function is use two compare two strings. strcmp() function does a case sensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Syntax:
int strcmp(string1, string2);

This function returns integer value after comparison.
Value returned is 0 if two strings are equal.
If the first string is alphabetically greater than the second string then, it returns a positive value.
If the first string is  alphabetically less than the second string then, it returns a negative value
 Example:
char *string1 = "Learn C Online";
char *string2 = "Learn C Online";
int ret;
ret=strcmp(string1, string2);
printf("%d",ret);
Output:
0

strncmp()

strncmp() is used to compare only left most ‘n’ characters from the strings.
Syntax:
int strncmp(string1, string2,no_of_chars);

This function returns integer value after comparison.
Value returned is 0 if left most ‘n’ characters  of two strings are equal.
If the left most ‘n’ characters of first string is alphabetically greater than the left most ‘n’ characters of second string then, it returns a positive value.
If the left most ‘n’ characters of first string is  alphabetically less than the left most ‘n’ characters of second string then, it returns a negative value
Example:
char *string1 = "Learn C Online is a great site";
char *string2 = "Learn C Online";
int ret;
ret=strncmp(string1, string2,7);
printf("%d",ret);
Output:
0

strcmpi()
strcmpi() function is use two compare two strings. strcmp() function does a case insensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Syntax:
int strcmpi(string1, string2);

This function returns integer value after comparison.
Example:
char *string1 = “Learn C Online”;
char *string2 = “LEARN C ONLINE”;
int ret;
ret=strcmpi(string1, string2);
printf("%d",ret);
Output:
0

strncmpi()
strncmpi() is used to compare only left most ‘n’ characters from the strings. strncmpi() function does a case insensitive comparison.
Syntax:
int strncmpi(string1, string2,no_of_chars);

This function returns integer value after comparison.
Example:
char *string1 = "Learn C Online is a great site";
char *string2 = "LEARN C ONLINE";
int ret;
ret=strncmpi(string1, string2,7);
printf("%d",ret);
Output:
0
[Read More...]


 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy