C Programming

2-Variables and Data Types in C Programming Language

C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C

In this topic, we will look at variables, data types and example of arithmetic operations in C language and run simple programs.

In C language, arithmetic operations are done through the following operators;

Addition +                          f+7

Subtraction –                     p-c

Multiplication *                 b*m

Division /                            x/y

Mod %                                 r%s

Example ; a*(b+c)

1) Parentheses take precedence

2) Multiplication, division, mode priorities are equal. If there is more than one, it is done from left to right.

3) Addition and subtraction are done last, their priorities are equal, and if there is more than one, they are done from left to right.

It is necessary to write x*x instead of x2.

Exponential operations are performed with the pow(power) function (will be discussed in future lessons).

Let’s write our first arithmetic program;

/* program for addition */

#include <stdio.h>
int main()
{
    int Integer1,Integer2,sum;   //defining variable types, decleration

    printf("Enter the first integer: ");  //printing message
    scanf("%d", &Integer1);  //reading first integer, & is address operator

    printf("Enter the second integer: ");  //printing message
    scanf("%d", &Integer2);  //reading second integer

    sum = Integer1 + Integer2; //addition operation

    printf("Result of addition is : %d\n", sum); //printing of result of the addition

    return 0;  //indicate that the program has successfully terminated
}

*Please refer to the first topic of this subject for installing a compiler.

addition in c language
arithmetic operations in c
data types
variables
Variables and Data Types in C
addition in c language

“int” defines variable types. It specifies integer values such as 7, -11, 0, 3, 2194.

For variables to be used in the program, they must be declared with a name and variable type, followed by the { sign that indicates the start of the main function.

The size of “int” is 4 bytes and can range from -2,147,483,648 to 2,147,483,647 for signed values and 0 to 4,294,967,295 for unsigned values.

*Hint:

C has lowercase and uppercase letters. For example, it is wrong to write Main instead of main.

The %d conversion specifier specifies that the number must be an integer.

The calculation can also be performed inside the printf statement;

printf(“Result of addition is: %d \n”, Integer1+Integer2);

Data Types and Their Ranges

data types in c language
variables
ranges
qualifier in c language
data types in c language

Type                              Size (bytes)                                Format Specifier

int                                          2 or 4                                                   %d, %i

char                                         1                                                          %c

float                                        4                                                          %f

double                                    8                                                          %lf

short int                                 2                                                          %hd

unsigned int                      2 or 4                                                    %u

long int                                4 or 8                                                    %ld, %li

long long int                      at least 8                                             %lld, %lli

unsigned long int            at least 4                                             %lu

unsigned long long int   at least 8                                             %llu

signed char                        1                                                             %c

unsigned char                   1                                                             %c

long double                       10, 12 or 16                                        %Lf

SPECIFIER           USED FOR

%c                          a single character

%s                          a string

%hi                        short (signed)

%hu                      short (unsigned)

%Lf                        long double

%n                         prints nothing

%d                         a decimal integer (assumes base 10)

%i                           a decimal integer (detects the base automatically)

%o                         an octal (base 8) integer

%x                         a hexadecimal (base 16) integer

%p                         an address (or pointer)

%f                          a floating point number for floats

%u                         int unsigned decimal

%e                         a floating point number in scientific notation

%E                         a floating point number in scientific notation

%%                        the % symbol

Another Example for Specifying Variable Sizes

#include <stdio.h>

int main()
{
  short x;
  long y;
  long long z;
  long double t;

  printf("Size of short = %d bytes\n", sizeof(x)); //Prints size of variable x on screen
  printf("Size of long = %d bytes\n", sizeof(y));  //Prints size of variable y on screen
  printf("Size of long long = %d bytes\n", sizeof(z));  //Prints size of variable z on screen
  printf("Size of long double = %d bytes\n", sizeof(t));  //Prints size of variable t on screen

  return 0;
}
variable values in c language
Variables and Data Types in C
output of the values of the variables

Leave a Reply

Your email address will not be published. Required fields are marked *