Lect 04
Lect 04
Lect 04
Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support
First Program
#include <stdio.h> Output :
int main()
{ Hello World!
/* My first program */
printf("Hello World! \n");
return 0;
}
C is case sensitive.
End of each statement must be marked with a semicolon (;).
Multiple statements can be on the same line.
White space (e.g. space, tab, enter, … ) is ignored.
First Program
#include <stdio.h>
int main() Output :
{
/* My first program */ Hello World!
printf("Hello World! \n");
return 0;
}
The C program starting point :
main().
indicates where the program actually starts and
main()
ends. {}
In general, braces {} are used throughout C to enclose a block of
statements to be treated as a unit.
COMMON ERROR: unbalanced number of open and close curly
brackets!
First Program
#include <stdio.h>
int main() Output :
{
/* My first program */ Hello World!
printf("Hello World! \n");
return 0;
}
#include <stdio.h>
Including a header file
stdio.h
Allows the use of function
printf
For each function built into the language, an associated header file
must be included.
return 0;
}
Comments
/* My first program */
Comments are inserted between “/*” and “*/”
Or, you can use “//”
Primarily they serve as internal documentation for program
structure and function.
Why use comments?
Documentation of variables, functions and algorithms
return 0;
}
Output :
The tax on 72.10 is 7.21
CREDIT RATE : 1/A
Names in C
Identifiers (variable name)
Must begin with a character or underscore(_)
May be followed by any combination of characters,
underscores, or digits(0-9)
Case sensitive
Ex)
summary, exit_flag, i, _id, jerry7
Keywords
Reserved identifiers that have predefined meaning to the C
compiler. C only has 29 keywords.
Ex)
if , else, char, int, while
Symbolic Constants
Names given to values that cannot be changed.
Use preprocessor directive
#define
#define N 3000
#define FALSE 0
#define PI 3.14159
#define FIGURE "triangle"
Symbols which occur in the C program are replaced by
their value before actual compilation
Declaring Variables
Variable
Named memory location where data value is stored
Each variable has a certain type (e.g. …)
int, char, float,
Contents of a variable can change
Variables must be declared before use in a program
Declaration of variables should be done at the opening brace of a
function in C. ( it is more flexible in C++ )
output:
a 97
A 65
1 49
$ 36
+ 43
a 97
A 65
1 49
$ 36
+ 43
getchar() , putchar()
int getchar()
Defined in <stdio.h>,
Get one character input from keyboard and return the ascii value
int putchar(int c)
Defined in <stdio.h>
prints one character provided as a parameter
c=getchar();
return 0;
}
korea.c
#include <stdio.h>
int main()
{
short no_univ = 276;
int population = 48295000;
long budget = 237000000000000L;
printf(“korea info\n”);
printf(“univ no : %d\n”, no_univ);
printf(“population : %d\n”, population);
printf(“budget : %d\n”, budget);
Output :
korea info
return 0;
univ no : 276
} putpulation: 48295000
budget: -590360576
Overflow?
(integer type) overflow
occurs when storing a value that is bigger than what can be
stored.
#include <stdio.h>
int main()
{
int a=2147483647;
printf("%d,%d\n",a,a+1);
return 0;
}