Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 2 Basics of C programming.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 2 Basics of C programming."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 2 Basics of C programming

2 Overall Structure of a C program # include DEFINITION OF ANY CONSTANTS int main(void) /* some explanatory comment */ { DECLARATIONS STATEMENTS return 0; }

3 Variables Variables are used to store and retrieve information each variable has a name variable names can consist of letters, numbers and _ (underscore) cannot start with a number no spaces or special characters ($, #) case sensitive use meaningful names!

4 Data types Information is stored in the computer as a series of 0s and 1s how these bits are interpreted for a given variable depends on its data type length (number of bytes) allowed operations the data type and name of a variable must be declared before it is used

5 Data Types There are two basic numeric data types in C integer for whole numbers and characters real for numeric values with decimal parts There are several variants of each type There is a type to store a single character which is a special variant of integer type There is no basic data type for strings!!

6 Data Types Basic Data Types int i,j;4 bytes float x,y;4 bytes double p,q;8 bytes char c;1 byte (ASCII)

7 Integer data types size is machine dependent 01000001 is ‘A’ or 65 each character has an integer equivalent can perform integer operations on characters ‘a’ + 3 is ‘d’ ‘d’ - (‘a’ - ‘A’) is ‘D’ convert lower to upper case int is the usual type for decimal integers

8 Sizeof Operator int i; ‘declare an integer variable called i’ sizeof(int) -Gives 4 sizeof(i) - Gives 4 Additional Data Types long int8 bytes (2 63 – 1); greater range short int 2 bytes; less storage unsigned int 4 bytes; greater range, positive numbers

9 * typical values - depends on system Integer Data Types

10 Typical Character Codes

11 Real data types Also called floating point fractional numbers8.231 powers of 106 x 10 23 7.4234 x 10 8 have 2 attributes precision number of significant figures rangelowest and highest number trade off between precision and range

12 Real data types * typical values - system dependent!!

13 Formatted input: scanf scanf scans in or reads input the input is stored in one or more variables we need to tell scanf the type of the information being input, and the name of the variable to store it in

14 Formatted input - scanf char input1; float input2; scanf("%c %f", &input1, &input2); scanf(, ) one conversion code for each variable in list. use double quotes around format string list of variables to be input (any number), separated by commas & before numeric and character variable names

15 scanf conversion codes %cchar %ddecimal integer %ffloat %lfdouble %Lflong double %sstring the name of the string variable is not preceded by a &

16 Formatted output: printf char input1; float input2; printf("You entered %c and %f ", input1, input2); printf(, ) one conversion code for each variable in list use quotes around format string can intersperse with text to be output list of variables to be input (any number), separated by commas

17 printf conversion codes %ccharacterL %d decimal integer 76 %escientific notation 7.620000e-02 %f floating point 0.076200 %ge- or f- format, whichever is shorter %ooctal integer094 %xhexadecimal integer0x4c %sstringI have a cat

18 Formatted output formatting can be done in the format string intersperse variables with text use spaces to separate output special characters \nnew line\” double quote \thorizontal tab\v vertical tab field width %3c, %3d, %3f3 characters wide %7.3f7 characters wide, 3 after decimal point

19 #include #define PI 3.14 int main(void) /* reads the radius of a circle and then calculates its area and circumference*/ { int radius; float circum,area; printf(“input radius: “); scanf(“%d”, &radius); circum=2*PI*radius; area=PI*radius*radius; printf(“\n\nresults:\n\n”); printf(“Circle of radius %d”,radius); printf(“\nCircumference %f and area %f\n”, circum,area); return 0; } Using formatted input & output

20 Common errors In printf and scanf the format string and variable lists must correspond. The format string in printf and scanf must have double quotes around it Don’t forget to use ‘&’ before integer and character variable names in scanf - but not strings!


Download ppt "Introduction to C Programming CE00312-1 Lecture 2 Basics of C programming."

Similar presentations


Ads by Google