Friday, January 11, 2013

Standard Labrary in C

C Language - Standard Library Functions

In the C Programming Language, the Standard Library Functions are divided into several header files. Below are the header files that we will cover:

Headers

<stdio.h> Input/Output Functions
<string.h> String Functions
<stdlib.h> General Utility Functions
<stdarg.h> Variable Argument List Functions
<assert.h> Diagnostics Functions
<signal.h> Signal Handling Functions

How to use comments in C

C Language: Comments

In the C Programming Language, you can place comments in your code that are not executed as part of the program. A comment starts with /* symbol and ends with */ and can be anywhere in your program. Comments can span several lines within your C program.

Syntax

The syntax for a comment is:
/* comment goes here */
or
// comment goes here

Example - Single Line

You can create an comment on a single line. For example:
/* Author: TechOnTheNet.com */
or
// Author: TechOnTheNet.com

Example - Spans Multiple Lines

You can create a comment that spans multiple lines. For example:
/*
 * Author: TechOnTheNet.com
 * Purpose: To show a comment that spans multiple lines.
 * Language:  C
 */
The compiler will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the C program.

Example - End of Code Line

You can create a comment that displays at the end of a line of code. For example:
#define AGE 6    /* This constant is called AGE */
or
#define AGE 6    // This constant is called AGE
In this example, the compiler will define a constant called AGE that contains the value of 6. Then it interprets everything after the /* symbol as a comment until it reaches the */ symbol.

How to declar variable in C

C Language: Create INT Variables

In the C Programming Language, you can create variables that store integer (int) values.

Syntax

The syntax for declaring an int variable is:
int name1 [= value1];
Or the syntax for declaring multiple int variables is:
int name1 [= value1], [name2 [= value2], ... name_n [= value_n]];
name1 is the name of the first variable to declare.
value1 is optional. It is the value to assign to name1. You do not need to initialize the variable in the declaration statement.
name2, ... name_n are optional. These are additional variable names with the same data type that you wish to declare.
value2, ... value_n are optional. These are the values that you wish to assign to name2 through name_n.

Note

  • Each declaration statement must end with a semicolon.

Example #1 - Declaring a variable

You can define a variable as an integer. For example:
int age;
In this example, the variable named age would be defined as an integer.
Below is an example C program where we declare this variable:
#include <stdio.h>

int main()
{
   int age;

   age = 10;
   printf("TechOnTheNet.com is over %d years old.\n", age);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old."

Example #2 - Declaring a variable and assigning a value

You can define a variable as an integer and assign a value to it in a single declaration. For example:
int age = 10;
In this example, the variable named age would be defined as an integer and assigned the value of 10.
Below is an example C program where we declare this variable and assign the value:
#include <stdio.h>

int main()
{
   int age = 10;

   printf("TechOnTheNet.com is over %d years old.\n", age);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old."

Example #3 - Declaring multiple variables in a statement

If your variables are the same type, you can define multiple variables in one declaration statement. For example:
int age, reach;
In this example, two variables called age and reach would be defined as integers.
Below is an example C program where we declare these two variables:
#include <stdio.h>

int main()
{
   int age, reach;

   age = 10;
   reach = 100;
   printf("TechOnTheNet.com is over %d years old and reaches over %d countries.\n", age, reach);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old and reaches over 100 countries."

Example #4 - Declaring multiple variables in a statement and assigning values

If your variables are the same type, you can define multiple variables in one declaration statement. You can also assign the variables a value in the declaration statement. For example:
int age = 10, reach = 100;
In this example, two variables called age and reach would be defined as integers and be assigned the values 10 and 100, respectively.
Below is an example C program where we declare these two variables and assign their values:
#include <stdio.h>

int main()
{
   int age = 10, reach = 100;

   printf("TechOnTheNet.com is over %d years old and reaches over %d countries.\n", age, reach);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old and reaches over 100 countries."

C Language: Create FLOAT Variables

In the C Programming Language, you can create variables that store floating-point (float) values.

Syntax

The syntax for declaring a float variable is:
float name1 [= value1];
Or the syntax for declaring multiple int variables is:
float name1 [= value1], [name2 [= value2], ... name_n [= value_n]];
name1 is the name of the first variable to declare.
value1 is optional. It is the value to assign to name1. You do not need to initialize the variable in the declaration statement.
name2, ... name_n are optional. These are additional variable names with the same data type that you wish to declare.
value2, ... value_n are optional. These are the values that you wish to assign to name2 through name_n.

Note

  • Each declaration statement must end with a semicolon.

Example #1 - Declaring a variable

You can define a variable as a float. For example:
float age;
In this example, the variable named age would be defined as a float.
Below is an example C program where we declare this variable:
#include <stdio.h>

int main()
{
   float age;

   age = 10.5;
   printf("TechOnTheNet.com is over %f years old.\n", age);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old."

Example #2 - Declaring a variable and assigning a value

You can define a variable as a float and assign a value to it in a single declaration. For example:
float age = 10.5;
In this example, the variable named age would be defined as a float and assigned the value of 10.5.
Below is an example C program where we declare this variable and assign the value:
#include <stdio.h>

int main()
{
   float age = 10.5;

   printf("TechOnTheNet.com is over %f years old.\n", age);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old."

Example #3 - Declaring multiple variables in a statement

If your variables are the same type, you can define multiple variables in one declaration statement. For example:
float age, load;
In this example, two variables called age and load would be defined as float.
Below is an example C program where we declare these two variables:
#include <stdio.h>

int main()
{
  float age, load;

   age = 10.5;
   load = 1.4;
   printf("TechOnTheNet.com is over %f years old and pages load in %f seconds.\n", age, load);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old and pages load in 1.400000 seconds."

Example #4 - Declaring multiple variables in a statement and assigning values

If your variables are the same type, you can define multiple variables in one declaration statement. You can also assign the variables a value in the declaration statement. For example:
int age = 10.5, load = 1.4;
In this example, two variables called age and load would be defined as float and be assigned the values 10.5 and 1.4, respectively.
Below is an example C program where we declare these two variables and assign their values:
#include <stdio.h>

int main()
{
   float age = 10.5, load = 1.4;

   printf("TechOnTheNet.com is over %f years old and pages load in %f seconds.\n", age, load);
   
   return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old and pages load in 1.400000 seconds."

How to create Constants in C

C Language: Create Constants using #define (macro definition)

In the C Programming Language, you can define constants using the macro definition or #define syntax. You generally use this syntax when creating constants that represent numbers or characters.

Syntax

The syntax for creating a constant using #define is:
#define CNAME value
or
#define CNAME (expression)
CNAME is the name of the constant. Most C programmers define their constant names in uppercase, but it is not a requirement of the C Language.
value is the value of the constant.
expression is an expression whose value is assigned to the constant. The expression must be enclosed in brackets if it contains operators.

Note

  • Do NOT put a semicolon character at the end of this statement. This is a common mistake.

Example #1 - Defining a constant using a value

You can define an integer constant (using a value) as follows:
#define AGE 10
In this example, the constant named AGE would contain the value of 10.
You can define a string constant (using a value) as follows:
#define NAME "TechOnTheNet.com"
In this example, the constant called NAME would contain the value of "TechOnTheNet.com".
Below is an example C program where we define these two constants:
#include <stdio.h>

#define NAME "TechOnTheNet.com"
#define AGE 10

int main()
{
   printf("%s is over %d years old.\n", NAME, AGE);
   return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old."

Example #2 - Defining a constant using an expression

You can define a constant (using an expression) as follows:
#define AGE (20 / 2)
In this example, the constant named AGE would also contain the value of 10.
Below is an example C program where we define this constant:
#include <stdio.h>

#define AGE (20 / 2)

int main()
{
   printf("TechOnTheNet.com is over %d years old.\n", AGE);
   return 0;
}
This C program would also print "TechOnTheNet.com is over 10 years old."