Вы находитесь на странице: 1из 27

Header file

Header file contains different predefined functions, which are required to run the program. All header files should be included explicitly before main ( ) function. It allows programmers to seperate functions of a program into reusable code or file. It contains declarations of variables, subroutines.

stdio.h (Standard Input/Output Header File): stdio.h refers to standard input/output header file. it is header file in C's standard library which contains constants, macros definitions and declarations of functions. It includes types used for various standard input and output operations. The functions which are declared in stdio.h are very popular.

Function Name Description scanf used to take input from the standard input stream gets read characters from stdin while a new line is inserted printf prints to the standard output stream putc writes and returns a character to a stream putchar It works as same of putc(stdout) puts outputs a character string to stdout fopen Opens a file to read or write fwrite writes data to a file fputs writes a string to a file fread reads data from a file fseek seeks file fclose Closes a file remove deletes or removes a file rename renames a file . Rewind: adjusts the specified file so that the next I/O operation will take place at the beginning of the file. "rewind" is equivalent to fseek(f,0L,SEEK_SET);

stdlib.h (Standard Library header file):


'stdlib' is an acronym for Standard Library. It is header file of standard library in C which contains the functions like allocation of memory, conversions, process controls and other utilities. Some of the standard member functions of stdlib.h header files are

Function Name Description abs It returns the absolute value of a number. atof It converts string into double value. atoi It converts string into integer value. atol It converts string into long integer value. abort It terminates execution as abnormally. exit It terminates execution of a program. malloc It allocates memory from heap.

1. 2. 3. 4. 5. 6. 7.

#include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { char no[10]; clrscr(); printf("\n\t Using abs(-194): %d",abs(-194));

8. printf("\n\n\t Enter any number : "); scanf("%s",no); 9. printf("\n\t Using atof() : %lf",atof(no)); 10. printf("\n\n\t Using atoi() : %d",atoi(no)); 11. getch(); 12. }

Output
Using abs(-194) : 194 Enter any number : 12 Using atof() : 12.000000 Using atoi() : 12_

Macros #define
Macros are the identifiers that represent statements or expressions he major use of the preprocessor is to define macros. The advantage of a macro is that it can be type-neutral (this can also be a disadvantage, of course), and it's inlined directly into the code, so there isn't any function call overhead. #define directive is used. You can see in the given example, we have define a macro i.e SQUARE(x) x*x. Here the macro determines the square of the given number. Macro Declar: #define name text

Square of number
#include <stdio.h> #include <conio.h> #define SQUARE(x) x*x int main (){ int i = 2; int j = SQUARE(i); printf ("The value of j is: %d\n", j); getch(); return 0; }

#include<stdio.h> #define max(a,b) ((a > b)?(a):(b)) void main() { int x=5,y; y=max(x,2); printf("max element (5,2)is: %d\n",y); printf("max element in (7,5) is: %d\n",max(7,x)); }

#define MULT(x, y) x * y and then wrote int z = MULT(3 + 2, 4 + 2); what value do you expect z to end up with? The obvious answer, 30, is wrong! That's because what happens when the macro MULT expands is that it looks like this: int z = 3 + 2 * 4 + 2; // 2 * 4 will be evaluated first! value 13. You can do this by surrounding them by parentheses in the macro definition: #define MULT(x, y) (x) * (y) // now MULT(3 + 2, 4 + 2) will expand to (3 + 2) * (4 + 2)

This header is the most widely and commonly used. It specifies the file to which the linker links the preprocessor of the code for source inclusion. There are two ways to specify a file to be included: Code: #include "file.h #include <file.h> The difference between both expressions is the places (directories) where the compiler is going to look for the file. In the first case where the file name is addressed in double-quotes, the file is searched first in the same directory that includes the file containing the directive. In case that it is not there, the compiler searches the file in the default directories where it is configured to look for the standard header files. If the file name is enclosed between angle-brackets <> the file is searched directly where the compiler is configured to look for the standard header files. This is why, standard header files are usually included in angle-brackets, while other specific header files are included using quotes.

It contains the declarations for character functions i.e. it contains information used by the character classification and character converstion macros. Some of the standard member functions of ctype.h header files are, Function Name Description isalnum - checks for alphanumeric character. isalpha - checks for alphabetic character. isxdigit - checks for hexadecimal digit. isupper - checks for upper case character. isspace - checks for any whitespace character. ispunct - checks for punctuation character. isdigit - checks for digits. islower - checks for lower case characters. isprint - checks for printable character with space character. isgraph checks for graphic character without space character.

Example : /* Program to demonstrate ctype.h header file working. #include <stdio.h> #include <conio.h> #include <ctype.h> #include <string.h> void main() { int len, i; char *str = "TECHNOEXAM"; clrscr(); len = strlen(str);

for(i=1;i<=len;i++) { str[i] = tolower(str[i]); //tolower() } printf("\n\t Using tolower() : %s"<,str); for(i=1;i<=len;i++) { str[i] = toupper(str[i]); //toupper() } printf("\n\n\t Using toupper() : %s",str); getch(); } Output : Using tolower() : Technoexam Using toupper() : TECHNOEXAM_

math.h is a header file which is commonly used for mathematical operations. Some functions of this header file uses floating point numbers. The functions which accepts angle are accepted in terms of radians. Some of the standard member functions of math.h header files are, Function Name Description acos - Arccosine - Returns inverse cosine. cos - Returns cosine. log - Returns log of a number. pow(x,y) - Returns power of x raise to y. i.e. xy sqrt - Returns square root of a number. tan - Returns tangent. ceil - Ceiling - Small int not less than that of parameter. exp - Uses as an Exponential function. floor - Floor - Largest int not greater than parameter.

/* Program to demonstrate math.h header file working. #include <stdio.h> #include <conio.h> #include <math.h> void main() { clrscr(); printf("\n\t Log of 10 : %f",log(10)); printf("\n\n\t Square Root of 16 : %f",sqrt(16)); printf("\n\n\t Square of 4 : %f",pow(4,2)); printf("\n\n\t Sine of 10 : %f",sin(10)); getch(); }
Output : Log of 10 : 2.302585 Square Root of 16 : 4.000000 Square of 4 : 16.000000 Sine of 10 : -0.544021_

# define directive in c This directive is also called as Macro substitution directive. Syntax: #define <Macro_constant> [(<Para1>, <Para2>,...)] <Token_string> Note: [] indicates optional term. Task of macro substitution directive is to replace the identifier with corresponding Token_string. For example: #include<stdio.h> #define pie 3.14 void main() { float r=3,area; area = 3 * r * pie; printf("%f",area); } In the above c code we have defined a macro constant pie. Before the starting of actual compilation an intermediate is formed which is:

The header file provides all date and time functions. In the given example, the time() function determines the current time in seconds and assigns it to time_t type variable t. C Current Time In this section, you will learn how to get the current time in C. The header file <time.h> provides all date and time functions. In the given example, the time() function determines the current time in seconds and assigns it to time_t type variable t. The ctime() function converts the number of seconds to the current date and time. The printf() function then prints the current date and time.

#include<stdio.h> #include<conio.h> #include<time.h> main() { time_t t; time(&t); printf("The current Date and Time is: %s\n",ctim e(&t)); getch(); }

#include <time.h> #include <stdio.h> #include <conio.h> void main() { time_t t; time(&t); printf("The current Date and Time is: %s\n", ctime(&t)); getch(); }

#include directive in c
#include <filename> or #include filename Task of include directive is to include or import any type file. There are two form of include directive.

#include<filename>: This statement will search the file in include directory (Default location of include directory is c:\tc\include). If that file is present then #include statement is replaced by content of file. If that file is not present then it will cause of compilation error. For example: (In turbo C 3.0)
//test.c

#include<stdio.h> #include<conio.h> void main(){ clrscr(); printf("HOW large is this file?"); getch(); }

In first look you may think above c program content only seven lines. But it is not true. If you will see its intermediate file first two include statement has been replaced by content of file stdio.h and conio.h respectively.

Myfile.c location: c:\tc\bin\raja int x; int max(int a,int b){return a>b?a:b;}

Write the following c code: #include<c:\tc\include\stdio.h> #include<c:\tc\bin\raja\myfile.c> int main(){ x = max(20,40); printf("%d",x); return 0; } Step4: Save above c code any where you want then compile and execute the above code. Output: 40

#include filename
This statement will first search the given file any current working directory and if it is not present then it will search include directory if it is also not present there then it will cause of compilation error, unable to open the file. If that file is present then include statement is replaced by content of file. Example 1: If file is present in the current working directory. Suppose my current working directory is c:\tc\bin //c:\tc\bin\one.c int a=5; int b=10; //c:\tc\bin\two.c #include"stdio.h" #include"one.c" int main(){ printf("a=%d b=%d",a,b); return 0; } Output: a=5 b=10

Вам также может понравиться