c++创建c语言头文件_如何在C / C ++中创建自己的头文件?

本文探讨了在C/C++中创建自定义头文件的必要性,并提供了简单的方法来实现这一过程。强调了头文件在代码组织和重用中的作用。
摘要由CSDN通过智能技术生成

c++创建c语言头文件

In this article I am going to tell you the easiest way to
create your own header files in programming languages like C and C++. For this
you do not have to be an expert. This can be done by anyone who has just
started learning programming languages. Ok! Before starting the process let me
tell you the thing that why we need to create our own header files.

Why we need to create our own header files?

When we are working on big projects, we create many
functions to perform particular task. But this makes the source code very long.
So to solve this kind of problem we create a header file with all those
function and include this header file in our program. This makes the program
shorter, effective and easy to understand. Now I am sure that you understand
the purpose of creating our own header files.

Simple way to create your own header files in C/C++

1. Open notepad and write the function that you want to use
in your program. An example is shown below.
int sum(int a,int b)
{
                return(a+b);
}
2. Now save the notepad file with .h extension. Like in above
example we are creating a function for sum, so save this file with name sum.h in
INCLUDE or BIN folder (you can use any other name).
3. After that write a program that uses this sum function and
include the header file that you have just created. I have written the program
below to make it easy to understand.
#include<stdio.h>
#include<conio.h>
#include<sum.h>                             //header
file created by you
void main()
{
                int
a,b,s;
                clrscr();
                printf(“Enter
the value of a and b:”);
                scanf(“%d%d”,&a,&b);
                s=sum(a,b);
                printf(“Sum=%d”,s);
                getch();
}
4. In this way you can add more functions to your header
file.
Note: Do not use long header file name (about 7 to 8
characters), otherwise you will get an error. Write only the function
definition in the header file, there is no need to write function prototype.
If you have any kind of problem regarding the whole process
than feel free to ask by commenting below.
In this article I am going to tell you the easiest way to
create your own header files in programming languages like C and C++. For this
you do not have to be an expert. This can be done by anyone who has just
started learning programming languages. Ok! Before starting the process let me
tell you the thing that why we need to create our own header files.

Why we need to create our own header files?

When we are working on big projects , we create many
functions to perform particular task. But this makes the source code very long.
So to solve this kind of problem we create a header file with all those
function and include this header file in our program. This makes the program
shorter, effective and easy to understand. Now I am sure that you understand
the purpose of creating our own header files.

Simple way to create your own header files in C/C++

1. Open notepad and write the function that you want to use
in your program. An example is shown below.
int sum(int a,int b)
{
                return(a+b);
}
2. Now save the notepad file with .h extension. Like in above
example we are creating a function for sum , so save this file with name sum.h in
INCLUDE or BIN folder (you can use any other name).
3. After that write a program that uses this sum function and
include the header file that you have just created. I have written the program
below to make it easy to understand.
#include<stdio.h>
#include<conio.h>
#include<sum.h>                             //header
file created by you
void main()
{
                int
a,b,s;
                clrscr();
                printf(“Enter
the value of a and b:”);
                scanf(“%d%d”,&a,&b);
                s=sum(a,b);
                printf(“Sum=%d”,s);
                getch();
}
4. In this way you can add more functions to your header
file.
Note: Do not use long header file name (about 7 to 8
characters), otherwise you will get an error. Write only the function
definition in the header file, there is no need to write function prototype.
If you have any kind of problem regarding the whole process
than feel free to ask by commenting below.

翻译自: https://www.thecrazyprogrammer.com/2013/06/how-to-create-your-own-header-files-in.html

c++创建c语言头文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值