Posted in: E Learn

Programming C- Notes

Basic Structure of Programming C

In the C language there is no specific method to write a program, C is a case sensitive language, that’s why we must write all statements in small letters as below

Function of printf()

printf ( ) function is used to print or display the values of variable using standard output device. We can write printf () function as below

here a1 and a2 are the two variables, whose values are using to display on the monitor.

And “string format” is a control string which can represents the format specification.

Example:

When this function will run, the SUM will be displayed on a new line because of “\n” and the format specifier is set as Integer type with “%d” and the value of a variable which is stored in “a2” will be displayed on the screen.

Function of scanf()

scanf( ) function is used to read the values of a variable using any standard input device (like keyboard)

here a1 and a2 are two variables, whose values are to be read from input keyboard.

“format string” is a control string which represents the format specification.

And “&” is a symbol to indicate the memory location that where the value is to be stored.

Example:

This scanf() function will read the integer value of variables a1 and a2.

“%d” is a format specifier to get integer value input

And “&a1” “&a2” are the two variables that will receive two integer values from user.

Explain Different types of Loops

Loop control structure is used to execute and repeat a block of statements, there are three types of loop control structures in C programming.

  1. FOR loop
  2. WHILE loop
  3. DO-WHILE loop
See also  CSS

FOR Loop

A for loop is used to execute and repeat a block of statement depending on a condition.

For (<initial value>; <condition>; <increment>)

{

<statement block>

}

Here <initial value> is the assignment of expression

<condition> is a logical expression

<increment> is the increment value of the variable

Example:

WHILE loop

A While loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of a loop.

While (<condition>)

{

<statement block>

}

Example:

DO WHILE loop

A Do while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the end of a loop.

Do

{

<statement block>

}

While (<Condition>);

Example:

Explain Macro

Macro substitution is a powerful feature of the C that allows us to define shortcuts or aliases for code snippets.

Macro definition

We define macro using #define directive followed by the macro, a space and the code snippet that servers as its replacement. And no semicolon is needed after the code.

EX: #define PI 3.14159  

Macro Usage:

Whenever we use the macro name in our code, the preprocessor replaces it with the defined code snippet during the preprocessor stage.

Example:

Macro Expansion:

During preprocessing, the preprocessor scans the code and replaces all occurrences of the macro name with its defined value.

EX:

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top