User:Paradox-01/C: Difference between revisions
Paradox-01 (talk | contribs) mNo edit summary |
Paradox-01 (talk | contribs) mNo edit summary |
||
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== |
Revision as of 19:24, 20 June 2022
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; _Bool b = 1;
Declaration
Initialization
Arrays, pointers, conversions, globals, ...
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); } }