1. When is the right time to use :
a. Struct
b. Pointer
c. Function
d. Array
2. Write an Example That uses Combination of the above!
Answer:
a. A structure is another user defined data type available in C that allows to combine data items of different kinds.
Structures are used to represent a record. whenever we want to create a new data type, struct is the correct way to use.
b.A pointer is a variable whose value is the address of another
variable, i.e., direct address of the memory location. Like any variable
or constant, you must declare a pointer before using it to store any
variable address. whenever we want to change the value of a variable using the address,
pointer is the correct way to use.
c.A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.whenever we want to create a certain instruction separated from the main() , function is the correct way to use.
d. An Array is a kind of data structure that can store a fixed-size sequential
collection of elements of the same type. An array is used to store a
collection of data, but it is often more useful to think of an array as a
collection of variables of the same type. whenever we want to create a storage of variable,
array is the correct way to use.
2.
and one with pointer :
source : www.tutorialspoint.com
Wikipedia
Hasil penelusuran
Sabtu, 05 Desember 2015
Minggu, 22 November 2015
stdarg.h in C
stdarg.his a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the headercstdarg.
- The contents of
stdarg.hare typically used in variadic functions, though they may be used in other functions (for example,vprintf) called by variadic functions.
- stdarg.h header defines a variable type va_list and three macros which can be used to get the arguments in a function when the number of arguments are not known i.e. variable number of arguments.
Library Variables
- va_list
Library Macros
This macro initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis. |
|
|
|
|
Example :
more at :
https://en.wikipedia.org/wiki/Stdarg.h
http://www.tutorialspoint.com/c_standard_library/stdarg_h.htm
Sabtu, 21 November 2015
File in C
A File represents a sequence of bytes, regardless of it being a text
file or a binary file. C programming language provides access on high
level functions as well as low level (OS level) calls to handle file on
your storage devices.
How to Create a File
FILE *fileptr // File pointer called fileptr
fileptr = fopen()
Opening a File
use the fopen( ) function to create a new file or to open an existing file.
If you are going to handle binary files, then you will use the ones with 'b' in the above.
Example :
Since line 6 - 9 already create the file., if we delete line 6-9, it will still compile the same result, because the file is already saved
You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character.
example :
more at :
http://www.tutorialspoint.com/cprogramming /c_file_io.htm
How to Create a File
FILE *fileptr // File pointer called fileptr
fileptr = fopen()
Opening a File
use the fopen( ) function to create a new file or to open an existing file.
FILE *fopen( const char * filename, const char * mode );
Here, filename is a string literal, which you will use to name your file, and access mode can have one of the following values −
| r/rb | :Opens an existing text file for reading purpose. | |
| w/wb | :Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file. | |
| a/ab | ;Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content. | |
| r+/r+b | ;Opens a text file for both reading and writing. | |
| w+/w+b | ;Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. | |
| a+/a+b; | Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended. |
Closing a File
To close a file, use the fclose( ) function. The prototype of this function is −int fclose( FILE *fp );
The fclose(-) function returns zero on success, or EOF if there is an error in closing the file.
Reading a File
int fgetc( FILE * fp );
The fgetc() function reads a character from the input file
referenced by fp. The return value is the character read, or in case of
any error, it returns EOF. The following function allows to read a string from a stream −char *fgets( char *buf, int n, FILE *fp );
The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.Example :
Since line 6 - 9 already create the file., if we delete line 6-9, it will still compile the same result, because the file is already saved
You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character.
example :
more at :
http://www.tutorialspoint.com/cprogramming /c_file_io.htm
Kamis, 19 November 2015
Structures in C
Structure is another user defined data type available in C that allows to combine data items of different kinds.
To define a structure,
you must use the struct statement.
The struct statement defines a new data type, with more than one member.
The format of the struct statement is :
You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
To define a structure,
you must use the struct statement.
The struct statement defines a new data type, with more than one member.
The format of the struct statement is :
struct [structure tag] { //*structure tag is optional member definition; member definition; ... member definition; } [one or more structure variables]; //*one or more structure variables also optional
And an Example of how to use Structure :
Structures as Function Arguments
You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
and it can be simplified like this :
more at : www.tutorialspoint.com
Pointer & Reference in C
What is a Pointer ?
A Pointer is a variable whose value is the address of another variable, or direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Format : type *var-name;
A Pointer is a variable whose value is the address of another variable, or direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Format : type *var-name;
type is the pointer's base type; it must be a valid C data type
var-name is the name of the pointer variable.
The asterisk (*) used to declare a pointer
Example of a Valid format :- int *ip; /* pointer to an integer */ - double *dp; /* pointer to a double */ - float *fp; /* pointer to a float */ - char *ch /* pointer to a character */
Example How To Use a Pointer:Relation Between Pointer and ArrayAn array name is a constant pointer to the first element of the array.Example :
Relation Between Pointer and FunctionC programming allows passing a pointer to a function.We just need to declare the function parameter as a pointer type.
here is an example where we pass a pointer to a function and change the value inside the function which reflects back in the calling function
source : www.tutorialspoint.comReferenceTheres also one more kind of passing, called Passing By ReferenceBasically, A reference is an alias for another variablethe syntax/ Format for this Reference is :'type &variable' and it only works on c++ compilerHeres what i learned For Example :
Minggu, 18 Oktober 2015
Arrays In C
- Array is a way to store variables in contingous space, that can be looped later
- Use Array when the number of inputs to be stored are too many
Look at the pic :
we can also write it this way :
To know the size of your Array, you can use function "sizeof" and divide it by one element of the array.
example :
Hope This Will Help
Peace !
Sabtu, 17 Oktober 2015
Recursion / Recursive Function in C
- Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows.
This program ends when we've counted to twenty, or more precisely, when count is
no longer less than twenty. This is a good base case because it means that if we
have an input greater than twenty, we'll stop immediately. If we'd chosen to
stop when count equaled twenty, then if the function were called with the input 21, it would run out of memory before stopping.
By the way, i wrote the function name as count_to_ten ., actually it should be count_to_twenty . but its just a name given by us, so i guess its okay. i forgot to modify that one
source :
Tutorialspoint : http://www.tutorialspoint.com/cprogramming/c_recursion.htm
Cprogramming : http://www.cprogramming.com/tutorial/c/lesson16.html
Hope this basic recursion example helps.
Peace!
Jumat, 16 Oktober 2015
Function , Arguments/Parameter in C
- Function can be described as a group of Code to do specified things.
- Argument = Parameter of the function
Defining a Function
return_type function_name( parameter list )
{
body of the function
}
- Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
- Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.
- Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
- Function Body: The function body contains a collection of statements that define what the function does.
Function is not always placed before int main ().
Every function usually have the return value,
i have explained it on the pic .
Here's an example without using return value,
usually we call it as a procedure.
source : http://www.tutorialspoint.com/cprogramming/c_functions.htm
Hope This Basic Functions will Help.
Peace!
Kamis, 01 Oktober 2015
Looping/ Iteration in C Programming
Sup' guys, today im going to share about Looping And Iteration In C Programming.
Loop is actually Looping, it allows you to iterate something until specified times, and stop at specified point.
to help you understand, see the Loop / Iteration Flowchart below:
in Looping, Basically i use :
-While
-For
-Do-While
i'll explain all of it to you , starting with
While .
for me, Example is the Best teacher. so ill give the example directly to you , jump to Code::Blocks !
*this example is about finding the average glass of water drunk per day in a week.
For.
*same example
Do-While.
*same example
Basically, Do While is a bit similar to While
the difference is in While, you state the loop condition first then give instruction. but in Do-Whle , you do the instruction first , Then state the loop condition
Example:
Hope this Helps
Peace !
Loop is actually Looping, it allows you to iterate something until specified times, and stop at specified point.
to help you understand, see the Loop / Iteration Flowchart below:
in Looping, Basically i use :
-While
-For
-Do-While
i'll explain all of it to you , starting with
While .
for me, Example is the Best teacher. so ill give the example directly to you , jump to Code::Blocks !
*this example is about finding the average glass of water drunk per day in a week.
For.
*same example
Do-While.
*same example
Basically, Do While is a bit similar to While
the difference is in While, you state the loop condition first then give instruction. but in Do-Whle , you do the instruction first , Then state the loop condition
Example:
Hope this Helps
Peace !
Selasa, 29 September 2015
Decision / Branch in C Programming
Hello , today im going to share about Decision making / branch in C Programming
In my Journey of learning C, what i used to make decision / branch is :
- IF - Else
- Switch - Case
If -Else :
heres a Flowchart of decision making to help you understand.
now lets move to the Code. im using Code::Blocks, Download it for free, see the tutorial in my last week's post.
this Tutorial is showing about giving a Grade A/B/C to According to your Score
And this is what Happened if i dont use break;
In my Journey of learning C, what i used to make decision / branch is :
- IF - Else
- Switch - Case
If -Else :
heres a Flowchart of decision making to help you understand.
now lets move to the Code. im using Code::Blocks, Download it for free, see the tutorial in my last week's post.
this Tutorial is showing about giving a Grade A/B/C to According to your Score
Switch-Case :
and for the Code :
Hope This pst will help.
Peace!
Minggu, 27 September 2015
C Data Types , Input & Output.
1. Data Types
C provides various data types that can be used in your programs.
As a beginner , I commonly use:
There are still a lot of data types in C , You can check wikipedia for more precise:
https://en.wikipedia.org/wiki/C_data_types
Here is a few i get from other resources :
https://developer.mbed.org/handbook/C-Data-Types
2. Input & Output
Input : In programming language input means to feed some data into program. This can be given in the form of file or from command line.
Output : In any programming language output means to display some data on screen, printer or in any file.
In input , I mostly use function "scanf" in the command line
and In output , the example i can give to you is function "printf". as a beginner ill show you the example of all the above explanation in 1 pic. this the tutorial for calculating the volume of a Sphere:
Other resources :
http://www.tutorialspoint.com/ansi_c/c_input_output.htm
Hope this will help ^^ .
Peace!
Input Redirection :
https://chortle.ccsu.edu/java5/Notes/chap22/ch22_2.html
C provides various data types that can be used in your programs.
As a beginner , I commonly use:
- int for most variables and "countable" things (for loop counts, variables, events) , Format : %d
- char for characters and strings , Format :%c / %s
- float for general measurable things (seconds, distance, temperature) , Format %f
- double as i know ,almost the same as Float, but more precise , Format %lf
There are still a lot of data types in C , You can check wikipedia for more precise:
https://en.wikipedia.org/wiki/C_data_types
Here is a few i get from other resources :
https://developer.mbed.org/handbook/C-Data-Types
2. Input & Output
Input : In programming language input means to feed some data into program. This can be given in the form of file or from command line.
Output : In any programming language output means to display some data on screen, printer or in any file.
In input , I mostly use function "scanf" in the command line
and In output , the example i can give to you is function "printf". as a beginner ill show you the example of all the above explanation in 1 pic. this the tutorial for calculating the volume of a Sphere:
Other resources :
http://www.tutorialspoint.com/ansi_c/c_input_output.htm
Hope this will help ^^ .
Peace!
Input Redirection :
https://chortle.ccsu.edu/java5/Notes/chap22/ch22_2.html
Jumat, 25 September 2015
C Programming Tutorial 1 , "Hello World"
C programming tutorial 1 , step by step :
1.Run Code::Blocks and Open:
2. Choose the C programming language since we're learning C language and Name your file
3.Tutorial 1 as shown in the Pic :
4. Here's the example of an Error code :
If its not clear , you can watch : https://www.youtube.com/watch?v=b00HsZvg-V0
Rabu, 23 September 2015
Tutorial Downloading + Installing Compiler Code::Blocks
1. Go to and Download
http://www.codeblocks.org/downloads/binaries
2. Run & Install The Program As Usual
3. Run Code::Blocks and Set The Compiler
4. Do as The Picture said
5. You Are Good to Go !
Happy Coding ! , Be Creative
If its Unclear, You can watch :
https://www.youtube.com/watch?v=UwoLE9caShM
Langganan:
Postingan (Atom)

































