Brushing up C part 2

In continuation of  my previous blog brushing up C, lets go further to know more about C 🙂 .


.

1. Data types

  • Integer, Float: They are used to declare numbers. For eg. int a=1, int b, float c=2.3 and so on.
  • Char: This is used for storing characters/text. They are declared as char. For eg char a=’g’, char b=’h’.
  • Arrays: They are used for storing multiple items of int, float or char type. For eg. int  Arr[5] will store 5 items of int type.
  • Static and global variables: Static variables exist through the life of the application. They are within the function. They retain the value even after the function is exited. Global variables are similar in nature, however they are accessible to all functions in the file they are defined.

2. Functions

Function is a way of encapsulating certain lines of code. It makes sense to create separate functions for different purposes. Functions play a big role in making code more readable and well managed.

Functions are declared as:

int myFunction(int a, int b)

{

/*Certain lines of code performing specific work*/

return 0;

}

These functions are then called at appropriate places from other functions.

For Eg:

int main (int)

{

int a = myFunction(1,1);

return 0;

}

Here, myFunction() is called from inside main() function.


3. Comments

Comments are a way of writing words and sentences to explain what the code is doing. They are not processed by the compiler.

Comments in C are written within

/* */

For eg .

/* This is a c tutorial*/


4. For statement

When you need to execute same code multiple times, a for  statement is used.

Eg.

For (int a=0; a < 5; a++)

{

printf ( ” I am a and my value is %d”,a);

}

This will print the value of a as 0,1, 2,3,4.


5. While loop

While is also used for multiple time execution of certain lines of code.

Eg.

while ( a < 5)

{

printf ( ” I am a and my value is %d”,a);

a++;
}

The loop is executed again and again till the value of a is less than 5.


6. Ifelseif statement

When you want different pieces of code to be executed under different conditions, you may use if-else-if  statement.

Eg.

If ( a  < 5 )

{

printf ( ” I am less than 5 \n”);

}

else if ( a == 5)

{

printf ( ” I am 5 \n”);

}

The example above is self explanatory ☺️ .


7. Switch statement

Switch statement is also used when you want different things to happen under different conditions.

Switch ( a)

{

case 1:

/*code for condition 1*/

break;

Case 2:

/*Code for condition 2*/

break;

Case default:

break;

}

In the above example, if the value of variable a is 1, the code under “case 1” will be executed. Likewise, for the other cases.

Summary
Friends, here I end Part 2 of this series. Please feel free to send in your queries .

I will be taking up pointers in the next part.

Happy coding ☺️ .

Here is the link to my blog on pointers, brushing up c part 3 pointers .


 

Advertisements

3 thoughts on “Brushing up C part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s