0% found this document useful (0 votes)
6 views17 pages

Lecture Week 3.2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

Lecture Week 3.2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Contents

Input/Output in C Format Number Systems Operator


Specification
Input and output in C
Basic Methods : scanf()

C performs input and output operations using stdio.h or standard input


output library
scanf()
• The scanf() reads the value from the console as per the type specified and
store it in the given address.
scanf("%X", &variableOfXType);

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

Decimal Number representation


• Examples are:
• 762
Types of Number System
Number System Cont..

From Decimal to Binary From Binary to Decimal


• Examples are: • Examples are:
• (15)10 to ( )2 • (1111)2 to ( )10
• (17.35)10 to ( )2 • (10001.1011)2 to ( )10
Number System Cont..: Try your own

From Decimal to Binary From Binary to Decimal


• (256)10 to ( )2 • (100000000)2 to ( )10
• (198.29)10 to ( )2 • (1011.011)2 to ( )10
Number System Cont..
Char to ASCII ASCII to Char
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
char c = ‘a’; int c = 100;
int asciiValue = (int)c; char asciiValue = (char)c;
printf("%d\n", asciiValue); printf("%c\n", asciiValue);
return 0; return 0;
} }
Upcoming lecture
Operators, Conditional statement
and Loop

You might also like