Variables in C Language - ProgrammerTech
Language:

Search

Variables in C Language

  • Share this:
Variables in C Language

Variables in C

Variables are a reserved place in the computer's memory that contains a certain value, whether the value is text, image, number, ... etc. We can store a value in it while the program is running, and this variable can change its value at any time.

 

Conditions for writing a variable name in C

  1. The variable name must not exceed 32 characters.
  2. The variable name should not be one of the reserved words such as main, scanf, print.
  3. The variable name must not contain symbols such as # , @ , ^.
  4. That the variable name does not contain operators such as (+, -, *).
  5. The variable name should not start with numbers such as 1cs.

 

Data types in C

There are different types of data that we use in programs and in the C language. It is necessary to declare the variables before using them, and before explaining how to declare, we first show the types of data.

 

The main classes of data types in C

Data types fall into two main categories:

  1. Data that the programmer constructs, such as: Array , union , enum , pointer.
  2. The data identified in the origin of the language are of four types, and they are:
  • Valid numeric data integers such as (1,2,3,-1,-8,....) and the word used in the declaration is int and the shortcode %d is used.
  • Numeric data with a floating decimal point such as (1.8, 5.0, 8.9 …..) and the word used in the declaration is float and the short symbol f% is used.
  • The literal data Char such as "A", "B", "X" and the word used in the declaration is Char and the short symbol c% is used.
  • Boolean selection (True, False) and the word used in the advertisement for it is Boolean.

 

Printing numbers in C

Below we will learn how to print numbers of different types:

  1. Print an integer.
  2. Float Printing.
  3. Print a single character char.

 

Integer printing in C

#include<studio.h>
main()
{
    printf("%d" ,10);
}

We wrote the printf function, which is the print function, then we wrote %d, which means printing integer numbers, then we wrote the number 10, and when we run the code, it will print the number 10 for us.

 

Printing a decimal number in C

#include<studio.h>
main()
{
    printf("%f" ,1.2);
}

We wrote the printf function, which is the print function, then we wrote %f, which means printing decimal numbers, then we wrote the number 1.2, and when we run the code, the number 1.20000 will be printed for us.

 

Typing a single character in C

#include<studio.h>
main()
{
    printf("%c" ,'s');
}

We wrote the printf function, which is the print function, then we wrote %c, which means printing a character, then we wrote s, and when we run the code, it will print the letter s.

 

Declaring a variable using C

In this paragraph, we will mention how to declare a variable, whether it is a variable (numerical, text, decimal).

Declaring an integer variable Integer

#include<studio.h>
main()
{
int a;
a=10;
    printf("%d" ,a);
}

We declare the int variable a and then assign a value to the variable a of 10 and then write the function printf to print. And we wrote %d in order to print an integer and then we wrote a ie it prints the value of the variable a and when the program is executed we will get a result of 10.

Example 1

#include<studio.h>
#include<stdlib.h>
    int main (iny argc, char** argv){
    int b = 1;
    printf("%d", b) ;
    return 0;
}

When we run this code, it will print the value of the variable b, which is 1.

Example 2

Write a scalar variable age and then print the age of a person named Muhammad in C.

#include<stdio.h>
int main()
{
    int age = 28;
    printf("Muhammad age = %d", age);
    return 0;
}

In this example, we created a scalar variable int, which is age, and its value is set to 28. Then we asked the program to print the name Muhammad age for us. Then we asked to print the value of the variable age, which is numeric.

Example 3

Write a program that prints the ages of three different people in C.

#include<stdio.h>
int main()
{
    int age1 = 22,
        age2 = 27,
        age3 = 28;
    printf("Kaissar age = %d, Saja age = %d, Muhammad age = %d",
            age1, age2, age3);
    return 0;
}

In this example, we created three numerical variables for age, age1 and its value 22, the second variable age2 and its value 27, and the third variable age3 and its value 28, then we asked the program to print the data formatted in the print function, and when we run the code we will get the result:

Kaissar age = 22, Saja age = 27, Muhammad age = 28

Declaring a Float variable

#include<studio.h>
main()
{
int c;
c=2.3;
    printf("%f" ,c);
}

We have declared the int variable c and then we assign a value to the variable c which is 2.3 and then we write the printf function for printing. And we wrote %f in order to print a decimal number and then we wrote c ie it prints the value of the variable c and when the program is executed we will get a result of 2.300000.

Declaring a literal variable Char

#include<studio.h>
main()
{
char a;
a='s';
    printf("%c" ,a);
}

We define a char variable a and then assign a value to a which is s and then write the function printf to print. And we wrote %c in order to print a character and then we wrote a ie it prints the value of the variable a and when the program is executed we will get the result of s.


Saja Alsadig

Saja Alsadig

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