Upload
yestin
View
41
Download
8
Embed Size (px)
DESCRIPTION
C workshop Yuli Kaplunovsky - [email protected] Today - Introduction to C. Recommended book: The C programming Language / Kernighan & Ritchie. My first program. #include void main() { printf("Hello World!\n"); }. Output: Hello World!. C structure. - PowerPoint PPT Presentation
Citation preview
C workshop
Yuli Kaplunovsky - [email protected]
Today - Introduction to C
Recommended book: The C programming Language / Kernighan & Ritchie
My first program
#include <stdio.h>
void main()
{
printf("Hello World!\n");
}
Output:Hello World!
C structure• Function oriented (‘goto’ is not recommended)
• First function is always called main• Contains many libraries (e.g. stdio.h, stdlib.h, math.h) with many
predefined functions.• CaSe SeNsItIvE
(e.g. ‘Main’ instead of ‘main’ won’t work)
• ALWAYS USE: – Indentation– Meaningful names for functions and variables– Plenty of remarks
Variables
• int – an integer number maximum value is 2,147,483,647 (or 2^31) minimum value is -2,147,483,648
• double – real number, represented as floating point (64 bits long)
• char – represents a single character
Variables sample #1
#include <stdio.h>
void main()
{
int I,J,K;
I = 10;
J = 20;
K = I + J;
printf("K is %d\n", K);
}
Output:K is 30
Variable sample #2#include <stdio.h>
void main(){ double X,Y,Z;
X = 10.0; Y = 20.0; Z = X / Y;
printf("Z is %g\n", Z);}
Output:Z is 0.5
while
#include <stdio.h>
/* Print Fahrenheit-Celsius table for fahr = 0, 20, .., 300 */
void main(){ int fahr, celsius; int lower, upper, step;
lower = 0; /* lower limit of temerature table */ upper = 300; /* upper limit */ step = 20; /* step size */
fahr = lower; while ( fahr <= upper ) { celsius = 5 * (fahr - 32) / 9; printf("%d\t%d\n", fahr, celsius ); fahr = fahr + step; }
}
Output:0 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
for• Syntax:
for ( initialization ; condition ; do ) { block }
#include <stdio.h>void main(){ int I; printf("I = "); for ( I = 0 ; I < 10 ; I++ ) printf("%d, ", I ); printf("\n");}
Output:I = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
for - another example
#include <stdio.h>
void main()
{
int fahr;
for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20 )
printf("%d %g\n", fahr, (5.0 / 9.0) * (fahr - 32) );
}
Output:0 -17.777820 -6.6666740 4.4444460 15.555680 26.6667100 37.7778120 48.8889140 60160 71.1111180 82.2222200 93.3333220 104.444240 115.556260 126.667280 137.778300 148.889
if• if ( condition ) { block #1 }
else { block #2 } (optional)
• Example:if ( Y > X ) Z = X;else Z = Y;
• For comparisons use: > < <= >= ==
• Important remark: a block should be surrounded with {} if it contains more than one command.
If - multiple conditions• if ( (condition1 || condition2 ) && condition3) ...• Examples:if ( Y > X && Y > Z ) Z = X;
int bTerminate; if ( I == 10 || bTerminate ) break;
• break is used to get out of for & while loops• condition2 is FALSE when bTerminate is 0
and is TRUE when bTerminate is NOT 0
Functions
• Return-value function-name( parameters ) { … return value; }
• Calling the function:I = function-name( 10, 20 );
Function example#include <stdio.h>int Add2( int A, int B ){ int C; C = A + B; return C;}
void main(){ int I; I = Add2( 10, 20 ); printf("Add2 function returns = %d\n", I);}
Output: Add2 function returns = 30
Arrays
• ALWAYS start from 0Last item is N-1
• Example:int Ar[10];Ar[0] = 22;Ar[9] = Ar[0] + 22;I = 4; Ar[I] = Ar[I+1];
#include <stdio.h>
// Guess what this program does...
void main()
{
double X[10] = { 2, 4.2, 11.2, 3, 99.2, -23.2, 33, 11, 43, 9 };
double Y;
int I, J;
for ( I = 0 ; I < 9 ; I++ ) {
for ( J = I+1 ; J < 10 ; J++ ) {
if ( X[I] > X[J] ) { // Switch variables in array
Y = X[I];
X[I] = X[J];
X[J] = Y;
}
}
}
// print results
for ( I = 0 ; I < 10 ; I++ )
printf("%g, ", X[I]);
}Output:-23.2, 2, 3, 4.2, 9, 11, 11.2, 33, 43, 99.2,