C Programming language

Day 1: C Constant, C Variables, Format specifiers in C
Previous Home Next

C constants

Constants also called a literals. Constant refer to fixed values that the program may not alter. constant can be any of the basic data types. C have a two type of constant:

  1. Numeric constant
  2. Character constant

C constant is showing in the figure:


A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.

Types of variables In C variable are divided in two types

  1. Global variable: global variable is a variable defined outside any function block. Global variable is also called external variable.
  2. Local variable: local variable is a variable defined inside a function block. Local variable is also called automatic variables.

Variable Declaration Examples

Single declarations

int age;
float amount;
char last;

Multiple declarationsYou can repeat the the declaration line with a different variable on each line .

int age;
int hrNumber;

But the normal way is to separate the variables by a comma. Although not required the names are easier to read if a space follows the comma.

int age, HouseNumber, quality;
float distance, Discount;
char first, second;

Scope of variables

The scope of a variable depends on where it is declared.

  • The area of the program where that variable is valid, i.e. the parts of the program that have access to that variable. This is determined by the location of the declaration.
  • The life span of that variable, i.e. the length of time that the variable remains in memory.
Where do you Declare Variables

Variables can be declared as

  • Prior to the start of the main( ) function.
  • Within the main function, after the opening {
  • Within a function after the opening {
  • Within a block of code, after the {
  • Global or external variable Declared before the start of the main( ) function.


Static Variable

When static is used with a global variable it indicates that the variable is local to the current file. If the programmer would like the value of the variable to be remembered when the function is revisited then that variable must be declared as static.


static int aVariable = 1;

In the example the initialisation to 1 occurs only on the first execution of this line of code, it is disregarded on further executions of the line.

static has a different meaning when used with global variables.

Format Specifiers in C
%cThe character format specifier.
%dThe integer format specifier.
%iThe integer format specifier (same as %d).
%fThe floating-point format specifier.
%eThe scientific notation format specifier.
%EThe scientific notation format specifier.
%gUses %f or %e, whichever result is shorter.
%GUses %f or %E, whichever result is shorter.
%oThe unsigned octal format specifier.
%sThe string format specifier.
%uThe unsigned integer format specifier.
%xThe unsigned hexadecimal format specifier.
%XThe unsigned hexadecimal format specifier.
%pDisplays the corresponding argument that is a pointer.
%nRecords the number of characters written so far.
%%Outputs a percent sign.
Previous Home Next