C Programming Cheat Sheet
Alpha_Edit
Posted on March 31, 2021
Here is a Cheat Sheet for C Programming Language.
1. About C Programming Language.
1.1 What is C Programming Language?
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.
1.2 Why was C programming language was developed?
It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.
1.3 Features of C Programming Language.
- Structured language.
- General purpose language.
- Portability.
- Code Re-usability & Ability to customize and extend.
- Limited Number of Key Word.
- Mid-level programming language.
1.4 C program Structure.
pre-processor directives
global declarations
main()
{
local variable deceleration
statement sequences
function invoking
}
1.5 C Keywords
Keywords are the words whose meaning has already been explained to the C compiler. There are only 32 keywords available in C. The keywords are also called ‘Reserved words’. When the current programming language is C or C++, these keywords cannot be abbreviated, used as variable names, or used as any other type of identifiers.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
1.6 C Character Set
The C character set consists of upper and lowercase alphabets, digits, special characters and white spaces, altogether called as the alphanumeric character. This can denotes any alphabet, digit or special symbol used to represent information. Following are the valid alphabets, numbers and special symbols allowed in C.
1.7 Rules for Writing, Compiling and Executing the C program
- C is case sensitive means variable named "COUNTER" is different from a variable named "counter".
- All keywords are lower cased.
- Keywords cannot be used for any other purpose (like variable names).
- Every C statement must end with a ;. Thus ;acts as a statement terminator.
- First character must be an alphabet or underscore, no special symbol other than an underscore, no commas or blank spaces are allowed with in a variable, constant or keyword.
- Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.
- Variable must be declared before it is used in the program.
- File should be have the extension .c
- Program need to be compiled before execution.
1.8 Data types & Placeholders
- C has 5 basic built-in data types.
- Data type defines a set of values that a variable can store along with a set of operations that can be performed on it.
- A variable takes different values at different times.
- General form for declaring a variable is:
type name;
. - An example for using variables comes below:
main()
{
int sum;
sum=12;
sum=sum+5;
printf("Sum is %d",sum);
}
printf
function will print the following:
Sum is 17
In fact %d
is the placeholder for integer variable value that its name comes after double quotes.
- Common data types are:
- int - integer
- char - character
- long - long integer
- float - float number
- double - long float
- Other placeholders are:
Placeholders Format
%c Character
%d Signed decimal integer
%i Signed decimal integer
%e Scientific notation[e]
%E Scientific notation[E]
%f Decimal floating point
%o unsigned octal
%s String of character
%u unsigned decimal integer
%x unsigned Hexadecimal (lower)
%X unsigned Hexadecimal (upper)
%p dispaly a pointer
%% print a %
1.9 Control characters (Escape sequences)
Certain non printing characters as well as the backslash () and the apostrophe('), can be expressed in terms of escape sequence.
-
\a
- Bell -
\n
- New line -
\r
- Carriage return -
\b
- Backspace -
\f
- Formfeed -
\t
- Horizontal tab -
\"
- Quotation mark -
\v
- Vertical tab -
\'
- Apostrophe -
\\
- Backslash -
\?
- Question mark -
\0
- Null
1.10 Receiving input values from keyboard
scanf
function used to receiving input from keyboard.
General form of scanf
function is :
scanf("Format string",&variable,&variable,...);
Format
string contains placeholders for variables that we intend to receive from keyboard. A &
sign comes before each variable name that comes in variable listing. Character strings are exceptions from this rule. They will not come with this sign before them.
Note: You are not allowed to insert any additional characters in format string other than placeholders and some special characters. Entering even a space or other undesired character will cause your program to work incorrectly and the results will be unexpected. So make sure you just insert placeholder characters in scanf format string. The following example receives multiple variables from keyboard.
float a;
int n;
scanf("%d%f",&n,&a);
Pay attention that scanf function has no error checking capabilities built in it. Programmer is responsible for validating input data (type, range etc.) and preventing errors
Posted on March 31, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.