IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Command Line Arguments - Discussion

@ : Home > C Programming > Command Line Arguments > Find Output of Program - Discussion

3. 

What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)?
cmd> sample 1 2 3

/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    int j;
    j = argv[1] + argv[2] + argv[3];
    printf("%d", j);
    return 0;
}

[A]. 6[B]. sample 6
[C]. Error[D]. Garbage value

Answer: Option E

Explanation:

Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation.

Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);


Ranjit Singh said: (Mon, Aug 9, 2010 12:49:48 PM)    
 
Can any describe it how the error will generate?

Brindha said: (Tue, Sep 7, 2010 02:24:47 AM)    
 
Instead of arg[1] atoi(arg[1]) is to be put.

argv[1] will contain "1" (as string), we have to convert into integer with atoi() function.

Bhagat Singh said: (Fri, Nov 26, 2010 01:05:18 PM)    
 
argv[3] is not declare automatically
so we never used argv[3]

The Prodigy said: (Wed, Dec 29, 2010 08:20:17 AM)    
 
The command line arguments are taken as strings (char *) and not integers.

Sweety said: (Wed, Jan 19, 2011 10:39:02 AM)    
 
@Brindha : perfect

Navneet said: (Wed, Feb 9, 2011 11:58:11 AM)    
 
argv is argument vector and it is string. here it is tried to concatenate string. C does not provide the string concatenation it this way. So that error!

Kavtihha said: (Sun, Jul 17, 2011 08:00:52 AM)    
 
I'm getting the error even if I put atoi() function. What to do?

Mukesh said: (Mon, Jul 18, 2011 02:54:34 AM)    
 
All the confusion accounts to the generosity of our loveable C compiler. The C standard says "standard conversions" but never defines an upper boundary for the conversions. Hence the compiler is let loose - it tries and performs all sorts of conversions and when successful(which it always is!!) says "Fine fine the program has no errors - yeah but this is a warning... "
For example, int a = "1";
"1" is a char*, so the RHS is an address to the string. The compiler converts this address to an integer and assigns it to int.
But the problem is the binary operator : +
The + operator has been defined to work only with a predefined set of types, and not to work on a predefined set of types. For instance, C standard says Ok + operator, you can work on integers, floats, doubles, blah.. but you shouldnt work on two pointers(since I forbid arithmetic operations with two pointers).
So if you write :
int j = argv[1] + 2; // WARNING, NO ERROR
int k = argv[1] + "2"; // 2 pointers?? error x-(

So just make sure you do the appropriate type-casts(like the one answered above)

Rupinderjit said: (Wed, Dec 7, 2011 04:05:10 PM)    
 
argv[i] is pointer to strings in an argument.So argv[1],argv[2] or whatever are pointer to respective strings pointed to by argv[index].

Since if we look at pointer arithmetic, then two pointer's addition is not allowable.

So there is an error.

But if we change type using typecast,and after on changing type to integer, these are then merely an constant int value.

So do the answer.

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!