C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Exercise 6

C Programming

In this exercise section of the tutorial we will write some C program.

Write a program in C to take user name as input and display a greetings message

In this code we are assuming that the user name will be less than 256 characters long.

We will be using the gets() function to take user name as input.

/**
 * file: string.c
 * author: yusuf shakeel
 * date: 2010-12-25
 * description: string program
 */

#include <stdio.h>
int main(void)
{
  //variable
  char name[256];
  
  //input
  printf("Enter your name: ");
  gets(name);
  
  //output
  printf("Hello, %s!\n", name);
  
  printf("End of code\n");
  return 0;
}

Output

Enter your name: Yusuf Shakeel
Hello, Yusuf Shakeel!
End of code

Write a program in C to print the following pattern

Pattern

A
Ap
App
Appl
Apple

The given pattern has 5 rows which is equal to the number of letters in the string "Apple".

To find the total number of characters in a string we will use the strlen() function from the string.h header file.

Note! A string is a sequence of characters stored in a character array. Therefore, we can access the elements in the array using the index.

/**
 * file: string-pattern.c
 * author: yusuf shakeel
 * date: 2010-12-25
 * description: string pattern program
 */

#include <stdio.h>
#include <string.h>
int main(void)
{
  //variable
  char str[] = "Apple";
  
  //length of the string
  int len = strlen(str);
  
  int r, c;
  
  //output
  for (r = 0; r < len; r++) {
    for (c = 0; c <= r; c++) {
      printf("%c", str[c]);
    }
    printf("\n");
  }
  
  printf("End of code\n");
  return 0;
}

Output

A
Ap
App
Appl
Apple
End of code

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT