Category: Learn C Online

Initialize all the data variables 1

Tip of the week#2: Initialize all the data variables

Whenever you declare a local variable, it is not initialized to any value. It contains a garbage value. Garbage value is never useful. It is just an uninitialized data. Presence of garbage value might introduce some bugs into your program. Hence whenever we declare a local variable, the best practice is to initialize it at the same time. Instead of writing “int variable1”, we can write “int variable1 = 0”. This will eliminate the risk of the program code giving unwanted output.

Looping performance 0

Tip of the week#1: Looping performance

Never use two loops if one loop will suffice. Example: for(i=0; i&lt;5; i++) { /*Do something 1*/ } for(j=0; j&lt;5; j++) { /*Do something 2*/ } In the above example, first and the second “for” loop will execute 5 times each. That is the total of 10 times. The performace of the above code can be optimized by using only one for loop as follows: for(i=0; i<5; i++) { /*Do something 1*/ /*Do something 2*/ } The above loop will be executed only 5 times. Thus the time required for execution is reduced. Note: If the instructions in the loop...

Tip of the Week Series 1

Tip of the Week Series

Hello Readers, Welcome to LearnCOnline.com Thanks a lot to all the readers for overwhelming response. Thanks once again for the wonderful feedback provided by you all. After reviewing the feedback we have planned to start “Tip of the Week” series. This series will provide you with the tips on the C Programming language which will be very much useful for the beginners and the advanced programmers as well. We are looking forward to receive your valuable feedback. Thanks, LearnCOnline Team

Program to determine the type of the character entered 5

Program to determine the type of the character entered

A character is entered through the keyboard. Below is a program to determine whether the character entered is a capital letter, a small case letter, a digit or special symbol. We will make use of isupper(), islower() and isdigit() functions to write this C Program. isupper() – This function will take character as parameter and will return true if the character is UPPERCASE. islower() – This function will take character as parameter and will return true if the character is lowercase. isdigit() – This function will take character as parameter and will return true if the character is a digit else it will return...

Create 10 different Random numbers 2

Create 10 different Random numbers

Below is the program in C language that will create and display 10 different random numbers. This program will make use of srand function in C. In order to generate random numbers, srand is usually initialized to some distinctive value. We will also make use of time function as time function will return different value at different time. The value returned by the time function will act as a parameter to srand function. Here is the code… #include #include #include void main() { int i; clrscr(); srand( (unsigned)time( NULL ) ); for (i=0; i<10; i++) printf(“%d “, rand()); printf(“\n”); getch();...

Program to count number of words from a given sentence 11

Program to count number of words from a given sentence

The below mentioned C Program will count number of words from a given sentence. The program will accept a sentence in a character array sen[100]. It will then calculate the number of words and display the result. #include #include #include void main() { char sen[100]; int i, count = 0; printf(“Enter a Sentence : \n”); gets(sen); for (i=0; i<strlen(sen);i++) { if (isspace(sen[i])) { count++; } } printf(“Number of words in the sentence = %d”, ++count); printf(“\n”); getch(); } Also See: Strings in C Programming Language

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet