What is a Variable? How is it Defined?

 

Variables are placeholders that store values either entered by the user or assigned during program execution. Defining a variable means specifying its type according to the data it will store. For example, if you have a variable named a and you want it to store an integer value, you must define a as an integer.

Some variable types: char, int, float, double, short, long... (Long and short are variations of integer with different ranges.)

#include<stdio.h>

int main(void) {
    int a;
    a = 25;
    printf("The value of a is %d", a);
    return 0;
}

int stands for integer. In the first line, we wrote "int a;", which means "I have a variable named a of integer type." The line a = 25; assigns the value 25 to a. This means that whenever a is used, its value 25 will be referenced. The printf(); function contains %d, which indicates that an integer variable will be displayed. The variable name a is provided after the comma to specify which value will be printed.

Some other format specifiers: %f (for float and double), %c (for characters), %s (for strings, e.g., "C programming").

To determine how much memory different variable types occupy, we use:

printf("%d byte\n", sizeof(char));
printf("%d byte\n", sizeof(int));
printf("%d byte\n", sizeof(short int));
printf("%d byte\n", sizeof(long int));
printf("%d byte\n", sizeof(double));
printf("%d byte\n", sizeof(float));

Example: Adding Two Numbers

#include<stdio.h>
int main(void) {
    int a;
    int b;
    int sum;
    a = 25;
    b = 18;
    sum = a + b;
    printf("a is %d, b is %d, Sum is %d.\n", a, b, sum);
    return 0;
}

Output:

a is 25, b is 18, Sum is 43.

In this program, three integer variables are declared (a, b, and sum). Values are assigned to a and b, and their sum is stored in sum. The values are then displayed using printf(); with three %d specifiers.

Formatting Output

  • Including a number between % and d specifies field width: %10d will print the integer with a width of 10 spaces.
  • Using %.nf limits decimal places for floats: %.2f limits to two decimal places.
  • %0nd pads numbers with zeros: %08d prints 188 as 00000188.

Example: Calculating Total Salary

#include<stdio.h>
int main(void) {
    int hours;
    float wage, total_wage;
    char initial;
    printf("Enter employee's initial> ");
    scanf("%c", &initial);
    printf("Enter working hours> ");
    scanf("%d", &hours);
    printf("Enter hourly wage> ");
    scanf("%f", &wage);
    total_wage = hours * wage;
    printf("%c's total wage is: %f\n", initial, total_wage);
    return 0;
}

This program takes an employee’s initial, hours worked, and hourly wage, then calculates total earnings. Different variable types are used: char for initials, int for hours, and float for wage and total earnings. scanf(); is used to take input from the user.

ASCII Table

Each character in C corresponds to an ASCII value. For example, char variable = 66; assigns the character B to variable. Similarly, int variable = 'B'; assigns 66 to variable.

Computing Average of Two Numbers

#include<stdio.h>
int main(void) {
    float num1, num2, average;
    printf("Enter two numbers> ");
    scanf("%f%f", &num1, &num2);
    average = (num1 + num2) / 2;
    printf("The average is: %f\n", average);
    return 0;
}

Reading Multiple Variables in One scanf()

scanf("%d%f%c", &d1, &d2, &d3);
printf("%d %f %c", d1, d2, d3);

sizeof Operator

The sizeof operator returns the memory size of a variable.

#include <stdio.h>
int main() {
    printf("%lu\n", sizeof(char));
    printf("%lu\n", sizeof(int));
    printf("%lu\n", sizeof(3));
    printf("%lu\n", sizeof(float));
    printf("%lu\n", sizeof(double));
    return 0;
}
// Output: 1 4 4 4 8

The sizeof operator’s output may vary based on system architecture.

Constants

To define a constant, use #define:

#define PI 3.14
#define MAX 100
#define RADIUS 14.22

Alternatively, use const:

int main(void){
    const int GRADE = 1257;
}

Character Arithmetic

#include<stdio.h>
void main(){
    char ch = 'b';
    printf("%c\n", ch+1);
}

This prints c, as adding 1 to b moves to the next ASCII character.

printf() Details

  • Extra arguments in printf(); do not cause errors, but missing arguments do.
  • printf(); returns the number of characters printed:
#include<stdio.h>
int main(void) {
    printf("%d", printf("hello"));
}

Output: hello5 (since "hello" has 5 characters).

Yorumlar

Bu blogdaki popüler yayınlar

Arithmetic Operators and Expressions

Functions