Вы находитесь на странице: 1из 16

4/27/2008

C Programming Primer Session I


Roland Jay Atalin Miguel
Instrumentation, Robotics and Controls Laboratory University of the Philippines, Diliman

Resources

The Indispensible Guide to C


Paul Davies

The GNU GCC Compiler


www.gnu.org

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

Compiling the Code


1) 2) 3)

Save as main.c Execute menu Compile and run option

Dissection of the program


#include <stdio.h> #include <stdlib.h> int main( void ) { printf("hello world\n"); system("PAUSE"); return 0; }
Include the stdio.h library, contains the function printf() Include the stdlib.h library, contains the function system() Main function, C starts execution here Print hello world on the screen Pause program execution Return to the operating system normally

4/27/2008

Using Variables and Constants


#include <stdio.h> #include <stdlib.h> #define PI 3.14159 int main (void){ float area, circ, radius = 2.5; area = PI*radius*radius; circ = 2.0 * PI * radius; printf(Area of circle radius %f = %f\n, radius, area); printf(Circumference of circle radius %f = %f\n,radius, circ); system(PAUSE); return 0; }

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

float area, circ, radius = 2.5; Syntax in declaring variables


<type> <name> <optional init value>;

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

Printing the Results


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

Getting user input


V1 R1 V2

How do we compute the voltage V2 using C?

Getting user input


#include <stdio.h> #include <stdlib.h> int main(void){ float r1,r2,v1,i; printf(Enter voltage V1: ); scanf(%f, &v1); printf(Enter resistance values r1 and r2:); scanf(%f %f, &r1, &r2); I = v1/(r1 + r2); printf(Current = %f Amps\n, i); printf(Potential at v2 = %f Volts\n, i*r2); system(PAUSE); return 0; }

R2

4/27/2008

Getting user input


scanf(%f, &v1); Get input from user, wait for the return <enter> character, put the value in the variable v1 as %f <float>

scanf(%f %f, &r1, &r2); Get 2 inputs from the user separated by a space

What if we want an integer?


scanf(%i, &v1); Use the same format preprocessor as printf();

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

Subset of Cs Math Operators


Operator () + ,++, -~ ! *, / , % + ,<< , >> <, >, <=,>= == != & ^ | Description Parenthesis and function Unary + and - Increment and decrement Ones complement Logical NOT Multiplication,Division, Modulo Addition and Subtraction Bitwise shift left, Bitwise shift right Comparison operators Equal to comparison Not equal to comparison Bitwise AND Bitwise XOR Bitwise OR

A note on the basic Math Functions


float and float results float float and integer results float integer and integer results integer What if we would want to convert variables? Use casting! Ex : float myvar; int x; x = (int) myvar; #myvar is converted to int

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

} While the expression holds TRUE, execute 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);

Now try with i=0 as initial value

For()

For (statement1; statement2; statement3){ statements;

} 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

Вам также может понравиться