C Programming Language Cheat Sheet
C Programming Language Cheat Sheet
1. C Programming
1.1 What is C?
• C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972 by Dennis Ritchie.
• Portability
• Portability is the ability to port or use the software written .
• 5 is added by ANSI
main()
{
local variable deceleration
statement sequences
function invoking
}
1.4 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’.
• Keywords cannot be used for any other purpose (like variable names).
• 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.
• Data type defines a set of values that a variable can store along with a set of operations that can be performed on it.
#include<stdio.h>
main()
{
int sum;
sum=12;
sum=sum+5;
printf("Sum is %d",sum);
}
• char - character
• long - long integer
Share Tweet Send
• float - float number
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 %
• \a - Bell
• \n - New line
• \r - Carriage return
• \b - Backspace
• \f - Formfeed
• \t - Horizontal tab
• \' - Apostrophe
• \\ - Backslash
• \? - Question mark
• \0 - Null
scanf("Format string",&variable,&variable,...);
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
Assignment =, *=, /=, %=, +=, -=, &=, ^=, |=, <<=,>>= Right to Left
Unary & +, -, and * have higher precedence than the binary forms.
Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in
the ifstatement. For example all the following if statements are valid
The expression (3 + 2 % 5) evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf("This
works"); gets executed.
if (expression)
{
block of statement;
}
else
statement;
Note
• The group of statements after the if upto and not including the else is called an if block . Similarly,the statements
after the else form the else block .
• Notice that the else is written exactly below the if . The statements in the if block and those in the else block have
been indented to the right.
• Had there been only one statement to be executed in the if block and only one statement in the else block we could
have dropped the pair of braces.
• As with the if statement, the default scope of else is also the statement immediately after the else . To override this
default scope a pair of braces as shown in the above "Multiple Statements within if" must be used.
if (expression1)
statement;
else
{
if (expression2)
statement;
else
{
block of statement;
}
}
if(expression1)
statement;
else if(expression2)
statement;
else if(expression3)
statement;
else if(expression4)
{
block of statement;
}
else
statement;
The expressions are evaluated in order; if an expressionis true, the "statement" or "block of statement" associated with it is
executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group
of them in braces. The last else part handles the "none of the above" or default case where none of the other conditions is
satisfied.
switch (expression)
{
case constant-expression:
statement1;
statement2;
break;
case constant-expression:
statement;
break;
...
default:
statement;
}
• In switch…case command, each case acts like a simple label. A label determines a point in program which execution
must continue from there. Switch statement will choose one of case sections or labels from where the execution of the
program will continue. The program will continue execution until it reaches break command.
• break statements have vital rule in switch structure. If you remove these statements, program execution will continue to
next case sections and all remaining case sections until the end of switch block will be executed (while most of the time
we just want one case section to be run).
• default section will be executed if none of the case sections match switch comparison.
There are three methods for , while and do-while which we can repeat a part of a program.
The statements within the while loopwould keep on getting executed till the condition being tested remains true. When the
condition becomes false, the control passes to the first statement that follows the body of the while loop.
• Initialization part is performed only once at for loop start. We can initialize a loop variable here.
• Test condition is the most important part of the loop. Loop will continue to run if this condition is valid (true). If the
condition becomes invalid (false) then the loop will terminate.
• Run every time command section will be performed in every loop cycle. We use this part to reach the final condition for
terminating the loop. For example we can increase or decrease loop variable’s value in a way that after specified number
of cycles the loop condition becomes invalid and for loop can terminate.
do
{
statements;
}while (expression);
The statement is executed, then expression is evaluated. If it is true, statement is evaluated again, and so on. When the
expression becomes false, the loop terminates. Experience shows that do-while is much less used than while and for . A
do-whileloop is used to ensure that the statements within the loop are executed at least once.
In above example loop execution continues until either num>=20 or entered score is negative.
while (num<20)
{ Share Tweet Send
printf("Enter score : ");
scanf("%d",&scores[num]);
if(scores[num]<0)
break;
}
Continue statement can be used in loops. Like breakcommand continue changes flow of a program. It does not terminate
the loop however. It just skips the rest of current iteration of the loop and returns to starting point of the loop.
#include<stdio.h>
main()
{
while((ch=getchar())!='\n')
{
if(ch=='.')
continue;
putchar(ch);
}
}
In above example, program accepts all input but omits the '.' character from it. The text will be echoed as you enter it but the
main output will be printed after you press the enter key (which is equal to inserting a "\n" character) is pressed. As we told
earlier this is because getchar() function is a buffered input function.
Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some
deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly
since it only exits from the innermost loop. Thus:
for ( ... )
{
for ( ... )
{
...
if (disaster)
{
goto error;
}
}
}
...
error:
/* clean up the mess */
This organization is handy if the error-handling code is non-trivial, and if errors can occur in several places.
A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same
function as the goto. The scope of a label is the entire function.
5. Arrays
Arrays are structures that hold multiple variables of the same data type. The first element in the array is numbered 0, so the
last element is 1 less than the size of the array. An array is also known as a subscripted variable. Before using an array its type
and dimension must be declared.
int marks[30] ;
Here, int specifies the type of the variable, just as it does with ordinary variables and the word marks specifies the name of
the variable. The [30] however is new. The number 30 tells how many elements of the type int will be in our array. This
number is often called the "dimension" of the array. The bracket ( [ ] ) tells the compiler that we are dealing with an array.
Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this.
In scanf() function, we have used the "address of" operator (&) on the element marks[i] of the array. In so doing, we are
passing the address of this particular array element to the scanf() function, rather than its value; which is what scanf()
requires.
5.5 Example
Let us try to write a program to find average marks obtained by a
class of 30 students in a test.
#include<stdio.h>
main()
{
int avg, i, sum=0;
int marks[30] ; /*array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}
6 Strings
6.1 What is String?
Strings are arrays of characters. Each member of array contains one of characters in the string.
#include<stdio.h>
main()
{
char name[20];
printf("Enter your name : ");
scanf("%s",name);
printf("Hello, %s , how are you ?\n",name);
}
Output Results:
If user enters "Vineet" then the first member of array will contain 'V' , second cell will contain 'i' and so on. C determines end
of a string by a zero value character. We call this character as NULL character and show it with \0 character. (It's only one
character and its value is 0, however we show it with two characters to remember it is a character type, not an integer).
Equally we can make that string by assigning character values to each member.
name[0]='B';
name[1]='r';
name[2]='i';
name[3]='a';
name[4]='n';
name[5]='\0';
As we saw in above example placeholder for string variables is %s . Also we will not use a & sign for receiving string values.
• The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t
perform bounds checking on character arrays.
• scanf() is not capable of receiving multi-word strings. Therefore names such as "Vineet Choudhary" would be
unacceptable. The way to get around this limitation is by using the function gets() .The usage of functions gets() and
its counter part puts() is shown below.
#include<stdio.h>
main( )
{
char name[25] ;
printf ("Enter your full name ") ;
gets (name) ;
puts ("Hello!") ;
puts (name) ;
}
The program and the output are self-explanatory except for the fact that, puts() can display only one string at a time (hence
the use of two puts() in the program above). Also, on displaying a string, unlike printf() , puts() places the cursor on the
next line. Though gets() is capable of receiving only one string at a time, the plus point with gets() is that it can receive a
multi-word string.
If we are prepared to take the trouble we can make scanf() accept multi-word strings by writing it in this manner:
char name[25] ;
printf ("Enter your full name ") ;
scanf ("%[^\n]s", name) ;
Though workable this is the best of the ways to call a function, you would agree.
7. Functions
7.1 What is a Function?
A function is combined of a block of code that can be called or used anywhere in the program by calling the name. Body of a
function starts with { and ends with } . This is similar to the main function. Example below shows how we can write a
simple function.
#include<stdio.h>
/*Function prototypes*/
myfunc();
main()
{
myfunc();
}
/*Function Defination*/
myfunc()
{
printf("Hello, this is a test\n");
}
In above example we have put a section of program in a separate function. Function body can be very complex though. After
creating a function we can call it using its name. Functions can also call each other. A function can even call itself.
By the way pay attention to function prototypes section. In some C compilers we are needed to introduce the functions we
are creating in a program above the program. Introducing a function is being called function prototype .
Note:
• After each function has done its thing, control returns to main(). When main()runs out of function calls, the program ends.
• Writing functions avoids rewriting the same code over and over.
• Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program
can be divided into separate activities, and each activity placed in a different function, then each could be written and
checked more or less independently. Separating the code into modular functions also makes the program easier to design
and understand.
What is the moral of the story? Don’t try to cram the entire logic in one function. It is a very bad style of programming.
Instead, break a program into small units and write functions for each of these isolated subdivisions. Don’t hesitate to write
functions that are called only once. What is important is that these functions perform some logically isolated task.
#include<stdio.h>
/* use function prototypes */
sayhello(int count);
main()
{
sayhello(4);
}
sayhello(int count)
{
int c;
for(c=0;c<count;c++)
printf("Hello\n");
}
In above example we have called sayhello() function with the parameter 4 . This function receives an input value and
assigns it to count variable before starting execution of function body. sayhello() function will then print a hello message
count times on the screen.
This return value has a type as other values in C. It can be int , float , char or anything else. Type of this return value
determines type of your function.
Default type of function is int or integer. If you do not indicate type of function that you use, it will be of type int . As we told
earlier every function must return a value. We do this with return command.
int sum()
{
int a,b,c;
Share Tweet Send
a=1;
b=4;
c=a+b;
reurn c;
}
Above function returns the value of variable c as the return value of function. We can also use expressions in return command.
For example we can replace two last lines of function with return a+b; If you forget to return a value in a function you will
get a warning message in most of C compilers. This message will warn you that your function must return a value. Warnings
do not stop program execution but errors stop it.
In our previous examples we did not return any value in our functions. For example you must return a value in main()
function.
main()
{
.
.
.
return 0;
}
Default return value for an int type function is 0. If you do not insert return 0 or any other value in your main() function a
0 value will be returned automatically. If you are planning to return an int value in your function, it is seriously preferred to
mention the return value in your function header and make.
void test ()
{
/* fuction code comes here but no return value */
}
void functions cannot be assigned to a variable because it does not return value. So you cannot write:
a=test();
Output-
Foxed? Well, that is recursion for you in its simplest garbs. I hope you agree that it’s difficult to visualize how the control flows
from one function call to another. Possibly Following figure would make things a bit clearer.
#include<stdio.h>
int min(int a,int b);
main()
{
int m;
m=min(3,6);
printf("Minimum is %d",m);
return 0;
}
int min(int a,int b)
{
if(a<b)
Share Tweet Send
return a;
else
return b;
}
As you see you can add your variables to arguments list easily.
#include<stdio.h>
void test(int a);
main()
{
int m;
m=2;
printf("\nM is %d",m);
test(m);
printf("\nM is %d\n",m);
return 0;
}
void test(int a)
{
a=5;
}
In main() function, we have declared a variable m . We have assigned value 2 to m . We want to see if function call is able to
change value of m as we have modified value of incoming parameter inside test() function.
M is 2
M is 2
So you see function call has not changed the value of argument. This s is because function-calling method only sends value
of variable m to the function and it does not send variable itself. Actuallyit places value of variable m in a memory location
called stack and then function retrieves the value without having access to main variable itself. This is why we call this
method of calling "call by value".
#include<stdio.h>
void test(int *ptr);
main()
{
int m;
m=2; Share Tweet Send
printf("\nM is %d",m);
test(&m);
printf("\nM is %d\n",m);
return 0;
}
void test(int *ptr)
{
printf("\nModifying the value inside memory address%ld",&m);
*ptr=5;
}
To be able to modify the value of a variable used as an argument in a function, function needs to know memory address of the
variable (where exactly the variable is situated in memory).
In C programming language & operator gives the address at which the variable is stored. For example if m is a variable of
type int then &m will give us the starting memory address of our variable.We call this resulting address a pointer .
ptr=&m;
In above command ptr variable will contain memory address of variable m . This method is used in some of standard
functions in C. For example scanf function uses this method to be able to receive values from console keyboard and put it in
a variable. In fact it places received value in memory location of the variable used in function. Now we understand the reason
why we add & sign before variable names in scanf variables.
scanf(“%d”,&a);
Now that we have memory address of variable we must use an operator that enables us to assign a value or access to value
stored in that address.
ptr=&a;
*ptr=5;
Let’s look at our example again. We have passed pointer (memory address) to function. Now function is able to modify value
stored inside variable. If yourun the program, you will get these results:
M is 2
Modifying the value inside memory address 2293620
M is 5
So you see this time we have changed value of an argument variable inside the called function (by modifying the value inside
the memory location of our main variable).
Share Tweet Send
8. Pointers
8.1 What is a Pointer?
A pointer is a variable that contains the address of a variable. The main thing is that once you can talk about the address of a
variable, you'll then be able to goto that address and retrieve the data stored in it.
<variable_type> *<name>;
For example, you could declarea pointer that stores the address of an integer with the following syntax:
int *points_to_integer;
Notice the use of the *. This is the key to declaring a pointer; if you add it directly before the variable name, it will declare the
variable to be a pointer.
int i = 3;
We see that the computer has selected memory location 65524 as the place to store the value 3. The location number 65524 is
not a number to be relied upon, because some other time the computer may choose a different location for storing the value
3. The important point is, i’s address in memory is a number. We can print this address number through the following
program:
Address of i = 65524
Value of i = 3
Look at the first printf() statement carefully. & used in this statement is C’s "address of" operator. The expression &i
returns the address of the variable i , which in this case happens to be 65524. Since 65524 represents an address, there is no
question of a sign being associated with it. Hence it is printed out using %u , which is a format specifier for printing an
unsigned integer. We have been using the & operator all the time in the scanf() statement.
The other pointer operator available in C is * , called "value at address" operator. It gives the value stored at a particular
address. The "value at address" operator is also called "indirection" operator.
#include<stdio.h>
main()
{
int i = 3 ;
printf("\nAddress of i = %u",&i);
printf("\nValueof i = %d",i);
printf("\nValue of i = %d",*(&i));
}
Address of i = 65524
Value of i = 3
Value of i = 3
Note that printing the value of *(&i) is same as printing the value of i . The expression &i gives the address of the variable
i. This address can be collected in a variable, by saying,
j = &i ;
But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of
other variable ( i in this case). Since j is a variable the compiler must provide it space in the memory. Once again, the
following memory map would illustrate the contents of i and j.
As you can see, i's value is 3 and j's value is i's address. But wait, we can’t use j in a program without declaring it. And
since j is a variable that contains the address of i , it is declared as,
This declaration tells the compiler that j will be used to store the address of an integer value. In other words j points to an
integer. How do we justify the usage of * in the declaration,
int *j ;
Let us go by the meaning of * . It stands for "value at address". Thus, int *j would mean, the value at the address contained
in j is an int.
8.4 Example
Here is a program that demonstrates the relationships we have been discussing.
#include<stdio.h>
main( )
{
int i = 3 ;
int *j ;
j = &i ;
printf ("\nAddress of i = %u",&i) ;
printf ("\nAddress of i = %u",j) ;
printf ("\nAddress of j = %u",&j) ;
printf ("\nValue of j = %u",j) ;
printf ("\nValue of i = %d",i) ;
printf ("\nValue of i = %d",*(&i)) ;
printf ("\nValue of i = %d",*j) ;
}
Address of i = 65524
Address of i = 65524
Address of j = 65522
Value of j = 65524
Value of i = 3
Value of i = 3
Value of i = 3
9. Structures
A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for
convenient handling.
Once the new structure data type has been defined one or more variables can be declared to be of that type.
For example the variables b1, b2, b3 can be declared to be of the type struct book,
struct book
{
char name;
float price;
int pages;
};
as,
This statement sets aside space in memory. It makes available space to hold all the elements in the structure—in this case, 7
bytes — one for name, four for price and two for pages. These bytes are always in adjacent memory locations.
Like primary variables and arrays, structure variables can also be initialized where they are declared. The format used is quite
similar to that used to initiate arrays.
struct book
{
char name[10];
float price;
int pages;
};
b1.pages
b1.price
Share Tweet Send
Note that before the dot there must always be a structure variable and after the dot there must always be a structure element.
9.3 Example
The following example illustrates the use of this data type.
#include<stdio.h>
main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2, b3 ;
9.4 Summary
• A structure is usually used when we wish to store dissimilar data together.
• Structure elements can be accessed through a structure variable using a dot ( . ) operator.
• Structure elements can be accessed through a pointer to a structure using the arrow ( -> ) operator.
• All elements of one structure variable can be assigned to another structure variable using the assignment ( = ) operator.
• It is possible to pass a structure variable to a function either by value or by address.
FILE *fp;
fp is a pointer to a file.
The type FILE , is not a basic type, instead it is defined in the header file stdio.h , this file must be included in your program.
• r - read
• w - write, overwrite file if it ex ists
• a - write, but append instead of overwrite
• fp = fopen("data.dat","a"); - will open the disk file data.dat for writing, and any information written will be appended
to the file.
The following useful table from the ANSI C Rationale lists the different actions and requirements of the different modes for
opening a file:
fopen returns NULL if the file could not be opened in the mode requested. The returned value should be checked before any
attempt is made to access the file. The following code shows how the value returned by fopen might be checked. When the
file cannot be opened a suitable error message is printed and the program halted. In most situations this would be
inappropriate, instead the user should be given the chance of re-entering the file name.
#include <stdio.h>
int main()
{
char filename[80];
FILE *fp;
printf("File to be opened? ");
scanf("%79s", filename);
fp = fopen(filename,"r");
if (fp == NULL) Share Tweet Send
{
fpri ntf(stderr, "Unable to open file %s\n", filename);
return 1; /* Exit to operating system */
}
//code that accesses the contents of the file
return 0;
}
• ungetc(c, fp) - put a character back onto a file (only one character is guaranteed to be able to be pushed back)
• fopen( filename , mode) - open a file
• fclose(fp) - close a file
The standard header file stdio.h defines three file pointer constants, stdin , stdout and stderr for the standard input,
output and error streams. It is considered good practice to write error messages to the standard error stream.
The functions fscanf() and getc() are used for sequential access to the file, that is subsequent reads will read the data
following the data just read, and eventually you will reach the end of the file. If you want to move forwards and backwards
through the file, or jump to a specific offset from the beginning, end or current location use the fseek() function (though the
file mustbe opened in binary mode). The function ftell() reports the current offset from the beginning of the file.
If you wish to mix reading and writing to the same file, each switch from reading to writing (or vice versa) mustbe preceded by
a call to fseek() , fsetpos() , rewind() or fflush() . If it is required to stay at the same position in the file then use
fflush() or fseek(fp,0L,SEEK_CUR) which will move 0 bytes from the current position!
The function main() can have two arguments, traditionally named as argc and argv . Out of these, argv is an array of
pointers to strings and argc is an int whose value is equal to the number of strings to which argv points. When the
program is executed, the strings on the command line are passed to main() . More precisely, the strings at the command line
are stored in memory and address of the first string is stored in argv[0] , address of the second string is stored in argv[1]
and so on. The argument argc is set to the number of strings given on the command line.
Share Tweet Send
For example, in our sample program, if at the command prompt we give,
then,
#Primary #C Programming
Prev article
Strings - C Programming
Next article
Watch Microsoft Build 2016 Keynote
Discussion
Community Guidelines
Sign up now
LATEST ARTICLES
IOS 87
Primary 74
Swift 40
C Programming 39
Xcode 39
Newsletter
Stay up to date! Get all the latest &
greatest posts delivered straight to
your inbox
youremail@example.com
SUBSCRIBE
RELATED ARTICLES
Download and Install Code Blocks C & C++ IDE on Download and Install Borland C++ Compiler on
Windows 10 & 11 Windows 10 & 11
WIKI DOWNLOADS
C/C++ Compiler (gcc) for Android - Run C/C++ Download and Install Code Blocks C and C++ IDE
programs on Android on macOS
Compile C program with gcc compiler on Bash on Ubuntu on Windows 10 What is Digraphs, Trigraphs and Tokens? - C/C++ Programming Language
WIKI PRIMARY
Compile C program with gcc compiler on Bash on What is Digraphs, Trigraphs and Tokens? - C/C++
Ubuntu on Windows 10 Programming Language
C Programming C++ Data Structure Downloads iOS Microsoft Programming Puzzles Swift Programming Tech Wiki Xcode