basics
Basic print code
Format specifier
Define type of variable to be displayed
Float 6 - 7 digits Double has double the digits 15 -16 digits
%d
For integers (decimal)
%u
For unsigned integers (decimal)
%o
For octal integers
%x
, %X
For hexadecimal integers (lowercase or uppercase letters)
%f
For floating-point numbers (decimal notation)
%e
, %E
For floating-point numbers (scientific notation with lowercase or uppercase "E")
%g
, %G
For floating-point numbers (use %e
or %f
, depending on which is more compact)
%c
For characters
%s
For strings
%p
For pointers (prints the memory address)
%n
For writing the number of characters written so far to an integer pointer argument
%i
Same as %d
, for integers
%a
, %A
For hexadecimal floating-point numbers (lowercase or uppercase letters)
%lu
For long unsigned integers (decimal)
%lld
, %ld
, %d
For long long integers, long integers, and regular integers respectively
%lldu
, %ldu
, %du
For unsigned long long integers, unsigned long integers, and unsigned integers respectively
%Lf
For long double floating-point numbers
%%
For printing the % symbol itself
Type conversion
Implicit conversion (automatic)
Done automatically by the compiler when you assign a value of one type to another
Explicit conversion (manual)
Done manually by placing the type in parentheses ( )
Constants
Variable with fixed value that cannot be altered
Operators
Augmented Assigned Operators
+=
-=
*=
%=
Conditional statements
While loop
Ternary operators
Switch case
break vs continue
Logical operators
Arrays
Strings
In C programming, there is no string
variable type. Instead, strings are represented as arrays of characters. Here's an example of how to declare and print a string in C:
Note that the string must be enclosed in double quotes and that the array must be big enough to hold the string, including the null terminator (\\0
) at the end.
String Functions
Functions C provides several built-in functions for working with strings. Here are a few examples:
strlen()
Returns the length of a string (excluding the null terminator).
strcpy()
andstrncpy()
Copies one string to another.
strcpy()
copies the entire string, whilestrncpy()
lets you specify the maximum number of characters to copy.strcat()
andstrncat()
Concatenates one string onto the end of another.
strcat()
concatenates the entire string, whilestrncat()
lets you specify the maximum number of characters to concatenate.strcmp()
Compares two strings and returns an integer indicating their relationship. Returns 0 if the strings are equal, a negative number if the first string is less than the second, and a positive number if the first string is greater than the second.
Single quote vs double quote
In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals.
<string.h>
Include header for string functions
NULL Character , \0
Used to indicate the end of a string
Array of Strings
Scanner (Input)
fgets
Read input from one line
<math.h>
Math header file
Functions
Function prototype
Declare function without body
Give information to compiler about function
Ensure that call to function is made with correct parameters
Structs
Similar to classes , BUT no methods
Typedef
In C, typedef
is used to create an alias for an existing data type. This can be useful for creating shorter or more descriptive names for types, or for creating more abstract types that can be changed more easily in the future.
For example, you could define a new name for the int
data type:
Now, you can use myInt
instead of int
:
You can also use typedef
to create a new name for a struct:
Now, you can create a new Student
struct like this:
This can make your code more readable and easier to understand, especially when dealing with complex data structures.
Enums
(Enumerations)
User defined values which consist of constant integers
Makes the program easier to read
Pseudo Random number
Bitwise Operators
Memory address
Last updated