Notes on the C Programming language, many from the "C programming for beginners" course on Udemy.
C Programming For Beginners
Compilation
is two steps: pre-processing and the actual compilation.
the compiler translates assembly lang statements into actual machine instructions
the output from the compiler is known as
object code
(.obj
oro
extension)common command to compile is :
cc -c myprog.c
orgcc -c myprog.c
(the latter is the GNU c compiler.)
Linking
after creating the object code, it can be linked.
linking gets all dependencies in place and builds the executable file. All
.o
files will be linked.This usually happens at the same time as compiling when you use an IDE.
Linux will spit out a
myfile.out
Core concepts
The Preprocessor
it's fairly unique to C, and is advantageous
makes programs easier to read, modify and port.
it's part of the compilation process; happens before the program is analyzed.
preprocessor commands can be anywhere in your code but are usually at the top.
Preprocessor statements are identified by the
#
sign (a "preprocessor directive".)Preprocessors can be used to build your own library files, constants, macros,
can make more powerful programs with conditionals:
#ifdef
,#endif
,#else
,#ifndef
etc.
The #include
statement
is a preprocessor directive used in all C programs.
include files are usually header files b ecause it is usually included at the head of the document.
has a
.h
extension.using
#include "json.h"
means look in the current direcotry.using
#includ <json.h>
means look in the standard system directories.
Header files
they define information about some of the functions that are provided by that file.
header files usually have function prototypes (names of functions), typedefs, and constants.
executable code doesn't (usually) go into a header file.
Format specifiers
used for displaying variables as output.
they specify the type of the data that you want to display.
ex:
int sum = 89; printf("The sum is %d\n", sum)
for each type, printf expects a certain kind of datatype (
%i
,%e
,%c
) etc.
Format specifier | Datatype |
---|---|
%c | Character |
%d | Signed integer |
%e / %E | Scientific notation of floats |
%f | Float values |
%g / %G | Similar as %e or %E |
%hi | Signed integer (short) |
%hu | Unsigned Integer (short) |
%i | Unsigned integer |
%l / %ld / %li | Long |
%lf | Double |
%Lf | Long double |
%lu | Unsigned int or unsigned long |
%lli / %lld | Long / long |
%llu | Unsigned long long |
%o | Octal representation |
%p | Poi nter |
%s | String |
%u | Unsigned int |
%x or %X | Hexadecimal representation |
%n | Prints nothing |
%% | Prints % character |
Bitwise Operators
AND
,&
, ~OR~;, tests and set individual bits in an integer variable.These operate on the bits in integer values.
less commonly used.
Why? - it can store a lot of data in it.
You could use a single integer variable to store several characteristics of an "object"
you could use one bit, in an integer (which is made of multiple bits) to indicate on/off data.
a byte is 8 bits; an integer is usually 4 bytes (and thus, 32 bits)
Constants
Constants can be created using #define
, const
(in c90) or enums.
// Using the preprocessor (note, no use of "=") #define FOO 0.015 // using const const int foo 15
Strings
there is no standard datatype for strings in C.
strings are stored in an array of type
char
.Remember to add an extra length when declaring the string length - to make room for a null terminator (
\0
)
# you can put "7" in the array, but you should avoid this. Let the compiler set the size for you. # bad char word[7] = {"Hello"} # better char word[] = {"Hello"}
because strings are char arrays, you can't use operators on then.
thus, determing if two strings were the same would require checking each char at each point in the array.
and so, there are string functions in the standard library to avoid having to do this manually.
Use
#include <string.h>
to include string library functions.strcpy
,strncopy
- to change or modify a string. (try and usestrncopy
, it's safer)strlen
Pointers
pointers are a variable that store an address (in hexadecimal, usually)
the value of a pointer, is the address of another location in memory, that can contain a value.
A lot of string functions return pointers.