Programming Language & Its Applications — Introduction to C Programming, NEC licence examination syllabus (Nepal Engineering Council).
printf is more configurable than most people ever discover, and scanf is more dangerous.
C has two families of I/O functions. The formatted ones (printf, scanf) interpret a format string and convert between text and binary representations. The unformatted ones (getchar, gets, puts) move characters with no conversion. The formatted family is far more useful and far more full of traps — particularly scanf, which is responsible for a large share of beginner bugs.
scanf for real input and reaches for fgets. The unbounded %s below is the same flaw that made gets() so dangerous it was deleted from the C standard in 2011 — the only function ever removed. And the format-string mechanism itself has its own vulnerability class: search "format string attack %n" to see how passing user input as a format string lets an attacker write to arbitrary memory.#include <stdio.h>
int main(void) {
float pi = 3.14159265f;
int n = 42;
char s[] = "NEC";
printf("[%f]\n", pi); /* default 6 decimals */
printf("[%.2f]\n", pi); /* 2 decimals */
printf("[%10.2f]\n", pi); /* width 10, right */
printf("[%-10.2f]\n", pi); /* width 10, LEFT */
printf("[%05d]\n", n); /* zero padded */
printf("[%+d]\n", n); /* forced sign */
printf("[%x] [%o]\n", n, n);
printf("[%10s] [%-10s]\n", s, s);
return 0;
}
Output:
[3.141593]
[3.14]
[ 3.14]
[3.14 ]
[00042]
[+42]
[2a] [52]
[ NEC] [NEC ]
scanf(" %c", &ch) is one of the most useful things to know in C. Numeric conversions leave the newline in the input buffer; %c then consumes it instead of waiting for real input, so the program appears to skip a prompt. A single space in the format string tells scanf to discard whitespace first.
#include <stdio.h>
#include <string.h>
int main(void) {
char name[50];
int age;
printf("Name: ");
if (fgets(name, sizeof name, stdin) != NULL) {
name[strcspn(name, "\n")] = '\0'; /* strip newline */
}
printf("Age: ");
if (scanf("%d", &age) != 1) { /* CHECK the return! */
printf("Invalid number\n");
return 1;
}
printf("%s is %d years old\n", name, age);
return 0;
}
Input: Manish Yadav
23
Output: Manish Yadav is 23 years old
#include <stdio.h>
int main(void) {
int c, letters = 0, digits = 0, spaces = 0, other = 0;
printf("Type text, then Ctrl-D:\n");
while ((c = getchar()) != EOF) { /* note: int, not char! */
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
letters++;
else if (c >= '0' && c <= '9') digits++;
else if (c == ' ' || c == '\n' || c == '\t') spaces++;
else other++;
}
printf("letters=%d digits=%d spaces=%d other=%d\n",
letters, digits, spaces, other);
return 0;
}
Input: Hello 123!
Output: letters=5 digits=3 spaces=1 other=1
Note int c, not char c. EOF is typically −1, which cannot be represented in an unsigned char and is indistinguishable from character 255 in a signed one. Declaring c as char makes the loop either never terminate or terminate early — a classic bug.
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…