Skip to content

Commit

Permalink
Add tutorial printf.c (#93)
Browse files Browse the repository at this point in the history
* Create printf.c

* Tidy up tutorial format

* Update printf.c

Co-authored-by: Fum <[email protected]>
  • Loading branch information
hamzabohra and funbeedev authored Oct 31, 2022
1 parent 582b64a commit 4edc2c5
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions 11-common-built-in-functions/printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
---------------------------------------------------------------------------------------------------------------------------------
Tutorial: This tutorial is about how to use printf function in c language.
- Printf() is one of the primary output functions in C programming. A prepared output is sent to the screen by the function.
- We must use the #include <stdio.h> declaration to include the stdio.h header file in order to use printf() in our application.
- We use %d format specifier to print int types. %d gets replaced by the variable's value.
- To print float, we use %f format specifier.
- to print char, we use %c format specifier.
- To print double type , we use %lf format specifier.
- You can also perform some arithmetic operations in it.
- printf function returns the number of characters printed and negative if error occurs
-----------------------------------------------------------------------------------------------------------------------------------
*/

#include <stdio.h> // The "#include stdio.h>"" declaration must be used to include
// the stdio.h header file if we want to utilise printf().

int main() { // The main function
// print whole string using double quotation marks ("").
printf("Hello World \n"); // \n is used for line break

// you can also print the number of element in print function
int x = printf("Hi github");
printf("\n %d \n", x); // 9 is the output (note: the space character is counted)

// Declaring variables
int a = 10;
char c = 'c';
float f = 12.3;
double d = 15.43;

// For printing int types, we utilise the %d format specifier. The value
// of the variable is substituted for by %d.
printf("Integer a = %d \n", a);

// To print char type, %c is used.
printf("Character c = %c \n", c);

// To print float, we use %f format specifier.
printf("Float f = %f \n", f);

// To print double type, %lf is used.
printf("Double d = %lf \n", d);

// You can do some operation printf function and result will be displayed.
printf("a + d = %lf \n", a+d);

return 0;
}
/*
--------------------------------------------------------------------------------------------------------------------------------------
Challenge: Take input from user in variable a and b, print number of elements present in each variable and then print sum of number of
elements present in a and b.
example:
- a = 12343212 and b = 887788
- number of elements in a is 8 and in b is 6.
- sum is 14.
--------------------------------------------------------------------------------------------------------------------------------------
*/

0 comments on commit 4edc2c5

Please sign in to comment.