Show Menu
Cheatography

C info and some basic characteristics Cheat Sheet by

This is a Cheatsheet about C and its basic characteristics

C Cheat sheet

Basic Types
Arithmetic types and are further classified into: (a) integer types and (b) floati­ng-­point types.
Enumerated types
Arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
The type void
Type specifier void indicates that no value is available.
Derived types
Include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

Data types

Integer Types
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
%c
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes
-32,768 to 32,767 or -2,147­,48­3,648 to 2,147,­483,647
%d or %i
unsigned int
2 or 4 bytes
0 to 65,535 or 0 to 4,294,­967,295
%u
short
2 bytes
-32,768 to 32,767
%hi
unsigned short
2 bytes
0 to 65,535
long
8 bytes
-92233­720­368­547­75808 to 922337­203­685­4775807
%li
unsigned long
8 bytes
0 to 184467440737095516
15
Floating-Point Types
Storage size
Value range
float
4 byte
1.2E-38 to 3.4E+38 (6DP)
%f
double
8 byte
2.3E-308 to 1.7E+308 (15DP)
%lf
long double
10 byte
3.4E-4932 to 1.1E+4932 (19DP)
%Lf
string
x50 char
 
%s
* DP = Decimal precision.

Operators

Operators
||/|
Or
&&/&
And
==
Equal to
!
Not
!=
Non equal to
Arithmetic Operat­ors
+
plus
-
rest
/
divide
*
product
%
reminder
++/ --
Increa­sin­g/d­ecr­easing
Comp­ari­son
<
lower than
<=
lower or equal than
>=
greater or equal than
>
greater than
 

Main Libraries and Functions

<a­sse­rt.h­>
Program assertion functions
<c­typ­e.h­>
Character type functions
<l­oca­le.h­>
Locali­zation functions
<m­ath.h>
Mathem­atics functions
<s­etj­mp.h­>
Jump functions
<s­ign­al.h­>
Signal handling functions
<s­tda­rg.h­>
Variable arguments handling functions
<s­tdi­o.h­>
Standard Input/­Output functions
<s­tdl­ib.h­>
Standard Utility functions
<s­tri­ng.h­>
String handling functions
<t­ime.h>
Date time functions
std functi­ons:
rand()
Returns a (predi­ctable) random integer between 0 and RAND_MAX based on the randomizer seed.
RAND_MAX
The maximum value rand() can generate.
srand(­uns­igned integer);
Seeds the randomizer with a positive integer.
(unsigned) time(NULL)
Returns the computer's tick-tock value. Updates every second.

i/o functions

scanf() and printf() functi­ons
printf( )
returns the number of characters printed by it.
scanf()
returns the number of characters read by it.
getc­har() & putchar() functi­ons
getchar()
reads a character from the terminal and returns it as an integer.
putchar()
displays the character passed to it on the screen and returns the same character.
gets() & puts() functions
gets()
reads a line from stdin­(s­tandard input) into the buffer pointed to by str pointer, until either a termin­ating newline or EOF (end of file) occurs.
puts()
writes the string str and a trailing newline to stdout.
The standard input-­output header file, named stdio.h contains the definition of the functions printf() and scanf(), which are used to display output on screen and to take input from user respec­tively.

ontrol structures and statements

Loop struct­ure

A loop structure is used to execute a certain set of actions for a predefined number of times or until a particular condition is satisfied. There are 3 control statements available in C to implement loop struct­ures. While, Do while and For statem­ents.

The while statement
Syntax for while loop is shown below:

while­(co­ndi­tio­n)// This condition is tested for TRUE or FALSE. Statements inside curly braces are executed as long as condition is TRUE
{
statement 1;
statement 2;
statement 3;
}

The condition is checked for TRUE first. If it is TRUE then all statements inside curly braces are executed. Then program control comes back to check the condition has changed or to check if it is still TRUE. The statements inside braces are executed repeat­edly, as long as the condition is TRUE. When the condition turns FALSE, program control exits from while loop.

The do while statement
Syntax for do while loop is shown below:

do
{
statement 1;
statement 2;
statement 3;
}
`while­(co­ndi­tion);

The for statement
Syntax of for statement is shown below:


for(i­nit­ial­ization statem­ent­s;test condit­ion­;it­eration statem­ents)
{
statement 1;
statement 2;
statement 3;
}
 

Control structures

Sele­ctive control struct­ure
Selection structures are used to perform ‘decision making‘ and then branch the program flow based on the outcome of decision making. Selection structures are implem­ented in C with If, If Else and Switch statem­ents.

The syntax format of a simple if statement is as shown below.

if (expre­ssion) // This expression is evaluated. If expression is TRUE statements inside the braces will be executed
{
statement 1;
statement 2;
}
statement 1; // Program control is transfered directly to this line, if the expression is FALSE
statement 2;

Syntax format for If Else statement is shown below.

if(ex­pre­ssion 1) // Expre­ssion 1 is evaluated. If TRUE, statements inside the curly braces are executed.
{ // If FALSE program control is transf­erred to immediate else if statem­ent.

statement 1;
statement 2;
}
else if(exp­ression 2)// If expression 1 is FALSE, expression 2 is evaluated.
{
statement 1;
statement 2;
}
else if(exp­ression 3) // If expression 2 is FALSE, expression 3 is evaluated
{
statement 1;
statement 2;
}
else // If all expres­sions (1, 2 and 3) are FALSE, the statements that follow this else (inside curly braces) is executed.
{
statement 1;
statement 2;
}
other statem­ents;

Switch statement

Switch is a multi branching control statement. Syntax for switch statement is shown below.

switc­h(e­xpr­ess­ion)// Expre­ssion is evaluated. The outcome of the expression should be an integer or a character constant
{
case value1: // case is the keyword used to match the intege­r/c­har­acter constant from expres­sion.
//value1, value2 ... are different possible values that can come in expres­sion
statement 1;
statement 2;
break; // break is a keyword used to break the program control from switch block.
case value2:
statement 1;
statement 2;
break;
default: // default is a keyword used to execute a set of statements inside switch, if no case values match the expression value.
statement 1;
statement 2;
break;
}
                                                       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.