Constants and comments in C - ProgrammerTech
Language:

Search

Constants and comments in C

  • Share this:
Constants and comments in C

Constants in C

Constants is a fixed value that cannot be changed after being defined in the program and its value does not change throughout the program execution period, and a constant is like a read-only memory and this value is read-only. int ,float ,char".

 

Declaring constants in C

There are two ways to declare constants:

The first method: is to add the #define keyword to the definition before the main function.

#define int 101

Example 1

#include<stdio.h>
#define num 7
main()
{
printf("%d" ,num);
}

In this example, we declare the constant with #define num 7, then we write the printf function for printing, and we put %d because we want to print a number, not a letter or a decimal number, then we write num that we declare the constant with, and When you run the code, it will print the number 7.

The second method: It is close to declaring the variable, but the difference is by adding the word const, meaning constant, before the declaration, as in the following example:

#include<stdio.h>
main()
{
const int n=7;
printf("%d" ,n);
}

We defined the constant by writing the const function and then we wrote int so that this constant is an integer and its value is 7. And we wrote the printf function for printing and we put %d because we want to print a number and not a letter or a decimal number and we wrote n Which is the constant, and when you run this code it will print the value of the constant which is 7.

 

Comparison of variables and constants

In what follows, we will learn about the differences between variables and constants, and we will attach examples that illustrate these differences.

variables in C

Its value can be changed while the program is running, unlike the constants. In the following example, we will clarify this matter.

#include<stdio.h>
main(){
int db=1;
printf("%d" ,db);
db=2;
printf("%d" ,db);
}

We note that the variable that has the name db is of type int. We gave it two values, the first being 1, and we told him to print it, and we gave him the second value, which is 2, and we told him to print it, so the result of this operation will be 12, meaning the number 1 and the number 2 printed on the same line because we did not We drop a line by \n.

Const in C

Let's do the same movement in the previous example and see what the output will be in this case

#include<stdio.h>
main(){
const int db=1;
printf("%d" ,db);
db=2;
printf("%d" ,db);
}

The output when executing prints an Error.

 

Comments in C

Comments are a set of text strings in addition to numbers, letters, symbols written to clarify commands in the source program and the compiler ignores them and does not execute in the code, and writes the comment by adding two slashes // then we write the comment we want and the comments are divided into two types And they are comments with long texts and comments with lines.

 

Comments in lines

Comments are written in one line by writing two slash tags next to each other // and writing the comment after it, as in the following code.

//My First Program
//Programmer Tech
#include<stdio.h>
main(){
printf("Programmer_Tech");
}

But if we write words without putting the slash mark twice, this is not considered a comment, and the editor will consider it a programming error and put a flag next to it.

 

Comments in long texts

This type of Comments is done by writing the slash symbol and then the asterisk /* and then writing the comment we want with lines. Multiple then at the end we lock it with the star symbol and then slash */ and here it indicates the end of writing the comment or note.

/* My First Program
Programmer Tech */
#include<stdio.h>
main(){
printf("Programmer_Tech");
}

We also note that between /* and */ is considered a note and is not read by the language translator or interpreter.


Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان..