Basics
Coursera, C programming for everyone. cc4e.com/book/chap01.md
scanf
This reads the input and converts accordingly to the variables.
#include <stdio.h>
int main() {
int usf, euf;
printf("Enter US Floor\n");
scanf("%d\n", &usf);
euf = usf - 1;
printf("EU Floor %d\n", euf);
}
This can also be used to read in new lines. Inside the brackets are the regular expressions. Everything before a new line character.
#include <stdio.h>
int main() {
char line[1024]; // Assuming a maximum input line length of 1024 characters
printf("Enter line\n");
scanf("%[^\n]", line); // Reads until a newline is encountered
printf("Line: %s\n", line);
return 0;
}
fgets()
An alternative way to use fgets().
stdinis the predefined constant. This is the file handle.
#include <stdio.h>
int main() {
char line[1024]; // Assuming a maximum input line length of 1024 characters
printf("Enter line\n");
fgets(line, sizeof(line), stdin); // Reads a line from standard input
printf("Line: %s\n", line);
return 0;
}
Some other functions…
gets()receives whatever input excluding the\nchar, that’s why we can compare it to"done".strcmpreturns any numbers. When two strings are equal, it returns0, and unpredictable sign when they are unequal.atoirequires the#include <stdlib.h>. This converts a string of integer to integer.- We need
!=NULLbecause whengets()encounters EOF or an error, aNULLwill be returned
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int first = 1;
int val, maxval = 0, minval = 0; // Initialize maxval and minval
char rawInput[1000]; // Increased buffer size
while(gets(rawInput) != NULL) {
if (strcmp(rawInput, "done") == 0) {
break;
}
val = atoi(rawInput);
if (first) {
maxval = val;
minval = val;
first = 0;
} else {
if (val > maxval) maxval = val;
if (val < minval) minval = val;
}
}
if (!first) { // Check if at least one number was entered
printf("Maximum %d\n", maxval);
printf("Minimum %d\n", minval);
} else {
printf("No numbers entered.\n");
}
return 0;
}
Termination of Input
The termination can be controlled by the EOF constant. On Windows, for example, If we press ctrl + Z and then press Enter, the program will be terminated.
#include "stdio.h"
int main() {
float total, input;
int n, result;
while (1) {
result = scanf("%f", &input);
if (result == EOF) {
printf("User Terminated");
break;
}
printf("You Entered %f\n", input);
}
return 0;
}
Reading Files
Omitted.
Functions
int mymult(int a, int b) {
int c = a * b;
return c;
}
Even if we don’t declare the int for a and b. There will be no bug because for unspecified types, int will be applied.
String
No string in C, just array of characters marked by a zero as the termination.
"" and '' are very different things, as "" means the string array, only single quote is the char.
Example: remove the extra spaces
#include <stdio.h>
int main() {
int c, prevc;
prevc = EOF; // Initialize prevc to EOF
while ((c = getchar()) != EOF) {
if (c == ' ' && prevc == ' ') {
continue; // Skip the loop iteration if the current and previous characters are spaces
}
putchar(c); // Output the current character
prevc = c; // Update prevc to the current character
}
return 0;
}
Types, Operators, Expressions
int i,j; i = 42; j = i++;assignment happens first and then the increment happens- Use
[condition] ? [if true] : [if false]to replace the if and else.