You are on page 1of 7

WHAT IS VARIABLE PRIMITIVE DATA TYPES

A variable is an entity whose value may int, float, double and char
or may not change during the program

APTUTS
execution DATA TYPE SIZE
For 32-bit C compiler,
WHAT IS CONSTANT int occupies 4 bytes of memory space
A constant is an entity whose value float occupies 4 bytes of memory space

C PROGRAMMING can't be changed during program


execution
double occupies 8 bytes of memory
space

CHEATSHEET char occupies 1 byte of memory space

WHAT IS KEYWORD WHAT IS MODIFIER


10th Annual Tournament A keyword is the word reserved for C
About Modifier is used to re-define amount of
compiler and can only used to perform
memory space allocate to a variable
special task
Aptuts was founded by a software engineer
with 12+ years of experience in IT industry.
MODIFIERS IN C
VARIABLE CONSTRUCTION short, long, signed and unsigned
Our online courses are built in partnership
with technology experts and are relevant to RULES short can be used with int
the current education and industry needs. long can be used with int and double
A Variable name can consists of any
combination of alphabets, digits and signed and unsigned can be used with
www.aptuts.com int and char
underscores
The first character must either be

C Programming Course (86% Off)


alphabet or underscore. It should not COMMENTS IN C
start with the digit
Comments are used to explain or
No commas and blanks are allowed
describe source code logic.
Single line comment is represented
www.fb.com/Aptuts
by //
Programming by Aptuts Multi-line comments are enclosed
with-in /* and */
www.aptuts.com

VARIABLE DECLARATION ARITHMETIC INSTRUCTION IF STATEMENT


Syntax data-type variable_name; Arithmetic instructions in C are used to Syntax
Example int count; perform arithmetic operations on variables if(condition)
and constants {

ASSIGN VALUE TO A Example total = 100 + balance; /* block of code */


}
VARIABLE
Syntax variable_name = value; ARITHMETIC OPERATORS If condition returns true, execute the block
Example count = 10; + addition of code. Else, skip the block of code
- subtraction

BASIC C CODE * multiplication IF-ELSE STATEMENT


/ division
#include<stdio.h> Syntax
% modulo division
#include<stdlib.h> if(condition)
{

CONTROL INSTRUCTION
int main() /* 1st block of code */
{ } else {
Control instructions in C are used to alter /* 2nd block of code */
// program body
the logical flow of program execution. }
return 0;
}
if statement If condition is true, execute 1st block of code

TYPES OF INSTRUCTIONS if-else statement


conditional operator
and if condition is false, execute 2nd block
of code
Type declaration Instruction goto statement
Arithmetic Instruction
Control Instruction
switch statement
while loop
CONDITIONAL OPERATOR
do-while loop Syntax condition ? result1 : result2;

TYPE DECLARATION for loop


If condition is true, evaluate result1 and
break statement
Type Declaration instruction is used to return the value of result1. If condition is
continue statement
declare the type of variables used in C false, evaluate result2 and return the value
Example float amount; of result2
www.aptuts.com

SWITCH STATEMENT DO-WHILE LOOP FOR LOOP


Switch statement accepts expression and Syntax Syntax
executes a particular case matching the do for (initialization; condition; increment/decrement)
expression value { {
/*block of statement*/ /*block of statement*/

Syntax } while(condition); }
switch(expression) {
case constant 1: Here, the block of statement after the The for loop in C is executed as follows:
//perform this; keyword "do" is executed at least once. After
case constant 2: the execution of block of statement for the Step 1: The initial counter value is initialized.
//perform this; first time, the condition in the "while" is This initialization is done only once for the
. evaluated. entire for loop
.
. Block of statement will be repeated until Step 2: Test condition is checked. Test
condition evaluates to false condition can be any relational or logical
default: expression. If the test condition is satisfied
//perform this i.e. the condition evaluates to true then the
} BREAK STATEMENT block of statement inside the for loop is
executed
The break statement is used to break from
any kind of loop and switch statement
WHILE LOOP Step 3: After the execution of the block of
statement, increment/decrement of the
Syntax syntax break;
counter is done
while(condition)
{ Example
Step 4: After performing this, the test
/*block of statement*/ for(i=0;i<10;i++)
condition is again evaluated. The steps 2
} {
and 3 are repeated till the test condition
/*block of statement*/
returns false
Here, block of statement will be repeated if(i==5)
until condition evaluates to false break;
}
C Programming Course (86% Off)
www.aptuts.com

GOTO STATEMENT FUNCTIONS FUNCTIONS (CONT.)


goto statement is used for unconditional Functions are self contained block of Example of a function that returns a value
jump from one part of the program to statements that perform a particular task. C and with parameters
another part of the program functions increases the readability of the
program. Syntax
The goto statement consists of two parts: data-type function_name(parameters)
label and goto keyword. Example of a function that does not return a {
value and has no parameters // Function with parameters
// block of statements
CONTINUE STATEMENT Syntax }
continue statement is used to force next void function_name()
iteration of the loop. { Example
// Parameters are optional int add(int num1, int num2)
continue statement can be used with for // block of statements {
loop, while loop and do-while loop } return num1 + num2;
}
syntax continue; Example
void display() Function can be called using function name
Example { with parameters (if any)
for(i=0;i<10;i++) printf("This is a function");
{ } Example
/*block of statement*/ int main()
if(i==5) This function can be called from main() {
continue; using function name as follows: int sum;
} { sum = add(2, 3); //function call
display(); return 0;
} }

C Programming Course (86% Off)


www.aptuts.com

ARRAYS STRUCTURES POINTERS


An array is a collective name given to a Structure contains a number of data types A pointer in C is a variable that represents
group of similar variables. Arrays can be 1- grouped together. the location (rather than the value) of a data
Dimensional, 2-Dimensional, 3-Dimensional Syntax item.
and so on. struct struct-name
{ Syntax data-type *name;
Syntax for 1 -D array Element 1;
data-type arrayName[count] = {value1, Element 2; Example int *p;
.
value2, value3, ..., valueN}; .
.

Element n; Here, I have declared a pointer variable


Example of 1-D array } variable1, variable2, . . ., variableN; having data-type int. This pointer *p will
int arr[3]={10, 20, 30}; point to an address as shown below

Example 1
Here, arr[0] = 10, arr[1] = 20 and arr[2] = 30 struct animal int main()

{ {
Example of 2-D array int age; int a = 10;

char gender; int *p;


int A[3][3]={ };
{11,12,13}, //declaring structure variables p = &a;
{14,15,16}, struct animal a1, a2; //pointer p is now pointing to
{17,18,19} address of an int variable "a"
} Values can be accessed using
a1.age, a1.gender, a2.age and a2.gender //I can access value of a using *p
2-D array can be accessed using row and printf("%d", *p);
column index. Example 2 (declaring structure variables
along with structure) return 0;
For example, A[0][0] will return value 11 struct animal
similarly, A[1][1] will return value 15 i.e., value { }
at row index 1 and column index 1. int age;
char gender;
Note that index in an array always starts } a1, a2; C Programming Course (86% Off)
with 0
www.aptuts.com

STRINGS STRINGS (CONT.) FILE OPERATIONS (CONT.)


A string in C is a series of characters in a Example 3 Reading from a FILE
group that occupy contiguous memory. char *name = “This is another way to
A group of characters (Alphabets, digits and declare a string”; ch = fgetc(fp);
special characters) is called as a string.
In order print a string, you can make use of To read the file’s contents from the memory,
Example of a string printf statement with %s format specifier as there exists a function called fgetc().
"This is a string" shown below
fgetc() performs the following operations:
A string in C should always be enclosed with printf(name); Reads the characters from the current
in double quotes ( " ) printf("%s", name); pointer position
printf("%s",&name[0]); Advances the pointer position so that it
Syntax for declaring string now points to the next character
char stringName[num_of_characters]; Returns the character that is read, which
FILE OPERATIONS we collected in the variable ch
Example 1
Opening a FILE
char name[20] Closing the FILE

FILE *fp;
The above statement declares an array fclose(fp);
fp = fopen("file.c","r");
named “name” (or string named "name)
capable of holding 20 characters. The above statement will close the file
The above statement would open file
named “file.c” in “read” mode. It
Example 2
tells the computer that the file being
char name[]=”This is a string”;
opened would be used for reading
purpose only.
Here the size of an array is calculated
automatically

C Programming Course (86% Off)


APTUTS

WHY APTUTS?
Take the online courses with the
best personalized support
Learn by doing. Our courses are
hands-on centric
ABOUT FEEL FREE TO PRINT We help you grow professionally
100% money back guarantee
Aptuts was founded by a software AND SHARE THIS
engineer with 11+ years of experience in
IT industry. CHEATSHEET ON C
Our online courses are built in
PROGRAMMING
partnership with technology experts and
are relevant to the current education C Programming Course (86% Off)
and industry needs.

www.aptuts.com

www.fb.com/Aptuts

Programming by Aptuts

You might also like