90 likes | 237 Views
Dive into the essentials of C programming with a focus on the `printf` and `scanf` functions. Learn how to format your output for characters, decimals, and floating-point numbers, ensuring precision with specified decimal places. We'll cover coding standards for variable naming, file naming conventions, and proper comment practices. Additionally, get hands-on experience with practical problems, such as swapping two variables and calculating the volume of a cylinder from user inputs. Enhance your coding skills while adhering to best practices.
E N D
printf • intprintf ( const char * format, ... ); • printf ("Characters: %c \n", 'a'); • printf ("Decimals: %d %f\n", 1977, 3.14);
Printf - float • %f – floating point. Displays upto 6 decimal places. • %0.2f- displays 2 decimal places • %5.1f- displays 4 digits including one after decimal point. If less digits are there then it gives spaces instead • %05.1f- displays 4 digits including one after decimal point. If less digits are there then it gives leading zeros.
Printf – decimal • %5d- displays a the value. If number of digits is less than 5 then leading spaces are given. • %-5d- here trailing spaces are given. • %05d- here instead of spaces zeros are given.
Scanf • Reads data from standard input and stores them according to the parameter format into the locations pointed by the additional arguments. • Eg: -inti; scanf ("%d",&i);
Coding standards • Variable • Each variable must strictly be named in a meaningful way • only one declaration of one variable in one line • Eg: -inttotal_marks; inttotalmarks; • File Name • Filenames should be all lowercase and can include underscores (_) or dashes • Eg:- helloworld.c or hello_world.c
Coding standards • Comments • Meaningful comments and should be proper sentences • No spelling mistakes in comments • Indenting • Two spaces should be used. • Line length • Max 80 characters per line
Question 1 • Swap two variables using a temporary variable. • Input • Enter integer value 1- 10 • Enter integer value 2- 20 • Output • Value 1= 10 • Value 2= 20 • After Swapping • Value 1= 20 • Value 2= 10
Question 2 • Compute volume of a cylinder • Input • Enter the radius of cylinder (float) • Enter height of cylinder in float • Output • Volume of the cylinder(round off to 2 decimal places) =