×
>
<

Aptitude

C Variables And Constants | CrackEase

Variables and Constants

What is a variable?

A variable is a named location in memory used to store data that can change during program execution. In C you must declare a variable with a data type before using it. A variable's value can be assigned at declaration or later changed (at compile time with literals or at run time using input or computations).

Example of declaration and assignment:

  • int a = 10; — declares an integer variable a and initializes it to 10.
  • float price; then price = 99.99f; — declaration followed by assignment.
variables intro
Rules to declare a variable
variable rules

Rules for naming variables

  • Variable names identify memory locations that store values.
  • Names are case-sensitivea and A are different.
  • Must start with a letter (a–z, A–Z) or underscore (_), not a digit.
  • Can contain letters, digits and underscores (e.g., count1, _temp).
  • No spaces or special characters (like @, #, $ except underscore).
  • Should not use C keywords (like int, return, etc.) as identifiers.

Datatype of variables

Every variable has a type that determines what values it can hold and how much memory is allocated (for example char, int, float, double).

Declaration of a variable

What happens at declaration?

When you declare a variable the compiler reserves memory for it according to its type. If you declare a local variable without initializing it, it contains an unspecified "garbage" value.

Syntax for single declaration:

data_type variable_name;

Example:

Simple declaration Example
#include <stdio.h>

int main(void){
    int a = 25;
    printf("value of a : %d\n", a);
    return 0;
}
output
value of a : 25
 
Declaring multiple variables

You can declare and initialize several variables of the same type on one line:

int a = 25, b = 4, c = 67;
Source Code
#include <stdio.h>

int main(void)
{
   int a = 25, b = 4, c = 67;
   printf("value of a: %d\n", a);
   printf("value of b: %d\n", b);
   printf("value of c: %d\n", c);
   return 0;
}
output
value of a: 25
value of b: 4
value of c: 67 
scope
Scope of Variables

Scope describes where a variable is visible (accessible) in the code. The two primary categories are:

  • Local variables — declared inside a function or block; visible only within that block.
  • Global variables — declared outside all functions; visible to all functions after their declaration.

Local Variable

Local variables exist only while the function/block runs. They are not initialized automatically and may hold garbage values if not set explicitly.

Example:

Local variable example
#include <stdio.h>

int main(void)
{
     int a, b, c; // local variables
     a = 10;
     b = 30;
     c = a + b;
     printf("%d\n", c);  // prints 40
     return 0;
}

Here a, b, c are local to main and cannot be accessed from other functions.

Global variable

Global variables are declared outside any function and are accessible throughout the source file (and optionally other files via extern).

  • Global variables are initialized to zero by default (if not explicitly initialized).
  • Use them sparingly — excessive use can make code harder to reason about.

Example:

#include <stdio.h>

int d = 20; // global variable

int main(void)
{
      int a, b, c; // local variables
      a = 10;
      b = 30;
      d = d + 10;
      c = a + b + d;
      printf("%d\n", c);  // prints 70
      return 0;
}
constants

Constants in C

A constant is a value that does not change during program execution. Attempting to modify a constant results in an error. Constants are also called literals.

  • Constants can be of any data type (integer, floating, character, string).
  • Examples: 42 (integer constant), 3.14 (floating constant), 'A' (character constant), "hello" (string constant).
Floating constants

Floating constant

  • A floating constant is a decimal number, e.g., 3.14.
  • It behaves like a float or double depending on the literal and compiler.

Example:

Floating constant example
#include <stdio.h>

int main(void)
{
    const float num1 = 5.0f;
    const float num2 = 3.123f;
    printf("floating constant num1 = %f\n", num1);
    printf("floating constant num2 = %f\n", num2);
    return 0;
}
output
floating constant num1 = 5.000000
floating constant num2 = 3.123000
Character constants

Character constant

  • A character constant is a single character enclosed in single quotes: 'S'.
  • Character constants can include escape sequences like '\n', '\t'.
Character constant example
#include <stdio.h>

int main(void)
{
    const char ch = 'S';
    const char escape[] = "Hello\tWorld";
    printf("character constant is %c\n", ch);
    printf("string constant is %s\n", escape);
    return 0;
}
output
character constant is S
string constant is Hello    World
String constants

String constant

  • A string constant is a sequence of characters in double quotes, stored as a char array ending with '\0'.
  • Example: "Hello" is stored as {'H','e','l','l','o','\0'}.
String constant example
#include <stdio.h>

int main(void)
{
    const char st1[] = "S";           // string with single character
    const char st2[] = "Hey Somya";   // normal string
    const char st3[] = "Hey\nSomya";  // string with escape sequence
    printf("string constant with single character : %s\n", st1);
    printf("normal string constant : %s\n", st2);
    printf("string with escape sequences : %s\n", st3);
    return 0;
}
output
string constant with single character : S
normal string constant : Hey Somya
string with escape sequences : Hey
Somya
Defining constants

You can create constants using the #define preprocessor or the const qualifier. Prefer const for typed constants and #define for macro constants when appropriate.

Example (area of rectangle):

Constant example
#include <stdio.h>
#define LENGTH 10

int main(void)
{
    const int BREADTH = 5;
    int area = LENGTH * BREADTH;
    printf("%d\n", area);  // prints 50
    return 0;
}
output
50
Footer Content | CrackEase