Presentation is loading. Please wait.

Presentation is loading. Please wait.

NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.

Similar presentations


Presentation on theme: "NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing."— Presentation transcript:

1 NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing at the function main. Every C program begins executing at the function main. Comments begin with /* and end with */ Comments begin with /* and end with */ The preprocessor directive #include tells the compiler to include standard input output header file in the program. The preprocessor directive #include tells the compiler to include standard input output header file in the program.

2 Variables and Values A variable is a location in memory where a value can be stored for use by a program. A variable is a location in memory where a value can be stored for use by a program. Ex : int sum = 5 ; Ex : int sum = 5 ; would mean the variable “sum” is an integer and its value is 5 which is stored in memory. sum sum 5

3 Datatypes Datatypekeyword Datatypekeyword integerint integerint real/floatfloat real/floatfloat characterchar characterchar All variables in C must be declared before they can be used in the program.

4 printf Used to print the string contained in( “ ”) Used to print the string contained in( “ ”) and to print the value of expressions. and to print the value of expressions. Format when printing Format when printing 1 st argument - format control string 1 st argument - format control string 2 nd argument - expression whose value will be printed printf(“format control string”,variable); printf(“format control string”,variable); printf(“format control string”,list of vars); printf(“format control string”,list of vars);

5 Variable type 1 st argument 2 nd argument Variable type 1 st argument 2 nd argument Integer%d expression Float %f expression Char%c character

6 /* Program to print integers and floats */ # include # include main(){ int a = 5; float b = 2.0 ; printf(“The value of a is %d”, a); printf(“The value of b is %f”, b); }

7 Arithmetic in C C operation Arithmetic operator Addition+ Subtraction- Multiplication* Division/ Modulus%

8 /* Program to print the sum of two numbers */ # include # include main(){ int a = 5; float b = 2.0 ; float b = 2.0 ; float c; float c; c = a + b; c = a + b; printf(“The sum of %d and %f is %f”, a, b, c); printf(“The sum of %d and %f is %f”, a, b, c);}

9 scanf Used to obtain values user enters at the keyboard. Used to obtain values user enters at the keyboard. Format Format 1 st argument - format control string 1 st argument - format control string 2 nd argument - location in memory where the value will be stored. scanf(“format control string”, &variable ); scanf(“format control string”, &variable );

10 Example: Example: printf(“Enter an integer \n”); scanf(“%d”, &integer1); scanf(“%d”, &integer1); %d specifies that data entered should be an integer. Value is stored at location &integer1.

11 /* To calculate the volume of a cylinder */ #include #include #define PI 3.14 void main(void) { float radius = 0.0; float height = 0.0; float height = 0.0; float volume = 0.0; float volume = 0.0; printf(“Enter the radius: \n”); printf(“Enter the radius: \n”);

12 scanf(“%f”, &radius); scanf(“%f”, &radius); printf(“Enter the height: \n”); printf(“Enter the height: \n”); scanf(“%f”, &height); scanf(“%f”, &height); volume = PI * radius * radius * height; volume = PI * radius * radius * height; printf(“The volume of the cylinder with height %f and radius %f is: %f”, height, radius, volume); printf(“The volume of the cylinder with height %f and radius %f is: %f”, height, radius, volume);}


Download ppt "NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing."

Similar presentations


Ads by Google