Chapter 2
Variables and Data Types
1. Introduction to character set,tokens,keywords and
identifiers.
2. Constants and their types.
3. Variables and data types
4. Declaration and initialization of variables and
storage classes.
5. Assigning values to variables and reading data from
keyboard(scanf()).
6. Defining symbolic constants.
VARIABLES
Variables must be declared before use immediately
after “{”
Valid characters are letters, digits and “_”
First character cannot be a digit
31 characters recognised for local variables (more can
be used, but are ignored)
Some implementations recognise only 6 characters in
global variables (and function names)!
Upper and lower case letters are distinct
VARIABLE DECLARATION
int b;
double x;
unsigned int a;
char c;
C is a typed language that means a variable has a
name
type
C standard types
int, double, char, float, short, long, long double
unsigned char, unsigned short, unsigned int,
unsigned long
VARIABLE DEFINITIONS
• A declaration introduces a variable’s name into a program and
specifies its type
• A definition is a declaration which also sets asides memory for
that variable (which is usually the case)
• In C it is possible to initialize a variable at the same
time it is defined, in the same way one assigns a value
to an already defined variable.
• Examples of variable definitions:
int a;
double x = 5.9;
char answer = ’n’;
double y=x;
THE FORMAT OF C
Statements are terminated with semicolons
Indentation is ignored by the compiler
C is case sensitive - all keywords and Standard Library
functions are lowercase
Strings are placed in double quotes
Newlines are handled via \n
Programs are capable of flagging success or error,
those forgetting to do so have one or other chosen
randomly!
PRINTF AND SCANF
printf writes integer values to screen when %i
is used
scanf reads integer values from the keyboard
when %i is used
“&” VERY important with scanf (required to
change the parameter, this will be investigated
later) - absence will make program very ill
“&” not necessary with printf because current
value of parameter is used
HELLO WORLD PROGRAM
#include <stdio.h> // input-output library
int main() // function main
{
printf(”Hello World\n”); // send string to standard output
return 0;
}
Compile the source file helloworld.c with the command
gcc helloworld.c –o helloworld
HELLO WORLD PROGRAM
#include <stdio.h>
includes the standard I/O library which allows you to read from the
keyboard (standard in) and write to the screen (standard out)
int main()
declares the main function. Every C-program must have a function named
main somewhere in the code
{ ... }
The symbols { and } mark the beginning and end of a block of code.
printf(”Hello World\n”)
Sends output to the screen. The portion in quotes ”...” is the format
strings and describes how the data is formatted.
return 0;
This causes the function main to return an error code of 0 (no error) to the
shell that started the program.
COMPILATION & LINKING
header file
source file stdio.h
helloworld.c
#include <stdio.h>
compiler
object file
helloworld.o
linker
executable file
helloworld
CONSTANTS
• constants can be specified using the preprocessor
directive #define
example:
#define PI 3.14159
• the preprocessor replaces the identifier PI by the text
3.14159 throughout the program
• the major drawback of #define is that the data type of
the constant is not specified
• the preprocessor merely replaces all occurrences of the
string PI with 3.14159
CONVENTION: reserve CAPITAL letters for constants and use
small caps for variables and functions
COMMENTS
• comments are always a good thing to use because
• not everyone is as smart as you are and needs more
explanation in order to understand your program
• you may not be as smart next month when you have
to change your program
• comments should clarify your code and explain the
rational behind a group of statements
• comment syntax (C style) /* */
/* this is a comment
which can go across multiple
lines of code */
TYPE CONVERSIONS
• C is a hard-typed language, meaning that each variable has a fixed
type that does not change and which determines the possible
assignments and applicable operators
double pi=3.14;
char c=’x’;
• Some type conversions occur automatically for example int to float
or float to double, but also char to int
int i = 17;
float x = i; /* assigns 17 to x */
int j = 2;
float y = i/j; /* assigns 17/2 = 8 to y not 8.5 */
• Type conversions can be forced by a programmer through
a type cast
float z = (float) i / j; /* casts i into float before division */
KEYWORDS
The following names are reserved by the C language. Their
meaning is already defined, and they cannot be re-defined to
mean anything else.
LIBRARY FUNCTIONS
• Many functionalities in C are carried out by library functions.
• These functions perform file access, data conversion and
mathematical computations.
#include <math.h> /* include header file for math functions */
int main()
{
double x;
double y;
y=1.57;
x=sin(y);
}
Exercise
1. Write a program to show typical declaration,
assignments and values stored in various types of
variables.
2. Write a program which shows the use of scanf()
function.
3. Write a program for calculating the average of 3
numbers
/*PROGRAM FOR PRINT THE DIFFERENT DATA TYPES*/
#include <stdio.h>
int main()
{
int a;
double x;
char c;
printf(“Enter integer:”);
scanf(“%d”,&a);
printf(“\nEnter double:”);
scanf(“%lf”,&x);
printf(“\nEnter character:”);
scanf(“%c”,&c);
printf(“\nThe value of a is %d, of x is %lf, of c is %c\n”,a,x,c);
}
HEADER FILES
• a header file contains the declaration of functions
you want to use in your code
• the preprocessor directive #include takes care of
incorporating a header file into your source file
• example:
#include <math.h>
#include ”myprog.h”
• the brackets <> indicate that the compiler first searches
the standard include directory which contains the
standard C header files first
• the quotation marks indicate that the compiler first searches
for header files in the local directory
• if you do not include the appropriate header file
you get an error message from the compiler
HEADER AND LIBRARY FILES
library header file
#include <math.h>
math.h
myprog.c
#include ”myprog.h” user header file
myprog.h
compiler
object file library file
myprog.o libm.a
linker
executable file
myprog