Mithat Konar
Derived from Differences Between C and C++ by Robert Niemann
2020-05-03
<stdio.h>
input/output<stdlib.h>
standard utility functions<string.h>
string operations<ctype.h>
character class tests<math.h>
math functionsprintf()
write a formatted string to the console scanf()
read a formatted string from the keyboardputchar()
write a single character to the consolegetchar()
read a single character from the keyboardputs()
write a string to the consolegets()
read a string from the keyboardFILE
:FILE *fp; /* create a pointer to FILE */
fopen()
, fclose()
open/close a text file.feof()
detect end-of-file marker in a file.fscanf()
read formatted string from a file.fprintf()
write formatted string to a file.fgets()
, fputs()
read/write a string.fgetc()
, fputc()
read/write a single character.cout
cin
string
class
int
instead// single line comments
(in older versions of C)/* multi-line syntax */
instead#include <string.h> /* needed to use strlen() function */ int main(void) { char s1[81] = {'H', 'e', 'l', 'l', 'o', '\0'}; char s2[81] = "Hello World"; char s3[] = "O hai."; int len1 = strlen(s1), /* 5 */ len2 = strlen(s2), /* 11 */ len3 = strlen(s3); /* 6 */ ...
string.h
to operate on strings.int main( ) { int a,b,c; float x,y,z; ... }
/* int foo(); <--optional */ int main() { foo(); return 0; } int foo() { printf( "Hello world" ); }
const
modifier is available only in newer versions of C.#define
preprocessor directive typically used instead.#define PI 3.1415 #define TAX_RATE 0.065
#define
also often used to create function-like macros.#define square(x) ((x) * (x))
int myInt = 42; double myDouble; myDouble = (double)myInt/3; /* cast myInt to a double */
static_cast<>
, etc.malloc()
and free()
.struct MyStruct { double x; double y; }; struct MyStruct aStructInstance; /* requires struct keyword */
struct
keyword when defining struct
variables.