Computer Programming C Laboratory: Practical C Programs

Summary of C Language

Summary of C Language - Computer Programming C

SUMMARY OF 'C' LANGUAGE

Variables

Syntax: < Variable type> <namel>, <name2>.....

Examples:

int w;

float rate;

char name[10];

Initialization

Syntax: <variable type> <variable name> = <value>;

Examples:

int n=10;

int n[5] = {1,2,3,4,5};

float price = 10.50;

char counter 'Y';

Assignments

Syntax: <name> = <value>;

Example:

n = 3977;

r = 10.50;

C = 'Y';

Constants

Syntax: #define <constant name> <value>

Example:

#define PI 3.141592

Type Definition

Syntax: typedef <variable type> <symbolic name>;

Example:

typedef int Boolean;

Comments

Syntax: /* <body of comment> */

Example:

/*input data */

Structures

Syntax:

struct <identifier>

{

      <type> <variable name>;

      <type> <variable name>;

}

struct <identifier> <var name>, <var name>;

Example:

struct item

{

      int ic;

      char iname [20];

      float rate;

};

struct item pen, book;

Assignment

Syntax: <identifier> <member name>= <value>;

Examples:

pen.iname = 'REYNOLDS';

pen.plate = 10.50;

book.ic = 100;

Associativity of Operators


Input and Output

printf:

Syntax:

printf< "literal string>:");

printf("<literal string>\n");

printf("conversion specification", <variable names>);

Examples:

printf("The output in follows");

printf("Results are \n");

printf("%d %f %e \n", a,b,c);

scanf:

Syntax: scanf("<conversion specs >", &<variable>,&<variables>);

Examples:

scanf("%d"; & n);

scanf("%c", name);

Conversion specifications:


Primitive input/output

gets() and getchar(): Reads characters from standard input and stores in array name and quits when end of line is reached. New line character is not stored in the array.

Syntax:

c=getchar();

gets();

puts() and putchar(): Writes characters from array name on standard output followed by a new line character

Syntax:

putchar(c);

puts(<array name>);

Examples:

char name [10];

gets (name);

puts (name);

Control structures

if statement

Syntax:

if (<condition expression>)

{

           <compound statements>;

}

Example:

if (i<n)

{

          i++;

          scanf("%d", &a);

}

If else statement

Syntax:

if (<condition expression>)

{

         <compound statements>

else

{

         <compound statements>;

}

Example:

if (a>b)

       printf(“A is Big");

else

        printf("B is Big");

Instead of compound statements a single statement alsoallowed

switch statement

Syntax:

switch (expression>)

{

case <value 1>:

          <compound statement 1>;

         break;

case <value 2>:

         <compound statement 2>;

         break;

  …………..

  …………..

default:

        <compound statement for default>;

        break;

}

Example:

scanf("%d",&n);

switch(n);

{

          case 1: printf ("one");

                    break;

          case 2: printf ("two");

                    break;

          case 3: printf("three");

                    break

          default:

                    printf("no out of range");

                    break;

}

 

Loop structures

for loop

Syntax:

for (<expression 1>; <expression 2>; <expression 3>)

{

         <compound statement>;

}

Examples:

for (i=0; i<=n; i++)

{

       printf("value is %d", i);

}

while loop

Syntax:

while (<condition expression>)

{

        <compound statement>;

}

Example:

while (i <=n)

{

           printf("value of i is %d", i);

           i++;

}

do while loop

Syntax:

do

{

         <compound statement>;

}

while (<conditions expression>);

Example:

do

{

         printf("value %d", i);

         i++)

}

while (i<=n);

Function Definitions

Syntax:

<type returned> <function name>(List of parameters)

{

         <declaration of local variables>;

         <body of function> ;

}

Example:

int add (int x, int y)

{

     int z;

     z = x+y;

     return (z);

}


Structure of a C program

#include<stdio.h>  /* /i/o library */

#include<math.h>   /* math library ‒ optional */

#include <string.h>  /* string functions ‒ optional */

#include <ctype.h>  /*character class tests ‒ optional */

#include <stdlib.h>  /* utility functions ‒ optional */

#include …. /* other library files */

#define …. /* define constants */

/* Declare global variables */

<variable type> <list of variables>;

/* Function prototypes */

<type returned> <function name> <parameter list>);

/* main function */

main (optional argc and argv arguments)

{

        <declaration of local variables, arrays, structures etc>

        ………….

        body of code

        ……….

} /* End of main */

<type returned> <function name> <parameter list>)

{

        <declaration of local variables>

        ……..

        body of functions

        ……….

        return ()

} /* End of function */

Other functions if any.


File Opening Modes

r: Opens an existing text file for reading purpose.

w: Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file.

a: Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content.

r+: Opens a text file for both reading and writing.

w+: Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.

a+: Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.


Preprocessor Directives

#define: Substitutes a preprocessor macro.

#include: Inserts a particular header from another file.

#undef: Undefines a preprocessor macro.

#ifdef: Returns true if this macro is defined.

#ifndef: Returns true if this macro is not defined.

#if: Tests if a compile time condition is true.

#else: The alternative for #if.

#elif:  #else and #if in one statement..

#endif:  Ends preprocessor conditional.

#error: Prints error message on stderr.

#pragma: Issues special commands to the compiler, using a standardized method.

 

String Functions

C supports a wide range of functions that manipulate null‒terminated strings

Function: Purpose

1. strcpy(s1, s2); Copies string s2 into string s1.

2.  strcat(s1, s2); Concatenates string s2 onto the end of string s1.

3. strlen(s1); Returns the length of string s1.

4. strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

5. strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.

6. strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.

 

Memory Management

The C programming language provides several functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.

Function: Description

1. void *calloc(int num, int size); This function allocates an array of num elements each of which size in bytes will be size.

2. void free(void *address); This function releases a block of memory block specified by address.

3. void *malloc(int num); This function allocates an array of num bytes and leave them uninitialized.

4. void *realloc(void *address, int newsize); This function re‒allocates memory extending it upto newsize.

 

Computer Programming C Laboratory: Practical C Programs : Tag: : - Summary of C Language


Computer Programming C Laboratory: Practical C Programs



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation