C BASICS

Hello C Program:

Figure: 'Hello C' Program

Figure: "Hello C" Program Output

Variable in C:


Type

Syntax

Variable declaration data_type variable_name;
Example: int x,y,z;
                char ch;
Variable initialization data_type variable_name=value;
Example: int x=50, y=30;
                char ch='A';

There are 3 types of variables in C
1). Local Variable
2). Global Variable
3). Environmental Variable

1). Local Variable:

  • The Scope of local variables will be within the function only.
  • These variables are declared within the function and can't be accessed outside the function.
Example:
Write a C Program in Notepad++ as
Figure: Write a program in notepad++

In turboc3 you will see the program as 
Figure: localvar.c program

Figure: Output of a program

2). Global Variable:

Figure: Global variable Program

Figure: Global variable program Output

3). Environment Variable:

There are 3 functions which are used to access, modify and assign an environment variable in C. They are,
  • getenv()
  • setenv()
  • putenv()
getenv():
Open System environmental variables as
Figure: Open Computer properties

Figure: Click on environmental variables

Figure: Set path here

Now write program as
Figure: getenv program

Figure: Program Output

String in C:

  • C Strings are nothing but array of characters ended with null character ('\0').
  • This null character indicates the end of the string.
  • Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.

Syntax:

1). data_type string[array_size]={'a','b','c',...\0};   or
2). data_type string[array_size]="string";   or
3). data_type string[]="string";

Example:

1). char string[20]={'s','t','o','n','e','p','r','o','f','i','t','s','\0'}
This type allocation makes 20 bytes fixed memory and remaining free bytes will be \0.

2). car string[20]="stoneprofits";
This type of allocation makes 20 bytes of fixed memory.

3). char string[]="stoneprofits";
This type of allocation makes respective characters in a string.

String Functions:

String Functions

Description

strcat() Concatenates str2 at the end of str1.
strncat() Appends a portion of string to another.
strcpy() Copies str2 into str1.
strncopy() Copies given number of characters of one string to another.
strlen() Gives the length of str1.
strcmp() Returns 0, if str1 is same as str2. Returns <0, if str1<str2. Returns >0, if str1>str2.
strcmpi() Same as strcmp() function. But this function negotiates case. "A" and "a" treated as same.
strchr() Returns pointer to first occurance of char in str1
strrchr() Last occurance of given charcter in a string is found.
strstr() Returns pointer to first occurance of str2 in str1.
strrstr() Returns pointer to last occurance of str2 in str1
strdup() Duplicates the string.
strlwr() Converts string to lower case.
strupr() Converts string to upper case.
strrev() Reverses the given string.
strset() Sets all character in a string to given character.
strnset() It sets the position of a characters in a string to given character.
strtok() Tokenizing given string using delimiter.

Loop Control Statements:

There are mainly 3 types of loop control statements in C language. They are,
1). for
2). while
3). do-while

1). For loop:

Figure: For loop

Figure: For loop explanation

2). While loop:

Figure: While loop

Figure: Example of while loop

Figure: Output of while loop program

3). Do... while loop:

Figure: Do... while loop execution process

Figure: Do... while loop example

Figure: Do... while loop example output

Reading input from the keyboard and displaying on screen:

Figure: Reading data and displaying to screen

Figure: Output of reading and displaying data

Create a file and writing some data into file:

Figure: Creating and writing data to file

Figure: File created and contained some data

Figure: File containing some data

Read from a file and write to a file:

Figure: Reading from file and writing to a file

Figure: Output for reading from and writing to a file

Figure: Written to a new file

Figure: Data written to a new file

Counting number of lines in a file:




Number of occurrences of a character in a string:



Concatenating Two Strings:




Graphic Output:

Drawing a circle using circle function:




Printing STAR symbol in C using graphics header:



Drawing a Line:



Drawing a Rectangle and its bar:




Previous
« Prev Post