printf("welcome to plus2net");
The above command will print the message welcome to plus2net
printf("welcome to plus2net \n C tutorial section");
The output of the above command will be
Welcome to plus2net
C tutorial section
int main(void){
int num; // num is declared as integer variable
num=15; // data is stored
printf("num = %d ", num); // final output
return 0;
}
Here we have formatted the display by saying %d , that is we are sending an integer output. The above statement will display
Num = 15
To display a decimal value we will declare a float variable first and then format it to display. Like this ( changes in above code are only shown )
float num; // num is declared as float variable
num=17.873456; // data is stored
printf("num = %.3f", num); // final output
The output is
num = 17.873
To show 8 places after decimal
float num; // num is declared as float variable
num=17.873456789; // data is stored
printf("num = %.4f", num); // final output
Output is here
17.8735
#include<stdio.h>
#include<string.h>
int main()
{
char str[30]="plus2net";
printf("%c\n", str[3]);// Output is s
printf("%c\n",str[4]); // Output is 2
return 0;
}
We used \n
in above code to add line break. The char at position 3 and 4 ( first char is position 0 ) is returned.
#include<stdio.h>
#include<string.h>
int main()
{
char str[30]="plus2net";
int str_length;
str_length=strlen(str); // length of the string
for(int i=str_length; i>=0; i--){
printf("%c",str[i]); // printing the char at position i
}
return 0;
}
char var[20] = "Plus2net";
printf(" Welcome to %s ",var);
Output is here
Welcome to Plus2net
C language is case sensitive, lower and upper case letters are to be treated differently. Printf is not same as printf.
1.Write a program to display sum of two integers
2.Write a program to display a patern like this.
*
**
***
****
This example demonstrates how to print integer and float values using printf() with format specifiers.
#include <stdio.h>
int main() {
int num = 25;
float pi = 3.14159;
printf("Integer: %d\n", num);
printf("Float: %.2f\n", pi); // 2 decimal places
return 0;
}
Output:
Integer: 25
Float: 3.14
---
This example shows how to use printf() to print characters and strings.
#include <stdio.h>
int main() {
char ch = 'A';
char name[] = "plus2net";
printf("Character: %c\n", ch);
printf("String: %s\n", name);
return 0;
}
Output:
Character: A
String: plus2net
---
This example demonstrates how to format output with specific width and precision.
#include <stdio.h>
int main() {
float num = 123.456;
printf("Fixed width (10 characters): %10.2f\n", num); // Total width 10, 2 decimals
return 0;
}
Output:
Fixed width (10 characters): 123.46
---
This example shows how to print special characters like newlines and tabs using printf().
#include <stdio.h>
int main() {
printf("This is line 1.\nThis is line 2.\n");
printf("Column1\tColumn2\tColumn3\n");
return 0;
}
Output:
This is line 1.
This is line 2.
Column1 Column2 Column3
Pratibha Suralkar | 25-09-2016 |
what is the output of following : printf("%d",printf("welcome")); |