CS355 - Lecture 7 Summary

Command-Line Arguments in C

Introduction to Command-Line Arguments

  • Command-line arguments are values that can be passed to C programs during execution.

  • These arguments are crucial for controlling the program externally rather than hard-coding values within the code.

  • The handling of command-line arguments is achieved through parameters in the main() function, specifically using argc and argv[].

The Main Function and Its Parameters

  • int main(int argc, char *argv[])

    • argc: Represents the Argument Count – the total number of command-line arguments passed to the program.

    • argv[]: Known as the Argument Vector, it is an array of pointers that point to each command-line argument provided.

Understanding argc and argv[]

  • argv[0] holds the name of the program itself.

  • argv[1] is a pointer to the first command-line argument given by the user.

  • argv[n] points to the last argument supplied where n is the index.

  • Key Points:

    • If no arguments are provided, argc == 1.

    • If one argument is passed, argc == 2.

Example of Command-Line Arguments

#include <stdio.h> 
int main(int argc, char *argv[]) { 
    if(argc == 2) { 
        printf("The argument supplied is %s\n", argv[1]); 
    } else if(argc > 2) { 
        printf("Too many arguments supplied.\n"); 
    } else { 
        printf("One argument expected.\n"); 
    } 
}
Example Outputs:
  • Command: ./a.out testing

    • Output: The argument supplied is testing

  • Command: ./a.out testing1 testing2

    • Output: Too many arguments supplied.

  • Command: ./a.out

    • Output: One argument expected.

Passing Arguments with Spaces

  • The command-line arguments should be separated by spaces.

  • If an argument contains spaces, it should be enclosed in double quotes " or single quotes ''.

Another Example for Command-Line Arguments

#include <stdio.h> 
int main(int argc, char *argv[]) { 
    printf("Program name %s\n", argv[0]); 
    if(argc == 2) { 
        printf("The argument supplied is %s\n", argv[1]); 
    } else if(argc > 2) { 
        printf("Too many arguments supplied.\n"); 
    } else { 
        printf("One argument expected.\n"); 
    } 
}
Example Output:
  • Command: ./a.out "testing1 testing2"

    • Output: Program name ./a.out The argument supplied is testing1 testing2

Structures in C

Definition of a Structure

  • Structures in C are user-defined data types that can hold different types of data items.

  • Unlike arrays that can only hold data items of the same type, structures can combine various types of data.

  • Structures are frequently used to represent records.

Example Use Case for Structures
  • For instance, in a library management system, a structure could be used to keep track of books with the following attributes:

    • Title

    • Author

    • Subject

    • Book ID

Defining a Structure

  • A structure is defined using the struct statement.

  • The format is as follows:

struct [structure tag] { 
    member definition; 
    member definition; 
    ... 
} [one or more structure variables];
  • Example of defining a structure for books:

struct Books { 
    char title[50];  
    char author[50];  
    char subject[100];  
    int book_id;  
} book;

Accessing Structure Members

  • To access members of a structure, the member access operator . is used.

  • Syntax: structure_variable.member_name.

Example of Using Structures in C

#include <stdio.h>  
#include <string.h>   
struct Books {  
    char title[50];   
    char author[50];   
    char subject[100];   
    int book_id;  
};   
int main() {  
    struct Books Book1;  /* Declare Book1 of type Book */  
    struct Books Book2;  /* Declare Book2 of type Book */   
    /* Book 1 specification */  
    strcpy(Book1.title, "C Programming");  
    strcpy(Book1.author, "Nuha Ali");  
    strcpy(Book1.subject, "C Programming Tutorial");  
    Book1.book_id = 6495407;   
    /* Book 2 specification */   
    strcpy(Book2.title, "Telecom Billing");   
    strcpy(Book2.author, "Zara Ali");   
    strcpy(Book2.subject, "Telecom Billing Tutorial");   
    Book2.book_id = 6495700;   
    /* Print Book1 info */   
    printf("Book 1 title : %s\n", Book1.title);   
    printf("Book 1 author : %s\n", Book1.author);   
    printf("Book 1 subject : %s\n", Book1.subject);   
    printf("Book 1 book_id : %d\n", Book1.book_id);   
    /* Print Book2 info */   
    printf("Book 2 title : %s\n", Book2.title);   
    printf("Book 2 author : %s\n", Book2.author);   
    printf("Book 2 subject : %s\n", Book2.subject);   
    printf("Book 2 book_id : %d\n", Book2.book_id);   
    return 0;  
}
Output of the Example:
  • Book 1 title: C Programming

  • Book 1 author: Nuha Ali

  • Book 1 subject: C Programming Tutorial

  • Book 1 book_id: 6495407

  • Book 2 title: Telecom Billing

  • Book 2 author: Zara Ali

  • Book 2 subject: Telecom Billing Tutorial

  • Book 2 book_id: 6495700

Arrow Operator in Structures

  • The arrow operator (->) is used in C/C++ to access structure members when dealing with structure pointers.

  • Example of using the arrow operator:

#include <stdio.h>   
#include <stdlib.h>  
// Creating the structure  
struct student {  
    char name[80];  
    int age;  
    float percentage;  
};  
// Creating the structure object  
struct student* emp = NULL;  
int main() {  
    // Assigning memory to struct variable emp  
    emp = (struct student*) malloc(sizeof(struct student));  
    // Assigning value to age  
    emp->age = 18;  
    // Printing the assigned value  
    printf("%d", emp->age);  
    return 0;  
}
Output of the Example:
  • Output: 18

Conclusion
  • The arrow operator in C/C++ simplifies the syntax when working with structures, particularly with pointers to structures, making code more manageable and readable.

  • Structures provide a way to encapsulate diverse data types into a single unit, which is essential in creating complex data structures such as linked lists, trees, and more.