01 09 IntroductionToCP1 PDF
01 09 IntroductionToCP1 PDF
Programming
Hong Liu
HPC Consultant
NICS
• A Brief History of C
• Edit stage:
• Type program in using one of the Microsoft Windows editing
packages.
• Compile and link:
• Select Build from menu. Building option allows you to both
compile and link in the same option.
• Execute:
• Select the Build menu → then, Execute filename.exe menu
Unix systems
• #include <stdio.h>
• main()
• {
• printf("Hello world\n");
• }
• Please note that Unix is a case sensitive operating system and
files named firstprog.c and FIRSTPROG.c are treated as two
separate files on these system.
• if(a)
• If a isn't zero then this also acts as the value true
Mathematical operations
Add, subtract, multiply and divide.
• add a+b
• subtract a-b
• multiply a*b
• divide a/b
• What is the answer to this simple
calculation?
• a=10/3
• The answer depends upon how a was
declared. If it was declared as type int the
answer will be 3; if a is of type float then
the answer will be 3.333.
• #include <stdio.h>
• main(){
• int a,b,average;
• a=10; b=6;
• average = ( a+b ) / 2 ;
• printf("Here is the answers.. ");
• printf("\n");
• printf("%d.",average);
• printf("\n");
• }
Input and Output Functions
• void demo();
• is a function with no parameters and no return value.
• #include<stdio.h>
int sum(int a, int b){
int result;
result=a+b;
return result;
}
main(){
int r;
r=sum(1,3);
printf("The answer is %d.\n", r);
}
The Standard Library
Functions
• stdio.h: I/O functions:
• printf() as previously described
• scanf() as previously described
• string.h: String functions
• strcpy() copys contents of str2 to str1
• ctype.h: Character functions
• islower() returns non-0 if arg is lowercase letter
• isupper() returns non-0 if arg is uppercase letter
• math.h: Mathematics functions
• sqrt() returns square root of num
• time.h: Time and Date functions
• time() returns current calender time of system
Data Types Part II