Functions and Character ============================ In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the basic properties of a character ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is Call by Value ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is Call by Reference ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Character : Syntax ` * :ref:`Character : FAQs ` * :ref:`Character : Basic Properties ` * :ref:`Character : Call by Value ` * :ref:`Character : Call by Reference ` * :ref:`Summary ` .. _funcs_n_ptrs_char_ex_0_0: .. tab-set:: .. tab-item:: Character : Syntax .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow char c; .. _funcs_n_ptrs_char_ex_0_1: .. tab-set:: .. tab-item:: Character : FAQs Consider a Character .. code-block:: c char c; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions about a Character .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many characters can be stored in ``c`` ? .. dropdown:: See Answer Number of Characters = 1 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bytes are there in this Character ? .. dropdown:: See Answer Number of Bytes = 1 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the sizeof the Character ? .. dropdown:: See Answer sizeof(a) = Number of Bytes = 1 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bits are there in this Character ? .. dropdown:: See Answer Number of bits = sizeof(c) * 8 = 1 * 8 = 8 bits .. _funcs_n_ptrs_char_ex_1: .. tab-set:: .. tab-item:: Character : Basic Properties Consider a character .. code-block:: c char c; Then below are the properties of a character =================== ======================================== Expression ? =================== ======================================== c ``c`` is a character &c ``&c`` is address of character ``c`` \*c NOT VALID sizeof(c) 1 Byte sizeof(&c) 8 Bytes typeof(c) ``char`` typeof(&c) ``char *`` =================== ======================================== .. _funcs_n_ptrs_char_ex_2: .. tab-set:: .. tab-item:: Character : Call by Value .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Character passed to a function * Step 1 : Define a character .. code-block:: c char c = 5; * Step 2 : Pass character to a function .. code-block:: c fun(c); * Step 3 : Define a function ``fun`` .. code-block:: c void fun(char c) { } * Step 4 : Change character inside function ``fun`` .. code-block:: c void fun(char c) { c = 10; } * See full program below .. code-block:: c #include void fun(char c) { c = 10; } int main(void) { char c = 5; printf("Before : c = %d\n", c); fun(c); printf("After : c = %d\n", c); return 0; } * See full program below .. code-block:: c Before : c = 5 After : c = 5 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow After function ``fun`` is called, * There are two stack frames * Stack frame of ``main`` * Stack frame of ``fun`` * variable ``c`` is created on stack frame of ``main`` * variable ``c`` is created on stack frame of ``fun`` * Even though name of variable ``c`` is same in ``main`` and ``fun``, they are two different variables in memory * Hence changing value of ``c`` inside function ``fun`` does not change the value of ``c`` inside ``main`` .. _funcs_n_ptrs_char_ex_3: .. tab-set:: .. tab-item:: Character : Call by Reference .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Address of Character passed to a function * Step 1 : Define a character .. code-block:: c char c = 5; * Step 2 : Pass address of character to a function .. code-block:: c fun(&c); * Step 3 : Define a function ``fun`` .. code-block:: c void fun(char *p) { } * Step 4 : Change character inside function ``fun`` .. code-block:: c void fun(char *p) { *p = 10; } * See full program below .. code-block:: c #include void fun(char *p) { *p = 10; } int main(void) { char c = 5; printf("Before : c = %d\n", c); fun(&c); printf("After : c = %d\n", c); return 0; } * Output is as below .. code-block:: c Before : c = 5 After : c = 10 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us solve it with equations method ! * Rule 1 : Base Rule .. code-block:: c p = &c .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * RHS is actual parameter. In this case ``&c`` is actual parameter * LHS is formal parameter. In this case ``p`` is formal parameter * Rule 2 : Move ``&`` from RHS to LHS. This becomes ``*`` on LHS .. code-block:: c *p = c * Rule 3 : Changing ``*p`` also changes ``c``. Because we proved ``*p`` is equal to ``c`` .. _funcs_n_ptrs_char_ex_4: .. tab-set:: .. tab-item:: Summary ================================ ========================= ========================================================================== Function Call Function Definition Observations ================================ ========================= ========================================================================== fun(c) void fun(char c) { } Changing ``c`` inside ``fun`` DOES NOT change value of ``c`` in caller fun(&c) void fun(char \*p) { } Changing ``*p`` inside ``fun`` CHANGES value of ``c`` in caller ================================ ========================= ========================================================================== .. card:: See Also * Other topics of character and functions * :doc:`./char` * :doc:`./char_sd_array` * :doc:`./char_dd_array` * :doc:`./char_td_array` * :doc:`./char_sp` * :doc:`./char_dp` * :doc:`./char_tp` * Current Module * :doc:`../funcs_n_ptrs` * Previous Module * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * Next Module * :doc:`../../memcpy_ptr/memcpy_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../const_ptr/const_ptr` * :doc:`../../void_ptr/void_ptr` * :doc:`../../array_of_ptr/array_of_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../function_ptr/function_ptr` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr` * :doc:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`