Lecture_Week_3.2
Lecture_Week_3.2
where %X is the format specifier and & is the address operator which tells the
compiler to change the real value of variableOfXType, stored at this address in the
memory.
Basic Methods : printf()
• The printf() method prints the value passed as the parameter to it, on the
console screen.
• Syntax:
printf("%X", variableOfXType);
where %X is the format specifier which tells the compiler what type of
data is in a variable and variableOfXType is the variable to be printed.
How to take input and output of basic types in
C?
• Integer:
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
• Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
• Character:
Input: scanf(“ %c", &charVariable);
Output: printf("%c", charVariable);
#include <stdio.h>
int main()
{
C program to show input and
int num; output operation
char ch;
float f;
printf("Enter the integer: ");
// Input integer value
scanf("%d", &num);
printf("\nEntered integer is: %d", num); // Output integer value
printf("\n\nEnter the float: "); // Input float value
scanf("%f", &f);
printf("\nEntered float is: %f", f); // Output float value
printf("\n\nEnter the Character: "); // Input character value
scanf(“ %c", &ch);
printf("\nEntered character is: %c", ch); // Output character value
return 0;
}
#include <stdio.h>
int main()
{
char str[50];
printf("Enter a single Word: ");
scanf("%s\n", str);
User input and
printf("Entered Word is: %s", str); output for string
printf("Enter the complete sentence: ");
scanf("%[^\n]s", str);
printf("\nEntered Sentence is: %s", str);
return 0;
}
Format
specifiers in
C
Example
#include <stdio.h> unsigned int var;
int main() printf("Enter an integer:");
scanf("%u", &var);
{ int x; printf("Entered Unsigned Integer: %u", var);
printf("Enter the integer"); printf("Printing -%d using %u\n", var, -var);
scanf("%d", &x);
printf("Printed using d: %d\n", x); float a = 12.67;
printf("Using f: %f\n", a);
printf("Printed using i: %3i\n", x); printf("Using e: %e\n", a);
return 0; printf("Using E: %E", a);
}
int a = 67;
printf("%o\n", a);
printf("%x\n", a);
What do computers understand?
Number System: Represents a quantity through a set of Numbers