User:Paradox-01/C: Difference between revisions
Paradox-01 (talk | contribs) (Created page with "==IDE== * CodeBlocks * Visual Studio ** Choose C++ template (empty project). Add new file but rename extension from cpp to c. ==.h== .h (header) files define functions and ar...") |
m (+cat) |
||
(5 intermediate revisions by one other user not shown) | |||
Line 3: | Line 3: | ||
* Visual Studio | * Visual Studio | ||
** Choose C++ template (empty project). Add new file but rename extension from cpp to c. | ** Choose C++ template (empty project). Add new file but rename extension from cpp to c. | ||
==Language== | |||
C exists in different versions, the so called [[wp:ANSI_C#History_and_outlook|standards]]: ANSI/C90, [https://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf C99], C11, ... | |||
==.h== | ==.h== | ||
.h (header) files define functions and are needed so they can be called independently from their appearance in the c file. | .h (header) files define functions and are needed so they can be called independently from their appearance (order) in the c file. | ||
==.c files== | ==.c files== | ||
Line 77: | Line 80: | ||
//#include <stdbool.h> | //#include <stdbool.h> | ||
//bool a = true; | //bool a = true; | ||
// C99 standard | |||
[https://stackoverflow.com/questions/8724349/difference-between-bool-and-bool-types-in-c _Bool b = 1;] | [https://stackoverflow.com/questions/8724349/difference-between-bool-and-bool-types-in-c _Bool b = 1;] | ||
Line 84: | Line 89: | ||
---- | ---- | ||
Arrays, pointers, conversions, globals, ... | |||
Arrays, pointers, conversions, globals, [https://stackoverflow.com/questions/10586003/try-catch-statements-in-c try catch simulation], [https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros?rq=1 macros] ... | |||
---- | |||
==Files== | |||
==Directories== | |||
_getcwd can be used to get program path when main function arg is not an option. | |||
#include <direct.h> // _getcwd | |||
#include <stdlib.h> // free, perror | |||
#include <stdio.h> // printf | |||
#include <string.h> // strlen | |||
int main( void ) | |||
{ | |||
char* buffer; | |||
// Get the current working directory: | |||
if ( (buffer = _getcwd( NULL, 0 )) == NULL ) | |||
perror( "_getcwd error" ); | |||
else | |||
{ | |||
printf( "%s \nLength: %zu\n", buffer, strlen(buffer) ); | |||
free(buffer); | |||
} | |||
} | |||
[[Category:Userspace]] |
Latest revision as of 16:37, 8 April 2023
IDE
- CodeBlocks
- Visual Studio
- Choose C++ template (empty project). Add new file but rename extension from cpp to c.
Language
C exists in different versions, the so called standards: ANSI/C90, C99, C11, ...
.h
.h (header) files define functions and are needed so they can be called independently from their appearance (order) in the c file.
.c files
main function
Every program has a main function which runs first.
int main() { //single-line comment /* multi-line comment */ return 0; }
Input args may and may not to present.
- argc = argument count
- argv = argument vector (1D array of arguments)
Even when the program is started without any command line arg there is a argv[0] that contains the full path of the program.
To print this out in the console a standard library is needed.
Functions of standard libs are accessible when their h file gets included with <>.
Functions of own source files have to be included with quote signs "".
#include <stdio.h> // needed to print in console //#include "anotherSourceFile.h"
int main(int argc, char* argv[]) { printf(argv[0]); return 0; }
Prints:
drive:\pathTo\program.exe
printf
Normally the type of a variable must match the type a function expects. Ergo conversions are needed.
By using placeholders such a %d this problem can be mitigated. \n creates a line break.
printf("%d\n", argc); printf("%s\n", argv[0]);
%d, %i int, short, long integer %x, %X int, short, long integer in hexadecimal format %f float, double decimal %e, %E float, double decimal with exponential format %c char one sign %s char* string
Variables
var name (identifier)
var type
- var types are keywords. Keywords cannot be used for own constructions such as variable or function names as they are needed by the compiler. And they must be lower case.
var value
Keywords
Variable types
bool
//#include <stdbool.h> //bool a = true;
// C99 standard _Bool b = 1;
Declaration
Initialization
Arrays, pointers, conversions, globals, try catch simulation, macros ...
Files
Directories
_getcwd can be used to get program path when main function arg is not an option.
#include <direct.h> // _getcwd #include <stdlib.h> // free, perror #include <stdio.h> // printf #include <string.h> // strlen int main( void ) { char* buffer; // Get the current working directory: if ( (buffer = _getcwd( NULL, 0 )) == NULL ) perror( "_getcwd error" ); else { printf( "%s \nLength: %zu\n", buffer, strlen(buffer) ); free(buffer); } }