A First C Program: Department of Computer and Information Science, School of Science, IUPUI
A First C Program: Department of Computer and Information Science, School of Science, IUPUI
A First C Program
Dale Roberts
main()
Indicates a
{
program
building
block called
function
comment
one statement
printf(Hello world );
printf(Welcome to CSCI230\n);
printf(I am John Smith\n);
statement terminator
Dale Roberts
Identifiers
Variable identifiers
Begin with a letter or underscore: A-Z, a-z, _
The rest of the name can be letters, underscore, or digits
Guarantee that east least the first 8 characters are significant (those
come after the 8th character will be ignored) while most of C compiler
allows 32 significant characters.
Example:
_abc
ABC
Time
time
_a1
abcdefghi (may be the same as abcdefgh)
abcdefgh
Case sensitive
Keywords: reserved names (lexical tokens)
auto
double if
static break
case
entry long
switch char
typedef float return union do
else
int
struct
extern register
go
sizeof continue
Dale Roberts
char
Abbreviation
Size
(byte)
Range
char
-128 ~ 127
unsigned char
0 ~ 255
2 or 4
2 or 4
0 ~ 65535 or 0 ~ 232-1
int
int
Note:
unsigned int
unsigned
short int
short
-32768 ~ 32767
unsigned short
0 ~ 65535
long int
long
-231 ~ 231-1
unsigned long
0 ~ 232-1
float
double
Dale Roberts
Variable Declarations
type v1,v2,v3, , vn
Example:
int i;
int j;
float k;
char c;
short int x;
long int y;
unsigned int z;
int a1, a2, a3, a4, a5;
Dale Roberts
hexadecimal
decimal int
long (explicit)
32
32L or 32l
an ordinary integer literal that is too long to fit in an int is also too
long for long
floating-point
No single precision is used; always use double for literal
Example:
1.23
123.456e-7
0.12E
Dale Roberts
48 - 57
65 - 90
97 - 122
new line
back slash
null
formfeed
double quote
10
9
0
12
34
\t
\
\b
\r
tab
single quote
back space
carriage return
9
39
8
13
Dale Roberts
ABCD
Example:
A
\0
...
Dale Roberts
/* x = 97*/
Notes:
a and a are different; why?
a is the literal 97
a is an array of character literals, { a, \0} or {97, 0}
a + b +c is invalid but a+b+c = ? (hint: a = 97 in ASCII)
a + b + c = 97 + 98 + 99 = 294 = 256 + 38
in the memory
38
if the code used is not ASCII code, one should check out each
value of character
Dale Roberts
Initialization
If a variable is not initialized, the value of
variable may be either 0 or garbage depending
on the storage class of the variable.
int i=5;
float x=1.23;
char c=A;
int i=1, j,k=5;
char c1 = A, c2 = 97;
float x=1.23, y=0.1;
Dale Roberts
Memory Concepts
Each variable has a name, address, type, and
value
int x;
2) scanf(%d, &x);
1)
3)
user inputs 10
4)
x = 200;
After
After
After
After
the
the
the
the
execution
execution
execution
execution
of
of
of
of
(1)
(2)
(3)
(4)
x
x
x
x
10
200
Sample Problem
Write a program to take two numbers as input data and
print their sum, their difference, their product and their
quotient.
Problem Inputs
float x, y;
problem Output
float sum;
float difference;
float product;
float quotient;
/* two items */
/*
/*
/*
/*
sum of x and y */
difference of x and y */
product of x and y */
quotient of x divided by y */
Dale Roberts
Dale Roberts
Example Program
#include <stdio.h>
function
int main(void)
name
list of argument along with their types
{
return value and its type
float x,y;
Body
float sum;
printf(Enter the value of x:);
scanf(%f, &x);
printf(\nEnter the value of y:);
scanf(%f, &y);
sum = x + y;
printf(\nthe sum of x and y is:%f,sum);
printf(\nthe sum of x and y is:%f,x+y);
printf(\nthe difference of x and y is:%f,x-y);
printf(\nthe product of x and y is:%f,x*y);
if (y != 0)
inequality operator
printf(\nthe quotient of x divided by y is:%f,x/y);
else
printf(\nquotient of x divided by y does not exist!\n);
return(0);
}
Dale Roberts