0% found this document useful (0 votes)
2 views

C Programming Input Output

The document outlines additional topics covered in the CS1101 course at IIT Patna, including input/output functions like scanf and printf, variable scope, enum specifications, bitwise operations, and multifile programming. It provides detailed examples of using scanf for various data types and formats, as well as printf for formatted output. The document also discusses storage classes and their implications in programming.

Uploaded by

ABHISHEK GOUTAM
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)
2 views

C Programming Input Output

The document outlines additional topics covered in the CS1101 course at IIT Patna, including input/output functions like scanf and printf, variable scope, enum specifications, bitwise operations, and multifile programming. It provides detailed examples of using scanf for various data types and formats, as well as printf for formatted output. The document also discusses storage classes and their implications in programming.

Uploaded by

ABHISHEK GOUTAM
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/ 19

CS1101: Foundations of Programming

Additional topics

Dept. of Computer Science & Engineering


Indian Institute of Technology Patna

IIT Patna 1
Topics covered
• Input - scanf
• Output - printf
• Scope of variables
• enum specification
• Bitwise operation
• Multifile programming

IIT Patna 2
scanf - 1
• Conversion character, we have seen d, f, s, c. Please consult books for
more information

e,f,g floating point value


h short int
i decimal, hexadecimal, octal
o octal
u unsigned decimal integer
X hexadecimal
[...] data item is a string which may include white spaces

IIT Patna 3
scanf - 2
• The following prevents any previously entered extraneous characters from be-
ing assigned
scanf(" %c %d %f", &c, &x, &y);

IIT Patna 4
scanf - 3
• Reading a string that can contain spaces and upper cases letters, eg. name of
person
char str[80];
scanf(" %[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]s", str);
• Input: IIT Patna — only IIT P will get stored in str

IIT Patna 5
scanf - 4
• Reading a string that can contain spaces and upper cases letters, eg. name of
person
char str[80];
scanf(" %[^\n]s", str);
• Input: IIT Patna — everything will get stored
• ^ — denotes negation

IIT Patna 6
scanf - 5
int a, b, c;
scanf(" %3d %3d %3d", &a, &b, &c);

Case-1: Case-2:
123 123 456 789
a=1, b=2, c=3 a=123, b=456, c=789

Case-3: Case-4:
123456789 1234 5678 9
a=123, b=456, c=789 a=123, b=4, c=567

IIT Patna 7
scanf - 6
float x;
char c;
scanf(" %5f %c", &x, &c);
printf("x=%f, c=%c", x, c);

Input:
256.875 T
Output:
x=256.8, c=7

IIT Patna 8
scanf - 7
int mm, dd, yyyy; Input:
scanf(" %d/%d/%d", &dd, &mm, &yyyy); 10/11/2024
printf("Date: %d/%d/%d", dd, mm, yyyy); Output:
Date: 10/11/2024

int x; float y; Input:


scanf(" %d A %f", &x, &y); 8 A 3.4
printf("x=%d y=%f", x, y); Output:
x=8 y=3.4000000

IIT Patna 9
scanf - 8
char c1, c2, c3; Input:
scanf(" %c%c%c", &c1, &c2, &c3); abc
printf("c1=%c c2=%c c3=%c\n", c1, c2, c3); Output:
c1=a c2= c3=b

char c1, c2, c3; Input:


scanf(" %c%1s%1s", &c1, &c2, &c3); abc
printf("c1=%c c2=%c c3=%c\n", c1, c2, c3); Output:
c1=a c2=b c3=c

IIT Patna 10
printf - 1
• Conversion character, we have seen d, f, s, c. Please consult books for
more information

e floating point with exponent


f floating point without exponent
g floating point with no trailing zeros
i signed decimal integer
o octal without leading zeros
u unsigned decimal integer
X hexadecimal without leading 0x
% no argument, print %

IIT Patna 11
printf - 2
float x=5000, y=0.0025;
printf("%f %f \n", x, y);
printf("%e %e \n", x, y);

Output:
5000.000000 0.002500
5.000000e+03 2.500000e-03

IIT Patna 12
printf - 3
char str[80];
scanf("%[^\n]s",str);
printf("%s\n", str);

Input:
Happy Diwali!!
Output:
Happy Diwali!!

IIT Patna 13
printf - 4
int i=12345; float x=345.687;
printf(":%3d:%8d:\n",i, i);
printf(":%3f:%8f:\n",x, x);
printf(":%7f:%7.3f:%7.1f:\n",x, x, x);

Output:
:12345: 12345:
:345.678000:345.678000:
:345.678000:345.678: 345.7:

IIT Patna 14
printf - 5
• Printing ”Hello, world” (12 characters)

:%s: :hello, world:


:%10s: :hello, world:
:%.10s: :hello, wor:
:%-10s: :hello, world:
:%.15s: :hello, world:
:%-15s: :hello, world :
:%15.10s: : hello, wor:
:%-15.10s: :hello, wor :

IIT Patna 15
Storage class
• auto int a, b, c; — This is similar to local variables.
• register int a; — To store the value in (if possible) fast registers of the
machine. Applicable to automatic variables
• static int a; — A static variable inside a function is similar to other local
variables but unlike automatics, they remain in existence rather than coming
and going each time the function is activated. Private but permanent storage
• extern int a; — Usually referred as global variable. Used for external link-
age, more meaningful for multifile programming

IIT Patna 16
enum specification
• This is a kind of specification of constant integer
• Example:
• enum {NO, YES}; // NO is 0, and YES is 1
int x = 0;
if(x == NO){ ... }
• enum {Jan=1, Feb=2, ..., Dec=12};
int x = 12;
if(x == Dec){ ... }

IIT Patna 17
Bitwise operations
• In computer, all numbers are binary. C provides some support to perform bit-
wise manipulations
• & — bitwise AND, | — bitwise OR, ^ — bitwise XOR,
<< — left shift, >> — right shift, ~ — bitwise complement
unsigned int x; scanf("%u", &x); printf("%u = ",x);
unsigned int mask = 1 << 31; // left shift, multiplication by 231
for(int i=0; i<32; i++){
putchar(x & mask ? '1' : '0' );
x <<= 1; // shift left by 1
if( i % 8 == 0) putchar(' ');
}

IIT Patna 18
myheader.h
Multifile programming file1.c
#ifndef __MYHEADER_H__ #include "myheader.h"
#define __MYHEADER_H__ int gVar=1;
#include<stdio.h> void f1(int x){
typedef struct { gVar++;
int x; int y; printf("File: file1.c\n");
} point; printf("x=%d\n",x);
void f1(int); printf("gVar=%d\n",gVar);
void f2(point); }
#endif
file2.c
main.c #include "myheader.h"
#include "myheader.h" extern int gVar;
extern int gVar; void f2(point x){
int main(){ printf("gVar=%d\n",gVar);
point z = (point) {2, 3}; printf("x=%d y=%d\n",x.x, x.y);
f1(3); gVar++;
f2(z); }
printf("gVar=%d\n",gVar);
} $> gcc main.c file1.c file2.c

IIT Patna 19

You might also like