0% found this document useful (0 votes)
24 views

23-08 ch-1

The document discusses the basic structure of C programs which includes documentation, preprocessor directives, definitions, global declarations, the main function, and subprograms. It also describes common standard library functions for input/output, strings, memory allocation, and time.

Uploaded by

Krish Kukadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

23-08 ch-1

The document discusses the basic structure of C programs which includes documentation, preprocessor directives, definitions, global declarations, the main function, and subprograms. It also describes common standard library functions for input/output, strings, memory allocation, and time.

Uploaded by

Krish Kukadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction of c

Basic Structure of c
• The basic structure of a C program is divided
into 6 parts.
1. Documentation
2. Pre-processor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
1. Documentation
• This section consists of the description of the
program, the name of the program, and the
creation date and time of the program. It is
specified at the start of the program in the
form of comments. Documentation can be
represented as:
// description, name of the program,
programmer name, date, time etc.

/* description, name of the program,


programmer name, date, time etc. */
2. Pre-processor Section
• The pre-processor section contains all the
header files used in a program. It informs the
system to link the header files to the system
libraries. It is given by:
#include<stdio.h>
#include<conio.h>
• The #include statement includes the specific
file as a part of a function at the time of the
compilation.
3. Define section

• The define section comprises of different


constants declared using the define keyword.
It is given by:

#define a = 2
• Whenever this name is encountered by the
compiler, it is replaced by the actual piece of
defined code.
4. Global Declaration

• The global declaration section contains global


variables, function declaration, and static
variables. Variables and functions which are
declared in this scope can be used anywhere
in the program.
int num = 18;
5. Main() Function
• Every C program must have a main function. The
main() function of the program is written in this
section.
• Operations like declaration and execution are
performed inside the curly braces of the main
program.
• The return type of the main() function can be int
as well as void too.
• void() main tells the compiler that the program
will not return any value.
• The int main() tells the compiler that the
program will return an integer value.
• Example:
void main()
or
int main()
6. Sub Programs
• User-defined functions are called in this section
of the program. The control of the program is
shifted to the called function whenever they are
called from the main or outside the main()
function. These are specified as per the
requirements of the programmer.
int sum(int x, int y)
{
return x+y;
}
Example
#include <stdio.h>

// main function -
// where the execution of program begins

void main()
{

// prints hello world


printf("Hello World");

}
C library functions
• Library functions are built-in functions that are
grouped together and placed in a common
location called library.
• Each function here performs a specific
operation. We can use this library functions to
get the pre-defined output.
• All C standard library functions are declared by
using many header files.
• These library functions are created at the time
of designing the compilers.
Header File Functions
• stdio.h − It is a standard i/o header file in which
Input/output functions are declared
• conio.h − This is a console input/output header
file.
• string.h − All string related functions are in this
header file.
• stdlib.h − This file contains common functions
which are used in the C programs.
• math.h − All functions related to mathematics
are in this header file.
• time.h − This file contains time and clock related
functions.Built functions in stdio.h
Built functions in stdio.h
Sl.No Function & Description

1 printf()
This function is used to print the all char, int, float, string etc., values onto the output screen.

2 scanf()
This function is used to read data from keyboard.

3 getc()
It reads character from file.

4 gets()
It reads line from keyboard.

5 getchar()
It reads character from keyboard.

6 puts()
It writes line to o/p screen.

7 putchar()
It writes a character to screen.
8 fopen()
All file handling functions are defined in stdio.h header file.

9 fclose()
Closes an opened file.

10 getw()
Reads an integer from file.

11 putw()
Writes an integer to file.

12 fgetc()
Reads a character from file.

13 putc()
Writes a character to file.

14 fputc()
Writes a character to file.
LIST OF INBUILT C FUNCTIONS IN
CONIO.H

Functions Description

This function is used to clear the output


clrscr()
screen.

getch() It reads character from keyboard

It reads character from keyboard and


getche()
echoes to o/p screen

This function is used to change the text


textcolor()
color

This function is used to change text


textbackground()
background
string.h Library Functions
Function Name Function Description

strlen() Returns the length of the string.

strcpy() Copy one string to another.

strncpy() Copy first n characters of one string to another.

strcat() Concatenates two strings.

strncat() Concatenates first n characters of one string to another.

strcmp() Compares two strings.

strncmp() Compares first n characters of two strings.


Standard Library Functions - stdlib.h

calloc Allocate and Clear Memory Block

malloc Allocate Memory Block

realloc Resize Memory Block

free Free Memory Block


C Math Functions
1) ceil(number) rounds up the given number. It
returns the integer value which is
greater than or equal to given
number.

2) floor(number) rounds down the given number. It


returns the integer value which is
less than or equal to given
number.

3) sqrt(number) returns the square root of given


number.

4) pow(base, exponent) returns the power of given


number.

5) abs(number) returns the absolute value of given


number.
time.h Library Functions
This function returns the date and time
in the format
day month date
hours:minutes:seconds year.
1. asctime()
Eg: Sat Jul 27 11:26:03 2019.
asctime() function returns a string by
taking struct tm variable as a
parameter.

This function returns the processor


2. clock()
time consumed by a program

This function returns the date and time


in the format
day month hours:minutes:seconds year
3. ctime()
Eg: Sat Jul 27 11:26:03 2019
time is printed based on the pointer
returned by Calendar Time

This function returns the difference


4. difftime()
between the times provided.

You might also like