1 / 12

NOTE:

NOTE:. C programs consist of functions one of which must be main. Every C program begins executing at the function main. Comments begin with /* and end with */

wesleymyers
Download Presentation

NOTE:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NOTE: C programs consist of functions one of which must be main. Every C program begins executing at the function main. Comments begin with /* and end with */ The preprocessor directive #include <stdio.h> 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. • Ex : int sum = 5 ; would mean the variable “sum” is an integer and its value is 5 which is stored in memory. sum 5

  3. Datatypes Datatypekeyword • integer int • real/float float • character char All variables in C must be declared before they can be used in the program!

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

  5. Variables in printf Variable 1st 2nd argument argument Integer %d expression Float %f expression Char %c character

  6. /* Program to print integers and floats */ # include <stdio.h> 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 operationArithmetic operator Addition + Subtraction - Multiplication * Division / Modulus %

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

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

  10. Example: printf(“Enter an integer \n”); 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 <stdio.h> #define PI 3.1415926 void main(void) { float radius = 0.0; float height = 0.0; float volume = 0.0; printf(“Enter the radius: \n”);

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

More Related