4-c Fundamentals
4-c Fundamentals
78
First C program – print on screen
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ;
}
79
More printing … (code and see)
#include <stdio.h>
void main()
{
printf ("Hello, World! ") ;
printf ("Hello \n World! \n") ;
}
80
Some more printing
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ;
printf ("Hello \n World! \n") ;
printf ("Hell\no \t World! \n") ;
}
81
Reading values from keyboard
#include <stdio.h>
void main()
{
int num ;
scanf ("%d", &num) ;
printf (“No. of students is %d\n”, num) ;
}
82
Centigrade to Fahrenheit
#include <stdio.h>
void main()
{
float cent, fahr;
scanf(“%f”,¢);
fahr = cent*(9.0/5.0) + 32;
printf( “%f C equals %f F\n”, cent, fahr);
}
83
Largest of two numbers
#include <stdio.h>
void main()
{
int x, y;
scanf(“%d%d”,&x,&y);
if (x>y) printf(“Largest is %d\n”,x);
else printf(“Largest is %d\n”,y);
}
largest-1.c 84
What does this do?
#include <stdio.h>
void main()
{
int x, y;
scanf(“%d%d”,&x,&y);
if (x>y) printf(“Largest is %d\n”,x);
printf(“Largest is %d\n”,y);
}
largest-2.c 85
The C Character Set
The C language alphabet
Uppercase letters ‘A’ to ‘Z’
Lowercase letters ‘a’ to ‘z’
Digits ‘0’ to ‘9’
Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ? blank
87
Variables
Very important concept for programming
An entity that has a value and is known to the
program by a name
Can store any temporary result while executing a
program
Can have only one value assigned to it at any given
time during the execution of the program
The value of a variable can be changed during the
execution of the program
88
Contd.
Variables stored in memory
Remember that memory is a list of storage
locations, each having a unique address
A variable is like a bin
The contents of the bin is the value of the variable
The variable name is used to refer to the value of
the variable
A variable is mapped to a location of the memory,
called its address
89
Example
#include <stdio.h>
void main( )
{
int x;
int y;
x=1;
y=3;
printf("x = %d, y= %d\n", x, y);
}
90
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 10
m
e
X = X +1
X = X*5
91
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 20
m
e
X = X +1
X = X*5
92
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 21
m
e
X = X +1
X = X*5
93
Variables in Memory
Instruction executed
Memory location allocated
to a variable X
X = 10
T
i X = 20 105
m
e
X = X +1
X = X*5
94
Variables (contd.)
X = 20
X
Y=15 20
X = Y+3 ? Y
Y=X/6
95
Variables (contd.)
X = 20
X
Y=15 20
X = Y+3 15 Y
Y=X/6
96
Variables (contd.)
X = 20
X
Y=15 18
X = Y+3 15 Y
Y=X/6
97
Variables (contd.)
X = 20
X
Y=15 18
X = Y+3 3 Y
Y=X/6
98
Data Types
Each variable has a type, indicates what
type of values the variable can hold
Four common data types in C
int - can store integers (usually 4 bytes)
float - can store single-precision floating
point numbers (usually 4 bytes)
double - can store double-precision floating
point numbers (usually 8 bytes)
char - can store a character (1 byte)
99
Contd.
Must declare a variable (specify its type and
name) before using it anywhere in your program
All variable declarations should be at the
beginning of the main() or other functions
A value can also be assigned to a variable at the
time the variable is declared.
int speed = 30;
char flag = ‘y’;
100
More Data Types in C
Some of the basic data types can be augmented
by using certain data type qualifiers:
short size qualifier
long
signed
unsigned sign qualifier
Typical examples:
short int (usually 2 bytes)
long int (usually 4 bytes)
unsigned int (usually 4 bytes, but no way to store + or
-)
101
Some typical sizes (some of these can vary
depending on type of machine)
Integer data Bit
Minimum value Maximum value
type size
char 8 -27=-128 27-1=127
short int 16 -215=-32768 215-1=32767
int 32 -231=-2147483648 231-1=2147483647
long int 32 -231=-2147483648 231-1=2147483647
-263=- 263-
long long int 64
9223372036854775808 1=9223372036854775807
unsigned char 8 0 28-1=255
unsigned short int 16 0 216-1=65535
unsigned int 32 0 232-1=4294967295
unsigned long int 32 0 232-1=4294967295
unsigned long lon 264-
g int
64 0
1=18446744073709551615
102
Variable Names
Sequence of letters and digits
First character must be a letter or ‘_’
No special characters other than ‘_’
No blank in between
Names are case-sensitive (max and Max are two
different names)
Examples of valid names:
i rank1 MAX max Min class_rank
Examples of invalid names:
a’s fact rec 2sqroot class,rank
103
More Valid and Invalid Identifiers
int x, y, sum;
scanf(“%d%d”,&x,&y); Values assigned
sum = x + y;
printf( “%d plus %d is %d\n”, x, y, sum );
}
106
Example - 2
#include <stdio.h>
void main()
{ Assigns an initial value to d2,
can be changed later
float x, y;
int d1, d2 = 10;
scanf(“%f%f%d”,&x, &y, &d1);
printf( “%f plus %f is %f\n”, x, y, x+y);
printf( “%d minus %d is %d\n”, d1, d2, d1-d2);
}
107
Read-only variables
Variables whose values can be initialized during
declaration, but cannot be changed after that
Declared by putting the const keyword in front of
the declaration
Storage allocated just like any variable
Used for variables whose values need not be
changed
108
Correct
void main() {
const int LIMIT = 10;
int n;
scanf(“%d”, &n); Incorrect: Limit changed
if (n > LIMIT)
printf(“Out of limit”); void main() {
const int Limit = 10;
}
int n;
scanf(“%d”, &n);
Limit = Limit + n;
printf(“New limit is %d”, Limit);
}
109
Constants
Integer constants
Consists of a sequence of digits, with possibly a plus
or a minus sign before it
Embedded spaces, commas and non-digit characters
are not permitted between digits
Floating point constants
Two different notations:
Decimal notation: 25.0, 0.0034, .84, -2.234
Exponential (scientific) notation
3.45e23, 0.123e-12, 123e2
e means “10 to the power of”
110
Contd.
Character constants
Contains a single character enclosed within a pair of
single quote marks.
Examples :: ‘2’, ‘+’, ‘Z’
Some special backslash characters
‘\n’ new line
‘\t’ horizontal tab
‘\’’ single quote
‘\”’ double quote
‘\\’ backslash
‘\0’ null 111
Input: scanf function
Performs input from keyboard
It requires a format string and a list of variables into
which the value received from the keyboard will be
stored
format string = individual groups of characters
(usually ‘%’ sign, followed by a conversion
character), with one character group for each
variable in the list
Examples
scanf ("%d", &size) ;
scanf ("%c", &nextchar) ;
scanf ("%f", &length) ;
scanf (“%d%d”, &a, &b);
113
Reading a single character
A single character can be read using scanf with
%c
It can also be read using the getchar() function
char c;
c = getchar();