Академический Документы
Профессиональный Документы
Культура Документы
Resources
4/27/2008
The C Language
Bjarne Stroustrup, BELL Laboratories A powerful language, used to write the Linux OS Used by almost all embedded system platforms
Lets Code
C compiler (Turbo C, GCC etc) C Library Text editor
4/27/2008
Dev C++
First C Program
#include <stdio.h> #include <stdlib.h> int main( void ) { printf("hello world\n"); system("PAUSE"); return 0; }
4/27/2008
4/27/2008
Pre-processor constants
#define PI 3.14159 Notice that ; was not used Any instance of PI is replaced by 3.14159 Useful when a constant is used throughout the program
4/27/2008
Variables
Variable names may not start with a numerical character Reserved words are not to be used as variable names
Variable types
Data Type char short int int long int float double long double Size in Bytes 1 2 2 4 4 8 10 MinimumValue 0 -32768 -32768 -2147483648 1.17549 E-38 2.22507 E-308 3.3621 E-4932 Maximum Value 255 32767 32767 2147483647 3.40282 E+38 1.79769 E+308 1.18973 E+4932
4/27/2008
printf(Area of circle radius %f = %f\n, radius,area); %f = means that the variable is to be displayed in a floating-point format The variables to be displayed should be in order What if we want to display it in a different format?
C Format Processor
Formatter %d or %i %u %x or %X %f %e or %E Type of Display Signed decimal Unsigned decimal Unsigned hexadecimal Floating point Scientific notation Formatter %s %c %% %o Type of Display Char array Single character % sign Octal notation
4/27/2008
R2
4/27/2008
scanf(%f %f, &r1, &r2); Get 2 inputs from the user separated by a space
4/27/2008
Exercise
Modify the PI program to accept from the user the radius of the circle whose circumference is to be computed The output should be in %f format
C Math Operations
C supports all the basic math operations Some more advanced math functions (sine(), abs(), etc) are supported in the math.h library 2 Types of math operations : unary and binary
10
4/27/2008
11
4/27/2008
Control Structures
Many times, parts of the program needs to be repeated several times depending on the value of a certain variable Solution : use C control structures while( ){}; do{ }while(); for(){};
While()
while( expression ){
statements
12
4/27/2008
While()
int i = 10; while( i > 0){ print (this is while statement %i, i); i--; }
Do while()
do{ statements }while(condition); Do the statements while condition holds How does it differ from while() statement?
13
4/27/2008
Do while()
int i = 10; do{ print (this is while statement %i, i); i--; } while( i > 0);
For()
} Statement1 = evaluated once at the start of the loop Statement2 = evaluated at every iteration Statement3 = evaluated at the end of every iteration
14
4/27/2008
For()
int i; for (i = 10; i > 0; i--){ printf(this is iteration number %i, i); }
Exercise
Given an integer i between1to 20, create a program that would display a pyramid of asterisks (*) with height i; Ex: I = 5 * *** ***** ******* *********
15
4/27/2008
End.
30 Minutes to do the exercise
16